/* R. Wright Tooltips Javascript A Free Snippet I found on the web.
I modified this script to support passing in a reference to a div that contains the 
HTML text to display in the message-box div instead of declaring static anchor references
in the script. Added a delay timer to allow the msg box to stick around for a few seconds.
 */

var box;  //reference to tooltip box
var Xoffset= 0;    // modify these values to
var Yoffset= 20;    // change the popup position.
var showDelay = 100;   //Delay before showing tip
var autoKill =6000;// 2000;   //Delay to auto close;
var hideDelay=2000; //Delay closing even after mouseout so user can click content in the div.
var showTimer, killTimer,delayTimer; //timer handles
var mouseTracking = false;   //if true tooltip tracks mouse movement

function delayHide(){
//delay hiding the toolTip after mouseout so user can click on link in message-box, etc.

}
function setMessage(){
    /* get reference to tooltip message-box */
    if (document.getElementById){
        box = document.getElementById("message-box");
    }
    else if (document.all) {
        box = document.all.message-box;
    }
    else if (document.layers) {
        box = document.layers.message-box;
    }
    else {
        box = null;
    }

    //if (box) {
        //used for static anchors
    //}
}//eof setMessage

function popup(e, bkcolor){

    if (!e) e = window.event;  //event object IE or DOM
    if (!box) {return;} //Have tip box reference
    clearTimers();  //Clear any timers from previous tip
    /* Get reference to element triggering request for tip. */
    var obj = e.target || e.srcElement;
    while (obj.nodeType != 1) {
        /*  Some browsers register event on the text node (target or srcElement)
            so there is a loop to the first element. Example is Konqueror 3.2.
        */
        obj = obj.parentNode || obj.parentElement;
    }

    if (mouseTracking) {
        /* When following mouse */
        document.onmousemove=followMouse;
    }
    else {
        /* Set top-left */
        var coordinates = getCoordinates(obj);
        box.style.left = coordinates.Xoffset + 'px';
        box.style.top = coordinates.Yoffset + 'px';
    }

    box.style.backgroundColor = bkcolor;
//rw use the getAttribute method of the object if you pass a reference to the object
//else change the code to box.innerHTML=obj.toolTip if you pass the string name of
//an object. toolTip is our custom attribute defined for each item in the page .
    if (typeof box.innerHTML != "undefined") {
        box.innerHTML=obj.getAttribute("toolTip");
    }
    else if (document.layers) {
        box.document.write(obj.toolTip);
        box.document.close();
    }

    if (showDelay > 0) showTimer = setTimeout("showTip()", showDelay);

}//eof popup

/* Get toolTip X,Y when not tracking mouse */
function getCoordinates(obj) {

    var xy = getObjectUpperLeft(obj);

    /* Adjust position for screen right and bottom screen edge */
    var x = fitWindowWidth(xy.x + Xoffset);
    var y = fitWindowHeight(xy.y + Yoffset);

    /* return object with coordinates as properties */
    return {Xoffset: x, Yoffset: y};
}//eof getCoordinates

function getObjectUpperLeft(obj){
    /* For postioning in reference to link */

    var x = obj.offsetLeft;
    var y = obj.offsetTop;

    /* Calculate page X,Y of upper left corner of element
        where toolTip is to be shown
    */
    obj = obj.offsetParent;
    while (obj) {
        x += obj.offsetLeft;
        y += obj.offsetTop;

        if (typeof obj.clientLeft != "undefined" && obj.tagName != "BODY") {
                /*MS IE doesn't include borders in offset values;
                these are obtained with clientLeft and Top and added in*/
                x += obj.clientLeft;
                y += obj.clientTop;
        }

        if (obj.tagName == "HTML") break; //KHTML KDE has an unidentified object above html
        obj = obj.offsetParent;
    }//endwhile

    return {x:x, y:y};
}//eof getObjectUpperLeft

function fitWindowWidth(tipX) {
    /* Determine best page X that keeps object in window. If object
     * doesn't fit adjust to left edge of window.
     * Compare object X coordinate to max X not going out
     * of window and use left most.
     * Then check object X is not past left most visible
     * page coordinate.
     */

    /*Don't go past right edge of window */

    var rightMaxX = getRightPagePos() - (box.offsetWidth + 16); //16 for scrollbar

    tipX = (rightMaxX < tipX) ? rightMaxX : tipX;

    /* But, don't go past left edge of window either */
    var leftMinX = getLeftPagePos();
    tipX = (tipX < leftMinX) ? leftMinX : tipX;

    return tipX;
}//eof fitWindowWidth

function getRightPagePos() {
    /* Determine page offset at right of screen (it's different than window width)
     * Here the pixles the page has been scrolled left is added to window width
     */
    var nRight;

    if (typeof window.srcollX != "undefined") {
        //"NN6+ FireFox, Mozilla etc."
        nRight = window.innerWidth + window.scrollX;
    }
    else if (typeof window.pageXOffset != "undefined") {
        //NN4 code still in NN6 + but scrollX was added
        nRight = window.innerWidth + window.pageXOffset;
    }
    else if (document.documentElement && document.documentElement.clientWidth){
        //document.compatMode == "CSS1Compat" that is IE6 standards mode"
        nRight = document.documentElement.clientWidth + document.documentElement.scrollLeft;
    }
    else if (document.body && document.body.clientWidth) {
        //document.compatMode != "CSS1Compat" that is quirks mode IE 6 or IE < 6 and Mac IE
        nRight = document.body.clientWidth + document.body.scrollLeft;
    }

    return nRight;
}//eof getRightPagePos

function getLeftPagePos() {
    var nLeft;

    if (typeof window.srcollX != "undefined") {
        //"NN6+ FireFox, Mozilla etc."
        nLeft = window.scrollX;
    }
    else if (typeof window.pageXOffset != "undefined") {
        //NN4 code still in NN6 + but scrollX was added
        nLeft = window.pageXOffset;
    }
    else if (document.documentElement && document.documentElement.scrollLeft){
        //document.compatMode == "CSS1Compat" that is IE6 standards mode"
        nLeft = document.documentElement.scrollLeft;
    }
    else if (document.body && document.body.scrollLeft) {
        //document.compatMode != "CSS1Compat" that is quirks mode IE 6 or IE < 6 and Mac IE
        nLeft = document.body.scrollLeft;
    }

    return nLeft;
}//eof getLeftPagePos

function fitWindowHeight(tipY) {
    /* Compare calculated max acceptable page offset to toolTip X and return smallest */

    /* Don't go below bottom of window. Put above target if moved. */
    var bottomMaxY = getBottomPagePos() - (box.offsetHeight);
    tipY = (bottomMaxY < tipY ) ? tipY - (Yoffset + box.offsetHeight) : tipY;

    /* But, don't go past the top of window either */
    var topMinY = getTopPagePos();
    tipY = (tipY < topMinY) ? topMinY : tipY;

    return tipY
}//eof fitWindowHeight

function getBottomPagePos() {
    /* Determine page offset at bottom of screen (it's different than window height)
     * Here the pixles the page has been scrolled up is added to window height
     */
    var nBottom;

    if (typeof window.scrollY != "undefined" ) {
        //NN6+ FireFox, Mozilla etc.
        nBottom = window.innerHeight + window.scrollY;
    }
    else if (typeof window.pageYOffset != "undefined") {
        //NN4 still in NN6 + but NN6 and Mozilla added scrollY
        nBottom = window.innerHeight + window.pageYOffset;
    }
    else if (document.documentElement && document.documentElement.clientHeight){
        //document.compatMode == "CSS1Compat" that is IE6 standards mode
        nBottom = document.documentElement.clientHeight + document.documentElement.scrollTop;
    }
    else if (document.body && document.body.clientHeight) {
        //document.compatMode != "CSS1Compat" that is quirks mode IE 6 or IE < 6 and Mac IE
        nBottom = document.body.clientHeight + document.body.scrollTop;
    }
    return nBottom;
}//eof getBottomPagePos

function getTopPagePos() {
    var nTop;

    if (typeof window.scrollY != "undefined" ) {
        //NN6+ FireFox, Mozilla etc.
        nTop= window.scrollY;
    }
    else if (typeof window.pageYOffset != "undefined") {
        //NN4 still in NN6 + but NN6 and Mozilla added scrollY
        nTop = window.pageYOffset;
    }
    else if (document.documentElement && document.documentElement.scrollTop){
        //document.compatMode == "CSS1Compat" that is IE6 standards mode
        nTop = document.documentElement.scrollTop;
    }
    else if (document.body && document.body.scrollTop) {
        //document.compatMode != "CSS1Compat" that is quirks mode IE 6 or IE < 6 and Mac IE
        nTop = document.body.scrollTop;
    }
    return nTop;
}//eof getTopPagePos
function showTip(){
    box.style.visibility='visible';
    if (autoKill > 0) killTimer = setTimeout("kill()", autoKill);
}//eof showTip

function followMouse(e){
    /* Get mouse coordinates */

    if (box){
        var x = (e) ? e.pageX : event.clientX + document.body.scrollLeft;
        var y = (e) ? e.pageY : event.clientY + document.body.scrollTop;

        //Delete next line, it is just for demo page
        //document.mouse.trackingStatus.value = "X: " + x + ", Y: " + y;

        /* Adjust position for screen right and bottom screen edge */
        x = fitWindowWidth(x + Xoffset);
        y = fitWindowHeight(y + Yoffset);

        box.style.left = x + 'px';
        box.style.top = y + 'px';
    }
}//eof followMouse

function setTracking(cBox) {
    
}

function kill(){
    if (box) {
        delayTimer=setTimeout("clearTimers()",hideDelay);
        
        if (mouseTracking) {
            document.onmousemove="";           
        }
        //if (box.style.visibility == "visible") box.style.visibility = "hidden";
    }
}//eof kill

/*
function kill(){
    if (box) {
        clearTimers();
        if (mouseTracking) {
            document.onmousemove="";           
        }
        if (box.style.visibility == "visible") box.style.visibility = "hidden";
    }
}eof kill
*/

function clearTimers() {
    if (killTimer) clearTimeout(killTimer);
    if (showTimer) clearTimeout(showTimer);
    if (delayTimer) clearTimeout(delayTimer);
    if (box.style.visibility == "visible") box.style.visibility = "hidden";
}//eof clearTimers

window.onload = setMessage;
