//initialise tooltip variables
var ttMaxWidth = 200	//maximum width 
var ttFont = "tahoma"	//font 
var ttFontSize = 8		//font size
var ttBgColor = "#ffffe7"	//background colour
var mouseOffset = 20	//distance from mouse pointer
var edgeOffset = 5		//distance from window edge


//create tooltip container
if (document.layers) {
    document.write('<layer id=tt style="padding:2; position:absolute; visibility:hidden; " visibility=hide bgcolor=' + ttBgColor + ' width=' + ttMaxWidth + '></layer>')
} else if (document.all || document.getElementById) {
    document.write('<div id=tt style="padding:2; position:absolute; visibility:hidden; background-color:' + ttBgColor + '; border:1px solid; font-size:' + ttFontSize + 'pt; font-family:' + ttFont + '"></div>')
}


//initialise tooltip
function initTT() {
    if (document.layers) {
        tooltip = document.tt
    } else if (document.all) {
        tooltip = document.all.tt
    } else if (document.getElementById) {
        tooltip = document.getElementById("tt")
    }
}


//full tooltip size (including offsets) object constructor
function objSize(ttWidth, ttHeight, mouseOffset, edgeOffset) {
    this.width = ttWidth + mouseOffset + edgeOffset
    this.height = ttHeight + mouseOffset + edgeOffset
}


//"space available for tooltip" object constructor
function objMargin(x, y, scrollX, scrollY, windowWidth, windowHeight) {
    this.scrollX = scrollX
    this.scrollY = scrollY
    this.left = x - scrollX
    this.top = y - scrollY
    this.right = windowWidth - this.left
    this.bottom = windowHeight - this.top
}


//show tooltip
function showTT(obj,include) {
    //alert("X : " + evt.x + " ,Y : " + evt.y + ", offX : " + evt.offsetX + ", offY : " + evt.offsetY + ", pageX : " + evt.pageX + ", pageY : " + evt.pageY + ", clientX : " + evt.clientX + ", clientY : " + evt.clientY);
    if (typeof (tooltip) == "undefined") return

    var x = obj.x ? obj.x : obj.pageX
    var y = obj.y ? obj.y : obj.pageY

    var ttText = typeof (include) == "string" ? include : incArray[include]

    if (document.layers) {
        tooltip.document.write('<span style="color:#000000; border:1px solid; font-size:' + ttFontSize + 'pt; font-family:' + ttFont + '">' + ttText + '</span>')
        tooltip.document.close()
        positionTT(tooltip, x, y, tooltip.clip.width, tooltip.clip.height)
        tooltip.visibility = "show"
    } else if (document.all || document.getElementById) {
        tooltip.style.left = ""
        tooltip.style.top = ""
        tooltip.noWrap = true
        tooltip.innerHTML = ttText
        tooltip.style.color = "#000000"

        if (tooltip.offsetWidth > ttMaxWidth) {
            tooltip.noWrap = false
            tooltip.style.width = ttMaxWidth + "px"
        } else {
            tooltip.style.width = tooltip.offsetWidth
        }

        positionTT(tooltip.style, x, y, tooltip.offsetWidth, tooltip.offsetHeight)
                
        tooltip.style.display = "none"  //this removes blinking in NN6
        tooltip.style.visibility = "visible"
        tooltip.style.display = "block"
        tooltip.style.zIndex = 100
    }
}

//show tooltip
function shownewTT(parentobj,obj, include) {
    //alert("X : " + evt.x + " ,Y : " + evt.y + ", offX : " + evt.offsetX + ", offY : " + evt.offsetY + ", pageX : " + evt.pageX + ", pageY : " + evt.pageY + ", clientX : " + evt.clientX + ", clientY : " + evt.clientY);
    if (typeof (tooltip) == "undefined") return

    var x = parseInt(findPosX(obj), 10);
    var y = parseInt(findPosY(obj), 10);

    var ttText = typeof (include) == "string" ? include : incArray[include]

    if (document.layers) {
        tooltip.document.write('<span style="color:#000000; border:1px solid; font-size:' + ttFontSize + 'pt; font-family:' + ttFont + '">' + ttText + '</span>')
        tooltip.document.close()
        positionTT(tooltip, x, y, tooltip.clip.width, tooltip.clip.height)
        tooltip.visibility = "show"
    } else if (document.all || document.getElementById) {
        tooltip.style.left = ""
        tooltip.style.top = ""
        tooltip.noWrap = true
        tooltip.innerHTML = ttText
        tooltip.style.color = "#000000"

        if (tooltip.offsetWidth > ttMaxWidth) {
            tooltip.noWrap = false
            tooltip.style.width = ttMaxWidth + "px"
        } else {
            tooltip.style.width = tooltip.offsetWidth
        }

        //positionTT(tooltip.style, x, y, tooltip.offsetWidth, tooltip.offsetHeight)

        var curleft = curtop = 0;
        if (parentobj.offsetParent) {
            curleft = parentobj.offsetLeft;
            curtop = parentobj.offsetTop;
            while (parentobj = parentobj.offsetParent) {
                curleft += parentobj.offsetLeft;
                curtop += parentobj.offsetTop;
            }
        }

        tooltip.style.position = "absolute";
        tooltip.style.left = curleft + 75 + 'px';
        tooltip.style.top = curtop + 18 + 'px';


        tooltip.style.display = "block"  //this removes blinking in NN6
        tooltip.style.visibility = "visible"
        tooltip.style.display = "block"
        tooltip.style.zIndex = 100
    }
}
function findPosX(obj) {
    var curleft = 0;
    if (obj.offsetParent)
        while (1) {
        curleft += obj.offsetLeft;
        if (!obj.offsetParent)
            break;
        obj = obj.offsetParent;
    }
    else if (obj.x)
        curleft += obj.x;
    return curleft;
}

function findPosY(obj) {
    var curtop = 0;
    if (obj.offsetParent)
        while (1) {
        curtop += obj.offsetTop;
        if (!obj.offsetParent)
            break;
        obj = obj.offsetParent;
    }
    else if (obj.y)
        curtop += obj.y;
    return curtop;
}

//hide tooltip
function hideTT() {
    if (typeof (tooltip) == "undefined") return

    if (document.layers) {
        tooltip.visibility = "hide"
    } else if (document.all || document.getElementById) {
        tooltip.style.visibility = "hidden"
        tooltip.style.width = ""
    }
}


//position tooltip
function positionTT(posObj, x, y, ttWidth, ttHeight) {
    //fix width problem in ie4
    if (ttWidth > ttMaxWidth) ttWidth = ttMaxWidth

    var ttMargin, posLeft, posTop
    var ttSize = new objSize(ttWidth, ttHeight, mouseOffset, edgeOffset)

    if (document.all) {
        //fix event coordinates for explorer 5 and later
        if (newMSIE()) {
            x += document.body.scrollLeft
            y += document.body.scrollTop
        }

        ttMargin = new objMargin(x, y, document.body.scrollLeft, document.body.scrollTop, document.body.clientWidth, document.body.clientHeight)
    } else if (document.layers || document.getElementById) {
        ttMargin = new objMargin(x, y, window.pageXOffset, window.pageYOffset, window.innerWidth, window.innerHeight)
    }

    if (ttMargin.right >= ttSize.width) {
        posLeft = x + mouseOffset
    } else {
        posLeft = ttMargin.left + ttMargin.right >= ttSize.width ? x + ttMargin.right - ttSize.width : ttMargin.scrollX + edgeOffset
    }

    if (ttMargin.bottom >= ttSize.height) {
        posTop = y + mouseOffset
    } else {
        posTop = ttMargin.top + ttMargin.bottom >= ttSize.height ? y + ttMargin.bottom - ttSize.height : ttMargin.scrollY + edgeOffset
    }

    //make sure tooltip does not appear over the mouse pointer
    if (x >= posLeft && x <= posLeft + ttSize.width && y >= posTop && y <= posTop + ttSize.height) {
        if (ttMargin.top >= ttSize.height) {
            posTop = y - ttSize.height
        } else if (ttMargin.left >= ttSize.width) {
            posLeft = x - ttSize.width
        } else {
            posLeft = x + mouseOffset
            posTop = y + mouseOffset
        }
    }

    if (document.all || document.getElementById) {
        posLeft += "px"
        posTop += "px"
    }

    posObj.left = posLeft
    posObj.top = posTop
    
    
}


//returnes true if user agent is explorer 5 or later
function newMSIE() {
    var ua = navigator.userAgent
    var msie = ua.indexOf("MSIE")

    if (msie > 0) {
        if (parseInt(ua.substring(msie + 5, ua.indexOf(".", msie))) >= 5) return true
    }

    return false
}
