/*
Created by Knaìis... http://knagis.miga.lv/
*/
var tooltipDiv = null;
var tooltipHideTime = 100; //how long in miliseconds to wait before hiding the tooltip after it lost focus
var tooltipHideTimer;
var tooltipActiveElement;

function createTooltipDiv()
{
	if (!document.body) return false;
	if (typeof(document.readyState)!="undefined")
	{
		if (document.readyState!="complete") return false;
	}
	tooltipDiv = document.createElement("SPAN");
	tooltipDiv.style.visibility = "hidden";
	tooltipDiv.style.position = "absolute";
	tooltipDiv.className = "tooltip";
	document.body.appendChild(tooltipDiv);
	tooltipDiv.style.top = "0px";
	tooltipDiv.style.left = "0px";
	 //don't need this, because it is for the case when the tooltip do not move with mouse.
	/*tooltipDiv.onclick = hideTooltip;
	tooltipDiv.onmouseover = function()
	{
		if (tooltipHideTimer)
		{
			window.clearTimeout(tooltipHideTimer);
			tooltipHideTimer = 0;
		}
	}
	tooltipDiv.onmouseout = function()
	{
		tooltipHideTimer = window.setTimeout("hideTooltip()",tooltipHideTime);
	}
	*/
	return true;
}

function positionTooltip(ev)
{
	if (!ev) ev = window.event;
	tooltipDiv.style.top = (getScrollY() + ev.clientY + 16) + "px";
	tooltipDiv.style.left = (getScrollX() + ev.clientX) + "px";
	if (tooltipDiv.offsetLeft+tooltipDiv.offsetWidth > document.body.offsetWidth)
	{
		tooltipDiv.style.left = getScrollX() + ev.clientX - tooltipDiv.offsetWidth;
	}
	if (tooltipDiv.offsetTop+tooltipDiv.offsetHeight > document.body.offsetHeight + getScrollY())
	{
		tooltipDiv.style.top = ev.clientY - tooltipDiv.offsetHeight;
	}
}

function tooltip(ev,text,element)
{
	if (!ev) ev = window.event;
	if (!tooltipDiv)
	{
		if (!createTooltipDiv()) return;
	}
	if (!text) return;
	if (tooltipHideTimer)
	{
		window.clearTimeout(tooltipHideTimer);
		tooltipHideTimer = 0;
	}
	
	if (tooltipActiveElement)
	{
		tooltipActiveElement.onmouseout = function(){};
	}
	element.onmouseout = function()
	{
		tooltipHideTimer = window.setTimeout("hideTooltip()",tooltipHideTime);
	}
	tooltipActiveElement = element;

	tooltipDiv.innerHTML = text;

	positionTooltip(ev);
	
	//comment out this line if you do not want the tooltip to move with mouse
	document.body.onmousemove = function(event){positionTooltip(event)};

	tooltipDiv.style.visibility="visible";
}

function hideTooltip()
{
	if (tooltipHideTimer)
	{
		window.clearTimeout(tooltipHideTimer);
		document.body.onmousemove = function(){}; 
	}
	tooltipDiv.style.visibility="hidden";
	tooltipDiv.style.top="0px";
	tooltipDiv.style.left="0px";
	
	if (tooltipActiveElement)
	{
		tooltipActiveElement.onmouseout = function (){};
		tooltipActiveElement = null;
	}
}

function getScrollX()
{
	var a;
	if (window.pageXOffset)
		a = parseInt(window.pageXOffset);
	else if (document.body && document.body.scrollLeft)
		a = parseInt(document.body.scrollLeft);
	else if (document.documentElement && document.documentElement.scrollLeft)
		a = parseInt(document.documentElement.scrollLeft);
	if (!a) a=0;
	return a;
}
function getScrollY()
{
	var a;
	if (window.pageYOffset)
		a = parseInt(window.pageYOffset);
	else if (document.body && document.body.scrollTop)
		a = parseInt(document.body.scrollTop);
	else if (document.documentElement && document.documentElement.scrollTop)
		a = parseInt(document.documentElement.scrollTop);
	if (!a) a=0;
	return a;
}
