/* Carousel */

function Carousel() {
	/* Config variables */
	this.autoPlay=true;
	this.currentItemNum=0;
	this.segundosAnim=7;
	
	/* private variables */
	this.doingTransition=false;
	this.timeout=null;



	/* methods */

	this.clickItem=function(itemNum) {
		this.cancelInterval();
		this.autoPlay=false;
		this.setItem(itemNum);
	}

	this.setItem=function(newItemNum) {
		if (this.doingTransition) {
			return;
		}
		if (newItemNum==this.currentItemNum) {
			return;
		}
		this.doingTransition=true;

		//set current control active
		//$("#carousel_control a").eq(this.currentItemNum).removeClass('carousel_control_active');
		//$("#carousel_control a").eq(newItemNum).addClass('carousel_control_active');

		//hideCurrentItem
		var carousel=this;
		$('#carousel > div').eq(this.currentItemNum).fadeOut('fast', function() {
			carousel.endFadeOutAnimation(newItemNum);
		});
	}
	
	this.endFadeOutAnimation=function(newItemNum) {
		//showItem
		var carouselItem=$("#carousel > div").eq(newItemNum);
	
		var carousel=this;
		carouselItem.fadeIn(function() {
			carousel.endFadeInAnimation(newItemNum);
		});
	}
	
	this.endFadeInAnimation=function(newItemNum) {
		this.currentItemNum=newItemNum;

		this.doingTransition=false;
	}
	

	this.play=function() {
		this.setCInterval();
	}
	
	this.setCInterval=function() {
		if (this.autoPlay) {
			var carousel=this;
			this.timeout=setTimeout(function(){
				carousel.nextItem();
			}, this.segundosAnim*1000);
		}
	}
	
	this.cancelInterval=function() {
		if (this.timeout!=null) {
			clearTimeout(this.timeout);
		}
	}
	
	this.nextItem=function() {
		var maxItems=$("#carousel > div").size();
		var nextItemNum=this.currentItemNum+1;
		if (nextItemNum>=maxItems) {
			nextItemNum=0;
		}
		this.setItem(nextItemNum);
		this.setCInterval();
	}


}

var carousel=null;
$(document).ready(function() {
	carousel=new Carousel();
	carousel.play();
});		


