/*---------------------------------------------------------------------------------------------
DHTML Tooltips
version: 1.0
date: 3/10/2004

Copyright 2004: Lukas Koning
Website: http://www.lukaskoning.nl

You're free to distribute and use this script, however you may not do so for commercial
purposes. This file is provided as is; no warranties of any kind are given. If changes
are made to this file, clearly state what was changed and by whom. Distribution of this
file is permitted as long as this notice appears at the top of this file.
---------------------------------------------------------------------------------------------*/

//Array to store references
var LK_Tooltips = new Array();

//Constructor
function LK_Tooltip(sElementID, sContent) // [, iX, iY, iMouseBehavior, sCSSStyle]
{//Get element to attach tooltip to
this.oParent = document.getElementById(sElementID);

//Return if element not found
if(!this.oParent) { return; }

//Get position values or use default 10
this.iX = arguments[2] != null? arguments[2] : 10;
this.iY = arguments[3] != null? arguments[3] : 10;

//Get MouseBehavior (0 = Absolute position / 1 = Fixed position from cursor entry / 2 = Follow cursor)
this.iMouseBehavior = 0;
if(arguments[4]) { this.iMouseBehavior = arguments[4]; }

//Create div-element
this.oElement = document.createElement("DIV");

//Load style or use default-style
if(arguments[5]) { this.oElement.className = arguments[5]; }
else if(typeof(LK_TooltipCSSClass) != "undefined") { this.oElement.className = LK_TooltipCSSClass }
else if(typeof(LK_TooltipCSSStyle) != "undefined") { this.oElement.style.cssText = LK_TooltipCSSStyle; }
else { this.oElement.style.cssText = "border: 1px solid black; padding: 1px; background-color: #FFFFCC; white-space: nowrap;"; }

//Set positioning CSS attributes
this.oElement.style.position = "absolute";
if(!this.iMouseBehavior) //Set absolute position
{this.oElement.style.left = this.iX + "px";
this.oElement.style.top = this.iY + "px";
}

//Set visibility
this.oElement.style.visibility = "hidden";

//Add tooltip content and append element to the document
this.oElement.innerHTML = sContent;
document.body.appendChild(this.oElement);

//Create reference
this.ID = LK_Tooltips.length;
LK_Tooltips[this.ID] = this;

//Set up methods
this.show = LK_Tooltip_Show;
this.hide = LK_Tooltip_Hide;
this.move = LK_Tooltip_Move;

//Set up mousehandlers on parent
this.oParent.onmouseover = new Function("e", "if(!e) var e = window.event; LK_Tooltips[" + this.ID + "].show(e);");
this.oParent.onmouseout = new Function("e", "if(!e) var e = window.event; LK_Tooltips[" + this.ID + "].hide(e);");

//If tracking cursor create appropriate handler
if(this.iMouseBehavior == 2)
{this.oParent.onmousemove = new Function("e", "if(!e) var e = window.event; LK_Tooltips[" + this.ID + "].move(e);");
}

}

//Shows the tooltip
function LK_Tooltip_Show(e)
{//Set visibility
this.oElement.style.visibility = "visible";

if(this.iMouseBehavior != 0)
{//Get document & div dimensions
this.iSWidth = parseInt(document.body.clientWidth);
this.iSHeight = parseInt(document.body.clientHeight);
this.iWidth = parseInt(this.oElement.offsetWidth);
this.iHeight = parseInt(this.oElement.offsetHeight);

//Position tooltip
this.move(e);
}
}

//Hides tooltip
function LK_Tooltip_Hide(e)
{//Set visibility
this.oElement.style.visibility = "hidden";
}

//Moves tooltip with cursor
function LK_Tooltip_Move(e)
{var iPosX = 0; var iPosY = 0;

//Get position of cursor
if(e.pageX || e.pageY) { iPosX = e.pageX; iPosY = e.pageY; }
else if(e.clientX || e.clientY) { iPosX = e.clientX + document.body.scrollLeft; iPosY = e.clientY + document.body.scrollTop; }

//Check document width
if(iPosX + this.iX + this.iWidth > this.iSWidth && iPosX - this.iX - this.iWidth > 0)
{iPosX = iPosX - this.iX - this.iWidth;
}
else { iPosX += this.iX; }

//Check document height
if(iPosY + this.iY + this.iHeight > this.iSHeight && iPosY - this.iY - this.iHeight > 0)
{iPosY = iPosY - this.iY - this.iHeight;
}
else { iPosY += this.iY; }

//Change tooltip position
this.oElement.style.left = iPosX + "px";
this.oElement.style.top = iPosY + "px";
}
