
var $j = jQuery.noConflict();

$j(document).ready(function() {
s = new slider("#galerie");
});

var slider = function(id)
{
	var self=this;
	this.div=$j(id);
	this.slider = this.div.find(".slider");
	this.largeurCache = this.div.width();
	this.largeur=0;
	
	this.slider.find('a').each(function(){
		self.largeur+=$j(this).width();
		self.largeur+=parseInt($j(this).css("padding-left"));
		self.largeur+=parseInt($j(this).css("padding-right"));
		self.largeur+=parseInt($j(this).css("margin-left"));
		self.largeur+=parseInt($j(this).css("margin-right"));
	});
	
	this.prec = this.div.find(".prec");
	this.suiv = this.div.find(".suiv");
	this.saut = this.largeurCache/2;
	
	this.nbEtapes = this.largeur/this.saut - (this.largeurCache/this.saut);
	
	this.courant=0;
	
	this.prec.hide();
	if(this.largeurCache>this.largeur){this.suiv.hide();}

	
	// Avancer d'un cran
	this.next = function(){
	if(self.courant>=self.nbEtapes){ return false; }
	if(self.courant==0){ self.prec.fadeIn(); }
	self.courant++;
	self.slider.animate({
		left:-self.saut*self.courant
	},1000);
	if(self.courant>=self.nbEtapes){ self.suiv.fadeOut(); }
	}
	
	/*this.suiv.click(function(){
		
		if (self.courant  <= self.nbEtapes)
		{
		self.courant++;
		self.slider.animate({
			left:-self.courant*self.saut
		},1000);
		}
		
	});*/
	
	this.prev = function(){
		if(self.courant<=0){ return false; }
		if(self.courant>=self.nbEtapes){ self.suiv.fadeIn(); }
		self.courant--;
		self.slider.animate({
		left:-self.saut*self.courant
		},1000);
		if(self.courant==0){ self.prec.fadeOut(); }
		}
	
	/*this.prec.click(function(){
		if (self.courant  > 0)
		{
		self.courant--;
		self.slider.animate({
			left:-self.courant*self.saut
		},1000);
		}
		
	});*/
	
	this.prec.bind('click',this.prev);
	this.suiv.bind('click',this.next);
}
