// JavaScript Document

var coef = 0.05 ; // avancement de l'opacitŽ
var temps = 50 ; // temps entre chaque changement d'opacitŽ
var temps_pause = 4000 ; // temps d'attente entre 2 changements d'images
var nombre_image; // nombre d'images a faire bouger
var prefix_image; // chemin + prefix du nom des images
var suffix_image = '.jpg' ; // suffix + extension du nom des images
var status = 1;
var direction = 1;

var indice = 1; // les 2 premieres images sont deja chargees dans le HTML, on commence a la 3eme
var isIE = navigator.userAgent.toLowerCase().indexOf('msie')!=-1;
var isSafari = navigator.userAgent.toLowerCase().indexOf('safari')!=-1;
var isFF = (navigator.userAgent.toLowerCase().indexOf('mozilla')!=-1)&&(!isSafari);
var image1 = null;
var image2 = null ;
var sens = 1;
var tabImg;  // tab contenant les images
var opacity1 = 0 ;
var opacity2 = 0 ;

var timer1, timer2, timerOp, timerStart;

var cpte=0;

function prechargerImg(){
  tabImg = new Array(nombre_image);
  for (i=0; i<=nombre_image -1; i++){
	tabImg[i]=new Image();
	tabImg[i].src = prefix_image+(i+1)+suffix_image;
  }
}

function newSlide(gal, nb, color)
{
	nombre_image = nb;
	prefix_image = 'site/photos/' + gal + 'photo';
	
	document.write('<img id="defilement1" src="' + prefix_image + '2.jpg" style="opacity: 0;-moz-opacity:0;filter:alpha(opacity=0);position:absolute;"><img id="defilement2" src="' + prefix_image + '1.jpg" style="opacity: 1;-moz-opacity:1;filter:alpha(opacity=100);position:relative;" /><div id="controller"><div id="play"><a href="javascript:;" onclick="playPause();">||</a></div><div id="prevNext"><a href="javascript:;" onclick="change(-1);"><</a>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span id="courrant">1</span>&nbsp;/&nbsp;'+ nombre_image + '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<a href="javascript:;" onclick="change(1);">></a></div><div id="nothing"></div></div>');
	
	image1 = document.getElementById("defilement1") ;
	image2 = document.getElementById("defilement2") ;

	document.getElementById("controller").style.color = color;
	
	prechargerImg();
	timerStart = window.setTimeout("change_opacity(image1, image2)",temps_pause);
	
	if(gal != 'sensibiliser/' && document.getElementById('diaporama').className == "hide" && status == 1){
		playPause();
	}
}

function change_opacity(img1, img2)
{	

	if(document.getElementById('diaporama').className == "hide") playPause();
	
	if (isIE)  // for IE
	{	opacity1 = parseFloat(img1.filters.alpha.opacity);
		opacity2 = parseFloat(img2.filters.alpha.opacity);
	}
	else       // for mozilla
	{	
		if(isFF)
		{
			opacity1 = parseFloat(img1.style.getPropertyValue("opacity"));
			opacity2 = parseFloat(img2.style.getPropertyValue("opacity"));
		}
		else//for safari
		{
			opacity1 = parseFloat(img1.style.KhtmlOpacity);
			opacity2 = parseFloat(img2.style.KhtmlOpacity);
		}
	}

	if (sens)
	{	if (isIE)  // for IE
		{	img1.filters.alpha.opacity = opacity1 + coef * 100;
			img2.filters.alpha.opacity = opacity2 - coef * 100;
		}
		else       // for mozilla
		{	
			if(isFF)
			{
				img1.style.setProperty("opacity", opacity1 + coef, "");
				img2.style.setProperty("opacity", opacity2 - coef, "");
			}
			else//for safari
			{
				img1.style.KhtmlOpacity = opacity1 + coef;
				img2.style.KhtmlOpacity = opacity2 - coef;
			}
		}
	}
	else
	{
		if (isIE)  // for IE
		{	img1.filters.alpha.opacity = opacity1 - coef * 100;
			img2.filters.alpha.opacity = opacity2 + coef * 100;
		}
		else       // for mozilla
		{	
			if(isFF)
			{
				img1.style.setProperty("opacity", opacity1 - coef, "");
				img2.style.setProperty("opacity", opacity2 + coef, "");
			}
			else //for safari
			{
				img1.style.KhtmlOpacity = opacity1 - coef;
				img2.style.KhtmlOpacity = opacity2 + coef;
			}
		}
	}

	if (isIE)  // for IE
	{	opacity1 = parseFloat(img1.filters.alpha.opacity);
		opacity2 = parseFloat(img2.filters.alpha.opacity);
	}
	else       // for mozilla
	{	
		if(isFF)
		{
			opacity1 = parseFloat(img1.style.getPropertyValue("opacity"));
			opacity2 = parseFloat(img2.style.getPropertyValue("opacity"));
		}
		else//for safari
		{
			opacity1 = parseFloat(img1.style.KhtmlOpacity);
			opacity2 = parseFloat(img2.style.KhtmlOpacity);
		}
		
	}
	
	// on fait varier le sens d'opacitŽ
	if (opacity2 <= 0)
	{	
		indice += direction;
		document.getElementById('courrant').innerHTML = indice;
		this.refresh();
		if (indice == (tabImg.length)){
			indice=0;
		}
		else if(indice < 0) indice = (tabImg.length);
		img2.src=tabImg[indice].src;
		sens = 0;
		if(status == 1)timer1 = window.setTimeout("change_opacity(image1, image2)",temps_pause) ; // attente
		return 0;
	}
	else if (opacity1 <= 0)
	{	
		indice += direction;
		document.getElementById('courrant').innerHTML = indice;
		this.refresh();
		if (indice == (tabImg.length)){
			indice=0;
		}
		else if(indice < 0) indice = (tabImg.length);
		img1.src=tabImg[indice].src;
		sens = 1;
		if(status == 1)timer2 = window.setTimeout("change_opacity(image1, image2)",temps_pause) ; // attente
		return 0;
	}
		
	timerOp = window.setTimeout("change_opacity(image1, image2)",temps); // recursion toutes les x millisec

}

function refresh(){
	if(status == 1){
		document.getElementById('play').innerHTML = '<a href="javascript:;" onclick="playPause();">||</a>';
	}
	else{
		document.getElementById('play').innerHTML = '<a href="javascript:;" onclick="playPause();">>></a>';
	}
}

function playPause(){
	if(status == 1){
		status = 0;
		direction = 1;
		this.refresh();
		clearTimeout(timer1);
		clearTimeout(timer2);
		clearTimeout(timerOp);
		clearTimeout(timerStart);
	}
	else{
		status = 1;
		direction = 1;
		this.refresh();
		clearTimeout(timer1);
		clearTimeout(timer2);
		clearTimeout(timerOp);
		clearTimeout(timerStart);
		change_opacity(image1, image2);
	}
}

function change(dir){
	if(status == 1){
		this.playPause();
	}
	if(sens){ image1 = tabImg[(indice-2)];}
	else{ image2 = tabImg[(indice-2)];}
	direction = dir;
	this.change_opacity(image1, image2);
}











