var xc,yc,interval,focusTip;
var isIE = (navigator.appName == "Microsoft Internet Explorer");

function hideTip() {focusTip.hidetip();}
function showTip() {focusTip.display();}

function DocumentListener(elem) { // extends EventListener
  this.base = EventListener;
  this.base(elem);
  
  this.coords = {left:0, top:0};
  
  var me = this;
  
  this.init = function() {
    this.register('mousemove', this.setCoordinates);  
    this.register('mousemove', this.debug);      
  }
  
  this.debug = function() {
    document.getElementById("debug").innerHTML = "(" + me.coords.left + ", " + me.coords.top + ")";  
  }
  
  this.setCoordinates = function(evt) {
    me.invoke(evt);
    var x, y;
    if (evt.pageX) {
      x = me.evt.pageX;
      y = me.evt.pageY;  
    }
    else if (evt.clientX) {
      var b = document.body;
      x = me.evt.clientX + b.scrollLeft - b.clientLeft;
      y = me.evt.clientY + b.scrollTop - b.clientTop;
      if (b.parentElement && b.parentElement.clientLeft) {
        p = b.parentElement;
        x += p.scrollLeft + p.clientLeft;
        y  += p.scrollTop + p.clientTop;  
      }
      y -= 4; 
           
    }
    me.coords.left = x;
    me.coords.top  = y + 10;
  }  
}
DocumentListener.prototype = new EventListener;

function ToolTipListener(elem, tooltip, doc) { // extends EventListener
  this.base = EventListener;
  this.base(elem);
  this.tooltip = tooltip;
  this.doc = doc;
  this.tip = "";
  this.etype;
  
  var me = this;
  
  this.init = function() {
    this.register('mouseover', this.showtip); 
    this.register('focus', this.showtip);     
    this.register('mouseout',  this.hidetip);     
    this.register('blur',  this.hidetip);         
    this.tip = this.elem.getAttribute('title');
    this.elem.setAttribute('title', '');  
  }
  
  this.showtip = function(evt) {
    me.invoke(evt);
    me.etype = evt.type;
    clearInterval(interval);
    focusTip = me;
    interval = setTimeout("showTip()", 500);
  }

  this.hidetip = function(evt) {
    me.tooltip.style.display = "none";
    focusTip = "undefined";
    clearInterval(interval);    
  }
  
  this.display = function() {
	  if (this.etype.indexOf('mouse') != -1) {
	    this.tooltip.style.top = (this.doc.coords.top+20) + "px";
	    this.tooltip.style.left = this.doc.coords.left + "px";  
	  }
	  else {

	  }
	  this.tooltip.firstChild.innerHTML = this.tip;  	 
    this.tooltip.style.display = "block";    
    if (isIE) {
      this.tooltip.firstChild.style.background = "#fff";  
    }
    interval = setTimeout("hideTip()", 3000);    
  }

}
ToolTipListener.prototype = new EventListener;

function __init()
{
  var tooltip, doc, ancs, elem, listener, i;  
  
  tooltip = document.createElement("div");
  tooltip.id = "tip";
  inside = document.createElement("div");
  tooltip.appendChild(inside);
  document.getElementsByTagName("body").item(0).appendChild(tooltip);
  
  doc = new DocumentListener(document);
  doc.init();

  ancs = document.getElementsByTagName("a");  
  for (i = 0; elem = ancs.item(i++); ) {
    listener = new ToolTipListener(elem, tooltip, doc);
    listener.init();    
  }
}

addLoadEvent(__init);