var Scroller = Class.create();

Scroller.prototype = {
  initialize : function(scroller, scrollUp, scrollDown, showNum, randomize) {
    scroller = $(scroller);
    scrollUp = $(scrollUp);
    scrollDown = $(scrollDown);
    Event.observe(scrollDown, 'mousedown', this.snapDown.bindAsEventListener(this));
    Event.observe(scrollUp, 'mousedown', this.snapUp.bindAsEventListener(this));
    this.scroller = scroller;
    this.scrollUp = scrollUp;
    this.scrollDown = scrollDown;
        
    //We start at the top
    this.scrollUp.className = 'scrollDisabled';

    if (randomize) {
      this.randomizeChildren(this.scroller);
    }
    //Hide all but the number to show per group
    var children = this.getChildren(this.scroller);
    for (var i = 0; i < children.length; i++) {
      var node = children[i];
      if (i > showNum - 1) {
        node.style.display = 'none';        
      }
      else {
        node.style.display = '';
      }
    }

    if(i <= showNum) {
      //We're displaying them all, we can't scroll!
      this.scrollDown.className = 'scrollDisabled';
    }
  },
  getChildren : function(node) {
    var nodes = node.childNodes;
    var children = new Array();
    for (var i = 0; i < nodes.length; i++) {
      var child = nodes[i];
      if(child != null && child.tagName != null && child.tagName == "DIV") {
        children.unshift(child);        
      }
    }
    return children;
  },
  randomizeChildren : function(node) {
    var children = this.getChildren(node);
    while (node.childNodes.length > 0) {
      node.removeChild(node.firstChild);
    }
    while (children.length > 0) {
      var r = Math.round(Math.random() * (children.length - 1));
      var child = children[r];
      node.appendChild(child);
      children = children.without(child);
    }
  },
  snapDown:function() {
    var scroller = this.scroller;
    var kids = this.getChildren(this.scroller);
    for (var i = 0; i < kids.length; i++) {
      var node = kids[i];
      if (node.style.display == 'none' && typeof(visibleNode) != 'undefined') {
        var hiddenNode = node;
        break;
      }
      if (node.style.display != 'none' && typeof(visibleNode) == 'undefined') {
        var visibleNode = node;
      }
    }
    if(typeof(hiddenNode) != 'undefined' && typeof(visibleNode) != 'undefined') {
      hiddenNode.style.display = '';
      visibleNode.style.display = 'none';
      this.scrollUp.className = 'scrollActive';
      if(i == kids.length - 1) {
        this.scrollDown.className = 'scrollDisabled';
      }
    }
    else {
      //We must be at the bottom
      this.scrollDown.className = 'scrollDisabled';
    }
  
  },
  snapUp:function() {
    var scroller = this.scroller;
    var kids = this.getChildren(this.scroller);
    for (var i = 0; i < kids.length; i++) {
      var node = kids[i];
      if (node.style.display == 'none') {
        if (typeof(visibleNode) == 'undefined') {
          var hiddenNode = node;
          var hiddenIndex = i;
        }
        else {
          break;
        }
      }
      if (node.style.display != 'none') {
        var visibleNode = node;
      }
    }
    if(typeof(hiddenNode) != 'undefined' && typeof(visibleNode) != 'undefined') {
      hiddenNode.style.display = '';
      visibleNode.style.display = 'none';
      this.scrollDown.className = 'scrollActive';
      if(hiddenIndex == 0) {
        this.scrollUp.className = 'scrollDisabled';
      }
    }
    else {
      //We must be at the top
      this.scrollUp.className = 'scrollDisabled';
    }
  }
}