//
// $Id: menu.js, v. 0.2 2002/05/05 6:51:33 PM aselimovic Exp $
//

/*
 * MenuItemCollection
 *
 * Menu
 *    |
 *    +----MainMenu
 *    |
 *    +----MenuItem
 */

itemColor = "#0099FF";
itemHoverColor = "#FF6600";

/*
 * Prototype: MenuItemCollection(owner)
 *
 * Parameters: owner
 *                 The Menu that owns this collection.
 */

function MenuItemCollection(owner) {
   this.__owner = owner || null;
   this.item = [];
   this.add = add;
   this.indexOfKey = indexOfKey;

   function add(id, text) {
      var mi;

      (mi = new MenuItem(id, text)).parent = this.__owner;
      return this.item[this.item.length] = mi;
   }

   function indexOfKey(key) {
      if (!key)
         return -1;

      // binary search?
      for (i = 0; i < this.item.length; i++)
         if (this.item[i].id == key)
            return i;

      return -1;
   }
}

// default constructor
function Menu() {
   this.menuItems = new MenuItemCollection(this);
   //if (typeof() == "undefined") {
   //}
   this._menuWidth = 0;
   this.get_isParent = get_isParent;

   function get_isParent() {
      return (this.menuItems.item.length > 0);
   }
}

// default constructor
function MainMenu() {
   this.base = Menu;
   this.base();
   this.render = render;
   // events
   this.closeAll = closeAll;

   function render() {
   }

   function closeAll() {
      var item = this.menuItems.item;

      for (i = 0; i < item.length; i++) {
         item[i].closeItem();
         item[i].leave();
      }

      document.getElementById("submenu-items6").style.visibility = 'hidden';
   }
}
MainMenu.prototype = new Menu;

// constructor
function MenuItem(id, text, href) {
   this.base = Menu;
   this.base();
   this.id = id || "";
   this.parent = null;
   //shortcut
   this.text = text || "";
   this.href = href || "";
   this.__itemWidth = 0;
   this.itemHeight = 0;
   this.render = render;
   this._measureItem = _measureItem;
   // events
   this.popup = popup;
   this.select = select;
   this.leave = leave;
   this.click = null;
   this.closeItem = closeItem;

   function render() {
   }

   function _measureItem() {
      //this.__itemWidth = elem.style.fontSize * this.text.length;
   }

   function popup() {
      var elem = document.getElementById("submenu-items" + this.parent.menuItems.indexOfKey(this.id));

      if (this.get_isParent())
         elem.style.visibility = "visible";
   }

   function select() {
      var elem = document.getElementById(this.id);

      elem.style.backgroundColor = itemHoverColor;
   }

   function leave() {
      var elem = document.getElementById(this.id);

      elem.style.backgroundColor = itemColor;
   }

   // recursive?
   function closeItem() {
      var elem = document.getElementById("submenu-items" + this.parent.menuItems.indexOfKey(this.id));

      //if (elem.style.visibility == "visible")
         elem.style.visibility = "hidden";
   }
}
// The MenuItem class derives from Menu.
MenuItem.prototype = new Menu;