var Scroller = Class.create();
Scroller.prototype = {

 initialize: function(list) {
 
  this.list = $(list);
  this.lines = $A(this.list.getElementsByTagName('li'));
  this.lineCount = this.lines.length;
  this.currentLineIndex = 0;
  this.currentLine = this.lines[this.currentLineIndex];
  this.listHeight = Element.getHeight(this.list);
  this.lineTop = this.currentLine.offsetTop;
  this.transitionDelay = 4;
  this.transitionTime = 1;

  Element.makePositioned(this.list);
  Element.setStyle(this.list, {overflow: 'hidden'});

  this.lines.each(
   function(line) {
    Element.setStyle(line, {position:'absolute'});
    if(line != this.lines.first()) { Element.setStyle(line, {top: this.listHeight+'px'}); }
    line.effect = new fx.MoveVertical(line, {duration: this.transitionTime*1000});
   }.bind(this)
  );

  Event.observe(this.list, 'mouseover', this.stopTimer.bind(this));
  Event.observe(this.list, 'mouseout', this.startTimer.bind(this));

  if(this.lineCount > 1) { this.startTimer(); }

 },
 
 autoHeight: function() {
  var heights = this.lines.collect(Element.getHeight);
  var maxHeight = heights.max()
  Element.setStyle(this.list, {height: maxHeight+'px'});
  this.listHeight = Element.getHeight(this.list);
 },

 startTimer: function() {
  this.timerID = setInterval(this.next.bind(this), (this.transitionDelay+this.transitionTime) * 1000);
 },

 stopTimer: function() {
  clearInterval(this.timerID);
 },

 scrollIn: function(line) {
  var height = Element.getHeight(line);
  line.effect.custom(-this.listHeight, this.lineTop);
 },
 
 scrollOut: function(line) {
  line.effect.custom(this.lineTop,this.listHeight+(this.lineTop*2));
 },

 increment: function() {
  if(this.currentLineIndex == (this.lineCount-1)) {
   this.currentLineIndex = 0;
  } else {
   this.currentLineIndex++;
  }
  this.currentLine = this.lines[this.currentLineIndex];
 },

 next: function() {

  this.scrollOut(this.currentLine);
  this.increment();
  this.scrollIn(this.currentLine);

 }

}

fx.MoveVertical = Class.create();
Object.extend(Object.extend(fx.MoveVertical.prototype, fx.Layout.prototype), {	
	increase: function() {
		this.el.style.top = this.now + "px";
	}
});
