// Free for any type of use so long as original notice remains unchanged.
// Report errors to feedback@ashishware.com
//Copyrights 2006, Ashish Patil , ashishware.com
//////////////////////////////////////////////////////////////////////////

function ToolTip(id,isAnimated,aniSpeed)
{ 
	var isInit = -1;
  	var div,divWidth,divHeight;
  	var xincr=10,yincr=10;
  	var animateToolTip =true;
  	var html;
  
  	function Init(id)
  	{
  		div = document.getElementById(id);
   		if(div==null) return;
   
   		if((div.style.width=="" || div.style.height==""))
   		{
   			alert("Both width and height must be set");
   			return;
   		}
   
   		divWidth = parseInt(div.style.width);
   		divHeight= parseInt(div.style.height);
   		if(div.style.overflow!="hidden")div.style.overflow="hidden";
   		if(div.style.display!="none")div.style.display="none";
   		if(div.style.position!="absolute")div.style.position="absolute";
   
   		if(isAnimated && aniSpeed>0)
   		{
   			xincr = parseInt(divWidth/aniSpeed);
    		yincr = parseInt(divHeight/aniSpeed);
    		animateToolTip = true;
    	}
        
   		isInit++;   
  	}
      
  	this.Show =  function(e,strHTML,parentDiv,parentDivWidth, direction, imageHeight)
  	{  		
  		if (imageHeight == null)
  		{
  			imageHeight = 140;
  		}
  		if (direction == null)
  		{
  			direction = "";
  		}  			
    	if(isInit<0) return;
    
    	var newPosx,newPosy,height,width;    	
   		
    	if(typeof( document.documentElement.clientWidth ) == 'number' ){
	    	width = document.body.clientWidth;
	    	height = document.body.clientHeight;
		}
		else{
	    	width = parseInt(window.innerWidth);
	    	height = parseInt(window.innerHeight);	    	   
    	}
	    var curPosx ,curPosy;
	    
    	curPosx = ToolTip.findPosX(parentDiv);    		    		    
    	curPosy = ToolTip.findPosY(parentDiv, direction, imageHeight);
    		
       	newPosx= curPosx+parentDivWidth;
    	newPosy=curPosy+10;
    	    
    	
    	if(strHTML!=null)
    	{
    		html = strHTML;
     		div.innerHTML=html;
     	}    
     	
    	div.style.display='block';
    	
    	div.style.top= newPosy + "px";    	
    	div.style.left= newPosx+ "px";
    	
    	if(animateToolTip)
    	{
    		div.style.height= "0px";
    		div.style.width= "0px";
    		if (direction == "above")
    		{
    			if (imageHeight < 140)
    			{
    				ToolTip.animate(div.id,divHeight+200,divWidth+200);
    			}
    			else
    			{
    				ToolTip.animate(div.id,divHeight+200,divWidth+200);
    			}
    		}
    		else
    			ToolTip.animate(div.id,divHeight,divWidth);
    		//div.focus();
    	}
    
    }
	this.Hide= function(e)
    {
    	div.style.display='none';
    	if(!animateToolTip)return;
    	div.style.height= "0px";
    	div.style.width= "0px";
    }
    
   	this.SetHTML = function(strHTML)
   	{
   		html = strHTML;
    	div.innerHTML=html;
    } 
    ToolTip.getX = function(e){
      if(document.all) return (e.x)?parseInt(e.x):parseInt(e.clientX); 
      return e.layerX;
	}
	ToolTip.getY = function (e){
	  if(document.all) return (e.y)?parseInt(e.y):parseInt(e.clientY);
	  return e.layerY;
	}
	
	ToolTip.getScrollX = function(){
	  var ScrollLeft = document.body.scrollLeft;
	    if (ScrollLeft == 0)
	    {
	    	if (window.pageXOffset)
	    	    ScrollLeft = window.pageXOffset;
		    else
		        ScrollLeft = (document.body.parentElement) ? document.body.parentElement.scrollLeft : 0;
		}
		return ScrollLeft;
	  
	}
	
	ToolTip.getScrollY = function(){
		
		var ScrollTop = document.body.scrollTop;
	    if (ScrollTop == 0)
	    {
	    	if (window.pageYOffset)
	    	    ScrollTop = window.pageYOffset;
		    else
		        ScrollTop = (document.body.parentElement) ? document.body.parentElement.scrollTop : 0;
		}
		return ScrollTop;
	}
    
    this.move = function(e)
    {
    	div.style.display='block';

     	var posx = 0;
     	var posy = 0;
       	posx= ToolTip.getX(e) + ToolTip.getScrollX();
       	posy= ToolTip.getY(e) + ToolTip.getScrollY();
       	div.style.left= posx + "px";
    	div.style.top= posy+ "px";

    }
    ToolTip.animate = function(a,iHeight,iWidth)
  	{
    	a = document.getElementById(a);
         
   		var i = parseInt(a.style.width)+xincr ;
   		var j = parseInt(a.style.height)+yincr;  
   
   		if(i <= iWidth)
   		{
   			a.style.width = i+"px";
   		}
   		else
   		{
   			a.style.width = iWidth+"px";
   		}
   
   		if(j <= iHeight)
   		{
   			a.style.height = j+"px";
   		}
   		else
   		{
   			a.style.height = iHeight+"px";
   		}
   
   		if(!((i > iWidth) && (j > iHeight)))      
   		setTimeout( "ToolTip.animate('"+a.id+"',"+iHeight+","+iWidth+")",1);
    }
    ToolTip.findPosX=function(obj)
  	{
	    var curleft = 0;
	    if(obj.offsetParent)
	        while(1) 
	        {
	          curleft += obj.offsetLeft;
	          if(!obj.offsetParent)
	            break;
	          obj = obj.offsetParent;
	        }
	    else if(obj.x)
	        curleft += obj.x;
	    return curleft;
  	}

	  ToolTip.findPosY=function(obj, direction, imageHeight)
	  {
	    var curtop = 0;
	    if(obj.offsetParent)
	        while(1)
	        {
	          curtop += obj.offsetTop;
	          if(!obj.offsetParent)
	            break;
	          obj = obj.offsetParent;
	        }
	    else if(obj.y)
	        curtop += obj.y;	    
	    if (direction == "above")
	    {
	    	if (imageHeight < 140)
	    	{
	    		curtop = curtop - 210 + (140 - imageHeight);
	    	}
	    	else
	    	{
	    		curtop = curtop - 200;
	    	}	    		
	    }
	    if (direction == "middle")
	    {	
	    	curtop -= 10;
	    }	
	    	
	    return curtop;
	  }
    
    
   Init(id);
}

