  /*  Your are permitted to reuse this code as long as the following copyright
      notice is not removed:

      This HTML tip handling is copyright 1998 by insideDHTML.com, LLC. More information about this
      code can be found at Inside Dynamic HTML: HTTP://www.insideDHTML.com
      
      Modification by Alex Fung <Alex.Fung.Ho-san@graduate.hku.hk>. 
        Use Daisy Chain to handle events.
        remove NN4 code
        streamline IE4 code
        dynamic resize the tooltip container
        dynamic force the tooltip container to be within viewing window

      Modification by Alex Fung <Alex.Fung.Ho-san@graduate.hku.hk> Aug 1999:
        use separate functions instead of generic getOffset()

      Modification by Alex Fung <Alex.Fung.Ho-san@graduate.hku.hk> Aug 2000 (v0.15.7):
        use window.event.x and .y to positioning relative to mouse, instead of to element
        streamlined in performance and stability
        now if you click the base element, the tip will stay (even after mouse out).
        To unstick the tip, click again the base element, or mouse over another tip, or double click the tip itself.
        use getElementsByTagName() to find <hr>s inside the tip
        mouse over/out() now checks event.from/to element.
  */


  function checkName(src) {
    while ((src!=null) && (src._tip==null))src = src.parentElement;
    return src;
  }

  function displayContents(tip) {
    // Display the tooltip. 
    var el = tipBox;
    var src = window.event.srcElement;
    el.style.pixelLeft = 0;
    el.style.width = document.body.clientWidth;
    el.innerHTML = "<span>" + tip + "<\/span>";
    el._fitting = true;
    setTimeout("fitContents(" + window.event.x + "," + window.event.y + ")",100); 
    // give time for width requirement to settle down
    // note the event.x/y are no longer available at filContents()
  }

  function fitContents(x,y) {
    var el = tipBox;
    if (!el._fitting) return; // already mouse-out-ed
    // set top pos so bottom is above mouse
    if (y > el.offsetHeight + 15) el.style.pixelTop = y + document.body.scrollTop - el.offsetHeight - 15;
    else el.style.pixelTop = document.body.scrollTop;
    // set left pos to mouse
    el.style.width = el.children(0).offsetWidth + 10; 
    var toofar = x + el.offsetWidth - document.body.scrollLeft - document.body.clientWidth + 10;
    if (toofar > 0) {  // hit right edge
      if (el.children(0).offsetWidth + 10 > document.body.clientWidth - 10) el.style.pixelLeft = document.body.scrollLeft; // too wide for whole screen
      else el.style.pixelLeft = document.body.scrollLeft + document.body.clientWidth - el.children(0).offsetWidth - 20;
    } else el.style.pixelLeft = x;  
    if (el._fitting) {
      el.style.visibility = 'visible';
      el._display = true;
      if (document.childNodes) { // IE5+
        var elhr=el.getElementsByTagName("HR");
        var i = elhr.length;
        while (i--) elhr[i].width="75%";
      } else {
        var da=document.all;
        var i = el.sourceIndex + 1;
        for (var dai = da[i]; el.contains(dai); dai=da[++i]) {
          if (dai.tagName == "HR" && dai.width == 0) dai.width="75%";
        }
      }
      if (!el._fitting) { // already turned off
        el.style.visibility = "hidden";
        el._display = false;
      }
      el._fitting = false;
    }
  }
  
  function doMouseOver() {
    // Mouse moves over an element
    var el = checkName(window.event.srcElement);
    if (el) {
      if (tipBox._click) with(tipBox) {
        style.visibility = "hidden";
        _click = _fitting = _display = false;
      } else if (tipBox._display || tipBox._fitting) return;
      displayContents(el._tip);
    }
  }

  function doMouseOut() {
    // Mouse leaves an element
    var el = checkName(window.event.srcElement);
    if (el) {
      if (tipBox._click == true) return; // tip to stay
      if (tipBox._display) {
        if ((el.contains==null) || (!el.contains(window.event.toElement))) with(tipBox) {
          style.visibility = "hidden";
          _display = false;
        }
      } else if (tipBox._fitting) tipBox._fitting = false;
    }
  }

  function doClick() {
    var el = checkName(window.event.srcElement);
    if (el) tipBox._click = tipBox._click == true ? false : true;
  }

  function doTipDblClick() {
    with (tipBox) {
      _click = false;
      _display = false;
      style.visibility = "hidden";
    }
  }

  function doTooltipLoad() {
    // Do Loading
    if (tipBox) {
      tipBox.ondblclick = doTipDblClick;
      if (window.DaisyChain) {
        window.DaisyChain.add("mouseover", doMouseOver);
        window.DaisyChain.add("mouseout", doMouseOut);
        window.DaisyChain.add("click", doClick);
      } else {
        with (document) {
          onmouseover = doMouseOver;
          onmouseout = doMouseOut;
          onclick = doClick;
        }
      }
    }
  }

  if (window.DaisyChain) window.DaisyChain.add("load", doTooltipLoad);
  else window.onload = doTooltipLoad;
  

