
var toolTipDiv = false;
var iFrameDiv = false;
var timerShow = false;
var timerHide = false;
var posX, posY, _id,_divYPos, _divXPos;

//XPos: Position from the right
//YPos: Position from the top
function showTooltip(e, id, divXPos, divYPos)
{	
    // get coordinate of mouse
    e = e || window.event;    
    scrollTop    = document.body.scrollTop  || document.documentElement.scrollTop;
    scrollLeft   = document.body.scrollLeft || document.documentElement.scrollLeft;            
	posX = (e.pageX) ? e.pageX : (e.clientX + scrollLeft);
	posY = (e.pageY) ? e.pageY : (e.clientY + scrollTop);
	
	if(timerShow)
	{
		clearTimeout(timerShow);
	}
	
	// put in global variable for callback
	_id = id;
	_divXPos = divXPos;
	_divYPos = divYPos;

	timerShow = setTimeout("showToolTipCallback(_id, _divXPos, _divYPos)", 250);

}

function showToolTipCallback(id, divXPos, divYPos)
{
    var toolTip;
    
    timerShow = false;
    
    // create a div on the body scope if it doesn't yet exist
    if(!toolTipDiv)
    {
    	toolTipDiv = document.createElement('div');
    	toolTipDiv.className = "toolTipBox";    	
    	document.body.appendChild(toolTipDiv); 
    	toolTipDiv.style.position = "absolute";    	
    }

	// reset previously set style so adjust height values does not accumulate
	toolTipDiv.style.height  =  null;
        
    // get reference to actual tooltip element
    toolTip = document.getElementById(id);
 
    // copy tooltip element contents to new div on body scope
    toolTipDiv.innerHTML = toolTip.innerHTML;
    toolTipDiv.style.display = 'block';
    toolTipDiv.style.visibility = 'visible';
    
    // position it according to mouse coordinates
    	
    obj_width  = toolTipDiv.offsetWidth;
    obj_height = toolTipDiv.offsetHeight;	
    
    posX -= 50;    
    posY -= obj_height;
    
      if(divYPos != undefined)
	{
  		posY = posY+divYPos;
   }
   
   if(divXPos != undefined)
	{
  		posX = posX-divXPos;
   }
    // adjust height so it does not cover icons
    toolTipDiv.style.height = (obj_height - 25) + "px";
    
    //iFrameDiv.style.width = obj_width + "px";
	toolTipDiv.style.left = posX + "px";
	toolTipDiv.style.top  = posY + "px";	

}

/** Closes tooltip */

function hideTooltip(id)
{
	if(toolTipDiv)
	{
   		toolTipDiv.style.display = "none";
   	}
   	
}

/* hides tooltip onclick anywhere outside the tooltip */

function hideTooltipOnclick(e)
{
	var e = e || window.event;
	var clickedElem = e.target || e.srcElement;
	var isOnTooltip = false;
	
	// iterates through to see if the clicked element is a descendant of toolTipBox
	for(i=0; i < 5; ++i)
	{
		if(clickedElem.className == 'toolTipBox') 
		{
			isOnTooltip = true;
		}
		else if(clickedElem.parentNode)
		{
			clickedElem = clickedElem.parentNode;
		}
	}
	
	if(toolTipDiv && !isOnTooltip)
	{
		toolTipDiv.style.display = "none";
	}
}

function addBodyOnclickHide()
{
	var oldonclick = document.body.onclick;
	
	if (typeof document.body.onclick !='function')
	{
		document.body.onclick = hideTooltipOnclick;
	} 
	else 
	{
		document.body.onclick = function()
		{
			oldonclick();
			hideTooltipOnclick();
		}
	}
}

