/* ----------------------------------------------------------------------
	Methods to show tooltip at the position of mouse pointer
-----------------------------------------------------------------------	*/

var __tooltipOffsetX = -20;		// customize X offset of tooltip
var __tooltipOffsetY = 15;			// customize Y offset of tooltip
var __tooltipIsIE = document.all;
var __tooltipIsNS = document.getElementById && !document.all;
var __tooltipIsEnabled = false;
var __tooltipIsLoaded = false;
var __tooltipObj;
var __tooltipAjax;

function __ieTrueBody()
{
	return (document.compatMode && document.compatMode != "BackCompat") ? document.documentElement : document.body;
}

// create tooltip layer
function __createTooltip()
{
	if(document.getElementById('vieTooltip'))
		return;
	var cssClass = arguments[0]||'cssDefaultHintBox';
	try
	{		
		__tooltipObj = document.createElement('DIV'); 
		__tooltipObj.className = cssClass;
		__tooltipObj.id = 'vieTooltip';		
		__tooltipObj.style.display = 'block';
		__tooltipObj.style.position = 'absolute';
		document.body.appendChild(__tooltipObj);
	}
	catch(e)
	{
		return;
	}
}

// show tooltip
function __showTooltip(tooltip)
{	
	var cssClass = arguments[1]||'cssDefaultHintBox';
	var width = arguments[2]||500;
	if(document.readyState == "complete" || !__tooltipIsIE)
	{
		__createTooltip(cssClass);
		if(!__tooltipIsLoaded)
		{
			__tooltipObj.innerHTML = tooltip;
			__tooltipObj.style.zIndex = 1000;
			__tooltipObj.style.width = width;
			__tooltipIsLoaded = true;
		}
		__tooltipIsEnabled = true;
	}
}

// show tooltip with content of a layer
function __showTooltipOfLayer(tooltipLayerId)
{
	var tooltipLayer = document.getElementById(tooltipLayerId);
	if(!tooltipLayer) return;
	var cssClass = arguments[1]||'cssDefaultHintBox';
	var width = arguments[2]||500;
	__showTooltip(tooltipLayer.innerHTML, cssClass, width);
}

// show tooltip with content is loaded from AJAX object
function __showTooltipOfAjax(ajaxUrl)
{
	if(!ajaxUrl || ajaxUrl=='') return;
	var cssClass = arguments[1]||'cssDefaultHintBox';
	var width = arguments[2]||500;
	var ajaxFunction = "__showAjaxTooltip('" + cssClass + "'," + width + ");";
	__tooltipAjax = new Ajax(ajaxFunction, true);
	__tooltipAjax.makeGETRequest(ajaxUrl, '');
}

function __showAjaxTooltip(cssClass, width)
{
	if(!__tooltipAjax.isCompleted())
		return;
	__showTooltip(__tooltipAjax.getResponseText(), cssClass, width);
}

// hide tooltip
function __hideTooltip()
{
	if(!__tooltipObj || (!__tooltipIsNS && !__tooltipIsIE))
		return;
	
	__tooltipIsEnabled = false;
	__tooltipIsLoaded = false;
	
	__tooltipObj.style.visibility = "hidden";
	__tooltipObj.style.left = "-1000px";
	__tooltipObj.style.backgroundColor = '';
	__tooltipObj.style.width = '';
}

// re-positioning the tooltip
function __positionTooltip(e)
{
	if (!__tooltipIsEnabled)
		return;
		
	var curX = (__tooltipIsNS) ? e.pageX : event.clientX + __ieTrueBody().scrollLeft;
	var curY = (__tooltipIsNS) ? e.pageY : event.clientY + __ieTrueBody().scrollTop;
	
	// find out how close the mouse is to the corner of the window
	var rightEdge  = __tooltipIsIE&&!window.opera ? __ieTrueBody().clientWidth-event.clientX - __tooltipOffsetX : window.innerWidth - e.clientX - __tooltipOffsetX - 20;
	var bottomEdge = __tooltipIsIE&&!window.opera ? __ieTrueBody().clientHeight-event.clientY - __tooltipOffsetY : window.innerHeight - e.clientY - __tooltipOffsetY - 20;
	var leftEdge = (__tooltipOffsetX<0) ? __tooltipOffsetX*(-1) : -1000;

	// if the horizontal distance isn't enough to accomodate the width of the context menu	
	if (rightEdge < __tooltipObj.offsetWidth) // move the horizontal position of the menu to the left by it's width
		__tooltipObj.style.left = __tooltipIsIE ? __ieTrueBody().scrollLeft + event.clientX - __tooltipObj.offsetWidth + "px" : window.pageXOffset + e.clientX - __tooltipObj.offsetWidth + "px";
		
	else if (curX < leftEdge)
		__tooltipObj.style.left = "5px";
		
	else //position the horizontal position of the menu where the mouse is positioned
		__tooltipObj.style.left = curX + __tooltipOffsetX + "px";

	// same concept with the vertical position
	if (bottomEdge < __tooltipObj.offsetHeight)
		__tooltipObj.style.top = __tooltipIsIE ? __ieTrueBody().scrollTop + event.clientY - __tooltipObj.offsetHeight - __tooltipOffsetY + "px" : window.pageYOffset + e.clientY - __tooltipObj.offsetHeight - __tooltipOffsetY + "px";
	else
		__tooltipObj.style.top = curY + __tooltipOffsetY + "px";

	// show the layer		
	__tooltipObj.style.visibility = "visible";
}

// attach window event
document.onmousemove = __positionTooltip;
