// Copyright (c) 2009 notions (http://www.notions.de)
// 
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
// 
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
// 
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.

var MenuDecorator = Class.create();
MenuDecorator.prototype = {
	currentItem: null,
	currentContent: null,
	effectDuration: 0.5,
	userAction: false,
	
	initialize: function() {
    $('navigation').select('a').each(function(a) {
      a.observe('click', this.onClick.bind(this));
    }.bind(this));
    this.selectItem('item-1');
	},

	onClick: function(event) {
	  this.userAction = true;
    var item = event.findElement('li');
    if ( item != document ) {
      this.selectItem(item);
    }
    event.stop();
	},
	
	selectItem: function(item) {
    if ( this.currentItem == item ) {
      return;
    }
	
    var id = $(item).id.substr(5);
	
    if ( this.currentItem ) {
      this._deselectItem(this.currentItem);
    }
    this.currentItem = $(item);
    this.currentItem.addClassName('selected');

    if ( this.currentContent ) {
      new Effect.Fade(this.currentContent, { duration: this.effectDuration });
    }
    this.currentContent = $('content-'+id);
    if ( this.currentContent ) {
      new Effect.Appear(this.currentContent, { duration: this.effectDuration });
    }
  },

  _deselectItem: function(item) {
    $(item).removeClassName('selected');
  }
};

var MenuSwitcher = Class.create();
MenuSwitcher.prototype = {
	decorator: null,
	interval: 10,
	
	initialize: function(decorator) {
    this.decorator = decorator;
    new PeriodicalExecuter(function(executer) {
      if (this.decorator.userAction) {
        executer.stop();
      }
      else {
        this.selectNextItem();
      }
    }.bind(this), this.interval);
	},

	selectNextItem: function() {
    var nextItemId = parseInt($(this.decorator.currentItem).id.substr(5))+1;
    if ( $('item-'+nextItemId) ) {
      this.decorator.selectItem('item-'+nextItemId);
    }
    else {
      this.decorator.selectItem('item-1');
    }
	}
};

document.observe("dom:loaded", function() {
  decorator = new MenuDecorator();
  new MenuSwitcher(decorator);
});
