$(document).ready(function()
{
	//Speed of the slideshow
	var speed = 5000;
	
	//You have to specify width and height in #slider CSS properties
	//After that, the following script will set the width and height accordingly
	$('#gallery li').width($('#slider').width() - 40);
	$('#mask-gallery').width($('#slider').width());
	$('#gallery').width($('#slider').width() * $('#gallery li').length);
	
	//Assign a timer, so it will run periodically
	var run = setInterval('newsscoller(0)', speed);
	
	$('#gallery li:first, #excerpt li:first').addClass('selected');

	//Pause the slidershow with clearInterval
	$('#btn-pause').click(function () {
		clearInterval(run);
		return false;
	});

	//Continue the slideshow with setInterval
	$('#btn-play').click(function () {
		run = setInterval('newsscoller(0)', speed);	
		return false;
	});
	
	//Next Slide by calling the function
	$('#btn-next').click(function () {
		newsscoller(0);	
		return false;
	});	

	//Previous slide by passing prev=1
	$('#btn-prev').click(function () {
		newsscoller(1);	
		return false;
	});	
	
	//Mouse over, pause it, on mouse out, resume the slider show
	$('#slider').hover(
	
		function() {
			clearInterval(run);
		}, 
		function() {
			run = setInterval('newsscoller(0)', speed);	
		}
	);


	var currentPosition = 0;
	var slideWidth = 150;
	var slides = $('#imglist li');
	var numberOfSlides = slides.length;
    
	//Config
	var tempsTransition = 1000;
	var tempsAttente = 2000;
	var interval;
	var lectureEnCours = false;	
	
	// La longueur de #slideInner équivaut à la somme de la longueur de tous les slides
	$('#imglist').css('width', slideWidth * numberOfSlides);

	$('.scrollleft').bind('click', function(){
		currentPosition = currentPosition-1;
		if (currentPosition < 0)
		{
			currentPosition = 0;
		}
		$('#imglist').animate({
        	'marginLeft' : slideWidth*(-currentPosition)
      	});
      	return false;
	});
	$('.scrollright').bind('click', function(){
		suivant();
      	return false;
	});
	
	function start() {
  		lectureEnCours = true;
        interval = setInterval(suivant, tempsAttente );
	}

	function suivant(){
		currentPosition = currentPosition+1;
		if (currentPosition > numberOfSlides - 5)
		{
			currentPosition = 0;
		}
		$('#imglist').animate({
        	'marginLeft' : slideWidth*(-currentPosition)
      	}, tempsTransition);
	}
	
	function pause() {
	  	lectureEnCours = false;
	        clearInterval(interval);
	}
	
	start();	
});


function newsscoller(prev) {

	//Get the current selected item (with selected class), if none was found, get the first item
	var current_image = $('#gallery li.selected').length ? $('#gallery li.selected') : $('#gallery li:first');

	//if prev is set to 1 (previous item)
	if (prev) {
		
		//Get previous sibling
		var next_image = (current_image.prev().length) ? current_image.prev() : $('#gallery li:last');
	
	//if prev is set to 0 (next item)
	} else {
		
		//Get next sibling
		var next_image = (current_image.next().length) ? current_image.next() : $('#gallery li:first');
	}

	//clear the selected class
	$('#gallery li').removeClass('selected');
	
	//reassign the selected class to current items
	next_image.addClass('selected');

	//Scroll the items
	$('#mask-gallery').scrollTo(next_image, 800);
	
};
