var g_idMouseOverMenu = null;
var g_idActiveMenu = null;
var g_hshSubMenusByDivID = new Array();
var g_arrSubMenus = new Array();

function SubMenu(){
  this.id = new String(); // sub menu div id
  this.varName = new String(); // SubMenu object JS variable access name
  this.arrItems = new Array();
  this.bolState = false;
  this.bolActiveMenu = false; // sub menu of actual page ("visible by default")
  this.hideTimeout = null;
  
  g_arrSubMenus[g_arrSubMenus.length] = this;
  
  
  /*
   * methods
   */
  this.setId = setId;
  this.setVarName = setVarName;
  this.setActiveMenu = setActiveMenu;
  this.addItem = addItem;
  this.show = show;
  this.hide = hide;
  this.getAsHTML = getAsHTML;
  this.checkAndHide = checkAndHide;
  this.checkAndShow = checkAndShow;
  this.delayHide = delayHide;
  
  function setId(id){
    this.id = id;
    g_hshSubMenusByDivID[this.id] = this;
  }
  
  function setVarName(varName){
    this.varName = varName;
  }
  
  function setActiveMenu(bolActiveMenu){
    this.bolActiveMenu = bolActiveMenu;
    if (bolActiveMenu){
      this.show();
      g_idActiveMenu = this.id;
    }
  }
  
  function addItem(strDisplay, strUrl){
    var tmpItem = new Object();
    tmpItem.display = strDisplay;
    tmpItem.url = strUrl;
    this.arrItems[this.arrItems.length] = tmpItem;
  }
  
  function show(){
    if (this.id == null) return;
    var objElement = getE(this.id);
    if (objElement!=null){
      showE(objElement);
    }
  }
  
  function hide(){
    if (this.id == null) return;
    var objElement = getE(this.id);
    if (objElement!=null){
      hideE(objElement);
    }
  }
  
  function getAsHTML(){
    var len = this.arrItems.length;
    var strResult=new String();
    for(var i=0; i<len; i++){
      strResult += '<a href="'+this.arrItems[i].url+'" class="submenu" onfocus="this.blur();">'+this.arrItems[i].display+'</a><br>';
    }
    return strResult;
  }
  
  function delayHide(){
    this.hideTimeout = setTimeout(this.varName+'.checkAndHide();', 500);
  }
 
  function checkAndHide(){
    if (g_idMouseOverMenu==this.id) g_idMouseOverMenu = null;    
    if (g_idMouseOverMenu==null && this.bolActiveMenu==true) return;
    this.hide();
    if (g_idMouseOverMenu==null && g_hshSubMenusByDivID[g_idActiveMenu]!=null) g_hshSubMenusByDivID[g_idActiveMenu].show();
  }
  
  function checkAndShow(){
    if (this.id == null) return;
    g_idMouseOverMenu = this.id;
    if (this.hideTimeout!=null) this.hideTimeout = clearTimeout(this.hideTimeout);
    var len = g_arrSubMenus.length;
    for (var i=0; i<len; i++){
      g_arrSubMenus[i].hide();
    }
    var objElement = getE(this.id);
    if (objElement!=null&&objElement.innerHTML!=""){
      showE(objElement);
    }
    
  }

}
