/*************************************************************************
$Id: hover.js,v 1.10 2009/10/29 21:51:12 nick Exp $
version date: Nov 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
  regarding conditions under which you may use this code.
  This notice must be retained in the code as is!

*************************************************************************/

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;

    if (!e.preventDefault) e.preventDefault = function () { return false; }
    if (!e.stopPropagation) e.stopPropagation = function () { if (window.event) window.event.cancelBubble = true; }

    return e;
  }

}

viewport = {
  getScrollX: function () {
    this.scrollX = 0;
  	if (typeof window.pageXOffset == "number") this.scrollX = window.pageXOffset;
  	else if (document.documentElement && document.documentElement.scrollLeft)
  		this.scrollX = document.documentElement.scrollLeft;
  	else if (document.body && document.body.scrollLeft)
  		this.scrollX = document.body.scrollLeft;
  	else if (window.scrollX) this.scrollX = window.scrollX;
  },

  getScrollY: function () {
    this.scrollY = 0;
    if (typeof window.pageYOffset == "number") this.scrollY = window.pageYOffset;
    else if (document.documentElement && document.documentElement.scrollTop)
  		this.scrollY = document.documentElement.scrollTop;
  	else if (document.body && document.body.scrollTop)
  		this.scrollY = document.body.scrollTop;
  	else if (window.scrollY) this.scrollY = window.scrollY;
  },

  getAll: function () {
    this.width = getWinWidth(); this.height =getWinHeight(); // defined in common.js
    this.getScrollX();  this.getScrollY();
  }

}

var Tooltip = {
  offX: 3,
  offY: 8,
  hideOnMouseOut: true,
  align: 'mouse',
  parentElement: null,
  color: '',

  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 );
    this.writeTip(msg);
    viewport.getAll();
    this.positionTip(e);
	Rounded("div#tipDiv","all","transparent",Tooltip.color,"border");
  	this.t1 = setTimeout("document.getElementById('" + Tooltip.tipID + "').style.visibility = 'visible'",50);
    },

    writeTip: function(msg) {
      if ( this.tip && typeof this.tip.innerHTML != "undefined" )
      {
	    this.tip.innerHTML = "";  // for mac ie
      	this.tip.innerHTML = '<div class=tipHTML style="background-color:'+Tooltip.color+';">'+msg+'</div>';
      }
    },

    positionTip: function(e) {
      var x; var y;
      if (this.align == 'topright')
      {
       	coords = getPageCoords(this.parentElement);
      	x = coords.x + this.parentElement.offsetWidth + 2;
      	y = coords.y;
	    if ( x + this.tip.offsetWidth > viewport.width + viewport.scrollX )
	      x = x - this.tip.offsetWidth - this.parentElement.offsetWidth - 4;

	    if ( y + this.tip.offsetHeight > viewport.height + viewport.scrollY )
	      y = y - this.tip.offsetHeight + this.parentElement.offsetHeight;
	  }
      else if (this.align == 'topcenter')
      {
       	coords = getPageCoords(this.parentElement);
       	x = coords.x + this.parentElement.offsetWidth/2 - this.tip.offsetWidth/2;
     	y = coords.y;
	    if ( x + this.tip.offsetWidth > viewport.width + viewport.scrollX )
	      x = x - this.tip.offsetWidth - this.parentElement.offsetWidth - 4;

	    if ( y + this.tip.offsetHeight > viewport.height + viewport.scrollY )
	      y = y - this.tip.offsetHeight + this.parentElement.offsetHeight;
	  }
      else if (this.align == 'center')
      {
	      x = viewport.width/2 + viewport.scrollX - this.tip.offsetWidth/2;
	      y = viewport.height/2 + viewport.scrollY - this.tip.offsetHeight/2;
	  }
	else if (this.align == 'bottomcenter')
	{
	 coords = getPageCoords(this.parentElement);
       	 x = coords.x + this.parentElement.offsetWidth/2 - this.tip.offsetWidth/2;
      	 y = coords.y + this.parentElement.offsetHeight;

      	 if (x + this.tip.offsetWidth > viewport.width + viewport.scrollX)
      	  x = viewport.width + viewport.scrollX - this.tip.offsetWidth;
         if (y + this.tip.offsetHeight > viewport.height + viewport.scrollY)
          y = y - this.parentElement.offsetHeight - this.tip.offsetHeight;
	}
	else //mouse
	{
	 x = e.pageX? e.pageX: e.clientX + viewport.scrollX;
	 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;
	}
	if (x < 0) x=0;
	if (y < 0) y=0;

      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'",50);
      this.tip = null;
    },

    trackMouse: function(e) {
    	e = dw_event.DOMit(e);
     	Tooltip.positionTip(e);
    }
}

function getPageCoords(element) {
  var coords = {x: 0, y: 0};
  do {
    coords.x += element.offsetLeft;
    coords.y += element.offsetTop;
  }
  while ((element = element.offsetParent));
  return coords;
}

Tooltip.tipOutCheck = function(e) {
  if (!Tooltip.hideOnMouseOut)
  	return;
  e = dw_event.DOMit(e);
  // is element moused into contained by tooltip?
  var toEl = e.relatedTarget? e.relatedTarget: e.toElement;
  if ( this != toEl && !contained(toEl, this, e) )
  	Tooltip.hide();
}

// returns true of oNode is contained by oCont (container)
function contained(oNode, oCont, e) {
  // some things (iframes) have no oNode
  if (!oNode)
  	return true;

  // if we're still in it's container (most likely an href)
  while ( oNode = oNode.parentNode )
  	if ( oNode == oCont )
  		return true;

  return false;
}

var tip;
function doToolTip(e, msg, hideOnMouseOut, align, parentElement, color, width) {

  // need to create the object on the first call
  if (!Tooltip.ready)
  {
		Tooltip.init();
		Tooltip.timerId = 0;
		Tooltip.clearTimer = function() {
		  if (Tooltip.timerId) clearTimeout(Tooltip.timerId);
		}

		tip = document.getElementById ? document.getElementById(Tooltip.tipID): null;
		if (tip)
		{
		  tip.onmouseout = Tooltip.tipOutCheck;
		  tip.onmouseover = Tooltip.clearTimer;
		}
  }

  // if it's not ready now, we're hosed.
  if ( typeof Tooltip == "undefined" || !Tooltip.ready )
  		return;

  if (!width)
  	  width=180;
//  document.getElementById(Tooltip.tipID).style.width=width+'px';
  if (width)
    document.getElementById(Tooltip.tipID).style.width=width+'px';
  if (typeof hideOnMouseOut != "undefined")
	  Tooltip.hideOnMouseOut = hideOnMouseOut;
  if (align)
	  Tooltip.align = align;
  if (parentElement)
	  Tooltip.parentElement = parentElement;
  if (color)
	  Tooltip.color = color;
  else
      Tooltip.color = '#f8faff';
  Tooltip.clearTimer();

  Tooltip.show(e, msg);

}

function hideTip() {
  if ( typeof Tooltip == "undefined" || !Tooltip.ready ) return;
  Tooltip.timerId = setTimeout("Tooltip.hide()", 300);
}