/*
@import Calyptus.ClientSide, MooTools.Class.js
@import Calyptus.ClientSide, MooTools.Fx.Tween.js
*/

var Fader = new Class({
	
	Implements: Options,
	
	options: {
		fade: 750,
		show: 4000,
		random: false
	},

	/*
	element: $empty,
	images: $empty,
	*/
	current: 0,
	
	initialize: function(element, options)
	{
		this.setOptions(options);
		this.element = $(element);
		this.images = this.element.getChildren();
		
		if(this.images.length > 1)
		{
			this.images.setStyles({'display': 'none', 'opacity': 0});			
			this.fadeIn();
		}
	},
	
	fadeIn: function()
	{
		this.images[this.current].setStyle('display', 'block');
		this.images[this.current].get('tween', { property: 'opacity', duration: this.options.fade }).start(1).chain(
			function(){
				this.fadeOut.delay(this.options.show, this);
			}.bind(this)
		);
	},
	
	fadeOut: function()
	{
		this.images[this.current].get('tween').start(0).chain(this.next.bind(this));
	},
	
	next: function()
	{
		this.images[this.current].setStyle('display', 'none');
		this.current = (this.options.random) ? $random(0, this.images.length-1) : (this.current >= this.images.length-1) ? 0 : this.current+1;
		this.fadeIn.apply(this);
	}
});