var diapoDuration = 8000;
var currentPosition = 3;
var diapoInterval;
var speed = 2000 ;
$(function() {
	var blocDiapo = $('#slidesContainer');
	
	var first = blocDiapo.children('.slide:first');
	var firstNode = first.get(0).nodeName;
	var last = blocDiapo.children('.slide:last');
	var lastNode = first.get(0).nodeName;
	blocDiapo.prepend('<'+lastNode+' class="' + last.attr('class') + '" style=\'' + last.attr('style') + '\'>'+last.html()+'</'+lastNode+'>');
	blocDiapo.append('<'+firstNode+' class="' + first.attr('class') + ' selected" style=\'' + first.attr('style') + '\'>'+first.html()+'</'+firstNode+'>');

	var width = blocDiapo.innerWidth();
	blocDiapo.children().each(function(i) {
		$(this).css('left', (i*width)+'px');
	});	

	blocDiapo.scrollLeft(width*currentPosition);

	diapoInterval = setTimeout('diaposScroll(1,' + speed + ')', diapoDuration/2);
	$('#thumb_' + currentPosition).addClass("selected");
});


function diaposScroll(value,speed_cur)
{
	var blocDiapo = $('#slidesContainer');
	
	blocDiapo.stop(true,true);
	
	if (diapoInterval > 0)
	{
		clearTimeout(diapoInterval);
	}
	
	var width = blocDiapo.innerWidth();
	var nbChilds = blocDiapo.children().length-2;
	
	currentPosition += value;
	
	if (currentPosition < 1)
	{
		currentPosition += nbChilds;
		blocDiapo.scrollLeft((currentPosition+1)*width);
	}
	else if (currentPosition > nbChilds)
	{
		currentPosition -= nbChilds;
		blocDiapo.scrollLeft((currentPosition-1) * width);
	}

	blocDiapo.animate({scrollLeft:(currentPosition * width)}, speed_cur);
	
	diapoInterval = setTimeout('diaposScroll(1,' + speed + ')', diapoDuration);
	
	$('.slideshow_thumbs_min').removeClass('selected');
	$('#thumb_' + currentPosition).addClass("selected");
	
}

$(document).ready(function(){
	$('.slideshow_thumbs_navigation')
		.bind('click', function(){
		if ($(this).hasClass('droite'))
		{		
				diaposScroll(1,speed);
		}else 
		{
					diaposScroll(-1,speed);
		}
  	});

			
	$(".slideshow_thumbs_min").click(function() {
	var num_thumb=$(this).attr('id').split("_");
    // Determine la nouvelle position
	//currentPosition = ($(this).attr('id')=='rightControl') ? currentPosition+1 : currentPosition-1;
	num_slide = parseInt(num_thumb[1]);
	while ( num_slide != currentPosition)
	{
		diaposScroll(1,0);
	}
	});
	
	$(".slide").click(function(){
	   $(location).attr('href',$(this).children('.slide_url').children('a').attr('href'));					   
	});
			

});
