var nb_photos,numero_photo,photo_hauteur_max,photo_largeur_max;

photo_hauteur_max="360";
photo_largeur_max="540";

// numero de la photo courante
numero_photo=1;

// nombre de photos dans la galerie
nb_photos=1;

var liste_photos=new Array();
var liste_gde_photos=new Array();
var auteurs=new Array();
var legendes=new Array();

var nom_galerie='';

var diaporama_actif=0;
var duree=3000;

var photo_petit_diapo = new Image();

function js_charger_galerie(annee,sujet,galerie,n) 
{
	var i = 0;
	var j = 1;
	var nom_fichier="000";
	var chemin = "/photos/" + annee + "/" + sujet + "/" + galerie + "/photos-dia/";
	var chemin2 = "/photos/" + annee + "/" + sujet + "/" + galerie + "/photos/";
	nb_photos = n;
	
	while (i<nb_photos)
	{
		liste_photos[i] = new Image();
		liste_photos[i].src = chemin + nom_fichier.substr(0,nom_fichier.length-j.toString(10).length) + j + ".jpg";
		liste_photos[i].alt = "Roller Skating Vitesse - Galerie : "+ nom_galerie + " - Photo : " + j + "/" + nb_photos;
		liste_photos[i].title = "Roller Skating Vitesse - Galerie : "+ nom_galerie + " - Photo : " + j + "/" + nb_photos;
		liste_gde_photos[i] = new Image();
		liste_gde_photos[i].src = chemin2 + nom_fichier.substr(0,nom_fichier.length-j.toString(10).length) + j + ".jpg";
		i++;
		j++;
	}
}

function js_premiere_photo()
{
	numero_photo=1;
	js_affiche_photo(numero_photo);
}

function js_en_avant()
{
	(numero_photo == nb_photos) ? numero_photo=1 : numero_photo=numero_photo+1;
	js_affiche_photo(numero_photo);
}

function js_en_arriere()
{
	(numero_photo==1) ? numero_photo=nb_photos : numero_photo=(numero_photo-1);
	js_affiche_photo(numero_photo);
}

function js_derniere_photo()
{
	numero_photo=nb_photos;
	js_affiche_photo(numero_photo);
}

function js_diaporama()
{
	if (diaporama_actif == 0) 
	{
		document.images['ico_diaporama'].src='/obj/images/ico_ph_fin.gif';
		document.images['ico_diaporama'].alt='Fin Diaporama';
		document.images['ico_diaporama'].title='Arrêter le Diaporama';
		diaporama_actif=1; 
		js_changer_photo();
	}
	else 
	{
		document.images['ico_diaporama'].src='/obj/images/ico_ph_deb.gif';
		document.images['ico_diaporama'].alt='Début Diaporama';
		document.images['ico_diaporama'].title='Lancer le Diaporama';
		diaporama_actif=0; 
		js_arret_diaporama();
	}
}

function js_changer_photo() 
{
	(numero_photo == nb_photos) ? numero_photo=1 : numero_photo=numero_photo+1;
	js_affiche_photo(numero_photo);
	roll=setTimeout("js_changer_photo()",duree);
}

function js_arret_diaporama() 
{
	window.clearTimeout(roll);
	document.getElementById('photo_agrandie').src=liste_photos[numero_photo-1].src;
}

function js_affiche_photo(k) 
{
	numero_photo=k;
	image=document.getElementById('photo_agrandie');
	image.alt=liste_photos[k-1].alt;
	image.title=liste_photos[k-1].title;
	image.src=liste_photos[k-1].src;
	/* Inutile si les images de photos-dia sont correctement dimensionnées
	if (liste_photos[k-1].width > photo_largeur_max || liste_photos[k-1].height > photo_hauteur_max)
	{
		if ((2*liste_photos[k-1].width) > (3*liste_photos[k-1].height))
		{
			image.width=540;
			image.height=liste_photos[k-1].height/(liste_photos[k-1].width/540);
		}
		else
		{
			image.height=360;
			image.width=liste_photos[k-1].width/(liste_photos[k-1].height/360);
		}
	}
	else
	{
		image.width=liste_photos[k-1].width;
		image.height=liste_photos[k-1].height;
	}
	*/	
	document.getElementById('photo').getElementsByTagName("tr")[0].getElementsByTagName("th")[0].getElementsByTagName("h2")[0].firstChild.nodeValue=liste_photos[k-1].title;
	document.getElementById('photo').getElementsByTagName("tr")[2].getElementsByTagName("td")[0].firstChild.nodeValue=auteurs[k-1];
	document.getElementById('photo').getElementsByTagName("tr")[3].getElementsByTagName("td")[0].firstChild.nodeValue=legendes[k-1];
	
	lien=document.getElementById('a_photo');
	lien.href=liste_gde_photos[k-1].src;
	lien.onclick= function() { openbox(liste_gde_photos[k-1].src,liste_gde_photos[k-1].width,liste_gde_photos[k-1].height);return false;};
		
	lien=document.getElementById('photo_agrandie');
	lien.src=liste_photos[k-1].src;
	
}

/* Pour tester la configuration de l'internaute */
function js_javascript_actif() 
{
	document.write("<p>Actuellement, sur votre navigateur, Javascript est activé.</p>");
}

/* Pour imprimer par un bouton du site */
function js_imprimer() 
{
	if (window.print) {window.print();}
}

function js_proposer(bouton) 
{
	switch (bouton) 
	{
		case "Imprimer":
			document.write("<div class='imprimer_droite'><a href='#' title='Imprimer' onclick='javascript:js_imprimer(); return false;'><img src='/obj/images/ico_imprimer.gif' alt='Imprimer' title='Imprimer la partie centrale de la page'/></a></div>");
			break;
		case "Diaporama":
			document.write("<div id='boutons_galerie'>");
			document.write('<a href="#photo" title="Première" onclick="javascript:js_premiere_photo();"><img src="/obj/images/ico_ph_premier.gif" alt="Première" title="Première photo de la galerie"/></a>');
			document.write('<a href="#photo" title="Précédent" onclick="javascript:js_en_arriere();"><img src="/obj/images/ico_ph_precedent.gif" alt="Précédente" title="Photo précédente"/></a>');
			document.write('<a href="#photo" title="Diaporama" onclick="javascript:js_diaporama();"><img id="ico_diaporama" src="/obj/images/ico_ph_deb.gif" alt="Début Diaporama" title="Lancer le diaporama"/></a>');
			document.write('<a href="#photo" title="Suivant" onclick="javascript:js_en_avant();"><img src="/obj/images/ico_ph_suivant.gif" alt="Suivante" title="Photo suivante"/></a>');
			document.write('<a href="#photo" title="Dernière" onclick="javascript:js_derniere_photo();"><img src="/obj/images/ico_ph_dernier.gif" alt="Dernière" title="Dernière photo de la galerie"/></a>');
			document.write('</div>');
			document.write('<div id="aide_galerie"><a href="/aide/roller-skating/2-photos.html" title="Aide" ><img src="/obj/images/ico_aide.gif" alt="Aide" title="Aide"/></a></div>');
			break;
		default: 
			alert("Erreur fonction Proposer(bouton) : paramètre non prévu."); 
			break;
	}
}

function js_mot_majuscule(mot) 
{
      var m = mot.charAt(0).toUpperCase() + mot.substring(1).toLowerCase();
      return m;
}

// Validation des formulaires
// - formulaire de contact : voir nous-contacter.php
function js_form_valider(type_form,form) 
{
	switch (type_form) 
	{
		case "nous-contacter":
			if (js_pas_vide(form,'nom','nom_alert','Nom')) {
				if (js_pas_vide(form,'email','email_alert', 'Email')) {
					if (js_email_valide(form,'email','email_alert')) {
						if (js_pas_vide(form,'objet_mess','objet_mess_alert','Objet du message')) {
							if (js_pas_vide(form,'texte_mess','texte_mess_alert','Message')) {
								return true;
							}
						}
					}
				}
			}
			return false;
			break;
		
		case "sondage-3":
			if (js_pas_vide(form,'reponse1','reponse_alert','Question 1')) {
				if (js_pas_vide(form,'reponse2','reponse_alert','Question 2')) {
					return true;
				}
			}
			return false;
			break;
		
		default: 
			alert("Erreur fonction js_form_valider(type_form,form) : paramètre non prévu."); 
			break;
	}
}

function js_pas_vide(form,elemName,alertName,nomZone) 
{
	var elem = form.elements[elemName];
	// on enleve les espaces
	elem.value = elem.value.trim();
	// on efface le message d'alerte
	mess_alert = document.getElementById(alertName);
	while(mess_alert.firstChild != null) 
	{
		mess_alert.removeChild(mess_alert.firstChild);
	}
	if (elem.value.length == 0) 
	{
		// si la zone est vide on affiche le message et on donne le focus 
		var texte = document.createTextNode("La zone "+nomZone+" est vide.");
		mess_alert.appendChild(texte);
		document.getElementById(elemName).focus();
		document.getElementById(elemName).select();
		document.getElementById(elemName).className='alerte';
		return false;
	} 
	else 
	{
		var texte = document.createTextNode(" ");
		mess_alert.appendChild(texte);
		return true;
    }
}

// Vérifie si l'adresse e-mail est valide
function js_email_valide(form,elemName,alertName) 
{
	var elem = form.elements[elemName];
	var str = elem.value;
    var re = /^[\w-]+(\.[\w-]+)*@([\w-]+\.)+[a-zA-Z]{2,7}$/;
	
	mess_alert = document.getElementById(alertName);
	while(mess_alert.firstChild != null) 
	{
		mess_alert.removeChild(mess_alert.firstChild);
	}
    
	if (!str.match(re)) 
	{
		var texte = document.createTextNode("Le format de l'adresse mail n'est pas valide.");
		mess_alert.appendChild(texte);
		document.getElementById(elemName).focus();
		document.getElementById(elemName).select();
		document.getElementById(elemName).className='alerte';
		return false;
	} 
	else 
	{
		var texte = document.createTextNode(" ");
		mess_alert.appendChild(texte);
		return true;
    }
}	
		
String.prototype.trim = function () 
{
   return this.replace(/^\s*|\s*$/,"");
}

String.prototype.simplify= function () 
{
   return this.replace(/\s+/g," ");
}

function createXHR() 
{
    var request = false;
    try {
            request = new ActiveXObject('Msxml2.XMLHTTP');
        }
    catch (err2) {
        try {
                request = new ActiveXObject('Microsoft.XMLHTTP');
            }
		catch (err3) {
			try {
				request = new XMLHttpRequest();
				}
				catch (err1) 
				{
					request = false;
				}
            }
        }
    return request;
}

function openbox(url,px_l,px_h)
{
	var box = document.getElementById('box'); 
	document.getElementById('sel_galerie').style.display='none';
	
	var content = document.getElementById('boxcontent');
	content.style.padding="0";
	
	box.style.heigth=px_h+'px';
	box.style.width=px_l+'px';
	box.style.marginLeft='-'+px_l/2+'px'; 
	content.innerHTML = "<img src=" + url + " alt='' title='Cliquer sur la photo pour revenir sur la galerie'/>";
	box.style.display='block';	
}	

function closebox()
{
   document.getElementById('box').style.display='none';
   document.getElementById('sel_galerie').style.display='block';
}

function js_lancer_petit_diapo() 
{
	setInterval("js_petit_diaporama()", duree);
}

function js_petit_diaporama() 
{
	(numero_photo == nb_photos) ? numero_photo=1 : numero_photo=numero_photo+1;
	photo_petit_diapo.src = liste_diapos[numero_photo-1];
	photo_petit_diapo.alt = "";
	photo_petit_diapo.title = "";
	document.getElementById('photo_diaporama').src=photo_petit_diapo.src;
	
}

