
tooltip = {};
tooltip.cache = {};
tooltip.mouseY = 0;
tooltip.mousePageY = 0;
tooltip.mousePageX = 0;

tooltip.interval = 0;
tooltip.current = null;
tooltip.currentAncestor = 0;

tooltip.enable = function (obj,flag,url,time,eventObj){
    if (tooltip.current !== null){
        tooltip.clearInterval(0);
        tooltip.hide();
    }
    tooltip.mousemove(eventObj);

    tooltip.current = null; 
    tooltip.currentAncestor = obj; 

    tooltip.current = tooltip.show(url,flag);
    tooltip.current.setAttribute('rel',time);

    $(obj).mouseout(function () {
        tooltip.setTimer();
    });
    $(tooltip.current).mouseout(function () {
        tooltip.setTimer();
    });

    $(obj).mouseover(function () {
        tooltip.clearInterval(time);
    });
    $(tooltip.current).mouseover(function () {
        tooltip.clearInterval(time);
    });
}

tooltip.clearInterval = function (time){
    if (tooltip.interval != 0) {
        window.clearInterval(tooltip.interval);
        tooltip.interval = 0;
    }

    if (tooltip.current !== null){
        tooltip.current.setAttribute('rel',time);
    }
}

tooltip.setTimer = function (){
    if (tooltip.interval == 0) {
        tooltip.interval = window.setInterval(function () {
            if (tooltip.interval == 0) {tooltip.clearInterval();return;}
            if (tooltip.current !== null){
                tooltip.current.setAttribute('rel',parseInt(tooltip.current.getAttribute('rel')) - 1);
                if (parseInt(tooltip.current.getAttribute('rel')) == 0){
                    tooltip.hide();
                    tooltip.clearInterval();
                    tooltip.current = null;
                    tooltip.currentAncestor = null;
                }
            }
        },100);
    }
}

tooltip.show = function (url,flag){

    var oDiv = null;
    var oContentObj = null;
    oDiv = document.createElement('div');
    oDiv.className = 'popup';
    oDiv.style.display = 'none';

    $(oDiv).html('<div class="marker"></div>'+'<table cellpadding="0" cellspacing="0"><tr><td class="plt"></td><td class="pct"></td><td class="prt"></td></tr><tr><td class="plc"></td><td class="pcc"><div class="popupContent"></div></td><td class="prc"></td></tr><tr><td class="plb"></td><td class="pcb"></td><td class="prb"></td></tr></table>'+'');

    $(document.body).append(oDiv);
    $('div.popupContent',oDiv).each(function(){ oContentObj = this});
    var offset = $(tooltip.currentAncestor).offset();
    var nHeight = $(tooltip.currentAncestor).height();
    var nWidth = $(tooltip.currentAncestor).width();
    var bRecalculate = false;
    oDiv.style.left = (tooltip.mouseX - 30 ) + 'px';
    oDiv.style.top = (tooltip.mouseY + 25 ) + 'px';
    bRecalculate = true;


    content = '';
    if(flag == 1){
        content = url;
        oContentObj.innerHTML = content;
        var nContentHeight = $(oDiv).height();
        if(!nContentHeight)
            nContentHeight = 225;
        $(oDiv).css('display','block');
        if (bRecalculate){
            $(oDiv).css('margin-top',nContentHeight+38+'px');
            oDiv.style.top = (tooltip.mouseY - nContentHeight - 30) + 'px';
            if ($.browser.msie && ($.browser.version < 7)){
                oDiv.style.top = (tooltip.mouseY - nContentHeight - 30) + 'px';
            }
        }

    }else{
    
        if (tooltip.cache[url] == undefined) {
            $.ajax({
                url: url,
                type: 'GET',
                dataType: 'text',
                error: function (request,errorStr) {
                    
                },
                success: function (contentLoaded) {
                    content = contentLoaded;
                    oContentObj.innerHTML = content;
                    var nContentHeight = $(oDiv).height();
                    if(!nContentHeight)
                        nContentHeight = 230;
                    tooltip.cache[url] = [content,nContentHeight];
                    $(oDiv).css('display','block');
                    if (bRecalculate){
                        $(oDiv).css('border-width','1px');
                        $(oDiv).css('margin-top',nContentHeight+'px');
                        
                        oDiv.style.top = (tooltip.mouseY - nContentHeight - 15) + 'px';
                        if ($.browser.msie && ($.browser.version < 7)){
                            oDiv.style.top = (tooltip.mouseY - nContentHeight - 15) + 'px';
                        }
                    }
                }

            });
        }else{
            content = tooltip.cache[url][0];
            oContentObj.innerHTML = content;
            var nContentHeight = $(oDiv).height();
            if(!nContentHeight)
                nContentHeight = 225;
            $(oDiv).css('display','block');
            if (bRecalculate){
                $(oDiv).css('margin-top',nContentHeight+38+'px');
                oDiv.style.top = (tooltip.mouseY - nContentHeight - 30) + 'px';
                if ($.browser.msie && ($.browser.version < 7)){
                    oDiv.style.top = (tooltip.mouseY - nContentHeight - 30) + 'px';
                }
            }
        }
    }
    return oDiv;
}

tooltip.hide =function (){
    $(tooltip.current).fadeOut('slow');
    $(tooltip.current).remove();
    
}


tooltip.mousemove = function (eventObj) {
    if (window.innerHeight && window.scrollMaxY) {
        xScroll = document.body.scrollWidth;
    } else if (document.body.scrollHeight > document.body.offsetHeight){
        xScroll = document.body.scrollWidth;
    } else { 
        xScroll = document.body.offsetWidth;
    }
    tooltip.mouseX = eventObj.pageX;
    tooltip.mouseY = eventObj.pageY;
    tooltip.mouseClientX = eventObj.clientX;
    tooltip.mousePageY = eventObj.screenY;
    tooltip.mousePageX = xScroll;
};


