// INITIALIZATION

var ve_tooltips_handler = new VEToolTipHandler();

function ve_tooltips_displayTooltip( tip, color, bgColor, font )
  {
  ve_tooltips_handler.displayToolTip( tip, color, bgColor, font );
  }

function ve_tooltips_hideTooltip()
  {
  ve_tooltips_handler.hideToolTip();
  }

// TOOLTIPS OBJECTS

function VEToolTipHandler()
  {
  this.popUp = null;
  this.popupCss = null;
  this.timer = null;
  
  this.allowFilters = navigator.userAgent.indexOf( "MSIE 5.5") >= 0 || navigator.userAgent.indexOf( "MSIE 6" ) >= 0  ? true : false;  
  
  this.create = function()
    {
    if( document.getElementById( "VEToolTipFrame" ) == null )  
  	  document.body.insertAdjacentHTML( "BeforeEnd", '<iframe scrolling="no" class="VEToolTipFrame" marginwidth="0" marginheight="0" frameborder="0" id="VEToolTipFrame" name="VEToolTipFrame" style="display:none;position:absolute;z-index:1000000"></iframe>' );

  	this.popUp    = document.all ? self.frames[ "VEToolTipFrame" ] : document.getElementById( "VEToolTipFrame" );
  	this.popUpCss = document.getElementById( "VEToolTipFrame" );
  	
  	document.body.attachEvent( "onmousedown", function()
  	    { 
  	    ve_tooltips_handler.popUpCss.style.display = "none";
  	    } 
  	  );
  	  
  	this.popUpCss.onblur = function()
  	    {
  	    ve_tooltips_handler.popUpCss.style.display = "none";
  	    };
    }
  
  this.showPopup = function( x, y )
    {
	  this.timer = setTimeout( 've_tooltips_handler.hideToolTip()', 4000 );
  	this.popUpCss.style.display = "block";
    }

  this.displayToolTip = function( tipText, color, bgColor, font )
    {
  	var e = window.event;
  	var x = moz ? e.clientX : e.x;
  	var y = moz ? e.clientY : e.y;

    if( tipText != null && tipText != '' )
      {
      if( this.popUp == null )
        this.create();
        
      var styleStr = "";

		  if( color )
  	    styleStr += "color: " + color + ";";
		    
		  if( bgColor )
  	    styleStr += "background-color: " + bgColor + ";";
		
		  if( font )
  	    styleStr += font;
    
  	  this.populatePopup( tipText, styleStr, window );

      var tooltip = this.getToolTip();
      
  	  if( this.allowFilters && tooltip.filters && tooltip.filters[0] )
        this.getToolTip().filters[0].Apply();

  	  this.showPopup( x,y );
  	  this.calculateSize();
  	  this.adjustPosition( x,y );
  	
  	  if( this.allowFilters && tooltip.filters && tooltip.filters[0] )
        this.getToolTip().filters[0].Play();
       }
    }
    
  this.hideToolTip = function()
    {
    if( this.popUpCss )
  	  this.popUpCss.style.display = "none";
  	
  	if( this.timer != null )
  	  {
	    clearTimeout( this.timer );
  	  this.timer = null;
  	  }
    }  

  this.getScrollTop = function()
    {
    return document.body.scrollTop;
    //window.pageXOffset and window.pageYOffset for moz
    }
 
  this.getScrollLeft = function()
    {
    return document.body.scrollLeft;
    }
 
  this.adjustPosition=function( x, y )
    {
  	var docheight = document.body.clientHeight;
  	var docwidth  = document.body.clientWidth;
  	var dh = (this.popUpCss.offsetHeight + y) - docheight;
  	var dw = (this.popUpCss.offsetWidth + x)  - docwidth;
  	
  	if( dw > 0 )
  		this.popUpCss.style.left = (x - dw) + this.getScrollLeft() + "px";		
  	else
  		this.popUpCss.style.left = x + this.getScrollLeft();
  
  	if( dh > 0 )
  		this.popUpCss.style.top = (y - dh) + this.getScrollTop() + "px"
  	else
  		this.popUpCss.style.top  = y + this.getScrollTop();
    }

  this.calculateSize = function()
    {
  	var body,h,w;
  	this.popUpCss.style.width = "10px";
  	this.popUpCss.style.height = "100000px";
  	body = moz ? this.popUp.contentDocument.body : this.popUp.document.body; 
  	
  	if( moz )
    	{
    	h = body.scrollHeight;
    	w = "100";
    	this.popUpCss.style.height = h + "px";
    	this.popUpCss.style.width = w + "px";
    	//use document.height for moz
    	}
    else
      {
    	// check offsetHeight twice... fixes a bug where scrollHeight is not valid because the visual state is undefined
    	var dummy = this.popUpCss.offsetHeight + " dummy";
    	h = body.scrollHeight + this.popUpCss.offsetHeight - body.clientHeight;
    	w = body.scrollWidth + this.popUpCss.offsetWidth - body.clientWidth;
    	this.popUpCss.style.height = h + "px";
    	this.popUpCss.style.width = w + "px";
    	}
    }

  this.populatePopup = function( tipText, styleStr, win )
    {
  	var doc = moz ? this.popUp.contentDocument : this.popUp.document;
  
  	if( doc.getElementsByTagName( "LINK" ).length == 0 ) 
  	  {
  		doc.open();
  		doc.write('<html><head><link rel="StyleSheet" type="text/css" href="/ve/res/css/vetooltips.css"></head><body></body></html>');
  		doc.close();
    	}

    var html = "<div id='VEToolTipHolder' class='VEToolTip' nowrap ";
    html += "style='" + styleStr + "'";
    html += ">";
    html += tipText;
    html += "</div>"
  	
  	doc.body.innerHTML = html;
    }
    
  this.getToolTip = function()
    {
  	var doc = moz ? this.popUp.contentDocument : this.popUp.document;
  	return doc.getElementById( "VEToolTipHolder" );
    }  
  }

