var rep = "";  // Répertoire des images du diaporama (vide si meme repertoire que le fichier html)
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;
tabImgSave[0] = 'photos/L.jpg';
tabImgSave[1] = 'photos/La_Bas.jpg';
tabImgSave[2] = 'photos/Chemin.jpg';
tabImgSave[3] = 'photos/collision.jpg';
tabImgSave[4] = 'photos/Carre_bleu.jpg';
tabImgSave[5] = 'photos/rectanglerouge.jpg';
tabImgSave[6] = 'photos/Mais_encore.jpg';
tabImgSave[7] = 'photos/La_Goutte.jpg';
tabImgSave[8] = 'photos/boulerouge.jpg';
tabImgSave[9] = 'photos/boulegrise.jpg';
tabImgSave[10] = 'photos/Pierre_et_Cristal.jpg';
tabImgSave[11] = 'photos/Poussiere_de_lune.jpg';
tabImgSave[12] = 'photos/sautedhumeur.jpg';
tabImgSave[13] = 'photos/ville.jpg';
tabImgSave[14] = 'photos/pandore.jpg';
tabImgSave[15] = 'photos/croix.jpg';
tabImgSave[16] = 'photos/trace.jpg';

var tabImgTitel = new Array;
tabImgTitel[0] = 'L';
tabImgTitel[1] = 'La bas'
tabImgTitel[2] = 'Chemin';
tabImgTitel[3] = 'Collision';
tabImgTitel[4] = 'Carre bleu';
tabImgTitel[5] = 'Rectangle rouge';
tabImgTitel[6] = 'Mais encore';
tabImgTitel[7] = 'La Goutte';
tabImgTitel[8] = 'Boule rouge';
tabImgTitel[9] = 'Boule grise';
tabImgTitel[10] = 'Pierre et cristal';
tabImgTitel[11] = 'Poussiere de lune';
tabImgTitel[12] = 'Saute d humeur';
tabImgTitel[13] = 'Ville';
tabImgTitel[14] = 'Pandore';
tabImgTitel[15] = 'Croix';
tabImgTitel[16] = 'Trace';

var nb_img = tabImgSave.length;
	       

//
// Les trois fonctions suivantes (+ éventuellement, "mySplice")
// servent à mélanger un tableau quelconque à partir d'une
// permutation tirée aléatoirement
//

// Méthode "splice" si elle n'existe pas
//   tab : tableau
//   s   : début de la suppression
//   l   : nombre d'éléments à supprimer
function mySplice(s, l) {
	if(s+l > this.length) l = this.length-s;
		
	for(var i=s; i<this.length; ++i)
		this[i] = this[i+1];

	delete this[this.length-1];
	this.length--;
}

// Est-ce que la méthode "splice" est disponible ?
if(!Array.prototype.splice) {
	// Non, alors on utilise la version "maison"
	Array.prototype.splice = mySplice;
}

// 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();
}
function next() {
	// 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*3);
	}
	else {
		// Non, alors on continue d'attendre qu'elle le soit
		myCounter = setTimeout("next()", 250);
	}
}

// Lance le slideshow
function launchFirst() {
	// Petite vérification de la temporisation choisie
	//window.document.settings.tempo.value == 3;
	//if(window.document.settings.tempo.value == "") {
	//	alert("Précisez une temporisation entre 0 et 60 secondes...");
	//	return false;
	//}

	// Ordre normal ou aléatoire
	//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
	// et on active le bouton "Stopper"
		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;
	
	launch();
}

// Poursuit le slideshow
function launch() {
	// 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=50, height=50, top='+(screen.height-50)/2+', left='+(screen.width-50)/2+', status=no, directories=no, toolbar=no, location=no, menubar=no, scrollbars=no, resizable=yes');
	}
   // var nomimage = tabImgTitel(num);
   
	// On écrit le contenu de la fenêtre popup
	wPopup.document.clear();
	wPopup.document.write("<html>");
	wPopup.document.write("<head>");
	

	wPopup.document.write('<meta http-equiv="Content-Language" content="fr-be">');
	wPopup.document.write('<meta http-equiv="Content-Type" content="text/html; charset=windows-1252">');
	wPopup.document.write('<meta name="GENERATOR" content="Microsoft FrontPage 4.0">');
	wPopup.document.write('<meta name="ProgId" content="FrontPage.Editor.Document">');
	wPopup.document.write("<title>Monique Pans : "+(num+1)+"/"+nb_img+"</title>");
	wPopup.document.write('</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 && document.images[0].complete) { w = document.images[0].width+50; h = document.images[0].height+100; window.resizeTo(w, h); window.moveTo((screen.width-w)/2, (screen.height-h)/2); document.images[0].style.visibility = "visible"; window.focus(); if(opener.next_img.src != opener.rep+opener.tabImg[opener.num]) { opener.next_img.src = opener.rep+opener.tabImg[opener.num]; } } else { setTimeout("checkSize()", 250); } }\n</'+'SCRIPT>');
	
	wPopup.document.write('<body onclick="window.opener.stop()" bgcolor="#FFFFFF" 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+'" height="400" border="0" onLoad="checkSize()" onClick="window.opener.stop()" style="visibility:hidden"></td></tr>');
	wPopup.document.write('<tr><td align="center"><input type="button" onClick="window.opener.stop()"  name="bStop"  value="Quitter" style="width: 150px"></td></tr></table>');
	
	wPopup.document.write('<div align="center" style="position:absolute;top:80%;width:100%;height:10%"><table><tr><td align="center"><a style ="color:white;font-style:italic;font-family:Monotype Corsiva;font-size:30pt;">'+tabImgTitel[num]+'</a></td></tr></table></div>');	

	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;
	//}

	// En cas de répétition en mode aléatoire, on remélange les images
	//if(num == 0 && window.document.settings.order[1].checked) {
	//	tabImg = shuffleArray(tabImgSave);
	//}

	next();
}

// Stoppe le slideshow
function stop() {
	clearTimeout(myCounter);
	wPopup.close();
	// On réactive tous les éléments du formulaire
	// et on désactive le bouton "Stopper"
	//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;
	}
}
