$(function(){
	$("#galerie .photo").hide();
	$("#galerie .photo:first").show();
	$("#galerie #legendes span").hide();
	$("#galerie #legendes span:first").show();

	numero_selected(0);
	
	// Navigation automatique
	$("#galerie").everyTime(4000, "galerie_timer", function(){
	
		photo_index++;
		if (photo_index >= total_photos) {
			photo_index = 0;			
		}
		
		switch_to_index(photo_index);
		
	});
})


// Pagination
function numero_selected(index){
	$("#numeros a").siblings(".selected").removeClass("selected");
	$("#numeros a").eq(index).addClass("selected");
	
	index == 0 ? $("a.precedent").hide() : $("a.precedent").show();
	index == (total_photos - 1) ? $("a.suivant").hide() : $("a.suivant").show();
}

// Changement de photo manuel
function change_manuel(index) {
	// On désactive le timer
	$("#galerie").stopTime("galerie_timer");
	
	switch_to_index(index);
}


function switch_to_index(index) {
	$("#galerie .photo:visible").fadeOut("slow");
	$("#galerie #legendes span:visible").hide();
	$("#galerie .photo").eq(photo_index).fadeIn("slow");
	$("#galerie #legendes span").eq(photo_index).show();
	
	numero_selected(photo_index);
}

// Navigation par numéros
$("#numeros a").click(function(){
	// Nombre de numéros avant celui cliqué
	photo_index = $(this).prevAll("#galerie .photo").length;
	
	change_manuel(photo_index);
	return false;
});


// Navigation par flèches
$("#pagination a.suivant").click(function(){
	if (photo_index < (total_photos - 1)) {	
		photo_index = photo_index + 1;
		change_manuel(photo_index);
	}	
	return false;
});

$("#pagination a.precedent").click(function(){
	if (photo_index != 0) {		
		photo_index = photo_index - 1;
		change_manuel(photo_index);
	}	
	return false;
});