/***********************************************************************************************************************
 * Cool DHTML tooltip script II- © Dynamic Drive DHTML code library (www.dynamicdrive.com) This notice MUST stay intact
 * for legal use Visit Dynamic Drive at http://www.dynamicdrive.com/ for full source code
 **********************************************************************************************************************/

/*
 * Cool, maybe. Crappy, most definitely. 
 * 
 * Added some fixes/mods (daniel):
 * 1) No, you should not write to the DOM and start picking up what you wrote before the document is ready.
 * 2) Yes, it matters where you inject htlm into the document. <head> is not a nice place for <div>s.
 * 3) No, you should not override the entire onmousemove event handler, but rather append your handler to the chain.
 * 4) No, you should not reposition your overlay div onmousemove while the div is still hidden.
 * 5) No, you should not define generically named variables in global scope, such as 'ie' and 'ns6'.
 * 6) No, you should not break the JS engine if the target tooltip div for some reason fails to be picked up by assuming it has been later.
 * 7) No, it's not required, but using indentation and ending your statements with ; doesn't hurt, does it?
 * 
 */
var offsetfromcursorX = 12; // Customize x offset of tooltip
var offsetfromcursorY = 10; // Customize y offset of tooltip
var offsetdivfrompointerX = 10; // Customize x offset of tooltip DIV relative to pointer image
var offsetdivfrompointerY = 14; // Customize y offset of tooltip DIV relative to pointer image. Tip: Set it to (height_of_pointer_image-1).

var ddrive_ie = document.all;
var ddrive_ns6 = document.getElementById && !document.all;
var ddrive_enabletip = false;

var ddtipobj = null;
var ddpointerobj = null;

$(document).ready(function() {
	$('#body').append('<div id="dhtmltooltip"></div>'); // write out tooltip DIV
	$('#body').append('<img id="dhtmlpointer" src="arrow2.gif" style="visibility:hidden;">'); // write out pointer image
	
	if (ddrive_ie || ddrive_ns6)
		ddtipobj = document.all ? document.all["dhtmltooltip"] : document.getElementById ? document.getElementById("dhtmltooltip") : "";
	
	ddpointerobj = document.all ? document.all["dhtmlpointer"] : document.getElementById ? document.getElementById("dhtmlpointer") : "";
	
});

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

function ddrivetip(thetext, thewidth, thecolor)
{
	if (ddtipobj !== null)
	{
		if (typeof thewidth != "undefined") ddtipobj.style.width = thewidth + "px";
		if (typeof thecolor != "undefined" && thecolor != "") ddtipobj.style.backgroundColor = thecolor;
		ddtipobj.innerHTML = thetext;
		ddrive_enabletip = true;
		$(document).bind('mousemove', positiontip);
		return false;
	}
}

function positiontip(e)
{
	if (ddrive_enabletip)
	{
		var nondefaultpos = false
		var curX = (ddrive_ns6) ? e.pageX : event.clientX + ietruebody().scrollLeft;
		var curY = (ddrive_ns6) ? e.pageY : event.clientY + ietruebody().scrollTop;
		// Find out how close the mouse is to the corner of the window
		var winwidth = ddrive_ie && !window.opera ? ietruebody().clientWidth : window.innerWidth - 20;
		var winheight = ddrive_ie && !window.opera ? ietruebody().clientHeight : window.innerHeight - 20;

		var rightedge = ddrive_ie && !window.opera ? winwidth - event.clientX - offsetfromcursorX : winwidth - e.clientX - offsetfromcursorX;
		var bottomedge = ddrive_ie && !window.opera ? winheight - event.clientY - offsetfromcursorY : winheight - e.clientY - offsetfromcursorY;

		var leftedge = (offsetfromcursorX < 0) ? offsetfromcursorX * (-1) : -1000;

		// if the horizontal distance isn't enough to accomodate the width of the context menu
		if (rightedge < ddtipobj.offsetWidth)
		{
			// move the horizontal position of the menu to the left by it's width
			ddtipobj.style.left = curX - ddtipobj.offsetWidth + "px";
			nondefaultpos = true;
		}
		else if (curX < leftedge)
			ddtipobj.style.left = "5px";
		else
		{
			// position the horizontal position of the menu where the mouse is positioned
			ddtipobj.style.left = curX + offsetfromcursorX - offsetdivfrompointerX + "px";
			ddpointerobj.style.left = curX + offsetfromcursorX + "px";
		}
		
		// same concept with the vertical position
		if (bottomedge < ddtipobj.offsetHeight)
		{
			ddtipobj.style.top = curY - ddtipobj.offsetHeight - offsetfromcursorY + "px";
			nondefaultpos = true;
		}
		else
		{
			ddtipobj.style.top = curY + offsetfromcursorY + offsetdivfrompointerY + "px";
			ddpointerobj.style.top = curY + offsetfromcursorY + "px";
		}
		ddtipobj.style.visibility = "visible";
		if (!nondefaultpos)
			ddpointerobj.style.visibility = "visible";
		else
			ddpointerobj.style.visibility = "hidden";
	}
}

function hideddrivetip()
{
	if (ddtipobj !== null)
	{
		ddrive_enabletip = false;
		ddtipobj.style.visibility = "hidden";
		ddpointerobj.style.visibility = "hidden";
		ddtipobj.style.left = "-1000px";
		ddtipobj.style.backgroundColor = '';
		ddtipobj.style.width = '';
		$(document).unbind('mousemove', positiontip);
	}
}
