/*************************************************************************
  dw_tooltip.js   requires: dw_event.js and dw_viewport.js
  version date: May 2005
  ( March 2005 - minor changes in position algorithm and timer mechanism)
  
  This code is from Dynamic Web Coding at dyn-web.com
  Copyright 2003-5 by Sharon Paine 
  See Terms of Use at www.dyn-web.com/bus/terms.html
  regarding conditions under which you may use this code.
  This notice must be retained in the code as is!
*************************************************************************/

/*  Readable code available for licensed users */

/*************************************************************************
  
  dw_tooltip.js
  version date: Nov 2003
  requires: dw_event.js and dw_viewport.js
  
  This code is from Dynamic Web Coding at http://www.dyn-web.com/
  Copyright 2003 by Sharon Paine 
  See Terms of Use at http://www.dyn-web.com/bus/terms.html
  regarding conditions under which you may use this code.
  This notice must be retained in the code as is!

*************************************************************************/

var Tooltip = {
  followMouse: true,
  offX: 8,
  offY: 12,
  
  ready: false,
  t1: null,
  t2: null,
  tipID: "tipDiv",
  tip: null,
  
  init: function() {
    if ( document.createElement && document.body && typeof document.body.appendChild != "undefined" ) {
      var el = document.createElement("DIV");
      el.className = "tooltip";
      el.id = this.tipID;
	  el.style.width = "160px";
      document.body.appendChild(el);
      this.ready = true;
    }
  },
  
  showAbs: function(e, msg, obj, xOffset, yOffset) {
    if (this.t1) clearTimeout(this.t1);	
  	if (this.t2) clearTimeout(this.t2); 
    this.tip = document.getElementById( this.tipID );
  	// set up mousemove 
  	if (this.followMouse) 
      dw_event.add( document, "mousemove", this.trackMouse, true );
    this.writeTip(msg);
    viewport.getAll();
    this.positionTip(e, obj, xOffset, yOffset);
	document.getElementById('mnu_iframe').style.visibility = 'visible';
  	this.t1 = setTimeout("document.getElementById('" + Tooltip.tipID + "').style.visibility = 'visible'",0);	
    },
	
  show: function(e, msg) {
    if (this.t1) clearTimeout(this.t1);	
  	if (this.t2) clearTimeout(this.t2); 
    this.tip = document.getElementById( this.tipID );
  	// set up mousemove 
  	if (this.followMouse) 
      dw_event.add( document, "mousemove", this.trackMouse, true );
    this.writeTip(msg);
    viewport.getAll();
    this.positionTip(e);
	document.getElementById('mnu_iframe').style.visibility = 'visible';
  	this.t1 = setTimeout("document.getElementById('" + Tooltip.tipID + "').style.visibility = 'visible'",0);	
    },
    
    writeTip: function(msg) {
      if ( this.tip && typeof this.tip.innerHTML != "undefined" ) this.tip.innerHTML = msg;
    },
    
	positionTipAbs: function(e, obj, xOffset, yOffset) {
		
		var x = findPosX(obj) + xOffset;
		var y = findPosY(obj) + yOffset;
		var mnu_iframe = document.getElementById('mnu_iframe');
		
		mnu_iframe.style.top = y; //move iframe
		mnu_iframe.style.left = x; //move iframe
		mnu_iframe.style.width = this.tip.offsetWidth;
		mnu_iframe.style.height = this.tip.offsetHeight;
		mnu_iframe.style.zIndex = this.tip.style.zIndex-1;
		
		this.tip.style.left = x + "px"; this.tip.style.top = y + "px";
		
    },
	
    positionTip: function(e) {
		var x = e.pageX? e.pageX: e.clientX + viewport.scrollX;
		var y = e.pageY? e.pageY: e.clientY + viewport.scrollY;
		
		if ( x + this.tip.offsetWidth + this.offX > viewport.width + viewport.scrollX )
		x = x - this.tip.offsetWidth - this.offX;
		else x = x + this.offX;
		
		if ( y + this.tip.offsetHeight + this.offY > viewport.height + viewport.scrollY )
		y = ( y - this.tip.offsetHeight - this.offY > viewport.scrollY )? y - this.tip.offsetHeight - this.offY : viewport.height + viewport.scrollY - this.tip.offsetHeight;
		else y = y + this.offY;
		
		this.tip.style.left = x + "px"; this.tip.style.top = y + "px";
		
		var mnu_iframe = document.getElementById('mnu_iframe');
		mnu_iframe.style.top = y; //move iframe
		mnu_iframe.style.left = x; //move iframe
		mnu_iframe.style.width = this.tip.offsetWidth;
		mnu_iframe.style.height = this.tip.offsetHeight;
		mnu_iframe.style.zIndex = this.tip.style.zIndex-1;
    },
    
    hide: function() {
      if (this.t1) clearTimeout(this.t1);	
    	if (this.t2) clearTimeout(this.t2); 
		document.getElementById('mnu_iframe').style.visibility = 'hidden';
      this.t2 = setTimeout("document.getElementById('" + this.tipID + "').style.visibility = 'hidden'",0);
    	// release mousemove
    	if (this.followMouse) 
    		dw_event.remove( document, "mousemove", this.trackMouse, true );
      this.tip = null;
    },
    
    trackMouse: function(e) {
    	e = dw_event.DOMit(e);
     	Tooltip.positionTip(e);	
    }

}

function findPosX(obj)
{
	var curleft = 0;
	if (obj.offsetParent)
	{
		while (obj.offsetParent)
		{
			curleft += obj.offsetLeft
			obj = obj.offsetParent;
		}
	}
	else if (obj.x)
		curleft += obj.x;
	return curleft;
}

function findPosY(obj)
{
	var curtop = 0;
	if (obj.offsetParent)
	{
		while (obj.offsetParent)
		{
			curtop += obj.offsetTop
			obj = obj.offsetParent;
		}
	}
	else if (obj.y)
		curtop += obj.y;
	return curtop;
}

Tooltip.init();

