/** 
 * Adds a sliding/fading effect for the horizontal style of the menu component.
 * 
 * @author Ben Barber (bbarber@i2rd.com)
 * @require effects.js
 */

    if (typeof MB == 'undefined') {var MB = {};}
    if (typeof MB.SetActive == 'undefined') {MB.SetActive = function(){};}
    if (typeof MB.SetInActive == 'undefined') {MB.SetInActive = function(){};}

    var SlidingMenu = {
      speed : 0.2,
      transitionDown : function(element) {
          new Effect.Parallel([
              new Effect.BlindDown(element, {
                  duration : SlidingMenu.speed /* ,
                  transition : Effect.Transitions.linear */ 
              }),
              new Effect.Opacity(element, {
                  from: 0.0,
                  to: 1.0,
                  duration: (SlidingMenu.speed * 2)
              })
          ], {sync : true, queue : 'end'});
      },
      transitionUp : function(element) {
          Effect.BlindUp(element, {
              duration : 0.1,
              transition : Effect.Transitions.linear,
              queue : 'end',
              afterFinish : function() {
                  this._isShown = false;
              }.bind(element)
          });
      },
      sideMenuDown : function(event) { 
          // Activate parent.
          var p = this.up('li');
          if (p) MB.SetActive(p);
          // If this is shown, collapse and deactivate
          if (SlidingMenu.sideMenuShown === this) {
              if (event) {
                  var target = Event.element(event);
                  if (target.className.match("mi_parent")) {
                     SlidingMenu.transitionUp(this);
                  }
              }
              var p = this.up('li');
              if (p) MB.SetInActive(p);
              SlidingMenu.sideMenuShown = null;
          }
          else
          {
              // Collapse and deactivate the shown menu item
              if (SlidingMenu.sideMenuShown != null) {
                  SlidingMenu.transitionUp(SlidingMenu.sideMenuShown);
                  var p = SlidingMenu.sideMenuShown.up('li');
                  if (p) MB.SetInActive(p);
                  SlidingMenu.sideMenuShown = null;
              }
              // Show
              SlidingMenu.transitionDown(this);
              SlidingMenu.sideMenuShown = this;
          }
      },
      horizontalDown : function(event) {
          // We're listing on the document for the close event, so prevent
          // the event from propogating.
          Event.stop(event); 
          if (this._isShown) return;
          // Deactivate the initially-active menu item
          var el, initial = $$('li.initial-active');
          while (el = initial.pop()) {
            if (this.parentNode !== el) MB.SetInActive(el);
          }
          SlidingMenu.closeAll();
          SlidingMenu.transitionDown(this);
          this._isShown = true;
          SlidingMenu.shownList.push(this);
      },
      closeAll : function(event) {
          var el;
          while (el = SlidingMenu.shownList.pop()) {
              SlidingMenu.transitionUp(el);
          }
      },
      resetInitialState : function(event) {
          var el, initial = $$('li.initial-active');
          while (el = initial.pop()) {
              if (!el.className.match(/over/))
                 MB.SetActive(el);
          }
          SlidingMenu.closeAll();
      },
      init : function() {
          if (SlidingMenu.inited === true) return;
          SlidingMenu.inited = true;
          // Init horizontal menu transitions
          var el, parents = $$('ul.menubeanh > li.mi_parent > ul');
          while(el = parents.pop()) {
             el.hide();
             el.up("li.mi_parent").observe("mouseover", SlidingMenu.horizontalDown.bind(el));
          }
          document.observe("mouseover", SlidingMenu.resetInitialState);
          // Activate horizontal menu initial state
          parents = $$('ul.menubeanh li.mi_active.mi_parent');
          while(el = parents.pop()) {
              MB.SetActive(el);
              el.addClassName('initial-active');
          }

          // Init vertical menu transitions
          parents = $$('div.menubean > ul.menubeanv_persistent > li > ul');
          while(el = parents.pop()) {
              el.hide();
              el.up("li.mi_parent").observe("click", SlidingMenu.sideMenuDown.bind(el));
          }
          // Activate vertical menu initial state
          parents = $$('div.menubean > ul.menubeanv_persistent > li.mi_active > ul');
          while(el = parents.pop()) {
              SlidingMenu.sideMenuDown.apply(el);
          }
      },
      sideMenuShown : null,
      shownList : [],
      inited : false
  };
  document.observe("dom:loaded", SlidingMenu.init);
