
var ttCloseTimeoutID = null;
var ttCloseTimeoutValue = 800;
var ttshowTimeoutID = null;
var ttshowTimeoutValue = 500;
var ttTooltipWidth = 277;
var ttTooltipHeight = 160;
var ttAdvTooltipWidth = 337;
var ttAdvTooltipHeight = 100;


// transparent area size Y value
var ttTooltipTranspArea = 60;

var    ttLEFTTOP = 1;
var    ttRIGHTTOP = 2;
var    ttLEFTBOT = 3;
var    ttRIGHTBOT = 4;
var    ttADVLEFT = 5;
var    ttADVRIGHT = 6;


function ttPrepare(el,ttId, ttData){
    var tooltip,t,b,s,l;


    h=document.createElement("span");
    h.id = ttId;
    h.setAttribute("id",ttId);
    h.style.position="absolute";
    h.style.left = "0px";
    h.style.top = "0px";
    h.style.display="none";

    //alert(document.getElementById("tooltips"));
    document.getElementById("tooltips").appendChild(h);


    //if(t==null || t.length==0) t="link:";
    //el.removeAttribute("title");
    tooltip=CreateEl("span","tooltip");
    s=CreateEl("span","top");

    s.innerHTML = ttData;
    tooltip.appendChild(s);
   // b=CreateEl("b","bottom");
    //l=el.getAttribute("href");

    //if(ttHref.length>40) ttHref=ttHref.substr(0,37)+"...";
    //b.appendChild(document.createTextNode(ttHref));
    //tooltip.appendChild(b);
    h.appendChild(tooltip);
    setOpacity(tooltip);
    el.tooltip=h;

    el.onmouseover = ttMouseOverTimeout;
    el.onmouseout = ttMouseOutTimeout;
    //el.onmousemove=Locate;
    return el.tooltip;
}


function ttApplyTooltipType(el, type,  checkCoords) {

    if (checkCoords != null) {

        var winrect = system.getWindowRect();
        if (checkCoords.y-ttTooltipHeight<0) {

            if (type == ttLEFTTOP ) {

                type = ttLEFTBOT;
            }
            else if (type == ttRIGHTTOP ) {

                type = ttRIGHTBOT;
            }
        }

        else if (checkCoords.y+ttTooltipHeight+15>winrect.height) {

            if (type == ttLEFTBOT ) {

                type = ttLEFTTOP;
            }
            else if (type == ttRIGHTBOT ) {

                type = ttRIGHTTOP;
            }
        }

        if (checkCoords.x+ttTooltipWidth+15>winrect.width) {
            if (type == ttRIGHTBOT ) {

                type = ttLEFTBOT;
            }
            else if (type == ttRIGHTTOP ) {

                type = ttLEFTTOP;
            }
        }

    }


    switch (type) {

        case ttLEFTTOP:

            el.ttParams = []
            el.ttParams.xOffset = -ttTooltipWidth;
            el.ttParams.yOffset = -ttTooltipHeight;
            el.ttParams.transpType = 1;
            el.childNodes[0].style.background = 'url(../img/ttRB.gif)';
            el.childNodes[0].childNodes[0].style.paddingTop = 4+"px";
        break;

        case ttRIGHTTOP:
            el.ttParams = []
            el.ttParams.transpType = 1;
            el.ttParams.xOffset = 5;
            el.ttParams.yOffset = -ttTooltipHeight;
            el.childNodes[0].style.background = 'url(../img/ttLB.gif)';
            el.childNodes[0].childNodes[0].style.paddingTop = 4+"px";
        break;

        case ttLEFTBOT:
            el.ttParams = []
            el.ttParams.transpType = 0;
            el.ttParams.xOffset = -ttTooltipWidth+2;
            el.ttParams.yOffset = 8;
            el.childNodes[0].style.background = 'url(../img/ttRT.gif)';
            el.childNodes[0].childNodes[0].style.paddingTop = 60+"px";
        break;

        case ttRIGHTBOT:
            el.ttParams = []
            el.ttParams.transpType = 0;
            el.ttParams.xOffset = 8;
            el.ttParams.yOffset = 8;
            el.childNodes[0].style.background = 'url(../img/ttLT.gif)';
            el.childNodes[0].childNodes[0].style.paddingTop = 60+"px";

        break;

        case ttADVRIGHT:
            el.ttParams = []
            el.ttParams.transpType = 2;
            el.ttParams.xOffset = -ttAdvTooltipWidth;
            el.ttParams.yOffset = -(ttAdvTooltipHeight/2+8);
            el.childNodes[0].style.background = 'url(../img/ttAdvRight.gif)';
            el.childNodes[0].childNodes[0].style.paddingTop = 4+"px";
            el.childNodes[0].style.height = 100+"px";
            el.childNodes[0].style.width = 337+"px";
        break;

        case ttADVLEFT:
            el.ttParams = []
            el.ttParams.transpType = 2;
            el.ttParams.xOffset = 8;
            el.ttParams.yOffset = -ttAdvTooltipHeight/2;
            el.childNodes[0].style.background = 'url(../img/ttAdvLeft.gif)';
            el.childNodes[0].childNodes[0].style.paddingTop = 4+"px";
            el.childNodes[0].style.height = 100+"px";
            el.childNodes[0].style.width = 337+"px";
        break;

    }

    return type;

}

function showTooltip(coords, el){


    // hide Top 5 tooltips
    var tooltips = document.getElementById('tooltips');
    for (var idx=0; idx<tooltips.childNodes.length; idx++) {
        //alert(tooltips[idx]);
        if (tooltips.childNodes[idx].style
            && tooltips.childNodes[idx].style.display != 'none') {
            tooltips.childNodes[idx].style.display='none';
            el.parent.ttCoords = [];
        }
    }


    var winrect = system.getWindowRect();
    if (coords.x+ttTooltipWidth+10>winrect.width) {

        ttApplyTooltipType(el, ttLEFTTOP)
    }

    else if (coords.y+ttTooltipHeight+10>winrect.height) {

        ttApplyTooltipType(el,  ttRIGHTTOP)
    }


    ttShow(coords, el)
}

function hideTooltip(el){

    if (el != null) {
        el.style.display='none';
    }
    else {
        this.tooltip.style.display='none';
    }
}

function setOpacity(el){
    el.style.filter="alpha(opacity:95)";
    el.style.KHTMLOpacity="0.95";
    el.style.MozOpacity="0.95";
    el.style.opacity="0.95";
}

function CreateEl(t,c){
    var x=document.createElement(t);
    x.className=c;
    x.style.display="block";
    return(x);
}

// show tooltip
function ttShow(e,el){

    el.style.display = 'block';
    if (el.ttParams) {
        el.style.top=(e.y+el.ttParams.yOffset)+"px";
        el.style.left=(e.x+el.ttParams.xOffset)+"px";
    }
    else {
        el.style.top=(e.y+5)+"px";
        el.style.left=(e.x+5)+"px";

    }
}


function ttCalcCoords(e) {
    var posx=0,posy=0;
    if(e==null) e=window.event;
    if(e.pageX || e.pageY){
        posx=e.pageX; posy=e.pageY;
        }
    else if(e.clientX || e.clientY){
        if(document.documentElement.scrollTop){
            posx=e.clientX+document.documentElement.scrollLeft;
            posy=e.clientY+document.documentElement.scrollTop;
            }
        else{
            posx=e.clientX+document.body.scrollLeft;
            posy=e.clientY+document.body.scrollTop;
            }
        }
    return {x: posx, y: posy};
}

function ttMouseOverTimeout(e,ell,dontcalc) {


    if (ttCloseTimeoutID != null) {
        //alert('clear close');

        clearTimeout(ttCloseTimeoutID);
        ttCloseTimeoutID = null;
    }

    var coords = e;
    if (! dontcalc ) {coords= ttCalcCoords(e);}

    var el;

    if (this.tooltip) {
        this.tooltip.parent.lastShown = this.tooltip;
        el = this.tooltip;
    }
    else {
        el = ell;
    }


    var f = function() {
       ttshowTimeoutID = null;
       showTooltip(coords, el);
    }
    ttshowTimeoutID = setTimeout( f, ttshowTimeoutValue);
}

function ttMouseOutTimeout(e,ell) {

    if (ttshowTimeoutID  != null) {
        clearTimeout(ttshowTimeoutID);
        ttshowTimeoutID = null;

    }
    else {
        var el;
        if (this.tooltip) {
            el = this.tooltip;
        }
        else {
            el = ell;
        }

        var f = function() {
           ttCloseTimeoutID = null;
           hideTooltip(el);
        }
        ttCloseTimeoutID = setTimeout( f, ttCloseTimeoutValue);
    }
}

function ttClearTimeout () {

    if (ttCloseTimeoutID>0) {
        clearTimeout(ttCloseTimeoutID);
        ttCloseTimeoutID = null;
    }
}