﻿// JScript File
//==================================================
//	   ID:			 ToolTipProvider
//	   Type:		 Class
//	   Version:	     1.0
//	   Author: 	     Jürgen Weber
//	   WWW:		     http://AEbt.eu
//	   Date:		 12/2006
//	   Description:  
//==================================================

function ToolTipProvider(id, toolTipTextId, toolTipHeaderId)
{
    var thisPtr = this;
    var canHide = false;
	this.XOffset = -30;
	this.YOffset = 10;

	this.MaxLeft = null;
	this.Object =  document.getElementById(id);
	this.ToolTipText = document.getElementById(toolTipTextId);
	this.ToolTipHeader = document.getElementById(toolTipHeaderId);
	
	if(this.ToolTipText == null)
	    this.ToolTipText = this.Object;
	
	this.CurrentWidth = 0;
	
	this.Content = "";
	
	this.onmouseover = function(){ thisPtr.Show();}
	
	this.Show = function(e)
	{		
	    canHide = false;
		thisPtr.Object.style.visibility  = "visible";
		
		if(e) thisPtr.Update(e);
	}
	
	this.ShowText = function(txt, header)
	{		
	    canHide = false;
		thisPtr.Object.style.visibility  = "visible";		
		thisPtr.ToolTipText.innerHTML = txt;
		    
		thisPtr.Content = txt;
		
	    thisPtr.CurrentWidth = parseInt(txt.length) * 6,5;
	    thisPtr.Object.style.width = ( thisPtr.CurrentWidth > 350 ? 350 : thisPtr.CurrentWidth < 150 ? 150 : thisPtr.CurrentWidth) + "px";
	    
	    
	    if(header!=null && header.length > 0 && thisPtr.ToolTipHeader != null)
	    {
	        thisPtr.ToolTipHeader = header;
	    }
	    
	    if(window.event) thisPtr.Update(window.event);
	}
	
	this.Hide = function()
	{
	    canHide = true; //Dies verhindet das nervöse zucken des Tooltips wenn kurzzeitig über eine Border gefahren wird. Das Ausblenden wird um paar ms verzögert. Sollte in der Zwischenzeit ein neues Tooltip angezeigt werden, wird von den SetText Functionen das Flag canHide zurückgesetzt.
	    
	    var hideFunction = function(){
	                            if(!canHide) return;
                                thisPtr.Object.style.visibility = "hidden";
                                thisPtr.Object.style.width = "";
                            };
                    
        window.setTimeout(hideFunction,100);
	}
	
	this.Update = function(e)
	{	    
	    var x = 0;
		var y = 0;
		
		var screenWidth = parseInt(window.screen.width);
        var scrollLeft=document.documentElement?document.documentElement.scrollLeft:document.body.scrollLeft;
        var scrollTop =document.documentElement?document.documentElement.scrollTop:document.body.scrollTop;
       
        var ev=(!e)?window.event:e;//IE:Moz
        
        
        if (ev.pageX){//Moz
            x=ev.pageX;//+window.pageXOffset;
            y=ev.pageY;//+window.pageYOffset;
        }
        else if(ev.clientX){//IE
            //compliant mode vs. quirk mode
            x=ev.clientX+scrollLeft;
            y=ev.clientY+scrollTop;
        }
        else{return false;}//old browsers

        
        x += this.XOffset;
		y += this.YOffset;	
		
	    
	    var objWidth = parseInt(thisPtr.Object.style.width) || thisPtr.CurrentWidth;
	    
		
	    if( x + objWidth > screenWidth + scrollLeft)
		{			
		    x = x - objWidth - thisPtr.XOffset ;
		}


		if(thisPtr.MaxLeft != null &&  x + objWidth > thisPtr.MaxLeft + scrollLeft)
		{
			x = x - objWidth - thisPtr.XOffset ;
		}	
		
     	
	
		
			
		this.Object.style.left = x + "px";
		this.Object.style.top  = y + "px";
	}
	
	this.Register = function(sender, text, header)
	{
	    thisPtr.ShowText(text, header);
		
		sender.onmousemove = thisPtr.Update.bindAsEventListener(thisPtr);
		sender.onmouseout = thisPtr.Hide.bind(thisPtr);		
	}
}


