/*****************************************************************************************
* CLASS: Tooltip
*****************************************************************************************/
function Tooltip()
{
	this.tag;
	this.delay = 100; // Delay before tooltip is displayed
	this.source; // Source Object that triggered the tooltip
	this.mode;
	this.params;
	this.output; // Display object
	this.mousepos;
	this.timer;
	this.url;
	
	this.trigger = function(e,tag,mode,params)
	{
  e = e || window.event;
		this.tag = tag;
		this.mode = mode;
  this.source = e.target || e.srcElement;
		this.source.style.cursor='help';
		this.mousepos = mouseCoords(e);
		this.params = params;
   
	
  document.onmousemove = this.trackMouse;
  tooltiptimer = setTimeout(createMethodReference(this,this.show),this.delay);
  this.source.onmouseout = this.clear;
		this.source.onmousedown = this.clear;
	}
	
	this.show = function()
	{
  var ajaxObj = new Ajax();		
  ajaxObj.onreadystatechange = function()
 	{
 		if (ajaxObj.readyState==4 && ajaxObj.status==200)
   {
				if (this.output = getObj('tooltip'))
    {
     this.output.innerHTML = ajaxObj.responseText;
					this.output.style.display = 'block';
				}
			}
  }
		
		var matches = this.source.src.match(/([^\/]+)\.(png|gif|jpg)$/);
		if (matches) { var icon = matches[0]; }
  	
  url = "/tooltip.php?id="+this.tag+"&mode="+this.mode+"&icon="+icon;
		if (this.params) { url += '&' + this.params; }
		
  if (url != this.url)
		{
		 //alert(url);
   ajaxObj.open("GET",url,true); 
 	 ajaxObj.send(null);
 	 this.url = url;
	 }
	}
		
 this.trackMouse = function(e)
	{
		e = e || window.event;
  this.source = e.target || e.srcElement;
  this.mousepos = mouseCoords(e);
		
  if (!getObj('tooltip'))
  {
   var tooltipObj = document.createElement('div');
  	tooltipObj.setAttribute('id','tooltip');
   tooltipObj.style.position="absolute";
 		tooltipObj.style.color='#FFF';
   document.body.appendChild(tooltipObj);	
  }
		
		// MOUSE POS IS RELATIVE TO SCREEN
		// IMG POS IS RELATIVE TO DOCUMENT
		var myTooltip = getObj('tooltip');
		myTooltip.style.left = (getLeft(this.source) + 45) + 'px';
  myTooltip.style.top = (getTop(this.source) - 45) + 'px';;  
 }
	
	this.clear = function()
	{
		this.tag = '';
		myTooltip.url = '';
		if (tooltiptimer)
		{
   clearTimeout(tooltiptimer);
		}

		document.onmousemove = '';
		if (getObj('tooltip'))
		{
		 //document.body.removeChild(getObj('tooltip'));
			getObj('tooltip').innerHTML = '';
			getObj('tooltip').style.display = 'none';
		}
  //debug(getObj('tooltip'));		
 }
}

/******************************************************************************************
* INITIALIZE
******************************************************************************************/
function tooltip(e,tag,mode,itemid)
{
 if (typeof(myTooltip)=='undefined')
	{
		myTooltip = new Tooltip();
	}
 myTooltip.trigger(e,tag,mode,itemid);
}