var info_tooltip = false;
var info_tooltipShadow = false;
var info_iframe = false;
var info_tooltipObj = false; //tooltip obj to over
var info_shadowSize = 4; //Shadow size
var info_tooltipMaxWidth = 500; //Max width for tooltip
var info_tooltipMinWidth = 300; //Min Width for tooltip
var info_tooltipTimer = -1; //Timer for tooltip

var tID = false; //timer id

//Possition of tooltip
var leftPos = 0;
var topPos = 0;
var st = 0;
var sl = 0;

var tooltip_is_msie = (navigator.userAgent.indexOf('MSIE')>=0 && navigator.userAgent.indexOf('opera')==-1 && document.all)?true:false;

function iniTooltip() {
        page_loaded = true;
        Tooltip();
}

function Tooltip(){
        //var tipElements = new Array('a','li');
        var tipElements = new Array('a');
        if (document.all && !page_loaded) {
                addEvent(window, 'load', iniTooltip);
                return false;
        }
        if ( !document.getElementById ||
                !document.createElement ||
                !document.getElementsByTagName ) {
                return;
        }

        var i,j;
        var tipLen = tipElements.length;
        for ( i=0; i<tipLen; i++ ) {
                var current = document.getElementsByTagName(tipElements[i]);
                var curLen = current.length;
                for ( j=0; j<curLen; j++ ) {
                        if (current[j].getAttribute('title') != null && current[j].getAttribute('title') != '') {
                                addEvent(current[j],'mouseover',this.overTooltip);
                                addEvent(current[j],'mouseout',this.hideTooltip);
                                current[j].setAttribute('tip',current[j].title);
                                current[j].removeAttribute('title');
                        }
                }
        }
}

function overTooltip(e){
        if(document.all)e = event;
        
        info_tooltipTimer = 0;
        info_tooltipObj = this;
        
        st = Math.max(document.body.scrollTop,document.documentElement.scrollTop);
        //if(navigator.userAgent.toLowerCase().indexOf('safari')>=0)st=0;

        sl = Math.max(document.body.scrollLeft,document.documentElement.scrollLeft);
        //if(navigator.userAgent.toLowerCase().indexOf('safari')>=0)sl=0;
        
        leftPos = e.clientX + 20 + sl;
        topPos = e.clientY + 10 + st;
        
        timerTooltip();
        return false;
}
        
function timerTooltip(){

        if(info_tooltipTimer>=0 && info_tooltipTimer<1){
                info_tooltipTimer++;
                tID = setTimeout('timerTooltip()',250);
                return;
        }

        if(info_tooltipTimer==1){
                tID = false;
                showTooltip();
        }
}

function showTooltip()
{
        var tooltipTxt;
        var bodyWidth = Math.max(document.body.clientWidth,document.documentElement.clientWidth) - 20;

        if(!info_tooltip){
                info_tooltip = document.createElement('DIV');
                info_tooltip.id = 'info_tooltip';
                info_tooltip.className = "div4";
                info_tooltipShadow = document.createElement('DIV');
                info_tooltipShadow.id = 'info_tooltipShadow';

                document.body.appendChild(info_tooltip);
                document.body.appendChild(info_tooltipShadow);

                if(tooltip_is_msie){
                        info_iframe = document.createElement('IFRAME');
                        info_iframe.frameborder='5';
                        info_iframe.style.backgroundColor='#FFFFFF';
                        info_iframe.src = '#';
                        info_iframe.style.zIndex = 100;
                        info_iframe.style.position = 'absolute';
                        document.body.appendChild(info_iframe);
                }

        }
        
        //Show tooltip
        info_tooltip.style.display='block';
        info_tooltipShadow.style.display='block';
        if(tooltip_is_msie)info_iframe.style.display='block';
        
        //Get the txt
        tooltipTxt = info_tooltipObj.getAttribute('tip');

        info_tooltip.style.width = null;        // Reset style width if it's set
        info_tooltip.innerHTML = tooltipTxt;
        info_tooltip.style.left = leftPos + 'px';
        info_tooltip.style.top = topPos + 'px';

        info_tooltipShadow.style.left =  leftPos + info_shadowSize + 'px';
        info_tooltipShadow.style.top = topPos + info_shadowSize + 'px';
        
        /* Exceeding max width of tooltip ? */
        if(info_tooltip.offsetWidth>info_tooltipMaxWidth){
                info_tooltip.style.width = info_tooltipMaxWidth + 'px';
        }

        //Set the tool tip width
        var tooltipWidth = info_tooltip.offsetWidth;
        if(tooltipWidth<info_tooltipMinWidth)tooltipWidth = info_tooltipMinWidth;

        //Check to see if the tooltip is outside the body
        if((leftPos + tooltipWidth)>bodyWidth){
                info_tooltip.style.left = (info_tooltipShadow.style.left.replace('px','') - tooltipWidth - 40 - info_shadowSize) + 'px';
                info_tooltipShadow.style.left = (info_tooltipShadow.style.left.replace('px','') - tooltipWidth - 40) + 'px';
        }

        //Apply width
        info_tooltip.style.width = tooltipWidth + 'px';
        info_tooltipShadow.style.width = info_tooltip.offsetWidth + 'px';
        info_tooltipShadow.style.height = info_tooltip.offsetHeight + 'px';

        if(tooltip_is_msie){
                info_iframe.style.left = info_tooltip.style.left;
                info_iframe.style.top = info_tooltip.style.top;
                info_iframe.style.width = info_tooltip.offsetWidth + 'px';
                info_iframe.style.height = info_tooltip.offsetHeight + 'px';

        }

}

function hideTooltip()
{
        if ( window.tID ) {
                clearTimeout(tID);
                return;
        }

        if (info_tooltip){
                info_tooltip.style.display='none';
                info_tooltipShadow.style.display='none';
                if(tooltip_is_msie)info_iframe.style.display='none';
        }
}
addEvent(window, 'load', iniTooltip);
