var toolTipLib = { 
    xCord : 0,
    yCord : 0,
    obj : null,
    attachToolTipBehavior: function() {
        if (!document.getElementById ||
            !(document.createElement || document.createElementNS) ||
            !document.getElementsByTagName) {
            return;
        }
        addEvent(document,'mousemove',toolTipLib.updateXY,false);
        if ( document.captureEvents ) {
                document.captureEvents(Event.MOUSEMOVE);
        }
        var tips = document.getElementsByTagName('span');
        for (var i = 0; i < tips.length; i++) {
            var className = tips[i].className;
            if (className === null) {
                className = tips[i].getAttribute('className');
            }
            if (className === null || className != 'tip_link') {
                continue;
            }
            // get the anchor node and make sure it has an anchor and span tag
            var anchor = tips[i].getElementsByTagName('a');
            var span = tips[i].getElementsByTagName('span');
            if (anchor.length != 1 || span.length != 1) {
                continue;
            }
            
            // add the events
            addEvent(anchor[0], 'mouseover', toolTipLib.tipOver, false);
            addEvent(anchor[0], 'mouseout', toolTipLib.tipOut, false);
        }
    },
    updateXY : function(e) {
        if ( document.captureEvents ) {
            toolTipLib.xCord = e.pageX;
            toolTipLib.yCord = e.pageY;
        } else if ( window.event.clientX ) {
            toolTipLib.xCord = window.event.clientX+document.documentElement.scrollLeft;
            toolTipLib.yCord = window.event.clientY+document.documentElement.scrollTop;
        }
    },
    tipOut: function(e) {
        if ( window.tID ) {
            clearTimeout(tID);
        }
        if ( window.opacityID ) {
            clearTimeout(opacityID);
        }
        var l = getEventSrc(e);
        var div = document.getElementById('toolTip');
        if ( div ) {
            div.parentNode.removeChild(div);
        }
    },
    tipOver : function(e) {
        toolTipLib.obj = getEventSrc(e);
        tID = setTimeout("toolTipLib.tipShow()",250);
    },
    tipShow : function() {
        // create a new div to hold the tool tip info
        var newDiv;
        if (document.createElementNS) {
            newDiv = document.createElementNS('http://www.w3.org/1999/xhtml', 'div');
        }
        else {
            newDiv = document.createElement('div');
        }
        var scrX = Number(toolTipLib.xCord);
        var scrY = Number(toolTipLib.yCord);
        var tp = parseInt(scrY+15, 10);
        var lt = parseInt(scrX+10, 10);
        
        // find the tooltip text
        var span = toolTipLib.obj.parentNode.getElementsByTagName('span');
        var toolTipText = 'Error';
		for (var i = 0; i < span[0].childNodes.length; i++)
	    {
	        if (span[0].childNodes[i].nodeName == '#text')
	        {
				toolTipText = span[0].childNodes[i].nodeValue;
				break;
	        }
	    }

        newDiv.id = 'toolTip';
        document.getElementsByTagName('body')[0].appendChild(newDiv);
        var p;
        if (document.createElementNS) {
            p = document.createElementNS('http://www.w3.org/1999/xhtml', 'p');
        }
        else {
            p = document.createElement('p');
        }
        p.appendChild(document.createTextNode(toolTipText));
        newDiv.appendChild(p);
        if ( parseInt(document.documentElement.clientWidth+document.documentElement.scrollLeft, 10) < parseInt(newDiv.offsetWidth+lt, 10) ) {
            newDiv.style.left = parseInt(lt-(newDiv.offsetWidth+10), 10)+'px';
        } else {
            newDiv.style.left = lt+'px';
        }
        if ( parseInt(document.documentElement.clientHeight+document.documentElement.scrollTop, 10) < parseInt(newDiv.offsetHeight+tp, 10) ) {
            newDiv.style.top = parseInt(tp-(newDiv.offsetHeight+10), 10)+'px';
        } else {
            newDiv.style.top = tp+'px';
        }
        
        // specifically set the width if defined
        var spanWidth = span[0].style.width;
        if (spanWidth != 0) {
        	newDiv.style.width = spanWidth;
        }
    }
};
addEvent(window,'load',toolTipLib.attachToolTipBehavior,false);