/*
Written by: Adam Crownoble (adam@bryan.edu)
Date: April 3 2006
License: LGPL (http://www.gnu.org/copyleft/lesser.html)
*/

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

 initialize: function(list, randomize) {
 
  this.list = $(list);
  this.lines = $A(this.list.getElementsByTagName('li'));
  if(randomize) { this.randomize(); }
  this.lineCount = this.lines.length;
  this.currentLineIndex = 0;
  this.currentLine = this.lines[this.currentLineIndex];
  this.transitionDelay = 1;
  this.transitionTime = 1;

  this.list.setStyles({'position':'relative','overflow':'hidden'});

  this.lines.each(
   function(line) {
    line = $(line);
    line.setStyle('position','absolute');
    line.effect = new fx.Style(line,'opacity',{duration: this.transitionTime*1000});
    if(line != this.lines[0]) { line.effect.hide(); }
   }.bind(this)
  );

  if(this.lineCount > 1) { this.startTimer(); }

 },

 randomize: function() {
  var i = this.lines.length;
  //if ( i == 0 ) return false;
  //while ( --i ) {
  // var j = Math.floor( Math.random() * ( i + 1 ) );
  // var tempi = this.lines[i];
  // var tempj = this.lines[j];
  // this.lines[i] = tempj;
  // this.lines[j] = tempi;
  //}
 },

 startTimer: function() {
  this.timerID = setInterval(this.next.bind(this), (this.transitionDelay+this.transitionTime) * 1000);
 },

 stopTimer: function() {
  clearInterval(this.timerID);
 },

 fadeIn: function(line) {
  line.effect.custom(0,1);
 },
 
 fadeOut: function(line) {
  line.effect.custom(1,0);
 },

 increment: function() {
  if(this.currentLineIndex == (this.lineCount-1)) {
   this.currentLineIndex = 0;
  } else {
   this.currentLineIndex++;
  }
  this.currentLine = this.lines[this.currentLineIndex];
 },

 next: function() {

  this.fadeOut(this.currentLine);
  this.increment();
  this.fadeIn(this.currentLine);

 }

}
