
////////////////  GLOBAL TOOPTIP CONFIGURATION  /////////////////////
var tt_overDelay    = 0;				// time span until tooltip shows up (milliseconds)
var tt_outDelay     = 200;				// time span until tooltip disappears (disappears immediately if another tooltip is activated)
var tt_offsetX		= 18;
var tt_tags         = new Array();	// list of tags to look for (may be extended or shortened)
/////////////////////////////////////////////////////////////////////



/////////////////////  INTERNAL VARIABLES  //////////////////////////
var tt_object = null;				// current tooltip
var tt_winCoords = null;			// current coordinates of window, updated when tooltip shown
var tt_elementCoords = null;		// coordinates of object tooltip applies to (for moving)
var tt_showTimeout = null;			// timeout id
var tt_hideTimeout = null;			// timeout id
var tt_count = 56789;				// for numbering layers and zindex
/////////////////////////////////////////////////////////////////////




// scans all tags for tooltips and creates them
function tt_Init()
{
	// loop through all tags we're looking for
	var i = tt_tags.length;
	while (i--) {
		// get tags of that type
		tags = document.getElementsByTagName(tt_tags[i]);
		
		// loop through tags
		var j = tags.length;
		while (j--) {
			// get tooltip text if set
			toolTipText = "";
			if (tags[j].getAttribute("tooltiptext"))
				toolTipText = tags[j].getAttribute("tooltiptext");
	
			// add mouseover and mouseout actions if we find tooltip text
			if (toolTipText != "") {
				tt_AttachToolTip(tags[j], toolTipText);
			}
		}
	}
	
	if (document.addEventListener) {
		document.addEventListener("mousemove", tt_Move, false);
	} else {
		document.attachEvent("onmousemove", tt_Move);
	}
}

function tt_AttachToolTip(element, toolTipText) {
	toolTipId = "tt_" + tt_count;
	div = document.createElement("div");
	div.id = toolTipId;
	div.style.position = 'absolute';
	div.style.zIndex = tt_count;
	div.style.visibility = 'hidden';
	div.innerHTML = unescape(toolTipText);
	document.body.appendChild(div);

	onMouseOver = element.getAttribute("onmouseover");
	onMouseOut = element.getAttribute("onmouseout");
	
	element.onmouseover = new Function('e',
		'tt_Show(e, "tt_' + tt_count + '", this);' + onMouseOver
	);
	element.onmouseout = new Function('e',
		'tt_Hide();' + onMouseOut
	);
	
	tt_count++;
}

// generates HTML for a single tooltip
function tt_CreateToolTipDiv(toolTipId, toolTipText)
{
	var html;
	html  = '<div id="' + toolTipId + '" style="position:absolute;z-index:' + (tt_count++) + ';left:0px;top:0px;visibility:hidden;">';
	html += toolTipText;
	html += '<\/div>';
	return html;
}

// called during mouseover of a tooltipped element
function tt_Show(e, toolTipId, element)
{
	// if there is currently a tip showing
	var overDelay = tt_overDelay;
	
	if (tt_showTimeout) {	// element was moused-over, but not shown yet
		if (tt_object.id == toolTipId) {	// same element
			return;
		} else {
			clearTimeout(tt_showTimeout);
			tt_showTimeout = null;
			tt_object = null;
		}
		overDelay = 0;
	} else if (tt_hideTimeout) {
		if (tt_object.id == toolTipId) {	// in process of hiding tooltip for this element
			clearTimeout(tt_hideTimeout);
			tt_hideTimeout = null;
			return;
		} else {
			clearTimeout(tt_hideTimeout);
			tt_HideToolTip();
		}
		overDelay = 0;
	}
	
	// save tooltip object we're showing
	tt_object = document.getElementById(toolTipId);
	
	// get current window coords
	tt_GetWindowCoords();
	
	// determine where to show the tooltip
	tt_elementCoords = tt_GetAbsoluteCoords(element);
	var maxWidth = tt_elementCoords.x2 - tt_elementCoords.x1 - 100;
	if (maxWidth < 300) maxWidth = 300
	tt_object.style.maxWidth = "" + maxWidth + "px";
	
	if (tt_object.className == "") {
		tt_object.className = "tooltip";
	}
	var tooltipHeight = tt_object.offsetHeight;
	if (tt_elementCoords.y2 + tooltipHeight <= tt_winCoords.y2) {	// show below
		tt_elementCoords.y1 += (tt_elementCoords.y2 - tt_elementCoords.y1) - 1;
	} else {	// show above if fits, otherwise below and let it get cut off
		if (tt_elementCoords.y1 - tooltipHeight >= tt_winCoords.y1) {
			tt_elementCoords.y1 -= tooltipHeight;
		} else {
			tt_elementCoords.y1 += (tt_elementCoords.y2 - tt_elementCoords.y1) - 1;
		}
	}
	
	tt_object.style.top = "" + (tt_elementCoords.y1-1) + "px";
	
	var posx = 0;
	if (!e) var e = window.event;
	if (e.pageX) 	{
		posx = e.pageX;
	} else if (e.clientX) {
		posx = e.clientX + document.body.scrollLeft + document.documentElement.scrollLeft;
	}
	tt_SetXPos(posx);

	// show
	if (overDelay == 0) {
		tt_ShowToolTip();
	} else {
		tt_showTimeout = setTimeout('tt_ShowToolTip();', tt_overDelay);
	}
}

// called during mouseout of a tooltipped element
function tt_Hide()
{
	if (tt_object) {
		if (tt_showTimeout) {	// element was moused-over, but not shown yet
			clearTimeout(tt_showTimeout);
			tt_showTimeout = null;
			tt_object = null;
		} else {
			if (!tt_hideTimeout) {
				tt_hideTimeout = window.setTimeout('tt_HideToolTip();', tt_outDelay);
			}
		}
	}
}

// toggle visibility of active tooltip, called by tt_Show and tt_Hide
function tt_ShowToolTip()
{
	tt_object.style.visibility = 'visible';
	tt_showTimeout = null;
}

function tt_HideToolTip()
{
	tt_object.style.visibility = 'hidden';
	tt_object = null;
	tt_hideTimeout = null;
}

// handles mousemove events
function tt_Move(e)
{
	if (!tt_object)
		return;
	
	var posx = 0;
	if (!e) var e = window.event;
	if (e.pageX) 	{
		posx = e.pageX;
	} else if (e.clientX) {
		posx = e.clientX + document.body.scrollLeft + document.documentElement.scrollLeft;
	}
	if (posx < tt_elementCoords.x1 || posx > tt_elementCoords.x2)
		return;
	
	// set x position
	tt_SetXPos(posx);
}

// sets x position of current tooltip
function tt_SetXPos(mouseX)
{
	var tooltipWidth = tt_object.offsetWidth;
	var x = mouseX + tt_offsetX;
	if (x+tooltipWidth > tt_winCoords.x2-2)
		x = tt_winCoords.x2-tooltipWidth-2;
	else if (x > tt_elementCoords.x2-tt_offsetX)
		x = tt_elementCoords.x2-tt_offsetX;
	
		
	//if (x < tt_elementCoords.x1)
	//	x = tt_elementCoords.x1;
	tt_object.style.left = "" + x + "px";
}

// retrieve absolute coordinates of the element to apply the tooltip to
function tt_GetAbsoluteCoords(element) {
	var coords = {
		x1: element.offsetLeft,
		y1: element.offsetTop,
		x2: element.offsetLeft + element.offsetWidth,
		y2: element.offsetTop + element.offsetHeight
	};
	
	if (element.offsetParent) {
		var temp = tt_GetAbsoluteCoords(element.offsetParent);
		coords.x1 += temp.x1;
		coords.y1 += temp.y1;
		coords.x2 += temp.x1;
		coords.y2 += temp.y1;
	}
	
	return coords;
};

// retrieve coordinate of current viewport
function tt_GetWindowCoords() {
	var layer = document.body;
	tt_winCoords = {
		x1: layer.scrollLeft,
		y1: layer.scrollTop,
		x2: layer.scrollLeft + layer.clientWidth,
		y2: layer.scrollTop + layer.clientHeight
	};
}
