
/* classes/vertical_scroll.js */

 var VerticalScroll = Class.create();
VerticalScroll.prototype = {
  columns: 5,

  initialize: function( element, upLink, downLink ) {
    this.element  = $(element);
    this.moveIncrement = this.element.up().getHeight();
    this.currentIndex = 0;

    this.upObserver = this.up.bindAsEventListener(this);
    this.dnObserver = this.down.bindAsEventListener(this);
    
    if( upLink ) {
      Event.observe( $(upLink),   'click', this.upObserver );
    }
    Event.observe( $(downLink), 'click', this.dnObserver );
  },

  elementCount: function() { return this.element.childElements().size(); },
  rowCount:     function() { return Math.ceil( this.elementCount() / this.columns ); },

  down: function(ev) {
    this.currentIndex = (this.currentIndex + 1) % this.rowCount();
    this.move();
    if(ev != null) Event.stop(ev);
    return this.currentIndex;
  },

  up: function(ev) {
    this.currentIndex = this.currentIndex - 1;
    if( this.currentIndex < 0 ) {
      this.currentIndex = this.rowCount() - 1;
    }
    this.move();
    if(ev != null) Event.stop(ev);
    return this.currentIndex;
  },

  move: function() {
    new Effect.Move( this.element,
      {
        y:        -(this.moveIncrement * this.currentIndex),
        mode:     'absolute',
        duration: 0.65,
        queue:    { position: 'end', scope: 'global_toolbar', limit: 3 }
      }
    );
  }
};
