// JavaScript Document

// Menu au format xml (fichier menu.xml)
var xmlMenu;
// Rubrique Courante
var rubriqueCourante = '';
// Liste des photos Courante tableau d'éléments xml
var listePhoto;
// Emplacement racine
var dirHome = '';
// Emplacement Sources (pages php etc...)
var dirPage = '';
// Index photo courante
var photoCouranteIndex = 0;
// Nombre d'index photo
var nbLiensIndex = 10;
// Flag audience active
var estAudienceActive = false;
// Interval photo courant 
var intervalPhotoCourant = -1;


// Récupère la rubrique dont le nom est passé en paramètre
function getRubriqueByNom(aNomRubrique) {
	var listRub = xmlMenu.getElementsByTagName("rubrique");
	var i = 0;
	while (i < listRub.length) {
		nom = getNamedChildNodeTextValue(listRub[i],"nom");
		if (nom == aNomRubrique) {
			return listRub[i];
		}
		i++;
	}
	return false;	
}

function loadMenu(homeDir) {

	var http_request = getXMLHttpRequest('GET', homeDir+'/menu.xml', false);
//    http_request.onreadystatechange = function() { alertContents(http_request); };
	http_request.send(null);

	xmlMenu = http_request.responseXML;

	var listRub = xmlMenu.getElementsByTagName("rubrique");
	var i = 0;
	var htmlMenu = "";
	var nom = "";
	var chemin = "";
	var libelle = "";
	var date = "";
	while (i < listRub.length) {
		nom = getNamedChildNodeTextValue(listRub[i],"nom");
		chemin = getNamedChildNodeTextValue(listRub[i],"chemin");
		libelle = getNamedChildNodeTextValue(listRub[i],"libelle");
		date = getNamedChildNodeTextValue(listRub[i],"date");
		htmlMenu += "<li id='"+nom+"'><a href=\"javascript:chargerRubrique('"+nom+"', 0);\">"+libelle;
		if (date != "") {
			htmlMenu += " - <em>"+date+"</em>";
		}
		htmlMenu += "</a></li>";
		i++;
	}
	document.getElementById("menuDiapo").innerHTML = htmlMenu;
}

function chargerRubrique(nomRubrique, anIndexPhoto) {
	if (rubriqueCourante == '') {
		// Premier chargement, on initialise la partie diaporama
		var http_request = getXMLHttpRequest('GET', 'cadrediapo.txt', false);
		http_request.send(null);
		document.getElementById("droite_ma").innerHTML = http_request.responseText;
	}
	if (nomRubrique != rubriqueCourante) {
		// Desélection du menu précédent
		if (rubriqueCourante != '') {
			document.getElementById(rubriqueCourante).className = "";
		}
		// Récupération des données relatives à la rubrique
		var rubNode = getRubriqueByNom(nomRubrique);
		var lPath = getNamedChildNodeTextValue(rubNode,"chemin");

		// Récupération de la liste des photos
		var http_request = getXMLHttpRequest('GET', dirHome + lPath +'/index.xml', false);
//    http_request.onreadystatechange = function() { alertContents(http_request); };
		http_request.send(null);
		var xmlPhoto = http_request.responseXML;
		
		listePhoto = xmlPhoto.getElementsByTagName("photo");
		
		
		var lLibelleNode = getNamedChildNodeTextValue(rubNode,"libelle");
		var lDateNode = getNamedChildNodeTextValue(rubNode,"date");
		if (lDateNode != '') {
			lLibelleNode += "&nbsp;-&nbsp;<em>" + lDateNode + "</em>";
		}
		if (lLibelleNode != '') {
			lLibelleNode = "&nbsp;&raquo;&nbsp;" + lLibelleNode;
		}
		// Génération du chemin
		var lCheminTxt = "Diaporama" + lLibelleNode;
						
		document.getElementById("chemin").innerHTML = lCheminTxt;
		
		// Mise en selection du menu cliqué
		document.getElementById(nomRubrique).className = "selected";
		
		// Mise à jour de la balise d'audience
		updateAudience("diapo|diaporama|"+nomRubrique);
		
		// Affectation d'un index -1 pour recharger les boutons index
		intervalPhotoCourant = -1;
		
		rubriqueCourante = nomRubrique;
	}
	chargerPhoto(anIndexPhoto);
}

function chargerPhoto(anIndexPhoto) {
	try {
		document.getElementById("p"+photoCouranteIndex).className = "";
	}
	catch (e) {}
	
	photoCouranteIndex = anIndexPhoto;

	var photoCourante = listePhoto[photoCouranteIndex];
	
	// Récupération des données relatives à la rubrique
	var rubNode = getRubriqueByNom(rubriqueCourante);
	var lPath = getNamedChildNodeTextValue(rubNode,"chemin");
//	alert(listePhoto);
	
	var nomPhoto = getNamedChildNodeTextValue(photoCourante,'nom');
	var descPhoto = getNamedChildNodeTextValue(photoCourante,'desc');
	
	var htmlDiapo = "<img src=\""+dirHome + lPath +'/' + nomPhoto+"\" alt=\""+descPhoto+"\" title=\""+descPhoto+"\"/><br/>"
	htmlDiapo += "<h4>"+descPhoto+"</h4>"
	document.getElementById("diapo").innerHTML = htmlDiapo;
	
	chargerBoutonsIndex();
	
	// On passe le bouton en mode selectionné
	document.getElementById("p"+photoCouranteIndex).className = "imsel";

	afficheBoutons();
}

function chargerBoutonsIndex() {
	lIntervalACharger = getIntervalAAfficher(photoCouranteIndex);
	var htmlCode = "";
	if (intervalPhotoCourant != lIntervalACharger) {
		for(var i=lIntervalACharger*nbLiensIndex;((i<(lIntervalACharger+1)*nbLiensIndex) && (i<listePhoto.length));i++) {
			htmlCode += "<a id=\"p"+i+"\" href=\"javascript:chargerPhoto("+i+");\" title=\"\">"+(i+1)+"</a>";
		}
		document.getElementById("photoIndex").innerHTML = htmlCode;
		intervalPhotoCourant = lIntervalACharger;
	}
}

function getIntervalAAfficher(aPhotoIndex) {
	var lIndex = aPhotoIndex;
	var lInterval = 0;
	while (lIndex > nbLiensIndex-1) {
		lIndex -= nbLiensIndex;
		lInterval++;
	}
	return lInterval;
}

function updateAudience(aNomPageAudience) {
	if (estAudienceActive) {
		document.getElementById("xiti-logo").innerHTML = genererXitiString(aNomPageAudience);
	}
}

function init(aDirHome, aDirPage, aRubrique, anIndexPhoto, audienceActive) {
	dirHome = aDirHome;
	dirPage = aDirPage;
	estAudienceActive = audienceActive;
	// Chargement du menu, mise en mémoire du fichier xml.
	loadMenu(dirHome + dirPage);
	// Mise à jour de la balise d'audience
	updateAudience("diapo|diaporama|index");
	
	if (aRubrique != '') {
		chargerRubrique(aRubrique, anIndexPhoto);
	}
}

function suiv() {
  chargerPhoto(photoCouranteIndex+1);
}

function prec() {
  chargerPhoto(photoCouranteIndex-1);
}

function isuiv() {
//  photoCouranteIndex = (intervalPhotoCourant + 1) * nbLiensIndex;
  chargerPhoto((intervalPhotoCourant + 1) * nbLiensIndex);
}

function iprec() {
//  photoCouranteIndex = (intervalPhotoCourant - 1) * nbLiensIndex;
  chargerPhoto((intervalPhotoCourant - 1) * nbLiensIndex);
}

function afficheBoutons() {
  if (photoCouranteIndex >= listePhoto.length - 1) {
  	document.getElementById("suiv").style.visibility = "hidden";
  }
  else {
  	document.getElementById("suiv").style.visibility = "visible";
  }
  if (photoCouranteIndex <= 0) {
  	document.getElementById("prec").style.visibility = "hidden";
  }
  else {
  	document.getElementById("prec").style.visibility = "visible";
  }
  // Boutons suivant index
  if (intervalPhotoCourant >=  getIntervalAAfficher(listePhoto.length - 1)) {
  	document.getElementById("isuiv").style.visibility = "hidden";
  }
  else {
  	document.getElementById("isuiv").style.visibility = "visible";
  }
  if (intervalPhotoCourant <= 0) {
  	document.getElementById("iprec").style.visibility = "hidden";
  }
  else {
  	document.getElementById("iprec").style.visibility = "visible";
  }
  
}


