// Constant Initialization
//var AMAZON_imageserver = "images.amazon.com";
var DDRIVE_offsetxpoint = -60; //Customize x offset of tooltip
var DDRIVE_offsetypoint = 20; //Customize y offset of tooltip
var DDRIVE_ie = document.all;
var DDRIVE_ns6 = document.getElementById && !document.all;

// Variable Initialization
var DDRIVE_enabletip = false;
var DDRIVE_tipobj = null;

// Add the tooltip pane with requisite styling
document.write('<div id="DDRIVE_dhtmltooltip"></div>\n');
document.write('<style type="text/css">\n');
document.write('#DDRIVE_dhtmltooltip{\n');
document.write('position: absolute;\n');
document.write('width: 150px;\n');
document.write('border: 3px double black;\n');
document.write('padding: 2px;\n');
document.write('background-color: white;\n');
document.write('visibility: hidden;\n');
document.write('z-index: 100;\n');
document.write('filter: progid:DXImageTransform.Microsoft.Shadow(color=gray,direction=135);\n');
document.write('}</style>\n');

// Set the tooltip pane object
if (ToolTipCompatibleBrowser()){
	DDRIVE_tipobj = document.all ? document.all["DDRIVE_dhtmltooltip"] : document.getElementById? document.getElementById("DDRIVE_dhtmltooltip") : "";
}
	
// Gets the "body" element
function DDRIVE_ietruebody(){
	return (document.compatMode && document.compatMode!="BackCompat") ? document.documentElement : document.body;
}

// Hides the tooltip pane
function DDRIVE_hidetip(e){
	if (ToolTipCompatibleBrowser()){
		DDRIVE_enabletip = false;
		DDRIVE_tipobj.style.visibility = "hidden";
		DDRIVE_tipobj.style.left = "-1000px";
		DDRIVE_tipobj.style.backgroundColor = '';
		DDRIVE_tipobj.style.width = '';
	}
}


// Parses an Amazon referral URL and gets the associated image URL (empty string if N/A)
function GetAmazonImageSource(amazonUrl){
	var amazonImgSrc = "";
	var AmazonLinkMatches = amazonUrl.match(/^http:\/\/www\.amazon\.[^\/]+\/exec\/obidos\/ASIN\/([\w\d]{10})\/[\w\d]+$/i);
	if(AmazonLinkMatches && AmazonLinkMatches.length > 1){
		amazonImgSrc = "http://" + AMAZON_imageserver + "/images/P/" + AmazonLinkMatches[1] + ".01.MZZZZZZZ.jpg"
	}
	return amazonImgSrc;
}


// Rewrites the links in the document to provide image tooltips for Amazon referral links
function RewriteAmazonLinksWithTooltips(){
	for(i = 0; i < document.links.length; i++){
		var amazonImgSrc = GetAmazonImageSource(document.links[i].href);
		if(amazonImgSrc != ""){
			document.links[i].onmouseover = AmazonImgTooltip;
			document.links[i].onmouseout = DDRIVE_hidetip;
			// Preload all images
			var imgLoad = new Image();
			imgLoad.src = amazonImgSrc;
		}
	}
}

// Positions the tooltip pane
function DDRIVE_positiontip(e){
	if (ToolTipCompatibleBrowser() && DDRIVE_enabletip){
		var curX = (DDRIVE_ns6) ? e.pageX : event.x + DDRIVE_ietruebody().scrollLeft;
		var curY = (DDRIVE_ns6) ? e.pageY : event.y + DDRIVE_ietruebody().scrollTop;

		//Find out how close the mouse is to the corner of the window
		var rightedge = DDRIVE_ie && !window.opera ? DDRIVE_ietruebody().clientWidth - event.clientX-DDRIVE_offsetxpoint : window.innerWidth - e.clientX - DDRIVE_offsetxpoint - 20;
		var bottomedge = DDRIVE_ie && !window.opera ? DDRIVE_ietruebody().clientHeight - event.clientY-DDRIVE_offsetypoint : window.innerHeight - e.clientY - DDRIVE_offsetypoint - 20;

		var leftedge = (DDRIVE_offsetxpoint < 0) ? DDRIVE_offsetxpoint * (-1) : -1000;

		//if the horizontal distance isn't enough to accomodate the width of the context menu

		if (rightedge < DDRIVE_tipobj.offsetWidth){
			//move the horizontal position of the menu to the left by it's width
			DDRIVE_tipobj.style.left = DDRIVE_ie ? DDRIVE_ietruebody().scrollLeft + event.clientX - DDRIVE_tipobj.offsetWidth + "px" : window.pageXOffset + e.clientX-DDRIVE_tipobj.offsetWidth + "px";
		}
		else if (curX < leftedge){
			DDRIVE_tipobj.style.left = "5px";
		}
		else{
			//position the horizontal position of the menu where the mouse is positioned
			DDRIVE_tipobj.style.left = curX + DDRIVE_offsetxpoint + "px";
		}

		//same concept with the vertical position
		if (bottomedge < DDRIVE_tipobj.offsetHeight){
			DDRIVE_tipobj.style.top = DDRIVE_ie ? DDRIVE_ietruebody().scrollTop + event.clientY - DDRIVE_tipobj.offsetHeight - DDRIVE_offsetypoint + "px" : window.pageYOffset + e.clientY - DDRIVE_tipobj.offsetHeight - DDRIVE_offsetypoint + "px";
		}
		else{
			DDRIVE_tipobj.style.top = curY + DDRIVE_offsetypoint + "px";
		}
		DDRIVE_tipobj.style.visibility = "visible";
	}
}


// Checks to see if the current browser is "tooltip compatible"
function ToolTipCompatibleBrowser(){
	if(DDRIVE_ns6||DDRIVE_ie){
		return true;
	}
	else{
		return false;
	}
}

document.onmousemove = DDRIVE_positiontip;
//RewriteAmazonLinksWithTooltips();

// Shows a tooltip
//function DDRIVE_tooltip(thetext, thecolor, thewidth){
//	if (ToolTipCompatibleBrowser()){
//		if (typeof thewidth!="undefined"){
//			DDRIVE_tipobj.style.width = thewidth + "px";
//		}
//		else{
//			DDRIVE_tipobj.style.width = "";
//		}
//		if (typeof thecolor!="undefined" && thecolor!=""){
//			DDRIVE_tipobj.style.backgroundColor=thecolor;
//		}
//		DDRIVE_tipobj.innerHTML = thetext;
//		DDRIVE_enabletip = true;
//		return false;
//	}
//}

// Shows a tooltip with an Amazon product image in it
function DDRIVE_tooltip(name,e)
{
	
	if (!e) var e = window.event;
  	// Get the event target
	var targ;
	if (e.target) targ = e.target;
	else if (e.srcElement) targ = e.srcElement;
	if (targ.nodeType == 3) // defeat Safari bug
		targ = targ.parentNode;
	
	// We have to act on a link, so go up through the DOM tree until we get one
	var docElement = DDRIVE_ietruebody();
	while(targ != docElement && targ.nodeName.toLowerCase() != "a"){
		targ = targ.parentNode;
	}
	
	// We never got a link
	if(targ == docElement){
		return false;
	}

	// We got a link; show the tooltip
	var imgurl, thecolor, thewidth;
	if (ToolTipCompatibleBrowser()){
		var amazonImgSrc = "http://" + "www.stamfordscientific.com" + "/images/tooltip/" + name + ".jpg";
		//alert (amazonImgSrc);
		if(amazonImgSrc != ""){
			var img = new Image();
			img.src = amazonImgSrc;
			//alert( img.src);
			DDRIVE_tipobj.innerHTML = '<img src=\'' + img.src + '\'/>';
			DDRIVE_tipobj.style.width = img.width + "px";
			DDRIVE_enabletip = true;
			return false;
		}
	}

}

// Rewrites the links in the document to provide image tooltips for Amazon referral links
function RewriteAmazonLinksWithTooltips(){
	for(i = 0; i < document.links.length; i++){
		var amazonImgSrc = GetAmazonImageSource(document.links[i].href);
		if(amazonImgSrc != ""){
			document.links[i].onmouseover = AmazonImgTooltip;
			document.links[i].onmouseout = DDRIVE_hidetip;
			// Preload all images
			var imgLoad = new Image();
			imgLoad.src = amazonImgSrc;
		}
	}
}



