var carrousel = {
	
	nbSlide : 0,
	nbCurrent : 1,
	elemCurrent : null,
	elem : null,
	timer : null,
	
	init : function(elem){
		this.nbSlide = elem.find(".slide").length;
		
		
		$('#next').click(function(){
			$this = $(this);
			carrousel.next();
			return false;
		});

		$('#previous').click(function(){
			$this = $(this);
			carrousel.prev();
			return false;
		});
				
		// initialisation du carrousel
		this.elem = elem;
		//elem.find(".slide").hide();
		elem.find(".slide:first").show();
		this.elemCurrent = elem.find(".slide:first");
		this.elem.find('#navright span:first').addClass('active');
		
		// On crée le timer
		carrousel.play();
		
		// stop au rollover
		$('#slide-contener').mouseover(carrousel.stop);
		$('#slide-contener').mouseout(carrousel.play);
	},
	
	gotoSlide : function(num){
		carrousel.stop();
		if (num == this.nbCurrent){ return false; }
		var left = (parseInt(num)-1)*600;
		$('#slide-contener').animate({"margin-left": "-"+left});
		
		this.elem.find('#navright span').removeClass('active');
		this.elem.find('#navright span:eq('+ (num-1) +')').addClass('active');
		this.nbCurrent = num;
		this.elemCurrent = this.elem.find("#slide"+num);
		carrousel.play();
	},
	
	next : function(){
		var num = this.nbCurrent;
		if(num != this.nbSlide){
			num++;
		} else {
			num = 1;
		}
		this.gotoSlide(num);
	},
	
	prev : function(){
		var num = this.nbCurrent;
		if(num == 0){
			num = 1;
		} else {
			num--;
		}
		this.gotoSlide(num);
	},
	
	play : function(){
		window.clearInterval(carrousel.timer);
		carrousel.timer = window.setInterval('carrousel.next()', 5000);
		$('#play').html('<img src="../js/css/img/play-actif.jpg" alt="play" />');
		$('#pause').html('<img src="../js/css/img/pause.jpg" alt="play" />');
	},
	
	stop : function(){
		$('#pause').html('<img src="../js/css/img/pause-actif.jpg" alt="play" />');
		$('#play').html('<img src="../js/css/img/play.jpg" alt="play" />');
		window.clearInterval(carrousel.timer);
	}

}

$(function(){
	carrousel.init($("#carrousel"));
});
