/*************************************************************************
  tooltipgeneral.js
  version date: March 2004
  requires tooltipcustom.js
  
This code has been extended after merging several codes from
Dynamic Web Coding, available at http://www.dyn-web.com/.
The extension of the code has been made at University of Savoie
in France by P. Febvre. 
This code allows to display different kinds of tooltips on the same
Webpage: moving tooltips without links, fixed tooltips with possibilities
of links and menu tooltips.
Moreover it is possible to individually customize the background color and
offset position of the tooltips.

    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!
*************************************************************************/

// Tooptip used for (a priori) moving tips without links
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;
      document.body.appendChild(el);
      this.ready = true;
    }
  },
  
  show: function(e, msg, offsetx, offsety) {
    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, offsetx, offsety);
    viewport.getAll();
    this.positionTip(e);
  	this.t1 = setTimeout("document.getElementById('" + Tooltip.tipID + "').style.visibility = 'visible'",200);	
    },
    
    writeTip: function(msg, offsetx, offsety) {
      if ( this.tip && typeof this.tip.innerHTML != "undefined" ) {
	     this.tip.innerHTML = msg;
		 this.offX=offsetx;
	     this.offY=offsety;
	     }
	  },
    
    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";
    },
    
    hide: function() {
      if (this.t1) clearTimeout(this.t1);	
    	if (this.t2) clearTimeout(this.t2); 
      this.t2 = setTimeout("document.getElementById('" + this.tipID + "').style.visibility = 'hidden'",200);
    	// 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);	
    }

}

Tooltip.init();

// ToopLink used for fixed tips with links
var ToolLink = {
  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 = "toolLink";
      el.id = this.tipID;
      document.body.appendChild(el);
      this.ready = true;
    }
  },
  
  show: function(e, msg, offsetx, offsety) {
    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.writeLink(msg, offsetx, offsety);
    viewport.getAll();
    this.positionLink(e);
  	this.t1 = setTimeout("document.getElementById('" + ToolLink.tipID + "').style.visibility = 'visible'",200);	
    },
    
    writeLink: function(msg, offsetx, offsety) {
      if ( this.tip && typeof this.tip.innerHTML != "undefined" ) {
	     this.tip.innerHTML = msg;
		 this.offX=offsetx;
	     this.offY=offsety;
	     }
	  },
    
    positionLink: 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";
    },
    
    hide: function() {
      if (this.t1) clearTimeout(this.t1);	
    	if (this.t2) clearTimeout(this.t2); 
      this.t2 = setTimeout("document.getElementById('" + this.tipID + "').style.visibility = 'hidden'",200);
    	// release mousemove
    	if (this.followMouse) 
    		dw_event.remove( document, "mousemove", this.trackMouse, true );
      this.tip = null;
    },
    
    trackMouse: function(e) {
    	e = dw_event.DOMit(e);
     	ToolLink.positionLink(e);	
    }

}

ToolLink.init();

/*************************************************************************
Addition for fixed ToolLink
*************************************************************************/

function initHoverTip() {
  if ( document.getElementById && document.getElementById(ToolLink.tipID) ) { 
    ToolLink.followMouse = false;  // must be turned off for this version
    var tip = document.getElementById(ToolLink.tipID)
    tip.onmouseout = function(e) { ToolLink.tipOutCheck(e) }
    tip.onmouseover = function() { ToolLink.clearTimer() }
  }
}

ToolLink.tipOutCheck = function(e) {
  e = dw_event.DOMit(e);
  // is element moused into contained by tooltip? or tooltip itself?
  var toolLink = document.getElementById(ToolLink.tipID);
  var toEl = e.relatedTarget? e.relatedTarget: e.toElement;
    if ( toolLink != toEl && !contained(toEl, toolLink) ) 
      ToolLink.hide();
}

// returns true of oNode is contained by oCont (container)
function contained(oNode, oCont) {
  if (!oNode) return; // in case alt-tab away while hovering (prevent error)
  while ( oNode.parentNode ) {
    oNode = oNode.parentNode;
    if ( oNode == oCont ) return true;
  }
  return false;
}

ToolLink.timerId = 0;
ToolLink.clearTimer = function() {
  if (ToolLink.timerId) clearTimeout(ToolLink.timerId);
}

initHoverTip();
