/*
// "Mootools Image Rotator", version 2
// 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 23th, 2009***
// changes:
// - implemented more Moo-Centric effects
// - implemented revised stop/restart functionality (still needs some work)
*/

if( window.MooTools ) {

var MooImageRotator = new Class({
	version: 2,
	current: 0,
	paused: false,
	options: {
		images: new Array(),
		delayTime: 5000,
		transitionSpeed: 1000,
		transitionType: Fx.Transitions.Quart.easeInOut,
		imageRotatorContainer: 'imageRotatorContainer',
		imageRotatorPreloaderContainer: 'imageRotatorPreloader',
		fadeInFirstImage: false
	},
	initialize: function(options) {
		this.setOptions(options);
		
		if( $defined(options['pause']) )
			this.pause = options['pause'];
		
		this.transitionProperties = {
			'duration': this.options.transitionSpeed,
			'transition': this.options.transitionType
		};
		
		this.FaderFx = new Fx.Elements([], this.transitionProperties);
		
		this.main();
	},
		
	// Load everything up
	main: function() {
		this.start();
		this.hideLoading();
	},

	start: function() {
		this.options.images = $(this.options.imageRotatorContainer).getElements('img');

		this.options.images.each( function(img, index) {
			img.setStyles({'opacity':0});
		});
		
		if( this.options.fadeInFirstImage )
		{
			this.options.images[0].effects(this.transitionProperties).start({'opacity':[0,1]}).chain( this.fade_delay.bind(this) );
		}
		else
		{
			this.options.images[0].setOpacity(1);
			this.fade_delay();
		}

		
	},
	
	restart: function() {
		this.paused = false;

		nIndex = this.options.images[this.current+1]?this.current+1:0;
		cOpacity = this.options.images[this.current].getStyle('opacity');
		nOpacity = this.options.images[nIndex].getStyle('opacity');
		
		this.FaderFx.start({
			'0': {'opacity':[cOpacity,0]}, /* CURRENT Image */
			'1': {'opacity':[nOpacity,1]}  /* NEXT Image */
		}).chain( function() {
			//this.options.images[this.current].setStyle("display","none");
			this.current = nIndex;
			this.fade_delay();
		}.bind(this) );
	},
	
	stop: function() {
		this.FaderFx.stop();
		this.paused = 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() {
		nIndex = this.options.images[this.current+1]?this.current+1:0;
		this.options.images[nIndex].setStyle("display", "block");
		this.FaderFx.elements = [this.options.images[this.current], this.options.images[nIndex]];

		this.FaderFx.start({
			'0': {'opacity':[1,0]}, /* CURRENT Image */
			'1': {'opacity':[0,1]}  /* NEXT Image */
		}).chain( function() {
			this.options.images[this.current].setStyle("display","none");
			this.current = nIndex;
			this.fade_delay();
		}.bind(this) );
	},

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

});
MooImageRotator.implement( new Options );

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