/*
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({
    	//implements
    	Implements: [Options],

	options: {
		randomize						: false,
		transitionDelay			: 5,
		transitionTime			: 1,
		togglerClassActive	: 'blender-ctrl-a-active',
		elQuit              : 'el-quit'
	},

	initialize: function( wrapper, togglers, elements, options) {
		
		// register that carousel has stopped
		this.running = 1;

		this.wrapper 	= wrapper;
		this.togglers = togglers;
		this.lines		= elements;

		this.setOptions(options);

		if(this.options.randomize)
			this.randomize();

		this.lineCount					= this.lines.length;
		this.currentLineIndex 	= 0;
		this.currentLine				= this.lines[this.currentLineIndex];

		if ( this.togglers != null)
			this.togglers[this.currentLineIndex].addClass( this.options.togglerClassActive);

		this.wrapper.setStyles({
			'position': 'relative',
			'overflow': 'hidden'
		});

		if ( this.togglers != null) {
			this.togglers.each(
				function( toggler, i) {
				//alert(line.innerHTML);
				toggler.addEvent( 'click', function ( e) {
					//alert(i);
					var e = new Event(e).stop();
					this.moveTo(i);
				}.bind(this));

				}.bind(this)
			);
		}

		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,
					'wait': false
				});
				if(line != this.lines[0]) {
					line.effect.set( 'opacity', 0);
				}

			}.bind(this)
		);

		if(this.lineCount > 1)
			this.startTimer();

		if ( $(this.options.elQuit) && 1 == 1) {
			$(this.options.elQuit).addEvent( 'click', function ( e) {
				var e = new Event(e).stop();
				
				// register that carousel has stopped
				this.running = 0;
				this.stopTimer();
				var myFx = new Fx.Scroll(window).toElement(this.wrapper);

				var i = this.lines.length;
				for ( i = 0; i < this.lines.length; i++) {
					this.fadeIn( this.lines[i]);
					this.lines[i].setStyles({
							'position': 'relative',
						  'opacity': 1
					});
				}				
				this.wrapper.setStyles({
					'height': 'auto'
				});
												
			}.bind(this))
		}

	},
	
	



	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);

		this.timerID = this.next.periodical( (this.options.transitionDelay + this.options.transitionTime) * 1000, this);
		/*
		 * http://mootools.net/docs/Native/Function/#Function:periodical
		 * var Site = { counter: 0 };
		 * var addCount = function(){ this.counter++; };
		 * addCount.periodical(1000, Site); //Will add the number of seconds at the Site.
		 */
	},



	stopTimer: function () {
  		//clearInterval( this.timerID);

  		/*
		   * http://mootools.net/docs/Core/Core/#clear
		   * var myTimer = myFunction.delay(5000); //Waits 5 seconds then executes myFunction.
			 * myTimer = $clear(myTimer); //Cancels myFunction.
			 */
			 $clear( 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 () {

			//var _line_current = this.currentLineIndex;
		if ( this.togglers != null) {
			this.togglers[this.currentLineIndex].removeClass( this.options.togglerClassActive);
		}

		this.fadeOut(this.currentLine);
		this.increment();
		this.fadeIn(this.currentLine);

			//alert(_line_current);
			//alert(this.togglers);
			//alert(this.togglers[_line_current]);
		if ( this.togglers != null)
			this.togglers[this.currentLineIndex].addClass( this.options.togglerClassActive);

	},


	moveTo: function ( n) {
		this.stopTimer();

			/*
			if(this.currentLineIndex == (this.lineCount-1)) {
				this.currentLineIndex = 0;
			} else {
				this.currentLineIndex++;
			}
			this.currentLine = this.lines[this.currentLineIndex];
			*/
		if ( this.togglers != null)
			this.togglers[this.currentLineIndex].removeClass( this.options.togglerClassActive);

		this.fadeOut(this.currentLine);

		this.currentLineIndex = n;
		this.currentLine			= this.lines[this.currentLineIndex];

		this.fadeIn(this.currentLine);
		if ( this.togglers != null)
			this.togglers[this.currentLineIndex].addClass( this.options.togglerClassActive);

		// pause...
		//this.startTimer();


	},

	next2: function () {

		this.stopTimer();

		//this.increment();
		//this.fadeIn(this.currentLineIndex);
		this.next();


		// fade all out
		for ( q=0; q < this.lineCount; q++) {
			//alert(q);
			if ( q != this.currentLineIndex)
				this.fadeOut(this.lines[q]);
		}

		//this.startTimer().delay(this.options.transitionTime);

	}

});

