/* ----------------------------------- Fonctions générales ----------------------------------------------------- */
// Fonction qui retourne la taille de la page affichée avec la résolution d'ecran et le scrolling
function LAY_TaillePage()
{
	var xScroll, yScroll;
	if (window.innerHeight && window.scrollMaxY) 
	{	
		xScroll = document.body.scrollWidth;
		yScroll = window.innerHeight + window.scrollMaxY;
	}
	else if (document.body.scrollHeight > document.body.offsetHeight)
	{
		xScroll = document.body.scrollWidth;
		yScroll = document.body.scrollHeight;
	}
	else 
	{
		xScroll = document.body.offsetWidth;
		yScroll = document.body.offsetHeight;
	}
	var windowWidth, windowHeight;
	if (self.innerHeight) 
	{
		windowWidth = self.innerWidth;
		windowHeight = self.innerHeight;
	}
	else if (document.documentElement && document.documentElement.clientHeight) 
	{
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
	}
	else if (document.body) 
	{
		windowWidth = document.body.clientWidth;
		windowHeight = document.body.clientHeight;
	}
	// Hauteur totale < Hauteur fen. visualisation
	if(yScroll < windowHeight)
	{
		pageHeight = windowHeight;
	}
	else
	{ 
		pageHeight = yScroll;
	}
	// Largeur totale < Largeur fen. visualisation
	if(xScroll < windowWidth)
	{	
		pageWidth = windowWidth;
	}
	else 
	{
		pageWidth = xScroll;
	}
	var posScroll;
	if (self.pageYOffset) 
	{
		posScroll = self.pageYOffset;
	} 
	else if (document.documentElement.scrollTop)
	{
		posScroll = document.documentElement.scrollTop;
	} 
	else if (document.body) 
	{
		posScroll = document.body.scrollTop;
	}
	arrayTaillePage = new Array(pageWidth,pageHeight,windowWidth,windowHeight,posScroll);
//	alert("Taille page:"+pageWidth+"x"+pageHeight+"\nTaille fenetre:"+windowWidth+"x"+windowHeight);
	return arrayTaillePage;
}
// Masquage des fenetres layer
function LAY_cache_fenetre()
{
	var corps = document.getElementsByTagName('body').item(0);
	if (document.getElementById("container") && document.getElementById("fen_ville"))
		LAY_fondnoir(corps,100,2);
	else if (document.getElementById("container") && document.getElementById("fen_tourisme"))
		LAY_fondnoir(corps,100,2);
	else if (document.getElementById("fondnoir"))
		corps.removeChild(document.getElementById('fondnoir'));
	if (document.getElementById("fenetre"))
		corps.removeChild(document.getElementById("fenetre"));
	if (document.getElementById("fen_ville") && (!document.getElementById("container")))
	{
		corps.removeChild(document.getElementById("fen_ville"));
		LAY_ajout_lien_photos("ph_loc");
		clearTimeout(slide_timeout);
		diapo_en_cours = false;
	}
	if (document.getElementById("fen_tourisme") && (!document.getElementById("container")))
	{
		corps.removeChild(document.getElementById("fen_tourisme"));
		LAY_ajout_lien_photos("ph_loc");
		clearTimeout(slide_timeout);
		diapo_en_cours = false;
	}
	if (document.getElementById("deconnexion"))
	{	
		corps.removeChild(document.getElementById('deconnexion'));
		document.location.href = "http://www.gites-montagne.com";
	}

	if (document.getElementById("container"))
	{
		corps.removeChild(document.getElementById('container'));
		if (document.getElementById('legende')) 
			corps.removeChild(document.getElementById('legende')); 
		clearTimeout(slide_timeout);
		diapo_en_cours = false;
	}
}
// Creation du fond noir
function LAY_fondnoir(lebody,hauteur,zindex)
{
	if (!document.getElementById('fondnoir')) 
	{
		var fond = document.createElement('a');
		fond.setAttribute('id', 'fondnoir');
		fond.style.width = '100%';
		fond.style.height = hauteur+'px';
		if (zindex < 3) // Cas d'un sur-fond
			fond.onclick = function() { LAY_cache_fenetre(); return false; }
		else
			fond.onclick = "";
		fond.style.zIndex = zindex;
		lebody.appendChild(fond);
	}
	else
	{
		fond = document.getElementById('fondnoir');
		if (fond.style.zIndex != zindex)
			fond.style.zIndex = zindex;
		if (zindex < 3) // Cas d'un sur-fond
			fond.onclick = function() { LAY_cache_fenetre(); return false; }
		else
			fond.onclick = "";
	}
}
// Creation du bouton de fermeture
function LAY_boutonferme(fenetre,haut,droite)
{
	if (!document.getElementById('ferme')) 
	{
		var ferme = document.createElement('a');
		ferme.setAttribute('id', 'ferme');
		ferme.onclick = function() { LAY_cache_fenetre(); return false; }
		ferme.style.top = haut + 'px';
		ferme.style.right = droite + 'px';
		ferme.style.zIndex = 6;
		ferme.style.display = "block";
		fenetre.appendChild(ferme);
	}
}
/* ----------------------------------- Fonctions de création des layers ----------------------------------------------------- */
// Création de la fenetre d'affichage des photos
function LAY_new_photo(a)
{
	
	var id_photo = (a.getAttribute('class')) ? new String(a.getAttribute('class').match(new RegExp(relSerie+"[0-9]+$"))) : '';
	position_courante = parseInt(id_photo.substring(5)) + 1;
	var titre = a.getAttribute('title');
	lienImage = a.getAttribute('href');
	var page = LAY_TaillePage();
	var corps = document.getElementsByTagName('body').item(0);
	// Creation du fond ou sur-fond
	if (document.getElementById("fen_ville") || document.getElementById("fen_tourisme"))
		LAY_fondnoir(corps,page[1],4);
	else
		LAY_fondnoir(corps,page[1],2);	
	if (!document.getElementById('container')) 
	{
		//Création du container
		var container = document.createElement('div');
		container.setAttribute('id', 'container');
		corps.appendChild(container);
		//Positionnement
		container.style.top = page[4] + margin_top + 'px';
		container.style.left = (page[2] / 2) + 'px';
	}
	else
	{
		var container = document.getElementById('container');
		corps.removeChild(document.getElementById('legende'));
	}
	container.style.width = '200px';
	container.style.zIndex = 5;
	var inhtml  = '<table cellpadding="0" cellspacing="0" border="0" margin="0" padding="0" id="table_image">';
	inhtml += '<tr height="15"><td background="../../../../communs/boites/haut-gauche.gif" width="15"></td>';
	inhtml += '<td background="../../../../communs/boites/haut.gif"></td>';
	inhtml += '<td background="../../../../communs/boites/haut-droite.gif" width="15"></td>';
	inhtml += '</tr><tr>';
	inhtml += '<td background="../../../../communs/boites/gauche.gif" width="15"></td>';
	inhtml += '<td bgcolor="#FCFCFC" id="td_image"><img id="obj_image" style="display:none;" /></td>';
	inhtml += '<td background="../../../../communs/boites/droite.gif" width="15"></td>';
	inhtml += '</tr><tr height="15">';
	inhtml += '<td background="../../../../communs/boites/gauche.gif" width="15"></td>';
	inhtml += '<td bgcolor="#FCFCFC"></td>';
	inhtml += '<td background="../../../../communs/boites/droite.gif" width="15"></td>';
	inhtml += '</tr><tr height="15">';
	inhtml += '<td background="../../../../communs/boites/bas-gauche.gif" width="15"></td>';
	inhtml += '<td background="../../../../communs/boites/bas.gif"></td>';
	inhtml += '<td background="../../../../communs/boites/bas-droite.gif" width="15"></td>';
	inhtml += '</tr></table>';
	container.innerHTML = inhtml;
	container.style.top = page[4] + margin_top + 'px';
	container.style.left = parseInt((page[2] - parseInt(container.style.width))/2)+'px';
	// Création du bouton "fermer"
	LAY_boutonferme(container,20,20);
	// Création de la legende
	var legende = document.createElement('div');
	legende.setAttribute('id', 'legende');
	legende.innerHTML = '<span class="arial_16"><strong>Image '+ position_courante +' sur ' + nb_images + ' : '+titre+'</strong></span>';
	corps.appendChild(legende);
	// Création du bouton d'agrandissement
	var loupe = document.createElement('a');
	loupe.setAttribute('id', 'agrd');
	loupe.setAttribute('title', 'Agrandir la photo');
	loupe.onclick = function()	{	LAY_changeTaille(1);	}
//	legende.appendChild(loupe);
	// Création des boutons de gestion du diaporama
	clearTimeout(slide_timeout);
	if (nb_images > 1)
	{
		// Création du bouton retour
		var prec = document.createElement('a');
		prec.setAttribute('id', 'prec');
		prec.setAttribute('title', 'Image précedente');
		prec.onclick = function()	{	LAY_navigue_images(-1);	}
		legende.appendChild(prec);

		// Création du bouton suivant
		var suiv = document.createElement('a');
		suiv.setAttribute('id', 'suiv');
		suiv.setAttribute('title', 'Image suivante');
		suiv.onclick = function()	{	LAY_navigue_images(1);	}
		legende.appendChild(suiv);

		// Création du bouton diaporama
		var diap = document.createElement('a');
		if (!diapo_en_cours) {
			diap.setAttribute('id', 'diap_deb');
			diap.setAttribute('title', 'Débuter le diaporama');
		}
		else {
			diap.setAttribute('id', 'diap_fin');
			diap.setAttribute('title', 'Arrêter le diaporama');
		}
		diap.onclick = function()	{	LAY_diaporama();	}
		legende.appendChild(diap);

		// Mise en route tempo. si diaporama
		if (diapo_en_cours) { slide_timeout = window.setTimeout("LAY_navigue_images(1)", slide_timeout_value); }
	}
	legende.style.zIndex = 5;
	// Affichage de l'image
	img = new Image();
	img.src = lienImage;
	if (!img.complete)	// L'image n'est pas encore téléchargée
	{
		img.onload = function()	{	LAY_change_image();	}
	}
	else				// L'image a déjà été téléchargée
		LAY_change_image();
}

/* ----------------------------------- Fonctions de gestion de l'affichage des photos diaporama ----------------------------------------------------- */
// Creation des liens pour l'affichage des photos
function LAY_ajout_lien_photos(rel)
{
	relSerie = rel;
	nb_images = 0;
	var lien_a = document.getElementsByTagName('a');
	var a ="";
	for (i = 0; i < lien_a.length; i++) 
	{
		var b = lien_a[i];
		if (b.href != a.href) // Cas de Google Chrome
		{
			a = lien_a[i];
			var rel_attr = new String(a.getAttribute('rel'));
			if (rel_attr.match(rel)) 
			{
				a.setAttribute('class', rel + nb_images);
				a.onclick = function () 
				{
					LAY_new_photo(this,rel);
					return false;
				}
				lien_img[nb_images + 1] = a;
				nb_images++;
			}
		}
	}
}

// Fonction de navigation entre les images pour la fenetre photo
function LAY_navigue_images(sens)
{
	position_courante = parseInt(position_courante);
	if ((!sens) || (sens == 1))
	{
		position_courante = (position_courante + 1 > nb_images) ? 1 : position_courante + 1;
	}
	else
		position_courante = (position_courante - 1 < 1) ? nb_images : parseInt(position_courante) - 1;
	LAY_new_photo(lien_img[position_courante]);
}

// Gestion du diaporama
function LAY_diaporama() 
{
	if (!diapo_en_cours)
	{
		diapo_en_cours = true;
		document.getElementById('diap_deb').setAttribute('id', 'diap_fin'); 
		document.getElementById('diap_fin').setAttribute('title', 'Arrêter le diaporama');
		slide_timeout = window.setTimeout("LAY_navigue_images(1)", slide_timeout_value/4);
	}
	else 
	{
		diapo_en_cours = false;
		document.getElementById('diap_fin').setAttribute('id', 'diap_deb'); 
		document.getElementById('diap_deb').setAttribute('title', 'Débuter le diaporama');
		clearTimeout(slide_timeout);
	}
}

// Gestion du Zoom sur une photo
function LAY_changeTaille(a)
{	
	var obj_image = document.getElementById('obj_image');
	var container = document.getElementById('container');
	var legende   = document.getElementById('legende');
	var ferme   = document.getElementById('ferme');
	var td_image   = document.getElementById('td_image');
	var table_image   = document.getElementById('table_image');
	var page = LAY_TaillePage();

	if (a == 0)
	{
		// Restauration de la position dans la page
		document.body.scrollTop = scrollTop;
		document.body.scrollLeft = scrollLeft;	
		// Affichage des boutons de diaporama
		if (document.getElementById('prec'))
			document.getElementById('prec').style.display = "block";
		if (document.getElementById('suiv'))
			document.getElementById('suiv').style.display = "block";
		if (document.getElementById('diap_deb'))
			document.getElementById('diap_deb').style.display = "block";
		if (document.getElementById('diap_fin'))
			document.getElementById('diap_fin').style.display = "block";
		var btn = document.getElementById('redc');
		btn.id = 'agrd'; 
		btn.title = 'Agrandir la photo';
		btn.onclick = function() {	LAY_changeTaille(1);	}
		LAY_change_image();
		scrollTop = scrollLeft = 0;
		largeurPage = 0;
	}
	else
	{
		// Sauvegarde de la position dans la page
		scrollTop = document.body.scrollTop;
		scrollLeft = document.body.scrollLeft;
		largeurPage = page[2];
		// Arret du diaporama
		clearTimeout(slide_timeout);
		diapo_en_cours = false;
		// Masquage des boutons de navigation si nb photo > 1
		if (document.getElementById('prec'))
			document.getElementById('prec').style.display = "none";
		if (document.getElementById('suiv'))
			document.getElementById('suiv').style.display = "none";
		if (document.getElementById('diap_deb'))
			document.getElementById('diap_deb').style.display = "none";
		if (document.getElementById('diap_fin'))
		{
			document.getElementById('diap_fin').setAttribute('id', 'diap_deb'); 
			document.getElementById('diap_deb').setAttribute('title', 'Débuter le diaporama');
		}
		var btn = document.getElementById('agrd');
		btn.id = 'redc'; 
		btn.title = 'Réduire la photo';
		btn.onclick = function() {	LAY_changeTaille(0);	}
		obj_image.onclick = function() {	LAY_changeTaille(0);	}
		// Redimensionnement de la photo
		obj_image.height = img.height;
		obj_image.width = img.width;
		container.style.width = Math.max(obj_image.width + 30, 300) + "px";
		container.style.height = obj_image.height + 30 + 'px';
		container.style.top = page[4] + margin_top + 'px';
		legende.style.top = page[4] + margin_top + (parseInt(obj_image.height + 15))+'px';
		legende.style.width = parseInt(parseInt(container.style.width) - 20) + 'px';
		td_image.height = obj_image.height;
		td_image.width = obj_image.width;
		table_image.height = obj_image.height + 30 + "px;";
		table_image.width = obj_image.width + 30 + "px;";
		if (img.width > page[2])
		{
			container.style.left = 0 + 'px'
			legende.style.left = 0 + 'px';
		}
		else
		{
			container.style.left = parseInt((page[2] - parseInt(container.style.width)) / 2) + 'px';
			legende.style.left = 15 + parseInt(container.style.left) + 'px';
		}
	}
}

// Affichage d'une image à la taille adaptée à l'écran de l'utilisateur
function LAY_change_image()
{
	var obj_image = document.getElementById('obj_image');
	var container = document.getElementById('container');
	var legende   = document.getElementById('legende');
	var td_image   = document.getElementById('td_image');
	var table_image   = document.getElementById('table_image');
	// Redimensionnement de la photo
	obj_image.src = lienImage;
	obj_image.style.display = "block";
	obj_image.height = Math.min(img.height,screen.height - 300);
	obj_image.width = ((obj_image.height * img.width) / img.height);
	td_image.height = obj_image.height;
	td_image.width = obj_image.width;
	table_image.height = obj_image.height + 30 + "px;";
	table_image.width = obj_image.width + 30 + "px;";
	obj_image.onclick = function() { LAY_cache_fenetre(); return false; }
	container.style.width = Math.max(obj_image.width + 30, 300) + "px";
	container.style.height = obj_image.height + 30 + 'px';
	var page = LAY_TaillePage();
	container.style.top = page[4] + margin_top + 'px';
	if (largeurPage != 0)
	{
		container.style.left = parseInt((largeurPage - parseInt(container.style.width)) / 2) + 'px';
		legende.style.left = 15 + parseInt(container.style.left) + 'px';
	}
	else
	{
		container.style.left = parseInt((page[2] - parseInt(container.style.width))/2)+'px';
		legende.style.left = 15 + parseInt(container.style.left) + 'px';
	}
	legende.style.top = page[4] + margin_top + (parseInt(obj_image.height + 15)) + 'px';
	legende.style.width = parseInt(parseInt(container.style.width) - 20) + 'px';
}
