/* DROPDOWN MENU INITIALIZATION */

Function.prototype.bind = function( object, args ) {
    var __method = this;
    var __args = args;
    return function() {
        __method.apply( object, __args || arguments );
    };
};

Limb.namespace('sopig');

sopig.Menu = function(menu_id, hide_menu_time)
{
  this.container = document.getElementById(menu_id);
  if(!this.container)
    throw new Limb.Exception('Menu element not found');

  this.submenus = [];
  this.entries_with_submenus = [];
  this.entries_without_submenus = [];
  this.current_submenu = null;
  this._findMenuItems();

  this.showCurrentMenu = false;
  this.hide_timeout = '';
  this.hide_menu_time = hide_menu_time || 500;

  this._initBehavior();
}

sopig.Menu.prototype =
{
  addSubmenu: function(menu_entry)
  {
    if(!menu_entry)
      return false;

    var submenu = menu_entry.nextSibling;
    while(submenu && (!submenu.tagName || submenu.tagName.toLowerCase() != 'div'))
      submenu = submenu.nextSibling;
    if(!submenu)
      return false;

    this.submenus.push(submenu);


    if(menu_entry.id == 'current')
     this.current_submenu = submenu;

    return true;
  },

  showMenu: function(menu)
  {
    if(!menu)
      return;
    this.showCurrentMenu = true;
    this.hideAllNow();
    menu.style.display = 'block';
    menu.parentNode.firstChild.className = 'menu_hover';

  },

  hideMenu: function(menu)
  {
    menu.style.display = 'none';
    menu.parentNode.firstChild.className = '';
  },

  hideAll: function(force_hide)
  {
    if(!force_hide && this.showCurrentMenu)
      return;

    for(i = 0; i < this.submenus.length; i ++)
      this.hideMenu(this.submenus[i]);
  },

  hideAllNow: function()
  {
    this.hideAll(true);
  },

  onMouseOut: function()
  {
    this.showCurrentMenu = false;
    this.hide_timeout = setTimeout(this.hideAllNow.bind(this), this.hide_menu_time);
  },

  onMouseOver: function()
  {
    if(!this.hide_timeout)
      return;

    this.showCurrentMenu = true;
    this._abortHide();
  },

  showDefaultMenu: function()
  {
    if(!this.current_submenu)
      return;

    this.showMenu(this.current_submenu);
  },


  _abortHide: function()
  {
    if(!this.hide_timeout)
      return;

    clearTimeout(this.hide_timeout);
    this.hide_timeout = null;
  },

  _initBehavior: function()
  {
    Limb.events.add_event(this.container, 'mouseover', this.onMouseOver.bind(this));
    Limb.events.add_event(this.container, 'mouseout', this.onMouseOut.bind(this));
    for(var i = 0; i < this.entries_with_submenus.length; i++)
      {Limb.events.add_event(this.entries_with_submenus[i], 'mouseover', this.showMenu.bind(this, [ this.submenus[i] ]));
    }
    for(var i = 0; i < this.entries_without_submenus.length; i++)
      Limb.events.add_event(this.entries_without_submenus[i], 'mouseover', this.hideAllNow.bind(this));
      function(){var fisheye = new fisheyeClass($('dock'))};
  },

  _findMenuItems: function()
  {
    var links = this.container.getElementsByTagName('a');
    for(var i = 0; i < links.length; i++)
    {
      if(links[i].className != 'menu_entry')
        continue;
      if(this.addSubmenu(links[i]))
        this.entries_with_submenus.push(links[i]);
      else
        this.entries_without_submenus.push(links[i]);
    }
  }
}
