// JavaScript Document
$(document).ready(function(){
	$("body").data("image_on",1);
	$("table .images img:not(:first-child)").hide();
	$(this).recalerElements(1);
	$(".nav a").hover(
		function(){
			$(this).animate({opacity:1},100);
		},
		function(){
			$(this).animate({opacity:0},100);
		}
	);
	$(".nav .next a").click(function(){
		$(this).diaporama(true);
		return false;
	});
	$(".nav .previous a").click(function(){
		$(this).diaporama(false);
		return false;
	});
	
});
(function($){
	$.fn.diaporama = function(avancer){
		var encours = parseInt($("body").data("image_on"));
		var nouvelle;
		//alert(encours);
		if(avancer){
			if(encours<$("table .images").children().length){
				nouvelle = encours+1;
			}else{
				nouvelle = 1;
			}
		}else{
			if(encours>1){
				nouvelle = encours-1;
			}else{
				nouvelle = $("table .images").children().length;
			}
		}
		//cache l'ancienne image
		$("table .images img:nth-child("+encours+")").animate({ opacity: 'hide'},1000);
		//cache la nav
		$("table .nav").animate({ opacity: 'hide'},500);
		//affiche la nouvelle image
		$("table .images img:nth-child("+nouvelle+")").animate({ opacity: 'show'},1000);
		//recale la nav et les éléments
		$(this).recalerElements(nouvelle);
		//affiche la nav
		$("table .nav").animate({ opacity: 'show'},500);
		$("body").data("image_on",nouvelle);
	};
	$.fn.recalerElements = function(image){
		//recale la nav et les éléments
		$("table .nav").css({ width: $("table .images img:nth-child("+image+")").width()+"px"});
		$("table .nav a").css({ paddingTop: ($("table .images img:nth-child("+image+")").height()-71)+"px"});
		var decaleTop = (500-$("table .images img:nth-child("+image+")").height());
		var decaleLeft = (500-$("table .images img:nth-child("+image+")").width());
		$("table .nav").css({ paddingTop: decaleTop+"px", marginLeft: decaleLeft+"px"});
		$("table .images img:nth-child("+image+")").css({ marginTop: decaleTop+"px", marginLeft: decaleLeft+"px"});		
	};
	$.fn.changerimage = function(element,etatdebut,etatfin,extension){
		var ancienne_source = element.attr("src");
		var verif = ancienne_source.split(etatfin+extension);
		if(etatfin!="" && verif[0] != ancienne_source) return;
		var off = ancienne_source.split(etatdebut+extension);
		if(off[0] != ancienne_source){
			element.attr({src: off[0]+etatfin+extension});
		}
	};
})(jQuery)
