$(function() {
	$('.homepub_container').each(function() {
		// Initialisation
		var self = $(this) ;
		var total = self.find('.pub').length ;
		if (total < 2)
			return ;
		// var numero = Math.floor(Math.random() * total) ;
		var numero = 0 ;
		self.find('.pub').hide() ;
		self.find('.pub:nth('+numero+')').show() ;
		// self.find('.pub').each(function(index) {
			// $(this).toggle(index == numero) ;
		// }) ;
		self.data('total', total) ;
		self.data('numero', numero) ;
		
		// Fonction de changement
		self.bind('slide.next', function() {
			var numero = self.data('numero') ;
			var total  = self.data('total' ) ;
			self.find('.pub:nth('+numero+')').hide() ;
			numero = (numero+1) % total ;
			self.find('.pub:nth('+numero+')').show() ;
			self.data('numero', numero) ;
		}) ;
		self.bind('slide.prev', function() {
			var numero = self.data('numero') ;
			var total  = self.data('total' ) ;
			self.find('.pub:nth('+numero+')').hide() ;
			numero = (numero+total-1) % total ;
			self.find('.pub:nth('+numero+')').show() ;
			self.data('numero', numero) ;
		}) ;
		self.bind('slide.clearinterval', function() {
			var interval = self.data('interval') ;
			clearInterval(interval) ;
			self.data('interval', null) ;
		}) ;
		
		// Déclaration
		self.data('interval', setInterval(function() {
			self.trigger('slide.next') ;
		}, 10000)) ;
		self.find('.prev').click(function() {
			self.trigger('slide.clearinterval');
			self.trigger('slide.prev') ;
		}) ;
		self.find('.next').click(function() {
			self.trigger('slide.clearinterval');
			self.trigger('slide.next') ;
		}) ;
		
	}) ;
	
});

