var menu1Selected = null;
var menuHovered = null;
var subMenuHovered = null;
var timer = null;

function Menu(parentContainer) {
  this.menuElements = new Array();
  this.container = document.getElementById(parentContainer);
}

Menu.prototype.addMenuElement = function(menuElement){
        lastIndex = this.menuElements.length;
        this.menuElements[lastIndex] = menuElement;
}

Menu.prototype.render = function() {
    if (this.container) {

            menuLine1 = document.createElement('div');
            menuLine1.className='menuBar';
            menuLine1.align = 'left';
            menuLine2 = document.createElement('div');
            menuLine2.className='subMenuBar';
            menuLine2.align = 'left';
            this.container.appendChild(menuLine1);
            this.container.appendChild(menuLine2);

            for (var i =0; i<this.menuElements.length; i++) {
                ele = this.menuElements[i];
                divIdName = "menu"+i;
                menuDiv = ele.createDivElement(divIdName);
                menuLine1.appendChild(menuDiv);

                ele.childDiv = document.createElement('div');
                ele.childDiv.className = 'subMenuHidden';
                ele.childDiv.setAttribute('id', divIdName+'child');
                for (var j=0; j<ele.subElements.length; j++) {
                  subEle = ele.subElements[j];
                  divIdName = "menu"+i+".submenu"+j;
                  ele.childDiv.appendChild(subEle.createDivElement(divIdName));
                }
                menuLine2.appendChild(ele.childDiv);
		restoreSelected();
            }
    }
}

function MenuElement(label, linkRef) {
        this.label = label;
        this.linkRef = linkRef;
        this.subElements = new Array();
        this.childDiv = null;
        this.divElement = null;
        this.parentMenu = null;
}
MenuElement.prototype.hover = function() {
        if (timer) {clearTimeout(timer); timer=null;}

        if (this.childDiv) {
                if (menuHovered) {
                        if (menuHovered.childDiv) {
                          menuHovered.childDiv.className='subMenuHidden';
                          if ((subMenuHovered)&&(subMenuHovered.divElement)) {subMenuHovered.divElement.className='menuItem';} 
                          subMenuHovered = null;
                        }
                        menuHovered.divElement.className='menuItem';
                }
                this.childDiv.className = 'subMenu';
                if (this.divElement) {this.divElement.className='menuItem Selected';}
                menuHovered = this;
        } else {
                if (subMenuHovered) {
                        if (subMenuHovered.divElement) {subMenuHovered.divElement.className='menuItem';}
                }
                if (this.divElement) {this.divElement.className='menuItem subSelected';}
                subMenuHovered = this;
        }
}

MenuElement.prototype.select = function() {
        menu1Selected = this;
        if (this.parentMenu) {
          this.parentMenu.hover();
        }
}

MenuElement.prototype.createDivElement = function(id) {
  this.divElement = document.createElement('div');
  this.divElement.setAttribute('id', id);
  this.divElement.innerHTML = this.label+"<img url='spacer.gif' height='15px' width='1px'/>";
  this.divElement.className='menuItem';

  var oThis = this;
  this.onSelect = function(E) {
        oThis.hover();
  }

  this.onUnselect = function(E) {
        if (timer == null) { timer = setTimeout('restoreSelected()', 3000);     }
  }

  this.clicked = function(E) {document.location.href=oThis.linkRef;}

  if (document.attachEvent) {
    this.divElement.attachEvent("onmouseover", this.onSelect);
    this.divElement.attachEvent("onmouseout", this.onUnselect);
    this.divElement.attachEvent("onclick", this.clicked);
  } else if (document.addEventListener) {
    this.divElement.addEventListener("mouseover", this.onSelect, false);
    this.divElement.addEventListener("mouseout", this.onUnselect, false);
    this.divElement.addEventListener("click", this.clicked, false);
  } else {
    this.divElement.onmouseover = this.onSelect;
    this.divElement.onmouseout = this.onUnselect;
    this.divElement.onclick = this.clicked;
  }
  return this.divElement;
}

MenuElement.prototype.addSubElement = function (subElement) {
        lastIndex = this.subElements.length;
        this.subElements[lastIndex] = subElement;
        this.subElements[lastIndex].parentMenu = this;
}

function restoreSelected() {
        if (menu1Selected) {
		if (menu1Selected.parentMenu) {
		  menu1Selected.parentMenu.hover(); 
                }
                menu1Selected.hover();
        }
}


