var utils = {};

utils.addListener = function (el, sType, fn) {
	if (window.addEventListener) {
		el.addEventListener(sType, fn, false);
	} else if (window.attachEvent) {
		el.attachEvent("on" + sType, fn);
	} else {
		el['on'+sType] = fn;
	}
}

// JavaScript Document
//Chargement des images
function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

var photosTab = new Array();
var isPlaying = true;
var incrementTableau;

function setOpacity(obj, opacity)
{
  opacity = (opacity == 100)?99.999:opacity;

  // IE/Win
  obj.style.filter = "alpha(opacity:"+opacity+")";

  // Safari<1.2, Konqueror
  obj.style.KHTMLOpacity = opacity/100;

  // Older Mozilla and Firefox
  obj.style.MozOpacity = opacity/100;

  // Safari 1.2, newer Firefox and Mozilla, CSS3
  obj.style.opacity = opacity/100;
}
function fadeIn(objId,opacity,timeLaps)
{
  if (document.getElementById)
  {
   obj = document.getElementById(objId);
    if (opacity <= 100)
	{
      //alert(obj + '1');
	  setOpacity(obj, opacity);
      opacity += 10;
      window.setTimeout("fadeIn('"+objId+"',"+opacity+","+timeLaps+")", 50);
    }
	else
	{
		finFadeIn(timeLaps);
	}
  }
}
function fadeOut(objId,opacity,timeLaps)
{
  if (document.getElementById)
  {
    obj = document.getElementById(objId);
    if (opacity > 0)
	{
	  setOpacity(obj, opacity);
      opacity -= 10;
      window.setTimeout("fadeOut('"+objId+"',"+opacity+","+timeLaps+")", 50);
    }
	else
	{
		finFadeOut(timeLaps);
	}
  }
}



function checkIsPlaying()
{
	if (isPlaying)
	{
		return (true);
	}
	else
	{
		return (false);
	}
}
function stopDiapo()
{
	isPlaying = false;
	return false;
}
function playDiapo(timeLaps)
{
	isPlaying = true;
	launch_diaporama(timeLaps);
	return false;
}
function launch_diaporama(timeLaps)
{
	window.setTimeout("changePhoto("+timeLaps+")",timeLaps);
}
function nextDiapo()
{
	stopDiapo();
	incremente();
	fadeOut('big_pict',100,4000);
	return false;
}
function prevDiapo()
{
	stopDiapo();
	decremente();
	fadeOut('big_pict',100,4000);
	return false;
}


function incremente()
{
	if(incrementTableau+1<photosTab.length)
	{
		incrementTableau++;
	}
	else
	{
		incrementTableau=0;
	}
}
function decremente()
{
	if(incrementTableau==0)
	{
		incrementTableau = photosTab.length - 1;
	}
	else
	{
		incrementTableau--;
	}
}


function finFadeIn(timeLaps)
{
	if(checkIsPlaying())
	{
		incremente();
		launch_diaporama(timeLaps,photosTab);
	}
}

function finFadeOut(timeLaps)
{
	var big_photo = document.getElementById('big_pict') ;
	var titre_photo = document.getElementById('banniere_pub').getElementsByTagName('a')[0] ;
	titre_photo.href = photosTab[incrementTableau]["lien"];
	big_photo.src = photosTab[incrementTableau]["photo"];
	big_photo.alt = photosTab[incrementTableau]["titre"];
	setOpacity(big_photo, 0);
	big_photo.style.visibility = 'visible';
	window.setTimeout("fadeIn('big_pict',0,"+timeLaps+")",200);
}


function changePhoto(timeLaps)
{
	if(checkIsPlaying())
	{
		fadeOut('big_pict',100,timeLaps);
	}
}

function displayPics()
{
	var photos = document.getElementById('galerie_mini') ;
	// On récupère l'élément ayant pour id galerie_mini
	var liens = photos.getElementsByTagName('a') ;
	// On récupère dans une variable tous les liens contenu dans galerie_mini
	var big_photo = document.getElementById('big_pict') ;
	// Ici c'est l'élément ayant pour id big_pict qui est récupéré, c'est notre photo en taille normale

	//var titre_photo = document.getElementById('photo').getElementsByTagName('a').href ;
	// Et enfin le titre de la photo de taille normale

	// Une boucle parcourant l'ensemble des liens contenu dans galerie_mini
	for (var i = 0 ; i < liens.length ; ++i) {
		// Au clique sur ces liens
		liens[i].onclick = function() {
			big_photo.src = this.href; // On change l'attribut src de l'image en le remplaçant par la valeur du lien
			big_photo.alt = this.title; // On change son titre
			//titre_photo.innerHTML = this.title; // On change le texte de titre de la photo
			return false; // Et pour finir on inhibe l'action réelle du lien
		};
	}
}
//Ne sert pas dans ce cas
//utils.addListener(window,'load',displayPics);

// Il ne reste plus qu'à appeler notre fonction au chargement de la page