
/* classes/vertical_scroll_widgets.js */

 var VerticalScrollWidgets = Class.create();
VerticalScrollWidgets.prototype = {

  isDescendantOf: function(element, id) {
    try {
        return $(element).descendantOf(id);
    }
    catch(e) {
      return false;
    }
  },

  initialize: function( element, upLink, downLink, rowsCount ) {
    this.element  = $(element);      
    if(this.element == null){ return; }
    Prototype.Browser.IE6 = Prototype.Browser.IE && parseInt(navigator.userAgent.substring(navigator.userAgent.indexOf("MSIE")+5)) == 6;
    Prototype.Browser.IE7 = Prototype.Browser.IE && parseInt(navigator.userAgent.substring(navigator.userAgent.indexOf("MSIE")+5)) == 7;
    Prototype.Browser.IE8 = Prototype.Browser.IE && !Prototype.Browser.IE6 && !Prototype.Browser.IE7;

    this.moveIncrement = (Prototype.Browser.IE && !Prototype.Browser.IE8 && !this.isDescendantOf(element, 'chatter')) ? this.element.up().getHeight() + 6 : this.element.up().getHeight();
    this.currentIndex = 0;
    this.rowsCount = rowsCount;
    this.upObserver = this.up.bindAsEventListener(this);
    this.dnObserver = this.down.bindAsEventListener(this);    
    Event.observe( $(upLink),   'click', this.upObserver );
    Event.observe( $(downLink), 'click', this.dnObserver );   

    this.currentRows = this.element.childElements('li').size();
    
    if( this.currentRows < rowsCount && this.moveIncrement != 0 && !($(element).descendantOf('todos_chatter_tools') || $(element).descendantOf('products_and_services')) ) {
      this.containerHeightVar = (this.moveIncrement/this.rowsCount)+20
      alert(this.containerHeightVar)
      $(this.element).up().setStyle({height:this.containerHeightVar+'px'});
      //alert(element+" element height:"+this.moveIncrement+"\nrow height:"+this.containerHeightVar+"\nrows:"+this.currentRows);
    };
  },

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

  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() {
    //alert("move "+parseInt((this.moveIncrement * this.currentIndex) - 10))
    new Effect.Move( this.element,
      {
        y:        -this.moveIncrement * this.currentIndex ,
        mode:     'absolute',
        duration: 0,
        queue:    { position: 'end', scope: 'chatter', limit: 3 }
      }
    );
  }
};
