var Tooltip = {

  mouseX : 0,
  mouseY : 0,

  timeoutId : 0,

  enable : function( caller ) {
    function show() {
      Tooltip.getTooltipTag().style.visibility = "visible";
    }

    this.timeoutId = window.setTimeout( show, 500);
    this.updateTooltipPos();

    var msg = caller.lastChild.innerHTML;
    this.getTooltipTag().innerHTML = msg.substring(2,msg.length-1);
  },

  disable : function() {
    window.clearTimeout(this.timeoutId);
    this.getTooltipTag().style.visibility = "hidden";
  },

  movemouseEvent : function( e ) {
    var e2 = !document.all ? e : event ;
    Tooltip.mouseX = e2.clientX;
    Tooltip.mouseY = e2.clientY;
    Tooltip.updateTooltipPos();
  },

  updateTooltipPos : function() {
    var tooltip = this.getTooltipTag();

    var width, height, screenwidth, screenheight, left, top;
    var offsetx = 10;
    var offsety = 10;
    var buffer = 25;
     
    // get the computed size of the div
    if (document.all) {
      width = document.all.tooltip.offsetWidth;
      height = document.all.tooltip.offsetHeight;
    } else {
      width = parseInt(document.defaultView.getComputedStyle(tooltip, '').getPropertyValue("width"));
      height = parseInt(document.defaultView.getComputedStyle(tooltip, '').getPropertyValue("height"));
    }
    
    // get the screen size
    if (typeof(window.innerWidth) == 'number') {
      //Non-IE
      screenwidth = window.innerWidth;
      screenheight = window.innerHeight;
    } else if (document.documentElement && (document.documentElement.clientWidth || document.documentElement.clientHeight)) {
      //IE 6+ in 'standards compliant mode'
      screenwidth = document.documentElement.clientWidth;
      screenheight = document.documentElement.clientHeight;
    } else if (document.body && (document.body.clientWidth || document.body.clientHeight)) {
      //IE 4 compatible
      screenwidth = document.body.clientWidth;
      screenheight = document.body.clientHeight;
    }

    // compute if div is going offscreen, if so set it to inside + buffer
    if (this.mouseX + width + offsetx + buffer >= screenwidth) left = screenwidth - width - buffer;
    else left = this.mouseX + offsetx;
    if (this.mouseY + height + offsety + buffer >= screenheight) top = screenheight - height - buffer;
    else top = this.mouseY + offsety;

    // set the coords
    tooltip.style.left = left + "px";
    tooltip.style.top  = top + "px";
  },

  getTooltipTag : function() {
    return document.getElementById( "tooltip" );
  }

}

// set the page mouse event
document.onmousemove = Tooltip.movemouseEvent;
