var constOffsetY = 10;
var constOffsetX = 10;

function TooltipItem(el, tipDivCssClass){
    this.tipIsVisible = false;
    this.sourceElement = el;
    this.tipText = this.sourceElement.title.replace(/\\n/g, '<br>');
    this.tooltip = document.getElementById('toltipDiv');        
    this.tooltipCssClass = tipDivCssClass;    
    
    this.closeTipTimer = 0;
    this.clearTimeoutId = -1;
    
    this.sourceElement.title = '';
    this.sourceElement.tooltip = this.tooltip;
    
    function TooltipMouseOver(obj){
		return (function(){
			if (obj.clearTimeoutId != -1){
				clearTimeout(obj.clearTimeoutId);
			} 
		});
    }

	function DoHideTooltip(obj){
		return (function(){
			obj.tipIsVisible = false;
			obj.tooltip.style.display = 'none';
		});
	}
    
    function GetMouseOver(obj)
    {
		return function(e){
		    e = e || event;
			var x = e.clientX + document.body.scrollLeft + constOffsetY + 'px';
			var y = e.clientY + document.body.scrollTop + constOffsetX + 'px';
			
			obj.tooltip.style.top = y;
			obj.tooltip.style.left = x;
					
			obj.tooltip.innerHTML = obj.tipText;
			obj.tooltip.className = obj.tooltipCssClass;			
			obj.tipIsVisible = true;		
			obj.tooltip.style.display = 'block';
			obj.tooltip.onmouseover = TooltipMouseOver(this);
			obj.tooltip.onmouseout = DoHideTooltip(this);
			if( this.clearTimeoutId != null && this.clearTimeoutId != -1 ){
				clearTimeout(this.clearTimeoutId);
			}
		}
    }
    
    function GetMouseMove(obj)
    {
		return function(e)
		{
			if( obj.clearTimeoutId != null && obj.clearTimeoutId != -1 ){
				clearTimeout(obj.clearTimeoutId);
			}
			if(!obj.tipIsVisible){return;}
			if(!e){e=event};
			
			var x = e.clientX + document.body.scrollLeft + constOffsetY + 'px';
			var y = e.clientY + document.body.scrollTop + constOffsetX + 'px';
			
			obj.tooltip.style.top = y;
			obj.tooltip.style.left = x;			
		}
    }
    
    function GetMouseOut(obj)
    {
		return function(){
			var functRef = DoHideTooltip(this);
			this.clearTimeoutId = setTimeout(functRef, 2000);
		}
    }
    
    this.sourceElement.onmouseover = GetMouseOver(this);
    this.sourceElement.onmousemove = GetMouseMove(this);
    this.sourceElement.onmouseout = GetMouseOut(this);
}

function TooltipDiv()
{
	var tipDiv;
	
	if (document.getElementById('toltipDiv') != null)  
		tipDiv = document.getElementById('toltipDiv');
	else 
	{
		tipDiv = document.createElement('div');			
		tipDiv.id = 'toltipDiv';
		tipDiv.style.position = 'absolute';
		tipDiv.style.zIndex = 99999;
		document.body.insertBefore(tipDiv, document.body.firstChild);
    }
    return tipDiv;
}

function AttachTooltip(tagName, cssClass){ 
	var tipDiv = new TooltipDiv();
	tipDiv.style.display = 'none';
	var tipItems = document.getElementsByTagName(tagName);
	for(var i=0; i < tipItems.length; i++){ 
		if(tipItems[i].className.indexOf('tooltipHost')>-1 && tipItems[i].title.length>0)
			new TooltipItem(tipItems[i],'tooltip '+cssClass);
	} 
} 
