jQuery(function($) {
	var container = $('#accueil-slideshow'),
		ul = container.find('ul'),
		li = container.find('li'),
		pager = container.find('.pager'),
		pages = pager.find('.pages'),
		dots = [],
		prevEl = pager.find('.prev'),
		nextEl = pager.find('.next'),
		delay = 4500,
		duration = 700,
		stopped = false,
		timer,
		act = 0,
		go = function(i) {
			act = i;
			if(act>=li.length) { act=0; }
			if(act<0) { act=li.length-1; }
			ul.stop().animate({
				left: -act*640
			});
			$.each(dots, function() { this.removeClass('activeSlide'); });
			dots[act].addClass('activeSlide');
			if(!stopped) {
				clearTimeout(timer);
				timer = setTimeout(next, delay);
			}
		},
		prev = function() {
			go(act-1);
		},
		next = function() {
			go(act+1);
		};
		
	li.each(function(i) {
		var $this = $(this);
		$this.css({
			position: 'absolute',
			top: 0,
			left: i*640
		});
		var d = $('<a href="#" />');
		dots.push(d.appendTo(pages).click(function(e) {
			e.preventDefault();
			go(i);
		}));
	});
	dots[0].addClass('activeSlide');
	
	
	prevEl.click(function(e) { e.preventDefault(); prev(); });
	nextEl.click(function(e) { e.preventDefault(); next(); });
	
	container.hover(function() {
		stopped = true;
		clearTimeout(timer);
	}, function() {
		stopped = false;
		timer = setTimeout(next, delay);
	});
	pager.css({width:prevEl.outerWidth()+nextEl.outerWidth()+pages.outerWidth()+5});
	timer = setTimeout(next, delay);
});
