// JavaScript Document


/*******************************************/
/* NOMS DES REPERTOIRES ET FICHIERS IMAGES */
/*******************************************/
var theGalleryDirectory  = "img/page_photos"; /* Ne pas mettre de '/' a la fin */
var theGalleryPrefix     = "galerie_";
var thePreviewPrefix     = "vignette_";
var thePhotoPrefix       = "photo_";
var theImageExtension    = ".jpg";

/*************************/
/* NOM DU CONTAINER HTML */
/*************************/
var theContainerIdPrefix = "photo_";

/******************************************/
/* NOMBRE DE GALERIES ET GALERIE COURANTE */
/******************************************/
var theGalleryNumber     = 4;
var theGalleryIndex      = 1; /* Demarre a 1 */

/********************************************/
/* NOMBRE DE VIGNETTES ET VIGNETTE COURANTE */
/********************************************/
var theContainerNumber   = 6;
var thePhotoIndex        = 1; /* Demarre a 1 */


/***********************/
/* TITRES DES GALERIES */
/***********************/
var theGalleryTitleArray =
new Array(
	/* GALERIE 1 =>*/ "Le train d'essais",
	/* GALERIE 2 =>*/	"Voiture Mauzin",
	/* GALERIE 3 =>*/ "Signalisation",
	/* GALERIE 4 =>*/ "Instruments de mesure",
	/* GALERIE 5 =>*/ "Galerie 5",
	/* GALERIE 6 =>*/ "Galerie 6"
);

var thePhotoTitleArray = 
new Array(
	/* GALERIE 1 =>*/[
		/* PHOTO 1 => */ "Vue du train d’essais sur la LGV Est européenne. Novembre 2006",
		/* PHOTO 2 => */ "Vue intérieure du wagon laboratoire du train d’essais",
		/* PHOTO 3 => */ "Vue intérieure du wagon laboratoire du train d’essais",
		/* PHOTO 4 => */ "Vue du train d’essais sur la LGV Est européenne. Beuvardes",
		/* PHOTO 5 => */ "Vue du train d’essais sur la LGV Est européenne. Novembre 2006",
		/* PHOTO 6 => */ "Vue intérieure de la cabine de pilotage du nouveau TGV Est"
									  ],
	/* GALERIE 2 =>*/[
		/* PHOTO 1 => */ "La voiture Mauzin et ses capteurs. Base d'Ocquerre. Juin 2006",
		/* PHOTO 2 => */ "Contrôle de la qualité de la voie",
		/* PHOTO 3 => */ "Graphes obtenus par la voiture Mauzin",
		/* PHOTO 4 => */ "Opérateur montrant les capteurs sous la voiture Mauzin",
		/* PHOTO 5 => */ "Voiture Mauzin. Base d'Ocquerre. Juin 2006",
		/* PHOTO 6 => */ "Traceurs embarqués dans la voiture Mauzin"
									  ],
	/* GALERIE 3 =>*/[
		/* PHOTO 1 => */ "Signalisation à Ocquerre",
		/* PHOTO 2 => */ "Boîtier de signalisation hors voie",
		/* PHOTO 3 => */ "Dispositifs de contrôle-commande",
		/* PHOTO 4 => */ "Jalons et repères sur la voie",
		/* PHOTO 5 => */ "Contrôle de la signalisation",
		/* PHOTO 6 => */ "Dispositifs de signalisation"
									  ],
	/* GALERIE 4 =>*/[
		/* PHOTO 1 => */ "Vue du pentographe et détection des arcs électriques",
		/* PHOTO 2 => */ "Vérification de la linéarité, de la vitesse et des caténaires",
		/* PHOTO 3 => */ "Divers équipements de mesure",
		/* PHOTO 4 => */ "Divers équipements de mesure",
		/* PHOTO 5 => */ "Divers équipements de mesure",
		/* PHOTO 6 => */ "Divers équipements de mesure"
									  ],
	/* GALERIE 5 =>*/[
		/* PHOTO 1 => */ "Photo 1",
		/* PHOTO 2 => */ "Photo 2",
		/* PHOTO 3 => */ "Photo 3",
		/* PHOTO 4 => */ "Photo 4",
		/* PHOTO 5 => */ "Photo 5",
		/* PHOTO 6 => */ "Photo 6"
									  ],
	/* GALERIE 6 =>*/[
		/* PHOTO 1 => */ "Photo 1",
		/* PHOTO 2 => */ "Photo 2",
		/* PHOTO 3 => */ "Photo 3",
		/* PHOTO 4 => */ "Photo 4",
		/* PHOTO 5 => */ "Photo 5",
		/* PHOTO 6 => */ "Photo 6"
									  ]
);
	

/********************************************************
go_to_gallery : permet d'aller a la galerie precedente ou suivante

Entree:
	pWhichWay (int) : -1=vers l'arriere, 1=vers l'avant

Sortie:
	
********************************************************/
function go_to_gallery(pWhichWay)
{
	if (pWhichWay == -1)
	{
		theGalleryIndex--;
		if (theGalleryIndex < 1)
			theGalleryIndex = theGalleryNumber;
		thePhotoIndex = 1;
		refresh_gallery();
	}
	else if(pWhichWay == 1)
	{
		theGalleryIndex++;
		if (theGalleryIndex > theGalleryNumber)
			theGalleryIndex = 1;
		thePhotoIndex = 1;
		refresh_gallery();
	}
}

/********************************************************
go_to_image : permet de selectionner une photo dans la galerie

Entree:
	pIndex (int) --> indice de la photo selectionnee

Sortie:
	
********************************************************/
function go_to_image(pIndex)
{
	if (1 <= pIndex && pIndex <= theContainerNumber)
	{
		thePhotoIndex = pIndex;
		refresh_gallery();
	}
}

/********************************************************
refresh_gallery : permet de mettre a jour l'ensemble {titre, vignettes, photo)

Entree:
	

Sortie:
	
********************************************************/
function refresh_gallery()
{
	/* Rafraichit le titre de la galerie */
	load_gallery_title();
	
	/* Rafraichit les photos de la galerie */
	load_gallery_previews();
	
	/* Rafraichit la photo associee a la vignette selectionnee */
	load_main_photo();
}

/********************************************************
load_gallery_title : permet de mettre a jour le titre de la galerie

Entree:
	

Sortie:
	
********************************************************/
function load_gallery_title()
{
	var a_TD_Object = this.document.getElementById("titre_galerie");
	delete_all_children(a_TD_Object);
	var aTextNode = this.document.createTextNode(theGalleryTitleArray[theGalleryIndex-1]);
	a_TD_Object.appendChild(aTextNode);
}

/********************************************************
load_gallery_previews : permet de mettre a jour les vignettes de la galerie

Entree:
	

Sortie:
	
********************************************************/
function load_gallery_previews()
{
	for (var i = 1; i <= theContainerNumber; i++)
		load_preview(i);
}

/********************************************************
load_main_photo : met a jour la photo principale ainsi que le titre de la photo

Entree:
	

Sortie:
	
********************************************************/
function load_main_photo()
{
	var a_H2_Object = this.document.getElementById("titre_photo_pour_js");
	delete_all_children(a_H2_Object);
	
	var aH2InnerHTML = thePhotoTitleArray[theGalleryIndex-1][thePhotoIndex-1];
	a_H2_Object.innerHTML = aH2InnerHTML
	
	/*var aTextNode = this.document.createTextNode(thePhotoTitleArray[theGalleryIndex-1][thePhotoIndex-1]);
	a_H2_Object.appendChild(aTextNode);*/
	
	var aText = remove_tags_in_string(thePhotoTitleArray[theGalleryIndex-1][thePhotoIndex-1]);
	var aSrc  = theGalleryDirectory + "/" + theGalleryPrefix + print_number(theGalleryIndex,2)
						+ "/" + thePhotoPrefix + print_number(thePhotoIndex,2) + theImageExtension;
	
	var a_IMG_Object = this.document.getElementById("photo_large_pour_js");
	a_IMG_Object.alt = aText;
	a_IMG_Object.src = aSrc;
	a_IMG_Object.title = aText;
}

/********************************************************
load_preview : met a jour la vignette specifiee

Entree:
	pIndex (int) --> indice de la vignette

Sortie:
	
********************************************************/
function load_preview(pIndex)
{
	var a_TD_ContainerName = theContainerIdPrefix + print_number(pIndex,2);
	var a_TD_Container = this.document.getElementById(a_TD_ContainerName);
	delete_all_children(a_TD_Container);
	
	/* On veut obtenir l'équivalent de la ligne HTML suivante:
	<a href="#" onclick="go_to_image(xx)"><img alt="xx" src="xx" galleryimg="no" /></a> */
	var aText = remove_tags_in_string(thePhotoTitleArray[theGalleryIndex-1][pIndex-1]);
	var aSrc  = theGalleryDirectory + "/" + theGalleryPrefix + print_number(theGalleryIndex,2)
						+ "/" + thePreviewPrefix + print_number(pIndex,2) + theImageExtension;
	
	if (pIndex != thePhotoIndex)
	{
		var a_A_Object = this.document.createElement("a");
		a_A_Object.href = "#";
		a_A_Object.onclick = function() {go_to_image(pIndex);}
		a_TD_Container.appendChild(a_A_Object);
	
		var a_IMG_Object = this.document.createElement("img");
		a_IMG_Object.alt = aText;
		a_IMG_Object.src = aSrc;
		a_IMG_Object.title = aText;
		a_IMG_Object.setAttribute("galleryimg", "no");
		a_A_Object.appendChild(a_IMG_Object);
	}
	else
	{
		var a_IMG_Object = this.document.createElement("img");
		a_IMG_Object.className = "courant";
		a_IMG_Object.alt = aText;
		a_IMG_Object.src = aSrc;
		a_IMG_Object.title = aText;
		a_IMG_Object.setAttribute("galleryimg", "no");
		a_TD_Container.appendChild(a_IMG_Object);
	}
}

/********************************************************
print_number : convertit un entier positif vers une chaine de caracteres

Entree:
	pNumber (int) --> entier positif a convertir
	pDigitsNumber (int) --> nombre minimal de chiffres (left-zero-padding)

Sortie:
	
********************************************************/
function print_number(pNumber, pDigitsNumber)
{
	
	var aString = "";
	aString += pNumber;
	var aLength = aString.length;
	for (var i = 1; i <= pDigitsNumber-aLength; i++)
		aString = "0" + aString;
	return aString;
}

/********************************************************
delete_all_children : detruit tous les fils d'un objet HTML

Entree:
	pObject --> objet HTML specifie

Sortie:
	
********************************************************/
function delete_all_children(pObject)
{
	while (pObject.childNodes.length > 0)
		pObject.removeChild(pObject.childNodes[0]);
}

/********************************************************
remove_tags_in_string : supprime toutes les balises HTML d'une chaine de caracteres

Entree:
	pString --> chaine de caracteres originale

Sortie:
	aPureString --> chaine de caracteres nettoyee
	
********************************************************/
function remove_tags_in_string(pString)
{
	var aRegExp = new RegExp("<{1}[^<>]*>{1}", "g");
	return pString.replace(aRegExp, "");
}
