//variables
//images
var running=false;
var pictCurrent=1;//valeur defaut photo en cours
var pictFirst=1;//valeur defaut premiere photo
var time=500;// minuterie 1/2 seconde
var slide=0;//valeur défaut photo diaporama
var rollSlide;//setTimeout
var xlast=0;//dernier gif animé du random 
var xflast=0;//dernier animFond du random
//news
var start=0;//valeur départ new
var currentNew=0;//new en cours
var nextNew;//new suivante
var timerFade=1;//timer fading
var fading=false;//fading en cours
var news;//timer news
var timerNews=11000;//changement news 10 secondes
var opacite=0;//opacité 0 à 100;
// gif animé  entre chaque photo
var animFond=new Array();
animFond[0]=new Array(Array);
animFond[0][0]="images/animFond1.gif";
animFond[0][1]="images/animFond2.gif";
animFond[0][2]="images/animFond3.gif";
animFond[0][3]="images/animFond4.gif";
animFond[0][4]="images/animFond5.gif";
//gif animé 
var anim=new Array(Array); 
anim[0]=new Array(Array);
anim[0][0]="images/blank.gif";
anim[1]=new Array(Array);
anim[1][0]="images/papillon1.gif";
anim[1][1]="images/papillon2.gif";
anim[1][2]="images/papillon3.gif";
anim[2]=new Array(Array);
anim[2][0]="images/papillonRond.gif";
anim[3]=new Array(Array);
anim[3][0]="images/araignee.gif";
anim[4]=new Array(Array);
anim[4][0]="images/troisPapillons1.gif";
anim[4][1]="images/troisPapillons2.gif";
anim[4][2]="images/troisPapillons3.gif";
anim[5]=new Array(Array);
anim[5][0]="images/libellule.gif";
anim[6]=new Array(Array);
anim[6][0]="images/papillonExotique.gif";

//news fadingDown
function fadingDown(val) {
	if (opacite > 10) {
		fading=true;
		fade1=document.getElementById("new"+val).style;
		fade1.opacity=(opacite/100);
		fade1.MozOpacity=(opacite/100);
		fade1.KhtmlOpacity=(opacite/100);
		fade1.filter="alpha(opacity=" + opacite + ")";
		fade2=document.getElementById("infosFond").style;
		fade2.opacity=(opacite/100);
		fade2.MozOpacity=(opacite/100);
		fade2.KhtmlOpacity=(opacite/100);
		fade2.filter="alpha(opacity=" + opacite + ")";
		//opacite--;
		opacite=opacite-3;
		setTimeout(function(){fadingDown(val)}, timerFade);
	}
	else {
		//transition news
		document.getElementById("new"+val).style.display="none";
		document.getElementById("new"+nextNew).style.display="block";
		fadingUp(nextNew);
	}
}

//news fadingUp
function fadingUp(val) {
	if (opacite < 100) {
		fade1=document.getElementById("new"+val).style;
		fade1.opacity=(opacite/100);
		fade1.MozOpacity=(opacite/100);
		fade1.KhtmlOpacity=(opacite/100);
		fade1.filter="alpha(opacity=" + opacite + ")";
		fade2=document.getElementById("infosFond").style;
		fade2.opacity=(opacite/100);
		fade2.MozOpacity=(opacite/100);
		fade2.KhtmlOpacity=(opacite/100);
		fade2.filter="alpha(opacity=" + opacite + ")";
		//opacite++;
		opacite=opacite+3;
		setTimeout(function(){fadingUp(val)}, timerFade);
	}
	else fading=false;
}

//affiche news
function showNews() {
	if (nbNews >= 1) {
		if (running) setTimeout(function(){showNews()}, 100);
		else {
			if (currentNew < nbNews) { currentNew++; }
			else { currentNew=1; }
			if (currentNew == nbNews) { nextNew=1; }
			else { nextNew=currentNew+1; }
			if (fading == false) fadingDown(currentNew);
			setTimeout(function(){showNews()}, timerNews);
		}
	}
}

//début news
function startNews() {
	if (nbNews >= 1) {
		nextNew=1;
		start=1;
		document.getElementById("new1").style.display = "block";
		document.getElementById("infosFond").style.display = "block";
		if (nbNews > 1) setTimeout(function(){showNews()}, timerNews);
	}
}

//animation gif animé
function animation(val,num,top,left) {
	if (anim[num].length > 1) {
		x=Math.floor(Math.random()*anim[num].length);
		while (x==xlast) {
			x=Math.floor(Math.random()*anim[num].length);
		}
		xlast=x;
	}
	else x=0;
	if (val == "animfleur1") valAnim=1;
	else valAnim=2;
	document.getElementById(val).src="images/blank.gif";
	document.getElementById("animationfleur"+valAnim).style.top=top+"px";
	document.getElementById("animationfleur"+valAnim).style.left=left+"px";
	document.getElementById(val).src=anim[num][x];
}

//image précédente
function prevPict(val) {
	if (!running) {
		if (fading) setTimeout(function(){prevPict()}, 100);
		else {
			running=true;
			//stop diaporama onclick previous
			if (val == 1 && slide == 1) autoSlide();
			//transition image
			document.getElementById("pict"+pictCurrent).style.display="none";//masque image et texte
			xf=Math.floor(Math.random()*animFond[0].length);//animfond aléatoire
			while (xf==xflast) {
				xf=Math.floor(Math.random()*animFond[0].length);
			}
			xflast=xf;
			document.getElementById("imgAnimFond").src=animFond[0][xf];
			//gestion image précédente
			if (pictCurrent == pictFirst) valshow="showPict(nbData)";
			else valshow="showPict(pictCurrent-1)";
			setTimeout(valshow, time);//fade entre chaque image
		}
	}
}

//image suivante
function nextPict(val) {
	if (!running) {
		if (fading) setTimeout(function(){nextPict()}, 100);
		else {
			running=true;
			//stop diaporama onclick next
			if (val == 1 && slide == 1) autoSlide();
			//transition image
			document.getElementById("pict"+pictCurrent).style.display="none";//masque image et texte
			xf=Math.floor(Math.random()*animFond[0].length);//animfond aléatoire
			while (xf==xflast) {
				xf=Math.floor(Math.random()*animFond[0].length);
			}
			xflast=xf;
			document.getElementById("imgAnimFond").src=animFond[0][xf];//image animfond
			//gestion image suivante
			if (pictCurrent == nbData) valshow="showPict(1)";
			else valshow="showPict(pictCurrent+1)";
			setTimeout(valshow, time);//fade entre chaque image
		}
	}
}

//affiche image
function showPict(xpict) {
	//gestion image en cours
	if (document.getElementById("pict"+xpict)) {
		document.getElementById("imgAnimFond").src="";
		document.getElementById("pict"+xpict).style.display="block";//affiche image et texte
		document.getElementById("number"+pictCurrent).className="";//couleur lien précédent
		document.getElementById("number"+xpict).className="active";//couleur lien en cours
		pictCurrent=xpict;
		running=false;
	}
}

//animation aléatoire entre chaque image
function number(val) {
	//gestion numero image
	if (val != pictCurrent) {
		x=val;
		if (!running) {
			running=true;
			//stop diaporama
			if (slide == 1) autoSlide();
			document.getElementById("pict"+pictCurrent).style.display="none";//transition image
			xf=Math.floor(Math.random()*animFond[0].length);//animfond aléatoire
			while (xf == xflast) {
				xf=Math.floor(Math.random()*animFond[0].length);
			}
			xflast=xf;
			document.getElementById("imgAnimFond").src=animFond[0][xf];//image animfond
			valshow="showPict(x)";//gestion image
			setTimeout(valshow, time);//fade entre chaque image
		}
	}
}

//début diaporama chargement page
function autoStartSlideShow() {
	pictCurrent=nbData;
	document.getElementById("animFond").style.zIndex=0;
	autoSlide();
}

//diaporama
function autoSlide() {
	//enchainement 
	if (slide == 0) {
		slide=1;
		if (nbData > 1) {
			slideShow();
			document.getElementById('player').src="images/buttonOff.gif";//player off
		}
		else nextPict();
	}
	//pas d'enchainement
	else {
		slide=0; stopSlideShow();
		if (nbData > 1) {
			document.getElementById('player').src="images/buttonPlay.gif";//player on
		}
	}
}

//début diaporama
function slideShow() {
	nextPict(); 
	rollSlide=setTimeout("slideShow()", time+7000);
}

//fin diaporama
function stopSlideShow() {
	window.clearTimeout(rollSlide);
}

// affichage div
function show(val) {
	document.getElementById(val).style.display="block";
}

//masque div
function hide(val) {
	document.getElementById(val).style.display="none";
}
