var offsetxpoint = -60; //Customize x offset of tooltip
var offsetypoint = 20; //Customize y offset of tooltip
var ie = document.all;
var ns6 = document.getElementById && !document.all;
var enabletip = false;
var tip_text = '';
var tip_color = '';
var tip_width = '';
var tip_timeid;
var tip_lastevent;
var tip_lastX;
var tip_lastY;

if (ie || ns6)
	var tipobj = (document.all ? document.all["dhtmltooltip"] : 
				(document.getElementById ? document.getElementById("dhtmltooltip") : ""));

function ietruebody()
{
	return ((document.compatMode && document.compatMode != "BackCompat")
		? document.documentElement : document.body);
}

function toggletip(thetext, thecolor, thewidth)
{
	if (tipobj.style.visibility == "visible")
		hideddrivetip();
	else
		ddrivetip(thetext, thecolor, thewidth);
}

function ddrivetip(thetext, thecolor, thewidth)
{
	if (ns6||ie)
	{
		if (typeof thewidth != "undefined")
			tip_width = thewidth + "px";
		else
		    tip_width = "undefined";
		if (typeof thecolor != "undefined" && thecolor != "")
			tip_color = thecolor;
		else
		    tip_color = "undefined";
		tip_text = thetext;

        tip_timeid = setTimeout("ddrivetipdelay()", 1000);

		return false;
	}
}

function ddrivetip_instant(thetext, thecolor, thewidth)
{
	if (ns6||ie)
	{
		if (typeof thewidth != "undefined")
			tip_width = thewidth + "px";
		else
		    tip_width = "undefined";
		if (typeof thecolor != "undefined" && thecolor != "")
			tip_color = thecolor;
		else
		    tip_color = "undefined";
		tip_text = thetext;

        ddrivetipdelay();

		return false;
	}
}

function ddrivetipdelay()
{
	if ((ns6 || ie) && tip_text != "")
	{
		if (tip_width != "undefined")
			tipobj.style.width = tip_width;
		if (tip_color != "undefined" && tip_color != "")
			tipobj.style.backgroundColor = tip_color;
		tipobj.innerHTML = tip_text;
				
		enabletip = true;
		positiontip(tip_lastevent);

		return false;
	}
}

function positiontip(e)
{
    tip_lastevent = e;
    
    if (!ns6 && event != null)
    {
        tip_lastX = event.x;
        tip_lastY = event.y;
    }
    
	if (enabletip)
	{
	    var curX = (ns6 ? e.pageX : tip_lastX); // + ietruebody().scrollLeft);
		var curY = (ns6 ? e.pageY : tip_lastY); // + ietruebody().scrollTop);
		
		//Find out how close the mouse is to the corner of the window
		var rightedge = (ie && !window.opera ? 
			ietruebody().clientWidth : 
			window.innerWidth);
		var bottomedge = (ie && !window.opera ? 
			ietruebody().clientHeight :
			window.innerHeight);
		var leftedge = (offsetxpoint < 0 ? offsetxpoint * (-1) : -1000);

		//if the horizontal distance isn't enough to accomodate the width of the context menu
		if (rightedge < curX + tipobj.offsetWidth - (ie ? ietruebody().scrollLeft : window.pageXOffset))
			//move the horizontal position of the menu to the left by it's width
			tipobj.style.left = (ie ? 
				ietruebody().scrollLeft + tip_lastX - tipobj.offsetWidth + "px" : 
				window.pageXOffset + e.clientX - tipobj.offsetWidth + "px");
		else if (curX < leftedge)
			tipobj.style.left = "5px";
		else
			//position the horizontal position of the menu where the mouse is positioned
			tipobj.style.left = curX + offsetxpoint + "px";

		//same concept with the vertical position
        if (bottomedge < curY + tipobj.offsetHeight - (ie ? ietruebody().scrollTop : window.pageYOffset))
		{
			var ycoord = (ie ?
				(ietruebody().scrollTop + tip_lastY - tipobj.offsetHeight - offsetypoint) + "px" :
				(window.pageYOffset + e.clientY - tipobj.offsetHeight - offsetypoint) + "px");
			if (ie && ycoord < ietruebody().scrollTop)
				ycoord = ietruebody().scrollTop;
			else if (!ie && ycoord < window.pageYOffset)
				ycoord = window.pageYOffset;
			if (ycoord < 0) ycoord = 0;
		    tipobj.style.top = ycoord;
		}
		else
			tipobj.style.top = curY + offsetypoint + "px";

		tipobj.style.visibility = "visible";
	}
}

function hideddrivetip()
{
	if (ns6 || ie)
	{
		enabletip = false;
		tipobj.style.visibility = "hidden";
		tipobj.style.left = "-1000px";
		tipobj.style.backgroundColor = '';
		tipobj.style.width = '';
		tipobj.innerHTML = "&nbsp;";
		tip_text = "";
		tip_color = "";
		tip_width = "";
		clearTimeout(tip_timeid);
	}
}

document.onmousemove = positiontip;