
var SlidingMenu = Class.create();

SlidingMenu.prototype = {

  // local vars
  // ---------------------
  // menu_control - Used for scoping the PeriodicalExecuter
  // menu_div - The div wrapping the menu
  menu_status : new String('stopped'),

  // initialize function
  initialize : function(menu_name, disable_scrolling){
    this.menu_name = menu_name
    this.disable_scrolling = (disable_scrolling != true ? false : true);
    this.menu_div = $(menu_name);
    this.sliding_div = this.menu_div.firstDescendant();
    if (this.menu_div == null) {
      //console.debug("Menu was not found on page!");
    } else if (this.sliding_div == null) {
      //console.debug("Menu was empty!");
    } else {
      this.stop_menu_function = this.stop_menu.bind(this)
      this.start_menu_function = this.start_menu.bind(this)
      if (!this.disable_scrolling) {
        this.start_menu();
        this.menu_div.observe('mouseover', this.stop_menu_function);
        this.menu_div.observe('mouseout', this.start_menu_function);
      }
    };
    //console.debug(this.menu_div.inspect());
  }, // intialize()

  start_menu : function() {
    if (this.menu_status == 'stopped') {
      this.menu_control = new PeriodicalExecuter(
          this.cycle_menu.bind(this), 5
        );
      this.menu_status = 'started';
      //console.debug("Menu animation started.")
    }
  }, // start_menu()

  stop_menu : function() {
    if (this.menu_status != 'stopped') {
      this.menu_control.stop();
      this.menu_status = 'stopped';
      //console.debug("Menu animation stopped.");
    }
  }, // stop_menu()

  cycle_menu : function() {
    if (this.disable_scrolling == true) {
      //console.debug('scrolling disabled');
      return false;
    } else {
      //console.debug('scrolling enabled');      
    }

    var menu_rows = this.sliding_div.childElements();
    if (menu_rows == null || menu_rows.length <= 1) {
      //console.debug("There is only 1 (or less) row in the menu. No need to shift any rows!")
      //console.debug(this.sliding_div.innerHTML)
    } else {
      disappearing_row = menu_rows.first();
      next_row = menu_rows[1];
      new Effect.SlideUp(disappearing_row, {
              x: 0,
              y: -45,
              duration: 1.8,
              afterFinish: function(e) {
                  var hidden_row = Element.remove(e.element);
                  this.sliding_div.appendChild(hidden_row);
                  hidden_row.show();
                  this.menu_div.addClassName("highlight-"+next_row.id);
                  this.menu_div.removeClassName("highlight-"+disappearing_row.id);
                }.bind(this),
              queue: { position: 'end', scope: this.menu_name }
            });
    }
  }, // cycle_menu()

  open_menu : function() {
    this.stop_menu();
    this.menu_div.addClassName('open');
    this.menu_div.stopObserving('mouseout', this.start_menu_function);
  }, // open_menu()

  close_menu : function() {
    this.start_menu_function = this.start_menu.bind(this)
    if (!this.disable_scrolling) {
      this.start_menu();
      this.menu_div.observe('mouseout', this.start_menu_function);
    }
    this.menu_div.removeClassName('open');
  } // close_menu()

}; // sliding_menu namespace

