var offsetfromcursorX=10; //Customize x offset of tooltip
var offsetfromcursorY=-10; //Customize y offset of tooltip

var offsetdivfrompointerX=-15; //Customize x offset of tooltip DIV relative to pointer image
var offsetdivfrompointerY=-50; //Customize y offset of tooltip DIV relative to pointer image. Tip: Set it to (height_of_pointer_image-1).

var ie=document.all;
var ns6=document.getElementById && !document.all;
var enabletip=false;
var tipobj, pointerobj;

var timeout1, timeout2, timeoutvar;

function ietruebody(){
    return (document.compatMode && document.compatMode!="BackCompat")? document.documentElement : document.body;
}

function findElement(item) {return document.getElementById ? document.getElementById(item) : document.all[item];}

function initTooltip() {
/*
    Creates HTML necessary for the tooltip and appends it into the BODY tag.

    <div id="sc_tooltip">
        <div class="tt_inner">
            <div class="tt_head"></div>
            <div id="sc_tooltip_body"></div>
            <div class="tt_foot"></div>
        </div>
    </div>
*/
    var level1 = document.createElement("div");
        level1.id = "sc_tooltip";
    var level2 = document.createElement("div");
        level2.className = "tt_inner";
    var level3a = document.createElement("div");
        level3a.className = "tt_head";
    var level3b = document.createElement("div");
        level3b.id = "sc_tooltip_body";
    var level3c = document.createElement("div");
        level3c.className = "tt_foot";
    var pointer = document.createElement("div");
        pointer.id = "sc_tooltip_pointer";
    level1.appendChild(level2);
    level2.appendChild(level3a);
    level2.appendChild(level3b);
    level2.appendChild(level3c);
    document.body.appendChild(level1);
    document.body.appendChild(pointer);
}

function showTooltip(divid, thewidth, thecolor){
    var el = findElement(divid);
    if(el) {
        var thetext = el.innerHTML;
        if (ns6||ie){
            if (typeof thewidth!="undefined") tipobj.style.width=thewidth+"px"
            if (typeof thecolor!="undefined" && thecolor!="") tipobj.style.backgroundColor=thecolor
            var innerDiv = findElement("sc_tooltip_body");
                innerDiv.innerHTML = thetext;

            var headerDiv = document.createElement("div");
                headerDiv.id = "sc_tooltip_header";
            var footerDiv = document.createElement("div");
                footerDiv.id = "sc_tooltip_footer";
            innerDiv.appendChild(headerDiv);
            innerDiv.appendChild(footerDiv);

            //tipobj.innerHTML=thetext
            timeout1 = setTimeout('enabletip=true',timeoutvar);
            timeout2 = setTimeout('turnTipOn()',timeoutvar);

            //hideSelects();
            return false
        }
    }
}

var nondefaultpos=false;

function positionTip(e){
    if (!e) var e = window.event;
    if(tipobj && pointerobj) {
        var nondefaultpos=false
        var curX=(ns6)?e.pageX : event.clientX+ietruebody().scrollLeft;
        var curY=(ns6)?e.pageY : event.clientY+ietruebody().scrollTop;

        //Find out how close the mouse is to the corner of the window
        var winwidth=ie&&!window.opera? ietruebody().clientWidth : window.innerWidth-20
        var winheight=ie&&!window.opera? ietruebody().clientHeight : window.innerHeight-20

        var rightedge=ie&&!window.opera? winwidth-event.clientX-offsetfromcursorX : winwidth-e.clientX-offsetfromcursorX
        var bottomedge=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
        /*STOP MOVING!
        if (rightedge<tipobj.offsetWidth){
        //move the horizontal position of the menu to the left by it's width
            tipobj.style.left=curX-tipobj.offsetWidth+"px"
            nondefaultpos=true
        }
        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+offsetfromcursorX-offsetdivfrompointerX+"px"
            pointerobj.style.left=curX+offsetfromcursorX+"px"
        /*}*/

        //same concept with the vertical position
        /*STOP MOVING!
        if (bottomedge<tipobj.offsetHeight){
            tipobj.style.top=curY-tipobj.offsetHeight-offsetfromcursorY+"px"
            nondefaultpos=true
        }
        else{
        */
            tipobj.style.top=curY+offsetfromcursorY+offsetdivfrompointerY+"px"
            pointerobj.style.top=curY+offsetfromcursorY+"px"
        /*}*/
    }

    if (enabletip){
        turnTipOn();
    }
}

function turnTipOn() {
    if(tipobj && pointerobj) {
        tipobj.style.visibility="visible"
        tipobj.style.display="block"
        if (!nondefaultpos) {
            pointerobj.style.visibility="visible"
            pointerobj.style.display="inline"
        } else {
            pointerobj.style.visibility="hidden"
            pointerobj.style.display="none"
        }
    }
}
function hideTooltip(){
    if (ns6||ie){
        if(tipobj && pointerobj) {
            if(timeout1) clearTimeout(timeout1);
            if(timeout2) clearTimeout(timeout2);
            enabletip=false
            tipobj.style.visibility="hidden"
            pointerobj.style.visibility="hidden"
            tipobj.style.display="none"
            pointerobj.style.display="none"
            tipobj.style.left="-1000px"
            tipobj.style.backgroundColor=''
            tipobj.style.width=''
            //showSelects(true);
        }
    }
}

function addEvent(elm, evType, fn, useCapture) {
    if (elm.addEventListener) {
        elm.addEventListener(evType, fn, useCapture);
        return true;
    }
    else if (elm.attachEvent) {
        var r = elm.attachEvent('on' + evType, fn);
        return r;
    }
    else {
        elm['on' + evType] = fn;
    }
}

addEvent(window, "load", function() {
    initTooltip();
    if (ie||ns6)
        tipobj=document.all? document.all["sc_tooltip"] : document.getElementById? document.getElementById("sc_tooltip") : "";
    pointerobj=document.all? document.all["sc_tooltip_pointer"] : document.getElementById? document.getElementById("sc_tooltip_pointer") : "";
});

document.onmousemove=positionTip;
