/****************************************************************************

	Fonction de navigation

****************************************************************************/

function lien(url, menuAColorer, nbmenu, titre) {

	// Extraction des paramètres de l'url
	var page = url.substring(0,url.indexOf("?",0));
	var args = url.substring(url.indexOf("?",0)+1, url.length);
	var fond_ecran = url.substring(0,url.indexOf(".",0));
	
	// Je modifie le titre
	changertitre(titre);
	
	// Gestion fond d'écran du div main
	if( fond_ecran == 'main' ) {
		setFondEcran('fond.grille4.jpg');
	}
	else{
		if( fond_ecran == 'accueil' ) {
			setFondEcran('fond.accueil.jpg');
		}
		else {
			setFondEcran('fond.vide.jpg');
		}
	}

	// Chargement du div main
	writedivArg(page, 'main', args);
		
	// Gestion de l'affichage du menu de gauche
	actualisercouleurMenu(menuAColorer, nbmenu);
}

function preview (nomInputAAfficher) {
	var preview = document.getElementById('preview');
	var img = document.getElementById(nomInputAAfficher).value;
	var urlImg = "<img src=\"../photos/" + img + "\" class=\"preview\" />";
	
	preview.innerHTML = urlImg;
}

/****************************************************************************

	Fonction de modification de l'affichage

****************************************************************************/

function setFondEcran(bgName) {
	// Récupération de l'élément <div id="main">
	var divMain = document.getElementById('main');
	
	// Changement du fond d'écran
	bgName = "images/"+bgName;
	if(divMain) {
		divMain.style.backgroundImage = 'url('+bgName+')';
	}
}

function actualisercouleurMenu(menuAColorer, nbmenu) {
	// Je boucle sur le nombre total de menus
	for(var i = 1; i <= nbmenu; i++) {
	
		// Construction de l'id menu
		var mn = "menu" + i.toString();;
		
		// Récupération de l'élément
		var d = document.getElementById(mn);
		
		// Si le menu courant est le menu à colorer... je change sa couleur
		if( menuAColorer == mn) {
			d.style.color = "#CCFF00";
		}
		else {	// Sinon je rétablis sa couleur d'origine
			d.style.color="";
		}
	}
}

function changertitre(nouveautitre) {
	document.title = nouveautitre;
	changerEntete(nouveautitre);
}

function changerEntete(titre) {
	var bann = document.getElementById("titre");
	if( bann ) {
		bann.innerHTML = titre;
	}
}

/***************************************************************************/

function affiche_alcove(position, page, lang) {
	
	// Gestion de "l'allumage" des photos en 4x4
	actualisercouleur4x4(position);

	// Affichage de la photo 'alcove' correspondante
	alcove(position, page, lang);	
}

function alcove(position, page, langue) {
	// Définition des arguments de la page image.php
	var args = "lang="+langue+"&position="+position+"&page="+page;
	
	// Affichage
	writedivArg("image.php","alcove",args);
}

/***************************************************************************/

function actualisercouleur4x4(position) {
	var i;
	// il faudrait trouver un moyen de ne pas écrire en dur la limite haute...
	// peut etre un while (div existe) { ... }
	for( i=1; i<=24; i++) {
		if(i==position) {
			//alert("j'affiche la photo dont la position est : " + i);
			afficher_photo_couleur(i);
		}
		else {
			//alert("je masque la photo dont la position est : " + i);
			afficher_photo_nb(i);
		}
	}
}

function afficher_photo_couleur(position) {
	// Zone a rendre invisible: nb
	var zoneNb = 'nb'+position.toString();
	//alert(zoneNb);
	var imgNb = document.getElementById(zoneNb);
	if( imgNb ) {
		//alert('Je rends invisible l element ' + zoneNb);
		imgNb.className = 'photoInv';
	}
	
	// Zone a rendre visible: couleur
	var zoneCol = 'couleur'+position.toString();
	//alert(zoneCol);
	var imgCol = document.getElementById(zoneCol);
	if( imgCol ) {
		//alert('Je rends visible l element ' + zoneCol);
		if(position <= 6) {
			imgCol.className = 'photo4x4';
		}
		else {
			imgCol.className = 'photo2x6';
		}
	}
}

function afficher_photo_nb(position) {
	// Zone a rendre invisible: couleur
	var zoneCol = 'couleur'+position;
	var imgCol = document.getElementById(zoneCol);
	if( imgCol ) {
		//alert('Je rends invisible l element ' + zoneCol);
		imgCol.className = 'photoInv';
	}
	
	// Zone a rendre visible: nb
	var zoneNb = 'nb'+position;
	var imgNb = document.getElementById(zoneNb);
	if( imgNb ) {
		//alert('Je rends visible l element ' + zoneNb);
		if(position <= 6) {
			imgNb.className = 'photo4x4';
		}
		else {
			imgNb.className = 'photo2x6';
		}
	}
}

/***************************************************************************/

function ouvregrand(nomphotogrande) {
	window.open("photos/"+nomphotogrande);
}

/****************************************************************************

	Fonction de récupération de flux html

****************************************************************************/

function file(fichier) {
	var xhr_object;
	if(window.XMLHttpRequest) // FIREFOX
		xhr_object = new XMLHttpRequest(); 
	else if(window.ActiveXObject) // IE
		xhr_object = new ActiveXObject("Microsoft.XMLHTTP"); 
	else 
		return(false);
	xhr_object.open("GET", fichier, false);
 	xhr_object.send(null); 
	if(xhr_object.readyState == 4) return(xhr_object.responseText);
	else return(false);
}

function fileArg(fichier, arguments) {
	var xhr_object;
	if(window.XMLHttpRequest) // FIREFOX
		xhr_object = new XMLHttpRequest(); 
	else if(window.ActiveXObject) // IE
		xhr_object = new ActiveXObject("Microsoft.XMLHTTP"); 
	else 
		return(false); 
	xhr_object.open("POST", fichier, false);
	xhr_object.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	xhr_object.send(arguments); 
	if(xhr_object.readyState == 4) return(xhr_object.responseText);
	else return(false);
}

function writedivArg(url, name, arguments) { // Arguments (toujours envoyé en post)
	// Récupération du flux
	var texte = fileArg(url, arguments);
	
	// Copie de ce flux dans le div
	var D = document.getElementById(name)
	if(D) { D.innerHTML = texte; }
}

function writediv(url, name) { // Pas d'argument
	// Récupération du flux
	var texte = file(url);

	// Copie de ce flux dans le div
	var D = document.getElementById(name)
	if(D) { D.innerHTML = texte; }
}