/*************************************************************************
  dw_tooltip.js
  version date: July 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
  Permission granted to use this code 
  as long as this entire notice is included.
*************************************************************************/
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) {
    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);
  	this.t1 = setTimeout("document.getElementById('" + this.tipID + "').style.visibility = 'visible'",200);	
    },
    
    writeTip: function(msg) {
      if ( this.tip && typeof this.tip.innerHTML != "undefined" ) this.tip.innerHTML = msg;
    },
    
    positionTip: function(e) {
      var x, y;
      e = dw_event.DOMit(e);
	
      if ( e.clientX + this.tip.offsetWidth + this.offX > viewport.width ) {
      	this.tip.style.width = 250;
        x = e.clientX - this.tip.offsetWidth - this.offX + viewport.scrollX;
        }
      else x = e.clientX + viewport.scrollX + this.offX;
      
      if ( e.clientY + this.tip.offsetHeight + this.offY > viewport.height )
        y = viewport.height + viewport.scrollY - this.tip.offsetHeight;
      else y = e.clientY + viewport.scrollY + this.offY;
      if (x < 0)
      	x = 2;
      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();

/*************************************************************************
    dw_event.js
    version date June 2003
    to negotiate cross-browser differences in event models
    for browsers that support addEventListener and attachEvent 
        
    This code is from Dynamic Web Coding 
    at http://www.dyn-web.com/
    See Terms of Use at http://www.dyn-web.com/bus/terms.html
    Permission granted to use this code 
    as long as this entire notice is included.
*************************************************************************/
var dw_event = {

  add: function(obj, etype, fp, cap) {
    cap = cap || false;
    if (obj.addEventListener) obj.addEventListener(etype, fp, cap);
    else if (obj.attachEvent) obj.attachEvent("on" + etype, fp);
  }, 

  remove: function(obj, etype, fp, cap) {
    cap = cap || false;
    if (obj.removeEventListener) obj.removeEventListener(etype, fp, cap);
    else if (obj.detachEvent) obj.detachEvent("on" + etype, fp);
  }, 

  DOMit: function(e) {
    e = e? e: window.event;
    e.tgt = e.srcElement? e.srcElement: e.target;
    
    // location of event in document
    if (typeof e.pageX != "number") {
      e.pageX = e.clientX  + document.documentElement.scrollLeft + document.body.scrollLeft;
      e.pageY = e.clientY  + document.documentElement.scrollTop + document.body.scrollTop;
    }
    
    if (!e.preventDefault) e.preventDefault = function () { return false; }
    if (!e.stopPropogation) e.stopPropogation = function () { if (window.event) window.event.cancelBubble = true; }
        
    return e;
  }  
}

/*************************************************************************
  dw_viewport.js
  version date July 2003
  
  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
  Permission granted to use this code 
  as long as this entire notice is included.

  Example usage (after body tag or after page loaded):
  viewport.getWinWidth();
  alert(viewport.width)
*************************************************************************/  
viewport = {
  getIECanvas: function () {
    var canv = null;
    if (!window.opera && document.all && typeof document.body.clientWidth != "undefined") {
      var cm = document.compatMode && document.compatMode == "CSS1Compat";
      canv = cm? document.documentElement: document.body;
    } 
      return canv;
  },
  
  getWinWidth: function () {
    var canv;
    if ( canv = this.getIECanvas() ) this.width = canv.clientWidth;
    else this.width = window.innerWidth - 18;
  },
  
  getWinHeight: function () {
    var canv;
    if ( canv = this.getIECanvas() ) this.height = canv.clientHeight;
    else this.height = window.innerHeight - 18;
  },
  
  getScrollX: function () {
    var canv;
    if ( canv = this.getIECanvas() ) this.scrollX = canv.scrollLeft;
    else if (window.pageXOffset) this.scrollX = window.pageXOffset;
    else if (window.scrollX) this.scrollX = window.scrollX;
    else this.scrollX = 0;
  },
  
  getScrollY: function () {
    var canv;
    if ( canv = this.getIECanvas() ) this.scrollY = canv.scrollTop;
    else if (window.pageYOffset) this.scrollY = window.pageYOffset;
    else if (window.scrollY) this.scrollY = window.scrollY;
    else this.scrollY = 0;
  },
  
  getAll: function () {
    this.getWinWidth();   this.getWinHeight();
    this.getScrollX(); this.getScrollY();
  }  
}

/*************************************************************************
  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
  Permission granted to use this code 
  as long as this entire notice is included.
*************************************************************************/
function initHoverTip() {
  if ( document.getElementById && document.getElementById(Tooltip.tipID) ) { 
    Tooltip.followMouse = false;  // must be turned off for this version
    var tip = document.getElementById(Tooltip.tipID)
    tip.onmouseout = function(e) { Tooltip.tipOutCheck(e) }
    tip.onmouseover = function() { Tooltip.clearTimer() }
  }
}

Tooltip.tipOutCheck = function(e) {
  e = dw_event.DOMit(e);
  // is element moused into contained by tooltip? or tooltip itself?
  var tooltip = document.getElementById(Tooltip.tipID);
  var toEl = e.relatedTarget? e.relatedTarget: e.toElement;
    if ( tooltip != toEl && !contained(toEl, tooltip) ) 
      Tooltip.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;
}

Tooltip.timerId = 0;
Tooltip.clearTimer = function() {
  if (Tooltip.timerId) clearTimeout(Tooltip.timerId);
}

initHoverTip();