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

http://obledesign.com/staff.html
*/

var Blender = new Class({

	options: {
		randomize	: false,
		transitionDelay	: 5,
		transitionTime	: 1
	},



	initialize: function( wr, el, options) {

		this.list 	= wr;
  		this.lines 	= el;

		this.setOptions(options);

		if(this.options.randomize)
			this.randomize();

		this.lineCount 		= this.lines.length;
		this.currentLineIndex 	= 0;
		this.currentLine 	= this.lines[this.currentLineIndex];

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

		this.lines.each(
			function(line) {

				line = $(line);
				line.setStyle('position', 'absolute');
				/*alert(this.options.transitionTime*1000);*/
				/*line.effect = new Fx.Style(line,'opacity',{duration: this.options.transitionTime*1000});*/
				line.effect = new Fx.Tween( line, {
					duration: this.options.transitionTime*1000
				});
				if(line != this.lines[0]) {
					line.effect.set( 'opacity', 0);
				}


			}.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.options.transitionDelay+this.options.transitionTime) * 1000);
	},



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



	fadeIn: function(line) {
  		line.effect.start( 'opacity', 1);
	},



	fadeOut: function(line) {
		line.effect.start( 'opacity', 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);
	}



});



Blender.implement(new Options);
