﻿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 tipObj;
var tipHolder;

var tipText;
var tipWidth;
var tipCssClass;
var tipId;
var tipMode;
var tipBoby;
var tipTextTagId;

function ResetParameters() {
    tipText = undefined;
    tipWidth = undefined;
    tipCssClass = undefined;
    tipId = undefined;
    tipMode = undefined;
    tipBoby = undefined;
    tipTextTagId = undefined;
}

function SaveParameters(id, text, width, cssClass, mode, boby, textTagId) {
    if (typeof id != "undefined") {
        tipId = id;
    }

    if (typeof text != "undefined") {
        tipText = text;
    }

    if (typeof width != "undefined") {
        tipWidth = width;
    }

    if (typeof cssClass != "undefined") {
        tipCssClass = cssClass;
    }

    if (typeof mode != "undefined") {
        tipMode = mode;
    }

    if (typeof body != "undefined") {
        tipBoby = body;
    }

    if (typeof textTagId != "undefined") {
        tipTextTagId = textTagId;
    }
}

function Initialize() {
    if (typeof tipId == "undefined") {
        tipId = "toolTip";
    }

    if (typeof tipMode == "undefined") {
        tipMode = "simple";
    }

    switch (tipMode) {
        case "simple":
            // do nothing
            break;

        case "complex":
            tipTextTagId = 'toolTipHolder';
            break;

        case "custom":
            if (typeof tipTextTagId == "undefined") {
                alert("The tooltip complex mode needs the textHolderId to be defined!");
                return;
            }

            if (typeof tipBoby == "undefined") {
                alert("The tooltip complex mode needs the divBody to be defined!");
                return;
            }

            break;
    }
}

function AddToolTip() {
    var newDiv = document.createElement("div");
    newDiv.setAttribute("id", tipId);

    switch (tipMode) {
        case "simple":
            // do nothing
            break;

        case "complex":
            var newDivInnnerHTML = new Array();
            newDivInnnerHTML[0] = "<table cellpadding='0' cellspacing='0' class='tableToolTip'>";
            newDivInnnerHTML[1] = "<tr>";
            newDivInnnerHTML[2] = "<td class='tdTopLeft'></td>";
            newDivInnnerHTML[3] = "<td class='tdTopCenter'></td>";
            newDivInnnerHTML[4] = "<td class='tdTopRight'></td>";
            newDivInnnerHTML[5] = "</tr>";
            newDivInnnerHTML[6] = "<tr>";
            newDivInnnerHTML[7] = "<td class='tdMiddleLeft'></td>";
            newDivInnnerHTML[8] = "<td class='tdMiddleCenter' id='toolTipHolder'></td>";
            newDivInnnerHTML[9] = "<td class='tdMiddleRight'></td>";
            newDivInnnerHTML[10] = "</tr>";
            newDivInnnerHTML[11] = "<tr>";
            newDivInnnerHTML[12] = "<td class='tdBottomLeft'></td>";
            newDivInnnerHTML[13] = "<td class='tdBottomCenter'></td>";
            newDivInnnerHTML[14] = "<td class='tdBottomRight'></td>";
            newDivInnnerHTML[15] = "</tr>";
            newDivInnnerHTML[16] = "</table>";
            newDiv.innerHTML = newDivInnnerHTML.join(" ");
            break;

        case "custom":
            newDiv.innerHTML = divBoby;
            break;
    }

    document.body.appendChild(newDiv);
}

function FindToolTipObj() {
    tipObj = document.all ? document.all[tipId] : document.getElementById ? document.getElementById(tipId) : "";
    tipHolder = document.all ? document.all[tipTextTagId] : document.getElementById ? document.getElementById(tipTextTagId) : "";
    if (tipHolder == null) {
        tipHolder = tipObj;
    }
}

function PopulateToolTip() {
    if (typeof tipWidth != "undefined") {
        tipObj.style.width = tipWidth + "px";
    }

    if (typeof tipCssClass != "undefined") {
        tipObj.className = tipCssClass;
    }

    tipHolder.innerHTML = tipText;
}

function TrueBodyIE() {
    return (document.compatMode && document.compatMode != "BackCompat") ? document.documentElement : document.body;
}

function ShowTip(id, text, width, cssClass, mode, boby, textTagId) {
    if (ns6 || ie) {
        ResetParameters();
        SaveParameters(id, text, width, cssClass, mode, boby, textTagId);
        Initialize();
        AddToolTip();
        FindToolTipObj();
        PopulateToolTip();
        enableTip = true;
    }
}

function PositionTip(e) {
    if (enableTip) {
        var curX = (ns6) ? e.pageX : event.clientX + TrueBodyIE().scrollLeft;
        var curY = (ns6) ? e.pageY : event.clientY + TrueBodyIE().scrollTop;

        // find out how close the mouse is to the corner of the window
        var rightedge = ie && !window.opera ? TrueBodyIE().clientWidth - event.clientX - offsetxpoint : window.innerWidth - e.clientX - offsetxpoint - 20;
        var bottomedge = ie && !window.opera ? TrueBodyIE().clientHeight - event.clientY - offsetypoint : window.innerHeight - e.clientY - offsetypoint - 20;

        var leftedge = (offsetxpoint < 0) ? offsetxpoint * (-1) : -1000;

        // if the horizontal distance isn't enough to accomodate the width of the context menu
        if (rightedge < tipObj.offsetWidth) {
            // move the horizontal position of the menu to the left by it's width
            tipObj.style.left = ie ? TrueBodyIE().scrollLeft + event.clientX - 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 < tipObj.offsetHeight) {
            tipObj.style.top = ie ? TrueBodyIE().scrollTop + event.clientY - tipObj.offsetHeight - offsetypoint + "px" : window.pageYOffset + e.clientY - tipObj.offsetHeight - offsetypoint + "px";
        }
        else {
            tipObj.style.top = curY + offsetypoint + "px";
        }

        tipObj.style.visibility = "visible";
    }
}

function HideTip() {
    if (ns6 || ie) {
        if (enableTip) {
            enableTip = false;
            document.body.removeChild(tipObj);
        }
    }
}

document.onmousemove = PositionTip;