/*
// "Mootools Image Rotator"
// License: http://www.gnu.org/copyleft/gpl.html
// Authors: Cornel Boudria (3PRIME, LLC.)
// Copyright (c) 2009 3PRIME, LLC. - http://www.3-prime.com
// ***Last update: March 16th, 2009***
*/

if( window.MooTools ) {

var MooImageRotator = new Class({
	current: 0,
	pause: false,
	options: {
		images: new Array(),
		delayTime: 5000,
		transitionSpeed: 40,
		imageRotatorContainer: 'imageRotatorContainer',
		imageRotatorPreloaderContainer: 'imageRotatorPreloader'
	},
	initialize: function(options) {
		this.setOptions(options);
		
		if( $defined(options['pause']) )
			this.pause = options['pause'];
		
		this.main();
	},
		
	// Load everything up
	main: function() {
		this.start();
		this.hideLoading();
	},

	start: function() {
		this.options.images = $(this.options.imageRotatorContainer).getElements('img');
		
		for(i=1;i<this.options.images.length;i++)
			this.options.images[i].xOpacity = 0;
		
		this.options.images[0].setStyle("display", "block");
		this.options.images[0].xOpacity = .99;
//		trans=this.options.transitionSpeed;

//		starter = function() { this.fade_delay(); }.bind(this);
//		this.options.images[0].setOpacity(0);
//		this.options.images[0].effects({duration:750}).start({'opacity':[0,1]}).chain( starter );
		
		// call fade, delaying by delayTime, binding to THIS (Class)
		//this.fade.delay(this.options.delayTime, this);
		this.fade_delay();
	},
	
	restart: function() {
		this.pause = false;
		this.fade();
	},
	
	stop: function() {
		this.pause = true;
	},
	
	fade_delay: function() {
		// call fade, delaying by delayTime, binding to THIS (Class)
		if( arguments.length == 1 ) delayAmount = arguments[0];
		else
			delayAmount = this.options.delayTime;

		if( !this.pause )
			this.fade.delay(delayAmount, this);
	},
	
	fade: function () {
		cOpacity = this.options.images[this.current].xOpacity;
		nIndex = this.options.images[this.current+1]?this.current+1:0;
		nOpacity = this.options.images[nIndex].xOpacity;
		cOpacity-=.05;
		nOpacity+=.05;
		this.options.images[nIndex].setStyle("display", "block");
		this.options.images[this.current].xOpacity = cOpacity;
		this.options.images[nIndex].xOpacity = nOpacity;
	
		this.options.images[this.current].setOpacity(cOpacity);
		this.options.images[nIndex].setOpacity(nOpacity);
	
		if(cOpacity<=0) {
			this.options.images[this.current].setStyle("display","none");
			this.current = nIndex;
			this.fade_delay();
		} else {
			this.fade_delay(this.options.transitionSpeed);
		}
	},

	hideLoading: function() {
		if( $(this.options.imageRotatorPreloaderContainer) )
			$(this.options.imageRotatorPreloaderContainer).setStyle("display", "none");
	}

});
MooImageRotator.implement( new Options );

}; /* End MooTools If/Else Wrapper */
