// JavaScript Document

var fichiers = new Array();
var legendePhoto = new Array();
var numPhoto = new Array();
var tabPins = new Array();

var afficheTips=false;
var afficheVideo=false;

var hPhoto;

var lScreen;

var refT = new Array();
var numT = new Array();
var legendeT = new Array();
var extensionT = new Array();

var iVignette=0;
var idMarque;

var cook = "";
var nPins=0;
var currentSlide = 0;
var debut=1;
var prem=true;

var photoZoom;
var fadeimages=new Array();
var slideReady=true;
var nextSlide=0;
var photoEncours=0;
var nLignes;

var transition1="progid:DXImageTransform.Microsoft.Fade(duration=0.8)";
var transition2="progid:DXImageTransform.Microsoft.GradientWipe(duration=2, Motion=forward)";
var transition3="progid:DXImageTransform.Microsoft.GradientWipe(duration=2, Motion=reverse)";

var pause=4000;

var nbMax = 0;
var largeurPhoto;
var largeurLigne=800;
var verticale = false;
var verticalePrec = false;
var agrandie = false;

var curphoto="photo0";
var zPhoto="photo0";
var oldphoto="photo1";

var opacite=1.0;

var typeNav=navigator.userAgent;
var firefox=false;
var ie=false;
var chrome=false;
var opera=false;

var dom=document.getElementById;
var preloadedimages=new Array();
var pos = 0;
var i=0;
var n=1;

var scroll=1;
var nMax=0;
var photoG;

pos=typeNav.indexOf("MSIE");
if (pos > 0) ie=true;

pos=typeNav.indexOf("Firefox");
if (pos > 0) firefox=true;

pos=typeNav.indexOf("Chrome");
if (pos > 0) chrome=true;

pos=typeNav.indexOf("Opera");
if (pos > 0) opera=true;

document.onkeydown = getKeyDn;
document.onkeyup = getKeyUp;

// fonctions pour afficher les images dans séries
function downloadedSerie() 
{
   if (photoG.complete)
   {
  	slideReady = true;
   	return true;
   }
   else 
   	setTimeout("downloadedSerie()", 50);
}

function cacheSerie() 
{
   if (typeof photoG == 'string')
	{ // On met les images en cache une seule fois
      slideReady = false;
      photoG = cacheImage(photoG);
	  downloadedSerie();
   }
   return true;
}


// fonctions pour enchaîner les photos dans index
function downloaded() 
{
   if (fadeimages[nextSlide].complete)
   {
  	slideReady = true;
  	return true;
   }
   else 
   	setTimeout("downloaded()", 50);
}

function cacheImage(imageSource) 
{ // Remplace le nom par un objet Image
	imageObj = new Image();
   	imageObj.src = imageSource;
   	return imageObj;
}


function cache() 
{
   if (typeof fadeimages[nextSlide] == 'string')
	{ // On met les images en cache une seule fois
      slideReady = false;
      fadeimages[nextSlide] = cacheImage(fadeimages[nextSlide]);
	  downloaded();
   }
   return true;
}

function noContextMenu () 
{
  return false;
}
document.onmousedown = noRightClick;
document.oncontextmenu = noContextMenu;


function fade() // Pour tous les autres
{
	opacite -= 0.05;
	document.getElementById(oldphoto).style.opacity = opacite;
	document.getElementById(curphoto).style.opacity = 1.0 - opacite;
  	if (opacite >= 0.0) 
		setTimeout("fade()", 40);
	else
	{
	document.getElementById(oldphoto).style.opacity = 0.0;
	document.getElementById(curphoto).style.opacity = 1.0;
	}
}

function prochainePhoto()
{
	if (slideReady)
	{
 		photoEncours = (photoEncours < (nbMax)) ? photoEncours+1 : 0;
 		if (photoEncours == 0) oldSlide=0; 
 		if(ie)
  		{
//	transition = (photoEncours%2 > 0) ? transition2 : transition3;
			document.getElementById("photo").style.filter=transition2;
			document.getElementById("photo").filters[0].apply();
			document.getElementById("photo").filters[0].play();
			document.getElementById("photo").src=fadeimages[photoEncours].src;
  		}
 		else // Tous les autres y compris Firefox
  		{
			oldphoto=curphoto;
  			curphoto=(curphoto=="photo0")? "photo1" : "photo0";
			document.getElementById(curphoto).src=fadeimages[photoEncours].src;
			opacite = 1.0;
			fade();
  		}
  	if (nextSlide < nbMax)
		nextSlide = photoEncours+1;
  	else
		nextSlide = 1;
  	cache();
	}
  	setTimeout("prochainePhoto()", pause);
}

 function remplace(expr,a,b) {
      var i=0
      while (i!=-1) {
         i=expr.indexOf(a,i);
         if (i>=0) {
            expr=expr.substring(0,i)+b+expr.substring(i+a.length);
            i+=b.length;
         }
      }
      return expr
   }

function afficheDescription(titre, texte)
{
	if (texte == "")
		return false;
	titre = remplace(titre, "£", "'");
	titre = remplace(titre, "§", "\"");
	texte = remplace(texte, "£", "'");
	texte = remplace(texte, "§", "\"");

	var description="<table border=1 bordercolor=#666666 cellpadding=8 cellspacing=0 bgcolor=#000000 width=700px height=200px><tr><td><table width=100% height=100% border='0'><tr height=30px valign=top><td width=100% align=center><span class=titreSerie>" + titre + "</span><br/></td></tr><tr height=150px valign=top><td><div align=justify style='width:700px' class=texteSerie>" + texte + "</div></td></tr></table></td></tr></table>";
//	var description="<img src=Images/bal.gif>";
	if (document.getElementById)
		{
			document.getElementById("texteSerie").innerHTML=description;
			document.getElementById("texteSerie").style.visibility="visible";
		}	
}
function effaceDescription()
{
	if (document.getElementById)
		{
			document.getElementById("texteSerie").innerHTML="";
			document.getElementById("texteSerie").style.visibility="hidden";
		}	
}

function initialisation() // comme le nom l'indique
{
	fadeimages[0] = cacheImage(fadeimages[0]);
//	fadeimages[1] = cacheImage(fadeimages[1]);
	
	fadeimages[photoEncours] = cacheImage(fadeimages[photoEncours]);
	fadeimages[photoEncours+1] = cacheImage(fadeimages[photoEncours+1]);
	
   	if (ie)
	{
		document.getElementById("photo").src=fadeimages[photoEncours].src;
	}
	else
	{
		document.getElementById("photo0").src=fadeimages[photoEncours].src;
		document.getElementById("photo1").src=fadeimages[photoEncours+1].src;
	}
   setTimeout("prochainePhoto()", pause);
}

function calcule()
{
	if (slideReady)
	{
		largeurPhoto =  photoG.width;
		if (photoG.height > photoG.width)
			verticale = true;
		else
			verticale = false;
		return true;
	}
	else
	   	setTimeout("calcule()", 50);

}

function calculeLargeur(photo)
{

	photoG=photo;
	cacheSerie();
	verticalePrec = verticale;
	calcule();
}

function affiche()
{
	if (slideReady)
	{
		if (ie)
		{
			curphoto = "photo"
			document.getElementById(curphoto).style.filter=transition1;
			document.getElementById(curphoto).filters[0].apply();
			document.getElementById(curphoto).filters[0].play();
			document.getElementById(curphoto).src=photoG.src;
		}
		else
		{
			oldphoto=curphoto;
  			curphoto=(curphoto=="photo0")? "photo1" : "photo0";

			document.getElementById(curphoto).src=photoG.src;
			opacite = 1.0;
			fade();
		}
		if (verticale)
		{
			if (agrandie)
			{
				document.getElementById("divPhoto").style.left="290px";
				
				if (format==1)
				{
					document.getElementById(curphoto).style.width="466px";
					document.getElementById("divPhoto").style.top="13px";
					document.getElementById(curphoto).style.height="700px";
				}
				else if (format==2)
				{
					document.getElementById(curphoto).style.width="525px";
					document.getElementById("divPhoto").style.top="13px";
					document.getElementById(curphoto).style.height="700px";
				}
				if (verticale != verticalePrec)
					document.getElementById(oldphoto).style.width="0px";
			} else
			{
				document.getElementById("divPhoto").style.top="13px";
				document.getElementById("divPhoto").style.left="290px";
				document.getElementById(curphoto).style.height="525px";
				if (format==1)
					document.getElementById(curphoto).style.width="350px";
				else if (format==2)
					document.getElementById(curphoto).style.width="393px";
			}
		}
		
		else
		{
			if (agrandie)
			{
				document.getElementById(curphoto).style.width="900px";
				document.getElementById("divPhoto").style.left="60px";
				if (format==1)
				{
					document.getElementById(curphoto).style.height="590px";
					document.getElementById("divPhoto").style.top="103px";
				}
				else if (format==2)
				{
					document.getElementById(curphoto).style.height="645px";
					document.getElementById("divPhoto").style.top="13px";
				}
				else
				{
					hPhoto = photoG.height * 1.28;
					document.getElementById(curphoto).style.height=hPhoto;
					document.getElementById(curphoto).style.width="900px";
					document.getElementById("divPhoto").style.top="200px";
				}
			}
			else // taille non agrandie
			{
			if (format==3)
					document.getElementById("divPhoto").style.top="200px";
				else
					document.getElementById("divPhoto").style.top="13px";
					
				document.getElementById("divPhoto").style.left="290px";
				document.getElementById(curphoto).style.width="700px";
				if (format==1)
					document.getElementById(curphoto).style.height="466px";
				else if (format==2)
					document.getElementById(curphoto).style.height="525px";
				else 
					document.getElementById(curphoto).style.height=photoG.height;
			}
		}
	}
	else
	   	setTimeout("affiche()", 50);
}
function afficheGrande(iV, foto, numro, legende)
{
	iVignette = iV;
// On démarque l'ancienne vignette et on marque celle qui a été cliquée
	if (currentSlide > -1)
	{
		idImageVignette = "image" + currentSlide;
		document.getElementById(idImageVignette).className="vignetteNoFocus";
	}
	idImageVignette = "image" + iVignette;
	document.getElementById(idImageVignette).className="vignetteFocus";
	
	var numer = numro + " /";
	calculeLargeur(foto);
	affiche();

// On remplace les caractères spéciaux # et @ qui ont été substitués à ' et " lors de l'enregistrement dans la base de données dans "TraitementPhotoSerie.php"
	tmp="";
/*
	for (var i=0; i<legende.length; i++)
	{
		tmp = tmp+legende.charAt(i);
		if (legende.charAt(i) == "#")
			tmp = tmp.replace("#", "'");
		if (legende.charAt(i) == "@")
			tmp = tmp.replace("@", "\"");
	}
	legende=tmp;
*/
//	alert(legende);
	legende = remplace(legende, "£", "'");
	legende = remplace(legende, "§", "\"");

	document.getElementById("zLegende").innerHTML=legende;
	document.getElementById("zCompteurG").innerHTML=numer;
	document.getElementById("zCompteurD").innerHTML=vMax;
	document.getElementById("zCompteur2G").innerHTML=numer;
	document.getElementById("zCompteur2D").innerHTML=vMax;

	currentSlide = parseInt(iVignette);
	if (!prem)
	{
		var l = fichiers.length-1;
		if (currentSlide==0 )
		{
			document.getElementById("triGauche").style.visibility="hidden";
			document.getElementById("divFgauche").style.visibility="hidden";
			document.getElementById("triGaucheGrande").style.visibility="hidden";
		}
		else
		{
			document.getElementById("triGauche").style.visibility="visible";
//			document.getElementById("divFgauche").style.visibility="visible";
			if (agrandie) document.getElementById("triGaucheGrande").style.visibility="visible";
		}
		if (currentSlide==l )
		{
			document.getElementById("triDroite").style.visibility="hidden";
			document.getElementById("divFdroite").style.visibility="hidden";
		}
		else
		{
			document.getElementById("triDroite").style.visibility="visible";
			if (agrandie) document.getElementById("divFdroite").style.visibility="visible";
		}
	}
// on affiche ou pas le pin de la photo agrandie
	idPinn = refSerie + iVignette;
/*
	if (document.getElementById(idPinn).style.visibility=="visible")
	{
		document.getElementById("idPin").style.visibility = "visible";
	}
	else
	{
		document.getElementById("idPin").style.visibility = "hidden";
	}
*/
	prem=false;
}

function scrolleBas(elem)
{
	if (elem != "0")
		document.getElementById(elem).blur();
		
	if (scroll < nLignes-4)
	{
//		document.getElementById("triHaut").style.visibility = "visible";
		scroll++;
		document.getElementById(scroll).scrollIntoView("zScroll");

	}
	if (scroll == nLignes-4)
	{
//		document.getElementById("triBas").style.visibility = "hidden";
		document.getElementById("triHaut").focus();
	}
}
function scrolleHaut(elem)
{
	if (elem != "0")
		document.getElementById(elem).blur();
	if (scroll > 1)
	{
		document.getElementById("triBas").style.visibility = "visible";
		scroll--;
		document.getElementById(scroll).scrollIntoView("zScroll");
	}
	if (scroll == 1)
	{
//		document.getElementById("triHaut").style.visibility = "hidden";
		document.getElementById("triBas").focus();
	}
}

function showSlide(elem, direction) 
{
 	if (slideReady) 
   {
		if (elem != "0")
			document.getElementById(elem).blur();
		
		nextSlide = currentSlide + direction;
		photoG = fichiers[nextSlide];
		numero = numPhoto[nextSlide];
		legende = legendePhoto[nextSlide];
		
		afficheGrande(nextSlide, photoG, numero, legende);
//    	currentSlide = nextSlide+direction;

// On met le focus sur la vignette correspondante
		idVignette = "vign" + currentSlide;
		document.getElementById(idVignette).focus();
	   
// On charge la suivante dans le sens de la flèche droite
		if (direction == 1) 
		{
			oldPhoto = photoG;
			photoG=fichiers[currentSlide+1];
			cacheSerie();
			photoG=oldPhoto;
		}
  	}
}

function getKeyDn(e)
{
	if (ie) 
		touche=String.fromCharCode(event.keyCode);
	else 
		touche=String.fromCharCode(e.which);

	hexa=touche.charCodeAt(0).toString(16);

	if (hexa == 'd')  //  <Entrée>
	{
		marqueFoto();
		return;
	}
	
	if (touche =="'") // Flèche droite 
	{
		if (currentSlide < fichiers.length)
		{
			document.getElementById("triDroite").src="Images/dOn.png";
			document.getElementById("triDroiteGrande").src="Images/dOn.png";
			showSlide("fDroite", 1);
		}
		return;
	}
	if (touche =="%") // Flèche gauche
	{
		if (currentSlide > 0)
		{
			document.getElementById("triGauche").src="Images/gOn.png";
			document.getElementById("triGaucheGrande").src="Images/gOn.png";
			showSlide("fGauche", -1);
		}
		return;
	}
	if (touche =="(") // Flèche bas 
	{
		document.getElementById("triBas").src="Images/basOn.png";
		if (currentSlide < nMax - cptMax)
			showSlide("fBas", +cptMax);
	
		return;
	}
	if (touche =="&") // Flèche haut 
	{
		document.getElementById("triHaut").src="Images/hautOn.png";
		if (currentSlide > cptMax - 1)
			showSlide("fHaut", -cptMax);
		return;
	}


	return;
}
function getKeyUp(e)
{
	if (ie) touche=String.fromCharCode(window.event.keyCode);
	else	touche=String.fromCharCode(e.which);

	if (touche =="(") // test flèche bas 
		document.getElementById("triBas").src="Images/basOff.png";
	if (touche =="&") // test flèche haut 
		document.getElementById("triHaut").src="Images/hautOff.png";

	if (touche =="'") // test flèche droite
	{
		document.getElementById("triDroite").src="Images/dOff.png";
		document.getElementById("triDroiteGrande").src="Images/dOff.png";
	}
	if (touche =="%") // test flèche gauche
	{
		document.getElementById("triGauche").src="Images/gOff.png";
		document.getElementById("triGaucheGrande").src="Images/gOff.png";
	}
	return;
}

function zoomPlus()
{
	w = parseInt(document.getElementById(photoZoom).style.width);
	h = parseInt(document.getElementById(photoZoom).style.height);
	t = parseInt(document.getElementById("divPhoto").style.top);
	l = parseInt(document.getElementById("divPhoto").style.left);

	if (!verticale) w +=20;
	else 
	{
		if (format==1) 
			w +=20;
		else if (format == 2)
			w +=19;
		else
			w +=20;
	}
	document.getElementById(photoZoom).style.width = w + "px";
	if (verticale)
	{
		h += 28;
		if (h > 700)
			h = 700;
	
	}
	else
		if (format==3)
			h += photoG.width / photoG.height + 4;
		else
			h += 12;

	document.getElementById(photoZoom).style.height = h + "px";
	
	if (!verticale)
	{
		l -= 23;
		document.getElementById("divPhoto").style.left = l + "px";
		if (format==1)
			t += 9;
		document.getElementById("divPhoto").style.top = t + "px";
	}
	if (w < wMax)
		setTimeout("zoomPlus()", 50);
	else
	{
		hPhoto = h;
//		affiche();
	}
	return;
}

function zoomMoins()
{
	w = parseInt(document.getElementById(photoZoom).style.width);
	h = parseInt(document.getElementById(photoZoom).style.height);
	t = parseInt(document.getElementById("divPhoto").style.top);
	l = parseInt(document.getElementById("divPhoto").style.left);
	
	if (!verticale) w -=20;
	else 
	{
		if (format==1) 
			w -=20;
		else if (format == 2)
			w -=19;
		else
			w -=20;
	}
	
	document.getElementById(photoZoom).style.width = w + "px";

	if (verticale)
	{
		h -= 28;
		if (h < 525) 
			h = 525;
	}
	else if (format==3)
		h -= 6;
	else
		h -= 12;

	document.getElementById(photoZoom).style.height = h + "px";
	
	if (!verticale)
	{
		l += 23;
		document.getElementById("divPhoto").style.left = l + "px";
		if (format==1)
			t -= 9;
		document.getElementById("divPhoto").style.top = t + "px";
	}
	if (w > wMin)
		setTimeout("zoomMoins()", 50);
	else 
	{
		document.getElementById("divPhoto").style.zIndex=1;
//		affiche();
	}
	return;
}


function zoom()
{
	if (ie)	
		photoZoom="photo";
	else
		photoZoom = curphoto;
	
	w = parseInt(document.getElementById(photoZoom).style.width);
	h = parseInt(document.getElementById(photoZoom).style.height);

	if (h > w)
	{
		if (format==1)
		{
			wMax = 466;
			wMin = 350;
		}
		else if (format==2)
		{
			wMax = 525;
			wMin = 393;
		}
		
		verticale = true;
	}
	else
	{
		wMax = 900;
		wMin = 700;
		verticale = false;
	}
	if (!agrandie)
	{
		zoomPlus();
		document.getElementById("imageFond").className="fondFront";
		agrandie = true;
		document.getElementById("zCompteur2D").style.visibility="visible";
		document.getElementById("zCompteur2G").style.visibility="visible";
		if (currentSlide != 0) 
			document.getElementById("triGaucheGrande").style.visibility="visible";
		document.getElementById("triDroiteGrande").style.visibility="visible";
		document.getElementById("divPhoto").style.zIndex=5;
	}
	else
	{
		zoomMoins();
		document.getElementById("imageFond").className="fondBack";
		agrandie = false;
		document.getElementById("zCompteur2D").style.visibility="hidden";
		document.getElementById("zCompteur2G").style.visibility="hidden";
		document.getElementById("triGaucheGrande").style.visibility="hidden";
		document.getElementById("triDroiteGrande").style.visibility="hidden";
	}

}

function marqueFoto()
{
	idPinn = refSerie + iVignette;
	if (document.getElementById(idPinn).style.visibility=="visible")
// suppression d'un pin du tableau
	{
		for (var i=0; i<tabPins.length; i++)
		{
			if (tabPins[i] == idPinn)
			{
				for (var j=i; j<tabPins.length-1; j++)
					tabPins[j] = tabPins[j+1];
				break;
			}
		}
		tabPins.pop();
		document.getElementById(idPinn).style.visibility="hidden";
//		document.getElementById("idPin").style.visibility="hidden";
	}
	else
// Ajout d'un pin dans le tableau
	{
		tabPins.push(idPinn);
		document.getElementById(idPinn).style.visibility="visible";
//		document.getElementById("idPin").style.visibility="visible";
	}
	var valueCookie = serialize(tabPins);
	var date = new Date();
	date.setTime(date.getTime()+(24*60*60*1000));
	var expires = "; expires="+date.toGMTString();
	document.cookie = "pinsCook"+"="+valueCookie+expires+"; path=/";
	
// On met le focus sur la vignette correspondante
	idVignette = "vign" + currentSlide;
	document.getElementById(idVignette).focus();
	
}

function serialize(tab)
{
	var result="";
	for (i=0; i< tab.length; i++)
	{
		result += tab[i];
		if (i < tab.length-1)
			result += "-";
	}
	return result;
}

function marqueVignettes()
{
	var cook = litCookie("pinsCook");
	if (cook)
	{
		tabPins = cook.split('-');
		for (var i=0; i<tabPins.length; i++)
		{
			idMarque = tabPins[i];
			if (document.getElementById(idMarque))
			{
				document.getElementById(idMarque).style.visibility="visible";
			}
		}

	}
}

function litCookie(name) 
{
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) 
	{
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function masqueTips()
{
	if (afficheTips)
	{
		document.getElementById("zTips").style.visibility="hidden";
		afficheTips=false;
	}
	else
	{
		document.getElementById("zTips").style.visibility="visible";
		afficheTips=true;
	}
}

function masqueVideo()
{
	if (afficheVideo)
	{
		afficheVideo=false;
		document.getElementById("vid").style.visibility="hidden";
//		document.getElementById("vid").settings.mute = true;
		document.getElementById("vid").controls.pause();
		
	}
	else
	{
		afficheVideo=true;
		document.getElementById("vid").style.visibility="visible";
//		document.getElementById("vid").settings.mute = false;
		document.getElementById("vid").controls.play();
	}
}

