//Variables, they should be local, but giving them fairly unique names can never hurt
var divTooltip;
var divTooltipShadow;
var tmrTooltipShadowPositioning;
var cursorX = -1000;
var cursorY = -1000;

//Functions
function TooltipShow(what) {
	if(typeof(divTooltip) == "undefined" || typeof(divTooltipShadow) == "undefined") {
		TooltipBuildDivs();
	}
	
	if(what.title != null && what.title != "") {
		what.titlePH = what.title;
		what.title = "";
	}
	
	// Fill the tooltip with the text
	if(what.titlePH != null && what.titlePH.replace(/&nbsp;/, "").replace(/ /g, "") != "") {
		// Look at the mouse moving and adjust the tooltip position to it
		TooltipAddEvent(document, 'mousemove', TooltipOnMouseMove);
		
		// Fill the tooltip with the text
		divTooltip.innerHTML = what.titlePH.replace(/\\n/g, "<br>");
		
		// Position the tooltip
		divTooltip.style.width = "";
		if(what.titlePH.indexOf("<img") == -1 && what.titlePH.length > 200)
			divTooltip.style.maxWidth = "450px";
		else
			divTooltip.style.maxWidth = "300px";
		if(parseInt(divTooltip.style.maxWidth) > what.offsetWidth)
			divTooltip.style.minWidth = what.offsetWidth;

		// Get the current size of the tooltip by displaying it outside the screen for a tiny second
		divTooltip.style.left = "-1000px";
		divTooltip.style.top = "-1000px";
		divTooltip.style.display = "block"; // causes reflow
		var tempTooltipWidth = divTooltip.offsetWidth;
		var tempTooltipHeight = divTooltip.offsetHeight;
		divTooltip.style.display = "none"; // stops reflow from occuring
		
		if(tempTooltipWidth > parseInt(divTooltip.style.maxWidth)) {
			divTooltip.style.minWidth = "";
			divTooltip.style.width = parseInt(divTooltip.style.maxWidth) + "px";
			tempTooltipWidth = parseInt(divTooltip.style.maxWidth);
		}

		// Check whether the tooltip is moving off-screen (horizontally)
		if( ( cursorX + 10 + tempTooltipWidth + 5 ) > document.body.clientWidth )
			divTooltip.style.left = ( document.body.clientWidth - tempTooltipWidth - 5 ) + "px";
		else
			divTooltip.style.left = ( cursorX + 10 ) + "px";
			
		// Check whether the tooltip is moving off-screen (vertically)
		if( ( cursorY + 15 + tempTooltipHeight + 5 ) > document.body.clientHeight )
			divTooltip.style.top = ( document.body.clientHeight - tempTooltipHeight - 5 ) + "px";
		else
			divTooltip.style.top = ( cursorY + 15 ) + "px";
				
		// Finally, when the tooltip is ready, really the tooltip
		divTooltip.style.display = "block"; // causes reflow
		
		// Then build shadows
		if(what.titlePH.indexOf("<img") > -1) {
			if(typeof(tmrTooltipShadowPositioning) != "undefined")
				clearInterval(tmrTooltipShadowPositioning);
			tmrTooltipShadowPositioning = setInterval("TooltipShadow();", 100);
			setTimeout("if(typeof(tmrTooltipShadowPositioning) != 'undefined'){clearInterval(tmrTooltipShadowPositioning);}", 3000);
		}
		TooltipShadow();
	}
}
function TooltipHide(really) {
	if(typeof(divTooltip) == "undefined" || typeof(divTooltipShadow) == "undefined")
		return;
	
	// First hide the div, then empty it
	divTooltip.style.display = 'none'; // stops reflow form occuring
	divTooltipShadow.style.display = 'none'; // stops reflow form occuring
	
	// Stop the timer
	if(typeof(tmrTooltipShadowPositioning) != "undefined")
		clearInterval(tmrTooltipShadowPositioning);
		
	// Empty it
	divTooltip.innerHTML = "";
	
	setTimeout("TooltipHideRemoveEvent();", 100);
}
function TooltipHideRemoveEvent() {
	if(typeof(divTooltip) == "undefined" || typeof(divTooltipShadow) == "undefined")
		return;
	
	if(divTooltip.style.display == 'none') {
		TooltipRemoveEvent(document, 'mousemove', TooltipOnMouseMove);
		cursorX = -1000;
		cursorY = -1000;
	}
}
function TooltipShadow() {
	if(typeof(divTooltip) == "undefined" || typeof(divTooltipShadow) == "undefined")
		TooltipBuildDivs();
	
	divTooltipShadow.style.display = 'none'; // ensure the shadow is hidden so we don't get reflow 4 times
	
	// Position the shadow 2 pixels to the right bottom of the original tooltip
	divTooltipShadow.style.width = ( divTooltip.offsetWidth ) + "px";
	divTooltipShadow.style.height = ( divTooltip.offsetHeight ) + "px";
	divTooltipShadow.style.top = ( divTooltip.offsetTop + 2 ) + "px";
	divTooltipShadow.style.left = ( divTooltip.offsetLeft + 2 ) + "px";
	
	// Then display it
	divTooltipShadow.style.display = 'block';
}
function TooltipBuildDivs() {
	// Make the tooltip DIV
	if(typeof(divTooltip) == "undefined") {
		// Create the tooltip DIV and set properties
		var divFreshBuild = document.createElement("div");
		divFreshBuild.id = "TooltipContainer";
		divFreshBuild.style.display = 'none';
		divFreshBuild.style.position = 'absolute';
		divFreshBuild.style.maxWidth = '300px';
		divFreshBuild.style.zIndex = '1000000';
		divFreshBuild.style.backgroundColor = "#FFFFCC";
		divFreshBuild.style.border = "1px solid black";
		divFreshBuild.style.fontSize = "11px";
		divFreshBuild.style.fontFamily = "Tahoma";
		divFreshBuild.style.padding = "1px";
		
		// Add the DIV to the page
		document.body.appendChild(divFreshBuild);
			
		// Remember the div so we won't have to make a new "link" to it all the time
		// Finding a DIV should be more intensive on resources than saving the link
		divTooltip = document.getElementById("TooltipContainer");
	}
	// Make the shadow DIV
	if(typeof(divTooltipShadow) == "undefined") {
		// Create the shadow DIV and set properties
		var divFreshBuild = document.createElement("div");
		divFreshBuild.id = "TooltipContainerShadow";
		divFreshBuild.style.display = 'none';
		divFreshBuild.style.position = 'absolute';
		divFreshBuild.style.zIndex = '999999';
		divFreshBuild.style.backgroundColor = "gray";
		divFreshBuild.style.border = "0";
		divFreshBuild.style.opacity = 0.6;
		divFreshBuild.style.MozOpacity = 0.6;
		divFreshBuild.style.KhtmlOpacity = 0.6;
		divFreshBuild.style.filter = "alpha(opacity=60)"; 

		
		// Add the DIV to the page
		document.body.appendChild(divFreshBuild);
		
		divTooltipShadow = document.getElementById("TooltipContainerShadow");
	}
}

//Events
function TooltipOnLoad() {
	// Go through all A tags to look for titles and on match add the javascript functions
	var links = document.getElementsByTagName('a');
	for (var i = 0, theLink; theLink = links[i]; i++) {
		if(theLink.className.indexOf("notooltip") == -1 && theLink.title != null && theLink.title != "") {
			// Ignore links with the class "notooltip"
			
			theLink.titlePH = theLink.title;
			theLink.title = "";
			TooltipAddEvent(theLink, 'mouseover', function() { TooltipShow(this); });
			TooltipAddEvent(theLink, 'mouseout', TooltipHide);
		}
	}
}
function TooltipOnMouseMove(e) {
	if(document.all) {
		cursorX = event.clientX;
		cursorY = event.clientY;
		if(self.pageYOffset) {
			scrollX = self.pageXOffset;
			scrollY = self.pageYOffset;
		} else if(document.documentElement && document.documentElement.scrollTop) {
			scrollX = document.documentElement.scrollLeft;
			scrollY = document.documentElement.scrollTop;
		} else if(document.body) {
			scrollX = document.body.scrollLeft;
			scrollY = document.body.scrollTop;
		}
		cursorX += scrollX;
		cursorY += scrollY;
	} else {
		cursorX = e.pageX;
		cursorY = e.pageY;
	}
	if(typeof(divTooltip) != "undefined" && divTooltip.style.display == 'block') {
	
		// Check whether the tooltip is moving off-screen (horizontally)
		if( ( cursorX + 10 + divTooltip.offsetWidth + 5 ) > document.body.clientWidth ) {
			divTooltip.style.left = ( document.body.clientWidth - divTooltip.offsetWidth - 5 ) + "px"; // causes reflow
		} else
			divTooltip.style.left = ( cursorX + 10 ) + "px"; // causes reflow
			
		// Check whether the tooltip is moving off-screen (vertically)
		if( ( cursorY + 15 + divTooltip.offsetHeight + 5 ) > document.body.clientHeight ) {
			divTooltip.style.top = ( document.body.clientHeight - divTooltip.offsetHeight - 5 ) + "px"; // causes reflow
		} else
			divTooltip.style.top = ( cursorY + 15 ) + "px"; // causes reflow
		
		// Position the shadow 2 pixels to the right bottom of the original tooltip
		if(divTooltipShadow.style.top != ( divTooltip.offsetTop + 2 ) + "px")
			divTooltipShadow.style.top = ( divTooltip.offsetTop + 2 ) + "px"; // causes reflow
		if(divTooltipShadow.style.left != ( divTooltip.offsetLeft + 2 ) + "px")
			divTooltipShadow.style.left = ( divTooltip.offsetLeft + 2 ) + "px"; // causes reflow
		divTooltipShadow.style.width = ( divTooltip.offsetWidth ) + "px"; // causes reflow
		divTooltipShadow.style.height = ( divTooltip.offsetHeight ) + "px"; // causes reflow
	}
}

function TooltipAddEvent(obj, type, fn) {
	if(obj.attachEvent) {
		obj['e' + type + fn] = fn;
		obj[type + fn] = function(){ obj['e'+type+fn](window.event); };
		obj.attachEvent('on' + type, obj[type + fn]);
	} else
		obj.addEventListener(type, fn, false);
}
function TooltipRemoveEvent(obj, type, fn) {
	if(obj.detachEvent) {
		obj.detachEvent('on' + type, obj[type + fn]);
		obj[type + fn] = null;
	} else
		obj.removeEventListener(type, fn, false);
}
if(typeof TooltipScriptLoaded == "undefined" || TooltipScriptLoaded == null || TooltipScriptLoaded == false) {
	// Only load if the script hasn't been loaded yet
	TooltipScriptLoaded = true; // should become a global variable, existing in all subscripts

	TooltipAddEvent(window, 'load', TooltipOnLoad);
}