/* 
 * Author : rpannisco
 * 
 */
var ElementSlider = Class.create();

ElementSlider.prototype = {
  initialize: function( container_element, item_element, back_button_element, forward_button_element , bg_pager , slider_color_refer) {
        this.container_element = container_element;
  	this.items = $( this.container_element ).select( item_element ).toArray();

  	this.back_button_element = back_button_element;
  	this.forward_button_element = forward_button_element;
        this.bg_pager = bg_pager;
        this.slider_color_refer = slider_color_refer;
        this.started = 0;

        this.current_item_index = 0;

  	this.observeBackButton();
  	this.observeForwardButton();
        this.observeBackButtonEnter();
  	this.observeForwardButtonEnter();
        this.observeMouseOver();
  	this.observeMouseOut();
  },

  observeBackButton: function() {
  	Event.observe( this.back_button_element, 'click', this.moveBack.bindAsEventListener(this), false );
  },

  observeForwardButton: function() {
  	Event.observe( this.forward_button_element, 'click', this.moveForward.bindAsEventListener(this), false );
  },
  observeBackButtonEnter: function() {
  	Event.observe( this.back_button_element, 'mouseover', this.stop.bindAsEventListener(this), false );
  },

  observeForwardButtonEnter: function() {
  	Event.observe( this.forward_button_element, 'mouseover', this.stop.bindAsEventListener(this), false );
  },

  observeMouseOver: function() {
  	Event.observe( this.container_element, 'mouseover', this.stop.bindAsEventListener(this), false );
  },

  observeMouseOut: function() {
  	Event.observe( this.container_element, 'mouseout', this.start.bindAsEventListener(this), false );
  },

  moveForward: function() {
    if ( this.items[this.current_item_index] == this.items.last() ) {
      this.current_item_index = 0;
    } else {
      this.current_item_index += 1;
    }
    if(this.bg_pager && this.slider_color_refer) {
        this.updateBgColor();
    }
    this.resetItems();
  },

  moveBack: function() {
    // se primo, setta ultimo
    if ( this.items[this.current_item_index] == this.items.first() ) {
      this.current_item_index = ( this.items.size() - 1 );
    } else {
      this.current_item_index = ( this.current_item_index - 1 );
    }
    if(this.bg_pager && this.slider_color_refer) {
        this.updateBgColor();
    }
    this.resetItems();

  },

  updateBgColor: function() {
      current_color = $$(this.slider_color_refer)[this.current_item_index].getStyle('color')
      $(this.bg_pager).morph('background-color:'+current_color);
  },

  resetItems: function() {
    this.items.each( function( item ) {$( item ).hide();})
    this.items[this.current_item_index].appear({duration: 1.0});
  },

  start: function() {
      var scope = this;
      if(this.started == 0){
          slider = new PeriodicalExecuter(function() {scope.moveForward()}, 6);
          this.started = 1;
      }
  },
  
  stop: function() {
      if(this.started == 1){
          slider.stop();
          this.started = 0;
      }
  }
}
