/*************************************************************************
dw_tooltip.js
requires dw_core.js and dw_util.js
version date: Feb 2003

This code is from Dynamic Web Coding
at http://www.dyn-web.com/
Copyright 2001-3 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 win_width, win_height, vert_scroll, hor_scroll;
var tip_t1, tip_t2;	// for setTimeouts
var mouseX, mouseY;

window.onload = initTip;
window.onresize = to_do_onresize;

var origWidth, origHeight;
if (document.layers) {
   origWidth = window.innerWidth;
   origHeight = window.innerHeight;
}
// onresize
function to_do_onresize() {
   if (document.layers) {
      if (window.innerWidth != origWidth || window.innerHeight != origHeight)
         window.location.reload();
   } else {
      win_width = getWinWidth();
      win_height = getWinHeight();
   }
}

function initTip() {
   scrollInit();
   auto_center();
   tooltip = new dynObj('tipDiv');
   if (!tooltip) return;
   if (!document.layers && typeof tooltip.doc.innerHTML == "undefined"){
      tooltip = null;	// for opera
      return; 
   }
   if (tooltip.el.style) {	// ns4 would lose all this on rewrites
      tooltip.css.width = tipWidth+"px";
      tooltip.css.fontFamily = tipFontFamily;
      tooltip.css.fontSize = tipFontSize;
      tooltip.css.lineHeight = tipLineHeight;
      tooltip.css.color = tipFontColor;
      tooltip.css.backgroundColor = tipBgColor;
      tooltip.css.borderColor = tipBorderColor;
      tooltip.css.borderWidth = tipBorderWidth+"px";
      tooltip.css.padding = tipPadding+"px";
      tooltip.css.borderStyle = tipBorderStyle;
      if (tipBgImg) tooltip.css.backgroundImage = "url("+tipBgImg+")";
   }
   // used in calculating tip position
   win_width = getWinWidth();
   win_height = getWinHeight();
}

function goTooltip(evt,cntnt) {
   if (tip_t1) clearTimeout(tip_t1);	
   if (tip_t2) clearTimeout(tip_t2); 
   // set up mousemove 
   if (tipFollowMouse) {
      if (document.addEventListener) {
         document.addEventListener("mousemove",trackMouse,true);
      } else if (document.attachEvent) {
         document.attachEvent("onmousemove",trackMouse);
      } else {
         if (document.layers && document.captureEvents) 
            document.captureEvents(Event.MOUSEMOVE);
         document.onmousemove = trackMouse;
      }
   }
   tooltip.writeLyr(cntnt);	// write tooltip content to tipDiv
   // to get document area in view, check scroll amounts
   vert_scroll = getScrollY();
   hor_scroll = getScrollX();
   // get coordinates of mouseover event
   evt = (window.event)? window.event: evt;
   if (evt) {
      mouseX = getMouseX(evt);
      mouseY = getMouseY(evt);
   }
   // get tooltip width and height, for calculating position
   // and yes style.width was set, but browsers differ ...
   tooltip.width = getWidth(tooltip.el);	
   tooltip.height = getHeight(tooltip.el);
   // again here, delay needed by some browsers
   if (!document.layers)
      setTimeout("tooltip.height=getHeight('','tipDiv')",100);
   // position the tooltip, delay for getHeight
   setTimeout("positionTip()",120);	
   tip_t1=setTimeout(tooltip.obj+".show()",200);	// show tooltip
}

// check coordinates and position tooltip
function positionTip() {
   var x, y;
   tooltip.height = getHeight(tooltip.el); // important here if images not preloaded
   if ((mouseX + tipOffX + tooltip.width) > win_width + hor_scroll) 
      x = mouseX - (tooltip.width + tipOffX);
   else x = mouseX + tipOffX;
	
   if ((mouseY + tipOffY + tooltip.height) > win_height + vert_scroll) 
      y = (mouseY - (tooltip.height + tipOffY) > vert_scroll)?  mouseY - (tooltip.height + tipOffY): win_height + vert_scroll - (tooltip.height + tipOffY);
   else y = mouseY + tipOffY;
   tooltip.shiftTo(x,y);
}

function hideTip() {
   if (!tooltip) return;
   tip_t2=setTimeout(tooltip.obj+".hide()",200);
   // release mousemove
   if (tipFollowMouse) {
      if (document.removeEventListener) {
         document.removeEventListener("mousemove",trackMouse,true);
      } else if (document.detachEvent) {
         document.detachEvent("onmousemove",trackMouse);
      } else {
         if (document.layers && document.releaseEvents)
            document.releaseEvents(Event.MOUSEMOVE);
         document.onmousemove = null;
      }
   }
}

// used with tipFollowMouse
function trackMouse(evt) {
   evt = (window.event)? window.event: evt;
   if (evt) {
      mouseX = getMouseX(evt);
      mouseY = getMouseY(evt);
   }
   positionTip();	
}

// functions to get page coordinates of mouse event
function getMouseX(evt) {
   return (evt.pageX)? evt.pageX: evt.clientX + getScrollX();
}

function getMouseY(evt) {
   return (evt.pageY)? evt.pageY: evt.clientY + getScrollY();
}
