var Navigation = Class.create({
  initialize: function(triggerSel, subTriggerSel) {
    this._timers = [];
    this._triggers = $$(triggerSel);
    this._subTrigs = $$(subTriggerSel);
    this._initTriggers();
    this._initSubTriggers();
  },
  _trigRollOver: function(evt) {
    var elId = evt.element().id;
    var menu = $(elId.replace("_trig", "_menu"));
    
    evt.element().setStyle({ color: "#FF9600" });
    
    this._triggers.each(function(trig) {
      if (trig.id != elId) {
        $(trig.id.replace("_trig", "_menu")).hide();
      }
    });
    
    if (menu.style.display == "none") menu.appear({ duration: 0.5 });
    
    if (this._timers[elId]) {
      this._timers[elId].stop();
      this._timers[elId] = null;
    }
  },
  _trigRollOff: function(evt) {
    var elId = evt.element().id;
    
    evt.element().setStyle({ color: "#2a82b4" });
    
    var pe = new PeriodicalExecuter(function(pe) {
      $(elId.replace("_trig", "_menu")).hide();
      pe.stop();
    }, 1);
    this._timers[elId] = pe;
  },
  _subTrigRollOver: function(evt) {
    var timerId = evt.element().up("div").id.replace("_menu", "_trig");
    this._timers[timerId].stop();
    this._timers[timerId] = null;
  },
  _subTrigRollOff: function(evt) {
    var elId = evt.element().up("div").id;
    var pe = new PeriodicalExecuter(function(pe) {
      $(elId).hide();
      pe.stop();
    }, 1);
    this._timers[elId.replace("_menu", "_trig")] = pe;
  },
  _initTriggers: function() {
    this._triggers.each(function(trig) {
      trig.observe("mouseover", this._trigRollOver.bindAsEventListener(this));
      trig.observe("mouseout", this._trigRollOff.bindAsEventListener(this));
    }, this);
  },
  _initSubTriggers: function() {
    this._subTrigs.each(function(subTrig) {
      subTrig.observe("mouseover", this._subTrigRollOver.bindAsEventListener(this));
      subTrig.observe("mouseout", this._subTrigRollOff.bindAsEventListener(this));
    }, this);
  }
});

var SideNavigation = Class.create({
  initialize: function(sideSel, sidePopMenuSel, popMenuSel) {
    this._timers = [];
    this._sideControls = $$(sideSel);
    this._sidePopTrigs = $$(sidePopMenuSel);
    this._popMenus = $$(popMenuSel);
    if (this._sideControls.length > 0) this._init();
  },
  _init: function() {
    this._sideControls.each(function(cntrl) {
      cntrl.observe("click", (function() {
        var submenu = this.id + "-submenu";
        $(submenu).toggle();
      }).bind(cntrl));
    });
    if (this._sidePopTrigs.length > 0) {
      this._sidePopTrigs.each(function(popTrig) {
        popTrig.observe("mouseover", this._popTrigRollOver.bindAsEventListener(this));
        popTrig.observe("mouseout", this._popTrigRollOff.bindAsEventListener(this));
        
        var popMenu = popTrig.id + "-pop-menu";
        $(popMenu).childElements().each(function(desc) {
          desc.observe("mouseover", this._subPopTrigRollOver.bindAsEventListener(this));
          desc.observe("mouseout", this._subPopTrigRollOff.bindAsEventListener(this));
        }, this);
      }, this);
    }
  },
  _popTrigRollOver: function(evt) {
    var elId = evt.element().id;
    var popMenu = $(elId + "-pop-menu");
    if (this._popMenus) {
      this._popMenus.each(function(menu) {
        if (menu.id != this.id) menu.hide();
      }, popMenu);
    }
    if (popMenu.style.display == "none") popMenu.appear({ duration: 0.5 });
    if (this._timers[elId]) {
      try {
        this._timers[elId].stop();
      } catch(e){}
      this._timers[elId] = null;
    }
  },
  _popTrigRollOff: function(evt) {
    var elId = evt.element().id;
    var pe = new PeriodicalExecuter(function(pe) {
      $(elId + "-pop-menu").hide();
      pe.stop();
    }, 1);
    this._timers[elId] = pe;
  },
  _subPopTrigRollOver: function(evt) {
    var timerId = evt.element().up("ul").id.replace("-pop-menu", "");
    this._timers[timerId].stop();
    this._timers[timerId] = null;
  },
  _subPopTrigRollOff: function(evt) {
    var elId = evt.element().up("ul").id;
    var pe = new PeriodicalExecuter(function(pe) {
      $(elId).hide();
      pe.stop();
    }, 1);
    this._timers[elId.replace("-pop-menu", "")] = pe;
  }
});

var navHash = new Hash();
navHash.set("time-delay-relays", "time-delay-relays-submenu");
navHash.set("vending-controls", "vending-controls-submenu");
navHash.set("phase-speed-controls", "phase-speed-controls-submenu");
navHash.set("current-sensor-modules", "current-sensor-modules-submenu");
navHash.set("flashers", "flashers-submenu");
navHash.set("liquid-level-controls", "liquid-level-controls-submenu");
navHash.set("alternating-duplex", "alternating-duplex-submenu");

var displaySingleNavigation = function(navKey) {
  var submenuId = navHash.get(navKey);
  if (!Object.isUndefined(submenuId)) $(submenuId).show();
};