jQuery.preloadImages = function()
{
  for(var i = 0; i<arguments.length; i++)
  {
    jQuery("<img>").attr("src", arguments[i]);
  }
}
onNeBougePlus=false;
$(document).ready(function(){
//	$(".contenuRes").hide();
	$('.courant').show();
	$('#archives').hide();
	$("#lienArchives").click(function(){
		$('#archives').show();
		$('#current .contenuRes:visible').SlideOutUp('500');
		//$(this).hide();
		$(this).bind("click",function(){});
		$(this).css("cursor","default");
	});
	$('.refsResumees li h4').each(function(){
		var leLien=$(this).children("a").attr("href");
		var idBateau=$(this).children("a").attr("rel");
		$(this).siblings("p").click(function(){
			$(this).siblings("h4").click();
		});
		$(this).click(
			function (){
				if($(this).siblings(".contenuRes:visible").length){
					if(!onNeBougePlus){
						//$(this).children("a").hide();
						onNeBougePlus=true;
						var leParent=$(this).parent();
						leParent.css("position","relative");
						$('.refsResumees li div').not(leParent).removeClass("courant");
						var leClone=leParent.clone(true);
						leClone.width(leParent.width());
						leClone.height(leParent.height());
						leParent.addClass("courant");
						// On cree le masque pour l'attente
						leParent.parent().append('<li id="massque"><img src="/medias/wait2.gif" style="padding:30px 0px 0px 160px;"></li>');
						$('#massque').each(function(){
							$(this).css("position","absolute");
							$(this).css("z-index","2000");
							$(this).css("top","0px");
							$(this).css("background-color","black");
							$(this).css("left","0px");
							$(this).hide();
							$(this).fadeIn(500);
							$(this).css("height",leParent.height());
							$(this).css("width",leParent.width());
						});
						leClone.each(function(){
							$(this).css("position","absolute");
							$(this).css("z-index","1000");
							$(this).css("top","0px");
							$(this).css("left","0px");
							$(this).css("height",leParent.css("height"));
							$(this).insertAfter(leParent);
							var moi=$(this);
							if(typeof(minuterie)!="undefined"){
								clearTimeout(minuterie);
							}
							$.ajax({
								type: "GET",
								url: "/aca/bateauToutSeul.php",
								dataType : "html",
								data: "idBateau=" + idBateau,
								success: function(data){
									$('#massque').fadeOut(100,function(){
										$(".conteneurDetail").append(data);
										$(".movable:eq(0)").css("position","absolute");
										$(".movable:eq(1)").width($(".movable:eq(0)").width());
										$(".movable:eq(1)").hide();
										$(".conteneurDetail").css("height",Math.max($(".movable:eq(1)").height(),$(".movable:eq(0)").height(),$("#droite").height()));
										rotate();
										$(".movable:eq(0)").SlideOutLeft(1000,function(){
											$(this).remove();
											$(".movable").css("position","relative");
										});
										$(".movable:eq(1)").SlideInRight(1000);
										moi.SlideOutLeft(1000,function(){
											moi.remove();
//											$(".movable:eq(0) p").hide().show();
											// On supprime le masque
											$('#massque').remove();
											first=1;
											premiereImage.click();
											//Timer();
											onNeBougePlus=false;
										});
									});
								}
							});
						});
					}
				}else{
					$('.contenuRes:visible').SlideOutUp('500');
					$(this).siblings("p").show();
				}
			}
		);
		$(this).children("a").attr("href","#");
	});
});


rotate=function(){
	// Id slideShow :
	// On va chercher la première image :
	premiereImage=$(idSlideShow).children(".slideItem:eq(0)");
	var aAfficher=premiereImage.attr("href");
	legende=premiereImage.children("img:eq(0)").attr('alt');
	$(idSlideShow).prepend('<img src="' + aAfficher + '" class="imgSlideCurrent"/>');
	$(idSlideShow).prepend('<p class="commentSlideCurrent"/>' + legende + '</p>');
	if($(idSlideShow).children(".slideItem").length>1){
		$(idSlideShow).append('<p class="linkAutoplay"/>stop</p>');
		$('.linkAutoplay').toggle(function(){
			clearTimeout(minuterie);
			$(this).empty().append("launch autoplay");
		},function(){
			minuterie=setTimeout("Timer()",5000);
			$(this).empty().append('stop');
		});
		$(idSlideShow).children(".slideItem").each(function(){
			numImage++;
			$(this).append('<div class="numero">' + numImage + '</div>');
			$(this).attr("rel",$(this).attr("href"));
			$(this).attr("href","#");
			$(this).click( function() {
				if(imageCourante){
					imageCourante.children("div").removeClass("numeroActif");
				}
				$(this).children("div").addClass("numeroActif");
				destination=$(this).attr('rel');
				legende=$(this).children("img:eq(0)").attr('alt');
				if(!first){
					if(minuterie){
						imageCourante=$(this);
					}
					$(idSlideShow).children(".imgSlideCurrent").fadeTo("fast",0,
						function(){
							$(idSlideShow).children(".imgSlideCurrent").attr('src',destination);
//							cache=$(document.createElement('img'));
							$(idSlideShow).children(".imgSlideCurrent").load(function(){
//								$(idSlideShow).children(".imgSlideCurrent").attr('src',destination);
								$(this).fadeTo("fast",1);
								if(legende){
									$(idSlideShow).children(".commentSlideCurrent").empty().prepend(legende).show();
								}else{
									$(idSlideShow).children(".commentSlideCurrent").empty();
								}
								if(minuterie){
									clearTimeout(minuterie);
									minuterie=setTimeout("Timer()",	5000);
								}
							});
							//cache.src=destination;
							//cache.trigger('load');
						}
					);
				}else{
					if(minuterie){
						clearTimeout(minuterie);
						minuterie=setTimeout("Timer()",	5000);
						imageCourante=$(this);
					}
					first=false;
				}
				return false;
			});
		});
		minuterie=true;
	}else{
		$(idSlideShow).children(".slideItem").hide();
		$(idSlideShow).children(".listePictures").hide();
		$(idSlideShow).children(".linkAutoplay").hide();
	}
}

function Timer(){
	if($(idSlideShow).children(".slideItem").length>1){
		if($(idSlideShow).children(".slideItem:last").children("img:eq(0)").attr('src')!=imageCourante.children("img:eq(0)").attr('src')){
			imageCourante.children("div").removeClass("numeroActif");
			imageCourante.next().click();
		}else{
			imageCourante.children("div").removeClass("numeroActif");
			$(idSlideShow).children(".slideItem:eq(0)").click();
		}
	}
}
