//NOTE: Requires Mootools 1.2 (http://mootools.net)

var Carousel = new Class({
	
	//User settings
	timeBetweenTransitions: 7000,
	transitionSpeed: 1000,
	
	//Class vars
	promos: [
		'tab1',
		'tab2',
		'tab3',
		'tab4',
		'tab5'
	],
	currentPromoNum: 0,
	
	//Constructor
	initialize: function(dbKey)
	{		
		//Exit if there aren't any other carousels to switch between
		if (this.promos.length <= 1) return;
	
		//Preload images
		var images = new Array();
		for (var i = 0; i < this.promos.length; i++)
		{
			var imgUrl = this.promos[i];
			images.push(imgUrl);
		}
		new Asset.images(images);
	
		//Start animation timers when DOM is ready
		window.addEvent('domready', this.initTransitions.bind(this));
	},
	
	//Sets up main interval timer
	initTransitions: function()
	{
		setInterval(this.startTransition.bind(this), this.timeBetweenTransitions);
	},
	
	//Begins carousel transition with fade out
	startTransition: function()
	{
		var currentPromo = $(this.promos[this.currentPromoNum]);
		
		var fadeOut = new Fx.Tween(currentPromo, {property: 'opacity', duration: this.transitionSpeed, transition: 'quad:out', onComplete: this.changePromo.bind(this)});
		fadeOut.start(1,0);
	},
	
	//Changes contents of DOM elements and fades in to view
	changePromo: function()
	{
		var currentPromo = $(this.promos[this.currentPromoNum]);
		currentPromo.setStyle('display', 'none');
		
		//Get the promo we'll be switching to
		this.currentPromoNum = this.currentPromoNum < this.promos.length - 1 ? this.currentPromoNum + 1 : 0;
		var newPromo = $(this.promos[this.currentPromoNum]);
		
		//Fade in
		var fadeIn = new Fx.Tween(newPromo, {property: 'opacity', duration: this.transitionSpeed, transition: 'quad:in'});
		newPromo.setStyle('display', 'block');
		newPromo.setStyle('visibility', 'hidden');
		fadeIn.start(0, 1);
	}
	
});