var currentFigure = null;
var stageW = null;
var stageH = null;

$(document).ready(function(){
	stretchFond();
	activerLiensExternes();
	initFigure();
});

$(window).resize(function(){
	stretchFond();			
});

/*---------------------------------------------------------------------
Affiche une photo random
---------------------------------------------------------------------*/
function initFigure(){
	currentFigure = Math.floor(Math.random()*$("figure").length);
	$("figure").not($("figure").eq(currentFigure)).css({left:stageW});
	
	if($("figure").length > 1) {
		$("figure img").click(function(){
			showNextFigure();
			return false;
		});
	}
}

function showNextFigure(){
	var nextFigure = currentFigure + 1;
	if(nextFigure >= $("figure").length) nextFigure = 0;
	if (!$("figure").eq(nextFigure).is(":animated")) {
		
		$("figure").eq(currentFigure).css({zIndex:1});
		$("figure").eq(nextFigure).css({zIndex:2});
		$("figure").eq(nextFigure).animate({left:0}, 1000, function(){
			$("figure").eq(currentFigure).css({left:stageW});
			currentFigure = nextFigure;
		});
	}
}

/*---------------------------------------------------------------------
Ouvre les liens externes dans une nouvelle fenêtre
---------------------------------------------------------------------*/
function activerLiensExternes(){
	$("a[href*='http']").attr("target", "_blank");
}

/*---------------------------------------------------------------------
Ajuste les dimensions des images de fond
---------------------------------------------------------------------*/
function stretchFond(){
	stageW = $(window).width();
	stageH = $(window).height();
	
	if(stageW/stageH > 1024/768){
		$("figure img").css("width", stageW);
		$("figure img").css("height", (stageW*768)/1024);
	} else {
		$("figure img").css("width", (stageH*1024)/768);
		$("figure img").css("height", stageH);
	}
	
	$("figure img, figcaption").css("margin-left", -($("figure img").width()/2));
	$("figure img, figcaption").css("margin-top", -($("figure img").height()/2));
}

