/**
 * Fader
 * @class
 * @author grotter
 * @version 1.1
 */
Fader = function (el) {
	var myFader = el;
	var num_faders = jQuery(".fader-item", myFader).length;
	var current_index = 0;
	var flip_duration = 8000;
	
	this.getItemFromIndex = function (index) {
		return jQuery(jQuery(".fader-item", myFader).get(index));
	}
   
	this.setFlipDuration = function (newDuration) {
		flip_duration = newDuration;
	}
	
	this.flip = function () {
		var last_index = current_index; 
		current_index++;
		if (current_index > (num_faders - 1)) current_index = 0;
		
		var last_item = this.getItemFromIndex(last_index);
		var next_item = this.getItemFromIndex(current_index);
		
		last_item.fadeOut("fast");
		next_item.fadeIn("fast");
	}
	
	this.initialize = function (initialFlipDelay) {
		if (typeof(initialFlipDelay) == "undefined") {
			initialFlipDelay = flip_duration;
		}
		
		//show the first
		this.getItemFromIndex(current_index).show();

		//only one item, don't do anything
		if (num_faders < 2) return;
		
		var obj = this;
		
		//do first flip, then start flipping infinitely
		setTimeout(function () {
			obj.flip();
			setInterval(function () { obj.flip(); }, flip_duration); 
		}, initialFlipDelay);
	}
}

/*
$(document).ready(function () {
	$(".fader").each(function () {
		var foo = new Fader($(this));
		foo.initialize();
	});
});
*/
