//Basé sur la ThickBox

MAX_HEIGHT = 480;
MAX_WIDTH = 600;
var barrenav = false;

$(document).ready(function(){   
	imgLoader = new Image();
	imgLoader.src = "../images/loading.gif";
	$("#LienClose").click(closediaporama);
});


function showImage(url, barre)
{
    if (typeof document.body.style.maxHeight === "undefined") {//if IE 6
      $("body","html").css({height: "100%", width: "100%"});
      $("html").css("overflow","hidden");
      if (document.getElementById("HideSelect") === null) //iframe to hide select elements in ie6
        $("body").append("<iframe id='HideSelect'></iframe>");
    }
	
    $("body").append("<div id='Loading'><img src='../images/loading.gif' /></div>");
    $('#Loading').show();

	imgPreloader = new Image();
	imgPreloader.onload = function(){    
	imgPreloader.onload = null;
	
	var imageWidth = imgPreloader.width;
	var imageHeight = imgPreloader.height;
	if (imageWidth > MAX_WIDTH) {
	imageHeight = imageHeight * (MAX_WIDTH / imageWidth); 
	imageWidth = MAX_WIDTH; 
	if (imageHeight > MAX_HEIGHT) { 
	  imageWidth = imageWidth * (MAX_HEIGHT / imageHeight); 
	  imageHeight = MAX_HEIGHT; 
	}
	} else if (imageHeight > MAX_HEIGHT) { 
	imageWidth = imageWidth * (MAX_HEIGHT / imageHeight); 
	imageHeight = MAX_HEIGHT; 
	if (imageWidth > MAX_WIDTH) { 
	  imageHeight = imageHeight * (MAX_WIDTH / imageWidth); 
	  imageWidth = MAX_WIDTH;
	}
	}
	
	$("#Image").remove();
    $("#LienClose").append("<img id='Image' src='"+url+"' width='"+imageWidth+"' height='"+imageHeight+"' border='0'/>");     

    $("#Conteneur").css({marginLeft: '-' + parseInt((imageWidth / 2),10) + 'px', marginTop: '-' + parseInt((imageHeight / 2),10) + 'px', width: imageWidth + 'px'});
    $("#Navigation").css({marginLeft: '-' + parseInt((MAX_WIDTH / 2),10) + 'px', width: MAX_WIDTH-6 + 'px', marginTop:'-' + parseInt((MAX_HEIGHT/2),10) - 22 + 'px'});
	  
    $("#Loading").remove();
    $("#Masque_fond").click(closediaporama);
	$("#Masque_fond").fadeTo("fast", 0.75);
	$("#Masque_fond").css({display:"block"}); //for safari using css instead of show
	$("#Conteneur").fadeIn("slow");
	$("#Conteneur").css({display:"block"});
	};
	
	imgPreloader.src = url;
	barrenav = false;
	if (arguments.length>1 || barre)
	{
		$("#Navigation").css({display:"block"});
		barrenav = true;
		if (typeof TabDiaporama != "undefined")
			$("#Navigation").text("Photo " + (compteur_diaporama+1) + " / " + TabDiaporama.length);
	}
}

function closediaporama()
{
	$("#Masque_fond").unbind("click");
	$("#Conteneur").fadeOut("slow");
	if(barrenav)
		$("#Navigation").fadeOut("fast");
	$("#Masque_fond").fadeTo("fast", 0, function(){$("#Masque_fond").css({display:"none"});$("#HideSelect").remove();});
	
	if (typeof document.body.style.maxHeight == "undefined") {//if IE 6
		$("body","html").css({height: "auto", width: "auto"});
		$("html").css("overflow","");
	}
	
	clearTimeout(timer);
	return false;
}

var compteur_diaporama=0;
var timer;
function LaunchDiaporama()
{
	if(compteur_diaporama>=TabDiaporama.length) compteur_diaporama=0;
	showImage(TabDiaporama[compteur_diaporama], true);
	compteur_diaporama++;
	timer = setTimeout("$('#Conteneur').fadeOut('slow', function(){LaunchDiaporama()})", 5000);
}
