if (!window.sea)
{
   window.sea = new Object();
}

sea.Menu = function(hideTimeout)
{
   this.hideTimeout = hideTimeout;
   this.children = new Array;
   this.timer = null;
   this.current = null;
}

sea.Menu.prototype.add = function(parent, child)
{
   this.children.push( {'parent':parent, 'child': child} );

   var menu = this;
   parent.origmouseover = parent.onmouseover;
   parent.onmouseover = function() 
   { 
      if (parent.origmouseover) 
	     parent.origmouseover(); 
      menu.onOpen(parent); 
   }
   
   parent.origmouseout = parent.onmouseout;
   parent.onmouseout = function() { 
      if (parent.origmouseout) 
	     parent.origmouseout(); 
      menu.onClose(parent); 
   }
   
   child.origmouseover = child.origmouseover;
   child.onmouseover = function() { 
      if (child.origmouseover) 
	     child.origmouseover();    
      menu.cancelTimer(); 
   }
   
   child.origmouseout = child.onmouseout;
   child.onmouseout = function() { 
      if (child.origmouseout) 
	     child.origmouseout();   
      menu.onClose(parent); 
   }

   this.hide(parent,child);
}

sea.Menu.prototype.calcTop = function(parent)
{
   var top = 0;
   var ob = parent;
   while (ob)
   {
        top += ob.offsetTop;
        ob = ob.offsetParent;
   }
   return parent.clientHeight + top + 20;
}

sea.Menu.prototype.calcLeft = function(parent)
{
   var left = 0;
   var ob = parent;
   while (ob)
   {
        left += ob.offsetLeft;
        ob = ob.offsetParent;
   }
   return left;
}



sea.Menu.prototype.show = function(parent, child)
{
   child.style.position = 'absolute';
   child.style.top = this.calcTop(parent) + 'px';
   child.style.left = this.calcLeft(parent) + 'px';

   child.style.display = '';
}

sea.Menu.prototype.hide = function(parent, child)
{
   child.style.display = 'none';
}



sea.Menu.prototype.cancelTimer = function()
{
   if (this.timer)
   {
      window.clearTimeout(this.timer);
      this.timer = null;
   }
}

sea.Menu.prototype.closeCurrent = function()
{
   this.cancelTimer();

   if (this.current)
   {
      this.hide(this.current.parent, this.current.child);
      this.current = null;
   }
}

sea.Menu.prototype.onOpen = function(parent)
{
   if (this.current && this.current.parent == parent)
   {
      this.cancelTimer();
      return;
   }
   
   this.closeCurrent();

   this.current = this.findChild(parent);
   this.show(this.current.parent, this.current.child);
}

sea.Menu.prototype.onClose = function(parent)
{
   var menu = this;
   this.timer = window.setTimeout(function(){menu.closeCurrent()}, this.hideTimeout);
}

sea.Menu.prototype.findChild = function(parent)
{
   for (var i=0; i<this.children.length; ++i)
   {
      if (this.children[i].parent == parent)
      {
         return this.children[i];
      }
   }
   throw new Exception('invalid parent');
}

