/**
 * Basic ToolTip controller
 */
function Int(x) { var y; return isNaN(y = parseInt(x)) ? 0 : y; }

var ToolTip = {
  init : function() {
    document.tt = new Object();
    document.tt.varsion = '1.0';
    document.tt.tip = false;
    document.tt.timer = false;
    document.tt.element = false;
    document.tt.show = false;
    document.tt.instance = this;
    document.tt.timeput = 1500;
    document.tt.mouseX = 0;
    document.tt.mouseY = 0;
    document.tt.event = false;
  },

  over: function(event,element)
  {
    if( document.tt.show || document.tt.tip )
    {
      if( document.tt.element == element )
        return;
      this.out();
    }
    document.tt.element = element;
    this.findTip();
    var e = event || window.event;
    var c = Utils.getCursorPosition(e);
    document.tt.mouseX = c.x;
    document.tt.mouseY = c.y;
    document.tt.timer = setTimeout( ToolTip.show, document.tt.timeout );
  },
  out: function()
  {
    clearTimeout( document.tt.timer );
    if( document.tt.tip )
      document.tt.tip.style.display = "none";
    document.tt.timer = false;
    document.tt.show = false;
    document.tt.tip = false;
    return false;
  },

  // PRIVATE FUNCTIONS
  findTip: function()
  {
    // Search all childrens for element with "tooltip" class
    var i;
    for(i=0; i<document.tt.element.childNodes.length; i++)
    {
      if( document.tt.element.childNodes[i].className == "tooltip" )
      {
        document.tt.tip = document.tt.element.childNodes[i];
        document.tt.show = true;
        return;
      }
    }
    document.tt.tip = false;
    document.tt.show = false;
  },

  show: function()
  {
    if( !document.tt.show )
      return ToolTip.out();

    var mx = document.tt.mouseX;
    var my = document.tt.mouseY;
    mx += 5; my += 10;
    document.tt.element.onmousemove = ToolTip.update;
    document.tt.tip.style.display = "block";
    document.tt.tip.style.left = mx + 'px';
    document.tt.tip.style.top = my + 'px';
  },

  update: function(event)
  {
    if( !document.tt.show || !document.tt.tip )
      return ToolTip.out();

    var e = event || window.event;
    var c = Utils.getCursorPosition(e);
    c.x += 10; c.y += 20;
    document.tt.tip.style.left = c.x + 'px';
    document.tt.tip.style.top = c.y + 'px';
  }
};

ToolTip.init();
