dojo.require("dojo.fx");

dojo.ready(function() {
	imgs = dojo.query("#diaporama img");
	boutons = [];
	dojo.forEach(imgs, function(img, index){
		setTimeout(function(){img.style.display = "block";}, 1900);
		dojo.style(img, "zIndex", index);
		if(index == imgs.length - 1){
			dojo.style(img, "display", "block");
		}
		boutons.push(dojo.create("div", {"class": "bouton"}, "boutonDiaporama"));
	});
	boutons.reverse();
	dojo.style(boutons[0], "backgroundColor", "black");
	imgs.reverse();

	function anim(){
		var animations = [];
		dojo.forEach(imgs, function(img, index){
			animations.push(dojo.fadeOut({
				node: img,
				duration: 2000,
				delay: 2000,
				onPlay: function(){
					dojo.style(boutons[index+1]?boutons[index+1]:boutons[0], "backgroundColor", "black");
					dojo.style(boutons[index], "backgroundColor", "white");
				},
				onEnd: function(){
					dojo.style(img, {opacity: 1, zIndex: dojo.style(img, "zIndex") - imgs.length});
				}
			}));
		});
		chain = dojo.fx.chain(animations).play();
		handle = dojo.connect(chain, "onEnd", anim);
	};
	setTimeout(function(){
		anim();
	}, 2000);
});
