var curPageX = 0;
var curPagey = 0;

var tipBox = "";

var tipWidth = "";
var tipWidthHeaderSub = "";
var tipWidthBodySub = "";
var tipBorderSize = "";
// The next one is necessary to calculate size; same value as above only without
// "px"
var tipBorderNumber = "";
var tipBorderColor = "";
var tipOpacity = "";

var tipHeaderBgColor = "";
var tipHeaderFontFamily = "";
var tipHeaderFontSize = "";
var tipHeaderFontColor = "";
var tipHeaderAlign = "";
var tipHeaderPadding = "";
var tipHeaderOpacity = "";

var tipBodyBgColor = "";
var tipBodyFontFamily = "";
var tipBodyFontSize = "";
var tipBodyFontColor = "";
var tipBodyAlign = "";
var tipBodyPadding = "";
var tipBodyOpacity = "";

window.document.onmousemove = trackMouse;

function trackMouse(e)
{
	var e = (e) ? e : ((event) ? event : null);

	// Functions do different things depending on whether or not a
	// doctype is present. Node type 10 is a doctype but IE reads
	// a doctype as Node type 8 - a comment.
	if (document.childNodes[0].nodeType == 10 || document.childNodes[0].nodeType == 8)
	{
		curPageX = e.clientX + document.documentElement.scrollLeft;
 		curPageY = e.clientY + document.documentElement.scrollTop;

 		scrollPageY = document.documentElement.scrollTop;

		viewPageX = e.clientX;
		viewPageY = e.clientY;

		viewScreenX = document.documentElement.clientWidth;
		viewScreenY = document.documentElement.clientHeight;

		totalScreenX = document.body.offsetWidth;
		totalScreenY = document.body.offsetHeight;
	} else {
		curPageX = e.clientX + document.body.scrollLeft;
 		curPageY = e.clientY + document.body.scrollTop;

 		scrollPageY = document.body.scrollTop;

		viewPageX = e.clientX;
		viewPageY = e.clientY;

		viewScreenX = document.body.clientWidth;
		viewScreenY = document.body.clientHeight;

		totalScreenX = document.documentElement.scrollWidth;
		totalScreenY = document.documentElement.scrollHeight;
	}

	if (tipBox != "" && tipBox.style.display == "block")
	{
		moveTip();
	}
}

function clearTip()
{
	while (tipBox.childNodes.length > 0)
	{
		tipBox.removeChild(tipBox.childNodes[0]);
	}

	tipBox.style.display = "none";
}

function toolText(headerText, tipText)
{
	if (tipBox == "")
	{
		tipBox = document.createElement("div");
		tipBox.id = "toolTipDiv";
		tipBox.style.position = "absolute";
		tipBox.style.display = "none";

		window.document.body.appendChild(tipBox);
	} else {
		clearTip();
	}

	// make sure box settings are up to date
	tipBox.style.width = tipWidth;
	tipBox.style.border = tipBorderSize + " solid " + tipBorderColor;

	if (headerText != "")
	{
		// Build the tip header
		var tipHead = document.createElement("div");
		tipHead.style.backgroundColor = tipHeaderBgColor;
		tipHead.style.fontFamily = tipHeaderFontFamily;
		tipHead.style.fontSize = tipHeaderFontSize;
		tipHead.style.color = tipHeaderFontColor;
		tipHead.style.padding = tipHeaderPadding;
		tipHead.style.textAlign = tipHeaderAlign;
		tipHead.style.width = tipWidthHeaderSub;
		tipHead.style.filter = "alpha(opacity=" + tipHeaderOpacity + ")";
		tipHead.style.opacity = "." + tipHeaderOpacity;
		tipHead.style.mozOpacity = "." + tipHeaderOpacity;

		var tipHeadText = document.createTextNode(headerText);
		tipHead.appendChild(tipHeadText);

		tipBox.appendChild(tipHead);
	}

	var tipBody = document.createElement("div");
	tipBody.style.backgroundColor = tipBodyBgColor;
	tipBody.style.fontFamily = tipBodyFontFamily;
	tipBody.style.fontSize = tipBodyFontSize;
	tipBody.style.color = tipBodyFontColor;
	tipBody.style.padding = tipBodyPadding;
	tipBody.style.textAlign = tipBodyAlign;
	tipBody.style.width = tipWidthBodySub;
	tipBody.style.filter = "alpha(opacity=" + tipBodyOpacity + ")";
	tipBody.style.opacity = "." + tipBodyOpacity;
	tipBody.style.mozOpacity = "." + tipBodyOpacity;

	tipBody.innerHTML = tipText;

	tipBox.appendChild(tipBody);

	tipBox.style.visibility = "hidden";
	tipBox.style.display = "block";

	tipHeight = tipBox.offsetHeight;

	moveTip();
	tipBox.style.visibility = "visible";
}

function moveTip()
{
	tipXloc = curPageX + 10;
	tipYloc = curPageY + 10;

	tipHeight = tipBox.offsetHeight;
	tipWidth = tipBox.offsetWidth;

	// If the tooltip extends off the side, pull it over
	if (viewPageX + 10 + tipWidth > viewScreenX)
	{
		tipXloc -= (tipWidth + 15);
	}

	// If the tooltip will extend off the bottom of the screen, pull it back up.
	if (viewPageY + 10 + tipHeight > viewScreenY)
	{
		pageDiff = (viewPageY + 10 + tipHeight - viewScreenY);
		tipYloc -= pageDiff;
	}

	// If the tooltip extends off the bottom and the top, line up the top of
	// the tooltip with the top of the page
	if (tipHeight > viewScreenY)
	{
		tipYloc = scrollPageY + 5;
	}

	tipBox.style.left = tipXloc + "px";
	tipBox.style.top = tipYloc + "px";
}
