// Script de diaporama ecrit par r0bl0che pour www.toutjavascript.com
// Retrouvez r0bl0che sur les forums du site 


// Paramétrage 
var rep = "../original/"; // Sous répertoires des images (/) si pas de sous repertoire
var num = 0;
var myCounter;
var next_img = new Image;
next_img.src = rep+"pixel_transparent.gif"; 
var wPopup;
var tabImg;
var tabImgSave = new Array;
// Images du diaporama
tabImgSave[0] = 'PICT_01.JPG'; 
tabImgSave[1] = 'PICT_02.JPG'; 
tabImgSave[2] = 'PICT_03.JPG'; 
tabImgSave[3] = 'PICT_04.JPG'; 
tabImgSave[4] = 'PICT_05.JPG'; 
tabImgSave[5] = 'PICT_06.JPG'; 
tabImgSave[6] = 'PICT_07.JPG'; 
tabImgSave[7] = 'PICT_08.JPG'; 
tabImgSave[8] = 'PICT_09.JPG'; 
tabImgSave[9] = 'PICT_10.JPG'; 
tabImgSave[10] = 'PICT_11.JPG'; 
tabImgSave[11] = 'PICT_12.JPG'; 
tabImgSave[12] = 'PICT_13.JPG'; 
tabImgSave[13] = 'PICT_14.JPG'; 
tabImgSave[14] = 'PICT_15.JPG'; 
tabImgSave[15] = 'PICT_16.JPG'; 
tabImgSave[16] = 'PICT_17.JPG'; 
tabImgSave[17] = 'PICT_18.JPG'; 
tabImgSave[18] = 'PICT_19.JPG'; 
tabImgSave[19] = 'PICT_20.JPG'; 
var nb_img = tabImgSave.length;
	       
	       


// NE PAS MODIFIER
// Les trois fonctions suivantes servent à mélanger un tableau quelconque
// à partir d'une permutation tirée aléatoirement
//

// Génère une fonctione sous-excédente
function fctSsExc() {
	var fct = new Array;
	for(var i=0; i<nb_img; i++) {
		fct[i] = Math.floor( Math.random()*(nb_img-i) );
	}
	return fct;
}

// Construit une permutation à partir d'une fonction sous-excédente
function buildSigma() {
	var fct_ss_exc = fctSsExc();
	var set_N       = new Array;

	for(var i=0; i<nb_img; i++) {
		set_N[i] = i;
	}

	var sigma = new Array;

	for(var i=0; i<nb_img; i++) {
		sigma[i] = set_N[fct_ss_exc[i]];
		set_N.splice(fct_ss_exc[i],1);
	}
	return sigma;
}

// Retourne une version mélangée du tableau passé en paramètre
function shuffleArray(myArray) {
	var sigma    = buildSigma();
	var newArray = new Array;

	for(var i=0; i<nb_img; i++) {
		newArray[i] = myArray[sigma[i]];
	}
	return newArray;
}
//
// Fin des fonctions de mélange
//

// Donne le focus au bouton "Lancer"
function focusOnLaunch() {
	window.document.forms.settings.bLaunch.focus();
}

// Donne le focus au bouton "Stopper"
function focusOnStop() {
	window.document.forms.settings.bStop.focus();
}

// Lance le chargement de l'image suivante
function next() {
	// Est-ce que l'image dans le popup est complètement chargée ?
	if(wPopup.document.images && wPopup.document.images[0].complete) {
		// Est-ce que la dernière image chargée est celle affichée dans le popup ?
		if(next_img.src != rep+tabImg[num]) {
			// Oui, alors on commence à charger la suivante
			next_img      = new Image;
			next_img.src = rep+tabImg[num];
		}
		// Est-ce que l'image suivante est oréchargée ?
		if(next_img.complete) {
			// Oui, alors après le temps de pause choisi par l'utilisateur, cette image remplacera l'actuelle
			myCounter = setTimeout("launch()", 1000*window.document.settings.tempo.value);
		}
		else {
			// Non, alors on continue d'attendre qu'elle le soit
			myCounter = setTimeout("next()", 3000);
			//myCounter = setTimeout("launch()", 1000*window.document.settings.tempo.value);

		}
	}
	else {
		// Non, alors on attend qu'elle le soit
		myCounter = setTimeout("next()", 3000);
		//myCounter = setTimeout("launch()", 1000*window.document.settings.tempo.value);

	}
}

// Lance le slideshow
function launch() {
	// Petite vérification de la temporisation choisie
	if(window.document.settings.tempo.value == "") {
		alert("Précisez une temporisation entre 0 et 60 secondes...");
		return false;
	}

	// Ordre normal ou aléatoire
	if(num == 0) {
		if(window.document.settings.order[1].checked) {
			tabImg = shuffleArray(tabImgSave);
		}
		else {
			tabImg = tabImgSave;
		}
	}

	// Avant de lancer le slideshow, on désactive tous les éléments du formulaire
	if(!window.document.forms.settings.bLaunch.disabled) {
		next_img.src = rep+tabImg[0];
		window.document.forms.settings.bStop.disabled    = false;
		focusOnStop();
		window.document.forms.settings.bLaunch.disabled  = true;
		window.document.forms.settings.repeat.disabled   = true;
		window.document.forms.settings.order[0].disabled = true;
		window.document.forms.settings.order[1].disabled = true;
		window.document.forms.settings.tempo.disabled    = true;
	}
	
	// Si la fenêtre n'existe pas ou est fermée, on la réouvre
	if(!wPopup || wPopup.closed) {
		wPopup = window.open('', 'img_popup', 'width=1, height=1, top=(screen.height-50)/2, left=(screen.width-50)/2, status=no, directories=no, toolbar=no, location=no, menubar=no, scrollbars=no, resizable=yes');
	}

	// On écrit le contenu de la fenêtre popup
	wPopup.document.clear();
	wPopup.document.write("<HTML><HEAD><TITLE>www.lhasa-apso slideshow : "+(num+1)+"/"+nb_img+"</TITLE></HEAD>");
	// La fonction qui attend que l'image soit chargée et affichée pour redimensionner la fenêtre à la bonne taille
	wPopup.document.write('<SCRIPT language="JavaScript">\nfunction checkSize() { if(document.images[0].complete) { w = document.images[0].width+50; h = document.images[0].height+50; window.resizeTo(w, h); window.moveTo((screen.width-w)/2, (screen.height-h)/2); document.images[0].style.visibility = "visible"; window.focus(); } else { setTimeout("checkSize()", 250); } }\n</'+'SCRIPT>');
	wPopup.document.write('<BODY bgcolor="#EFFD82" leftMargin="0" topMargin="0" marginWidth="0" marginHeight="0">');
	wPopup.document.write('<table width="100%" height="100%" align="center" cellpadding="0" cellspacing="0"><tr valign="middle"><td align="center"><img src="'+next_img.src+'" border="0" onLoad="checkSize()" onClick="window.opener.stop()" style="visibility:hidden"></td></tr></table>');
	wPopup.document.write('</BODY></HTML>');
	wPopup.document.close();
	num++;
	
	// On a passé toutes les images, on repart du début
	if(num == nb_img) num = 0;
	
	// Si "Répéter" n'est pas cochée, on stoppe le slideshow
	if(num == 0 && !window.document.settings.repeat.checked) {
		setTimeout("stop()", 1000*window.document.settings.tempo.value);
		return false;
	}
	next();
}

// Stoppe le slideshow
function stop() {
	clearTimeout(myCounter);
	wPopup.close();
	// On réactive tous les éléments du formulaire
	window.document.forms.settings.bLaunch.disabled  = false;
	focusOnLaunch();
	window.document.forms.settings.bStop.disabled    = true;
	window.document.forms.settings.repeat.disabled   = false;
	window.document.forms.settings.order[0].disabled = false;
	window.document.forms.settings.order[1].disabled = false;
	window.document.forms.settings.tempo.disabled    = false;
	num = 0;
}

// Vérification de la temporisation à chaque modification de celle-ci
function checkTempo() {
	var t = window.document.settings.tempo.value;
	if(isNaN(t) || t<0 || t>60) {
		window.document.settings.tempo.value = 3;
		alert("Mauvaise temporisation...\nEntrez un temps compris entre 0 et 60 secondes.");
		return false;
	}
}


