var offsetxpoint=5 //Customize x offset of tooltip
var offsetypoint=10 //Customize y offset of tooltip
var enabletip = false;

function tooltip(thetext){
	if( !document.getElementById ) return false;
	if( !document.getElementById('tooltipText') ) return false;
	
	document.getElementById('tooltipText').innerHTML = unescape(thetext);
	enabletip = true;
	document.getElementById('tooltipText').style.visibility = "visible";
	positiontip
	return true;
}

function ietruebody(){
	return (document.compatMode && document.compatMode!="BackCompat")? document.documentElement : document.body
}

function positiontip(e){
	if (!e) var e = window.event;
	
	if( !document.getElementById ) return false;
	if( !document.getElementById('tooltip_container') ) return false;
	if( !document.getElementById('tooltip') ) return false;
	if( !document.getElementById('tooltipText') ) return false;
	if( !(e.clientX && e.clientY) ) return false;
	
	//var contentobj=document.all? document.all["contents"] : document.getElementById? document.getElementById("contents") : ""
	var tipContainer = document.getElementById('tooltip_container');
	var tipBox = document.getElementById('tooltip');
	var tipText = document.getElementById('tooltipText');
	var statusBox = document.getElementById('status');
	

	
	var curX = e.clientX + ietruebody().scrollLeft;
	var curY = e.clientY + ietruebody().scrollTop;
	
	//Find out how close the mouse is to the corner of the window
	if( window.innerWidth && window.innerHeight ) // codepath taken by firefox
	{
		var rightedge = window.innerWidth + ietruebody().scrollLeft - 20;
		var bottomedge = window.innerHeight + ietruebody().scrollTop;
	}
	else // codepath taken by IE6
	{
		var rightedge = ietruebody().offsetWidth + ietruebody().scrollLeft - 20;
		var bottomedge = ietruebody().offsetHeight + ietruebody().scrollTop;
	}
	
	/*statusBox.innerHTML = "curY: " + curY + "px<br/>" 
						+ "rightedge: " + rightedge + "px<br/>" 
						+ "bottomedge: " + bottomedge + "px<br/>" 
						//+ "document.body.scrollTop: " + ietruebody().scrollTop + "px<br/>"
						//+ "document.body.offsetHeight: " + ietruebody().offsetHeight + "px<br/>" 
						//+ "window.innerHeight: " + window.innerHeight + "px<br/>" 
						//+ "document.body.clientHeight: " + ietruebody().clientHeight + "px<br/>"
						;*/
	if (enabletip){
		
		
		var leftedge = (offsetxpoint < 0) ? offsetxpoint*(-1) : -1000;
		
		//if the horizontal distance isn't enough to accomodate the width of the context menu
		if ( curX < leftedge )
		{
			tipContainer.style.left="5px";
			tipContainer.style.right="";
		}
		else if ( curX > rightedge - tipBox.offsetWidth - 5 )
		{
			tipContainer.style.left = rightedge - tipBox.offsetWidth - 25 + "px";
			//tipContainer.style.right="5px";
		}
		else
		{
			//position the horizontal position of the menu where the mouse is positioned
			tipContainer.style.left = curX + offsetxpoint + "px";
			tipContainer.style.right = "";
		}
		
		//same concept with the vertical position
		if ( curY > bottomedge - tipText.offsetHeight - 20 )
		{
			tipContainer.style.top = bottomedge - tipText.offsetHeight - 25 +"px";
		}
		else
		{
			tipContainer.style.top = curY + offsetypoint+"px";
		}
		tipContainer.style.visibility = "visible";
	}
}

function hidetip(){
	if( !document.getElementById ) return false;
	if( !document.getElementById('tooltip_container') ) return false;
	
	var tipobj = document.getElementById('tooltip_container');

	enabletip = false;
	tipobj.style.visibility = "hidden";
	tipobj.style.left = "-1000px";

}

addLoadEvent(function() {
document.onmousemove = positiontip});