/*
CARROUSEL PHOTO
*/

var carrousel = {
	
	nbSlide : 0,
	nbCurrent : 1,
	elemCurrent : null,
	elem : null,
	timer:null,
	
	init:function(elem){
		this.nbSlide = elem.find(".slide").length;
		
		//naviguation points
		elem.append('<div class="naviguation"></div>');
		for (var i=1; i<=this.nbSlide; i++){
			elem.find(".naviguation").append('<span>'+i+'</span>');
		}
		// click naviguation
		elem.find(".naviguation span").click(function(){carrousel.gotoSlide($(this).text());});
		
		//init slide
		this.elem = elem;
		elem.find('.slide').hide();
		elem.find('.slide:first').show();
		this.elemCurrent = elem.find('.slide:first');
		this.elem.find('.naviguation span:first').addClass('active');
		
		
		//timer animation
		carrousel.play();
		
		// stop anim on rollover
		elem.mouseover(carrousel.stop);
		elem.mouseout(carrousel.play);
		
	},
	
	gotoSlide:function(num){
		if (this.nbCurrent == num){return false;}
		/*
		// anim en fade
		this.elemCurrent.fadeOut();
		this.elem.find("#slide"+num).fadeIn();
		*/
		// anim en pos_Y
		var cssHaut = {"top": - this.elem.height()};
		var cssMid = {"top":0};
		var cssBas = {"top": this.elem.height()};
		this.elem.find("#slide"+num).show().css(cssBas);
		this.elem.find("#slide"+num).animate(cssMid, 150);
//		this.elemCurrent.animate(cssHaut, 200);
		this.elemCurrent.hide();
		this.elem.find('.naviguation span').removeClass('active');
		this.elem.find('.naviguation span:eq('+(num-1)+')').addClass('active');
		this.nbCurrent = num;
		this.elemCurrent = this.elem.find("#slide"+num);
	},
	
	next:function(){
		var num = parseInt(this.nbCurrent) + 1;
		if (num > this.nbSlide) {
			num = 1;
		}
		this.gotoSlide(num);
	},
	
	prev:function(){
		var num = parseInt(this.nbCurrent) - 1;
		if (num < 1) {
			num =  this.nbSlide;
		}
		this.gotoSlide(num);
	},
	
	stop:function(){
		window.clearInterval(this.timer);
	},
	
	play:function(){
		window.clearInterval(carrousel.timer);
		this.timer = window.setInterval("carrousel.next()", 4000);
	}
	
}

$(function(){
	carrousel.init($("#car_photos"));
});
