/*
Script: Fader.js
	an SEO-friendly rotating banner using mootools 1.2.1

Copyright:
	Copyright (c) 2009 The Carella Company, <http://www.carellaco.com/>.

License:
	MIT-style license.

Author:
	- Chris Griffin
*/

var Fader = new Class({ 
	Implements: Options, 
	options: { 
		pause: 4000, 
		duration: 1000, 
		width: 200,
		height: 100,
		loop: true, 
		randomStart: false,
		remove: null, //this is the fail-over image that should be removed
		onComplete: Class.empty, 
		onStart: Class.empty
	}, 
	initialize: function(container,options) { 
		this.setOptions(options); 
		this.container = document.id(container);
		this.container.setStyles({ 'position':'relative','height': this.options.height, 'width': this.options.width });
		this.banners = this.container.getElements('a')
		this.banners.each(function(el){
			el.setStyles({ 'display':'block','position':'absolute', 'top':0, 'left':0, 'opacity':0, 'cursor':'pointer' })
			el.inject(this.container);
		}.bind(this));
		this.next = (this.options.randomStart)?$random(0,this.banners.length-1):0; 
		this.banners[this.next].setStyle('opacity',1); 
		this.start(); 
	}, 
	start: function() { 
		this.show(); 
		this.periodical = this.show.bind(this).periodical(this.options.pause); 
	}, 
	stop: function(idx) { 
		$clear(this.periodical);
		if(idx!="undefined"){
			this.next = idx; 
			this.banners[this.next].fade('in'); 
			this.banners.each(function(el,index){if(index!=this.next)el.fade('out')}.bind(this)); 
		}
	}, 
	show: function() { 
		if (!this.options.loop && this.next==this.banners.length-1) this.stop(); 
		var prev = (this.next==0)?this.banners.length-1:this.next-1; 
		this.banners[this.next].fade('in'); 
		this.banners[prev].fade('out'); 
		this.banners[this.next].setStyle('z-index','10');
		this.banners[prev].setStyle('z-index','1'); 
		this.next = (this.next==this.banners.length-1)?0:++this.next; 
	} 
}); 
