    var _lastMenu = new Array();
    var _lastTimeoutId = new Array();
    var _autoHideTimeoutId = null;
    
    var TIMEOUT = 300;
    var TIMEOUT_AUTOHIDE = 5000;
    var OFFSET_X = 0;
    var OFFSET_X_SUBMENU = 252;
    var OFFSET_Y = -115;
    
    var msg = "";
    
    function log(txt) {
      msg += txt + '\n';
    }
    
    function getLastMenu(level) {
      return _lastMenu[level];
    }
    
    function setLastMenu(level, id) {
      _lastMenu[level] = id;
      //log("setting last menu: " + id + "/" + level + "\n");
      // reset all submenus
      if (!id) {
        for (var i=level; i<_lastMenu.length;++i) {
          _lastMenu[level] = null;
        }
      }
    }
    
    function getLastTimeoutId(id) {
      return _lastTimeoutId[id];
    }
    
    function setLastTimeoutId(id, timerid) {
      _lastTimeoutId[id] = timerid;
    }
  
    function $() {
      return document.getElementById ?
          function (id) { return document.getElementById(id); }
          :
          ( document.all ?
            function (id) { return document.all[id]; }
            : null );
    }
   
    function popup(id, display, sender, level, parentid) {
      var item = $();   
      if (!item) return;
      //alert("getting id " + id);     
      if (!item(id)) return;
      if (!item(id).style) return;

      // check position
      var css = item(id).style;

      if (sender) {
        var parent = sender;
        //alert(findPos(parent));
        if (level==0) {
          css.left = findPos(item(parentid), "x") + OFFSET_X + "px";
          //alert(findPos(item(parentid), "x") + "px");
        } else {
          //alert(findPos(item(parentid),"x"));
          css.top = (findPos(item(parentid), "y") + OFFSET_Y) + "px";
          css.left = (findPos(item(parentid), "x") + OFFSET_X + OFFSET_X_SUBMENU) + "px";
          css.position = "absolute";
        }
      }
      css.display = display;
    }
    
    function applyClassToParent(id, cls) {
      var item = $();   
      if (!item) return;
      if (!item(id)) return;
      item = item(id).parentNode;
      applyClass(item.id, cls);
    }
    
    function applyClass(id, cls) {
      var item = $();   
      if (!item) return;
      if (!item(id)) return;
      if (!item(id).style) return;

      removeClass(id, cls);
      item(id).className += (item(id).className.length>0?" ":"") + cls;
      //alert("new class for " + id + ": " + item(id).className);
    }
    function removeClass(id, cls) {
      var item = $();   
      if (!item) return;
      if (!item(id)) return;      
      if (!item(id).className) return;

      var cn = item(id).className;
      var arr = cn.split(" ");
      var newcls = "";
      for (var i = 0; i< arr.length; ++i) {
        //alert("Checking " + arr[i]);
        if (arr[i] != cls) {            
            newcls += (newcls.length>0?" " :"") + arr[i];
        }
      }
      item(id).className = newcls;
      //item(id).className = item(id).className.replace("/" + cls + "/", "");        
    }
    
    function getWidth(obj) {
      return obj.style.width;
    }
    
    function findPos(obj, type) {
	    var curleft = curtop = 0;
	    if (obj.offsetParent) {
		    curleft = obj.offsetLeft
		    curtop = obj.offsetTop
		    while (obj = obj.offsetParent) {
			    curleft += obj.offsetLeft
			    curtop += obj.offsetTop
		    }
	    }
	    return (type=='x'?curleft:curtop);
    }

    
    function showMenu(level, popupid, parentid, id, sender) {
      cancelHide(popupid);
      cancelHide(parentid);
      cancelHide(id);         
      if (level == 0) {
        removeClass(parentid, "mainitemoff");
        applyClass(parentid, "mainitemhover");
      }
      popup(id, 'block', sender, level, parentid);
      setLastMenu(level, id);      
      startAutoHideCountdown();
    }
    
    function hideMenu(level,id, resetall) {
      //log("hiding " + id);
      if (level==0) {
        removeClass("mainitem" + id, "mainitemhover");
        // apply to parent
        applyClass("mainitem" + id, "mainitemoff");
      }     


      popup(id,'none');
      setLastMenu(level,null);
      if (!resetall) return;       
      
      for (var i=level-1; i>=0; --i) {
        var el = getLastMenu(i);
        if (el) {
          delayHideMenu(i,el);
        }        
      }
    }
    
    function hideLast(level, curid) {
      for (var i=level; i<_lastMenu.length;++i) {
        if (curid != _lastMenu[i]) {
          popup(_lastMenu[i],'none');
          _lastMenu[i] = null;
        }
      }
    }
        
    function autoHide() {
      for (var i=5; i>=0; --i) {
        var el = getLastMenu(i);
        if (el) {
          popup(el,'none');
          setLastMenu(i,null);
        }        
      }
    }    
    
    function cancelHide(id) {
      //log("cancelling " + id);
      //log("cancel: " + id);
      window.clearTimeout(getLastTimeoutId(id));
      setLastTimeoutId(id, null);
    }
    
    function delayHideMenu(level, id) {
      //log("delayHide: " + level + "/" + id);
      setLastTimeoutId(id, window.setTimeout('hideMenu('+level+',"'+id+'", false)',TIMEOUT));
    }
    function delayHideMenuAll(level, id) {
      //log("delayHide: " + level + "/" + id);
      setLastTimeoutId(id, window.setTimeout('hideMenu('+level+',"'+id+'", false)',TIMEOUT));
    }
    
    
    function startAutoHideCountdown() {
      if (_autoHideTimeoutId) window.clearTimeout(_autoHideTimeoutId);
      _autoHideTimeoutId = window.setTimeout('autoHide()',TIMEOUT_AUTOHIDE);
    }
    
    function showMsg() {
      alert(msg);
    }
    