// Ablage fuer alle Menues
_menus = {};

// Menue-Klasse
function _menu (id)
{
   this.id = id;
   this.entries = {};
   this.children = {};
   this.active_item = null;
   this.hide_timer = null;

   // Funktionen registrieren
   this.hide = _menu_hide;
   this.onmouseout = _menu_onmouseout;
   this.onmouseover = _menu_onmouseover;

   // alle Eintraege ermitteln
   var entry, subMenu;
   var i = 0;
   do
   {
     entry = getElement(id + '_' + i + '_entry');
     subMenu = getElement(id + '_' + i);
     if (entry)
     {
        new _menuEntry(id + '_' + i, entry, subMenu, this, this);
     }
     i++;
   }
   while (entry);

   // registrieren
   _menus[this.id] = this;
}

function _menu_hide ()
{
   for (var i in this.entries)
   {
      this.entries[i].open(false);
   }
}

function _menu_onmouseout (id) {
   this.hide_timer = setTimeout("_menus['" + this.id + "'].hide();", 200);
   if (this.active_item && this.active_item.id == id)
      this.active_item = null;
}

function _menu_onmouseover (id) {
   this.active_item = this.entries[id];
   clearTimeout(this.hide_timer);
   var curr_item, visib;
   for (var i in this.entries)
   {
      curr_item = this.entries[i];
      visib = (curr_item.path.slice(0, curr_item.depth+1).join('_') ==
         this.active_item.path.slice(0, curr_item.depth+1).join('_'));
      curr_item.open(visib);
   }
}

function _menuEntry (id, entry, subMenu, parent, menu) {
   this.id = id;
   this.entry = entry;
   this.subMenu = subMenu;
   this.parent = parent;
   this.menu = menu;
   this.visible = null;
   this.initialized = false;

   // bei unseren uebergeordneten Eintraegen registrieren
   parent.children[this.id] = this;
   menu.entries[this.id] = this;

   // hierarchie ermitteln
   this.path = this.id.split('_');
   this.depth = this.path.length - 1;

   // methoden zuweisen
   this.open = _menuEntry_open;
   this.initialize = _menuEntry_initialize;

   // in die Tiefe initialisieren
   this.children = {};
   // alle Eintraege ermitteln
   var entry, subMenu;
   var i = 0;
   do
   {
     entry = getElement(this.id + '_' + i + '_entry');
     subMenu = getElement(this.id + '_' + i);
     if (entry)
     {
        entry = new _menuEntry(this.id + '_' + i, entry, subMenu, this, this.menu);
     }
     i++;
   }
   while (entry);

}

function _menuEntry_open(show)
{
   this.initialize();

   if (show != null && this.subMenu)
   {
      if (this.visible == show)
      {
         return;
      }
      this.visible = show;
      if (show)
      {
         showElement(this.subMenu, true);
      }
      else if (this.depth)
      {
         hideElement(this.subMenu);
      }
   }

   return (this.visible);
}

// positionen stehen ggf. erst später fest, daher lazy initialisieren
function _menuEntry_initialize()
{
   if (!this.initialized)
   {
      if (this.subMenu)
      {
         if (this.depth > 1)
         {
           setPageX(this.subMenu, getPageX(this.entry) + getWidth(this.entry));
           setPageY(this.subMenu, getPageY(this.entry));
         }
         else
         {
           setPageX(this.subMenu, getPageX(this.entry));
           setPageY(this.subMenu, getPageY(this.entry) + this.entry.offsetHeight);
         }
      }
      this.initialized = true;
   }
}

function _initMenu(id)
{
   new _menu(id);
}

