/**
 * @FileFR » Gestion du diaporama
 * @FileEN » Management of the slideshow
 */

/**
 * @VarFR gnTimer Integer » Temps écoulé entre 2 diapositives
 * @VarEN gnTimer Integer » Time spent between 2 slides
 */
var gnTimer = 0;

/**
 * @VarFR gnOpacity Integer » Opacité de la diapositive (en pourcent)
 * @VarEN gnOpacity Integer » Opacity of the slide (in percent)
 */
var gnOpacity = 0;

/**
 * @VarFR gsId String » Identifiant HTML du diaporama
 * @VarEN gsId String » HTML ID of the slideshow
 */
var gsId = "";

/**
 * @VarFR gnIndex Integer » Indice de la diapositive en cours
 * @VarEN gnIndex Integer » Index of the current slide
 */
var gnIndex = 0;
      
/**
 * @FunctionFR callFadeIn » Apparition de la diapositive
 * @FunctionEN callFadeIn » Fade in of the slide
 * @ParamFR asId String / » Identifiant HTML du diaporama
 * @ParamEN asId String / » HTML ID of the slideshow
 */
function callFadeIn(asId)
{
   gsId = asId;
   gnOpacity = 0;
   gnTimer = setInterval("fadeIn()", 20);
}

/**
 * @FunctionFR callFadeOut » Disparition de la diapositive
 * @FunctionEN callFadeOut » Fade out of the slide
 * @ParamFR asId String / » Identifiant HTML du diaporama
 * @ParamEN asId String / » HTML ID of the slideshow
 * @ParamFR anIndex Integer / » Indice de la diapositive en cours
 * @ParamEN anIndex Integer / » Index of the current slide
 */
function callFadeOut(asId, anIndex)
{
   gsId = asId;
   gnIndex = anIndex;
   gnOpacity = 1;
   document.getElementById(gsId+'Div').style.backgroundImage="url(" + gaDiaporamas[gsId].images[anIndex]["image"].src + ")";
	// var elem = document.getElementById(gsId);	
   // elem.style.visibility = 'visible';
	gnTimer = setInterval("fadeOut()", 20);
}

/**
 * @FunctionFR cancelFade » Annulation de l'apparition de la diapositive
 * @FunctionEN cancelFade » Cancel of the fade in of the slide
 */
function cancelFade()
{
   if (gnTimer == 0) return;
   clearInterval(gnTimer);
   gnTimer = 0;
}

function clickOnImage(index)
{
	document.getElementById("image"+index).onclick();
}

/**
 * @FunctionFR fadeOut » Disparition de la diapositive
 * @FunctionEN fadeOut » Fade out of the slide
 */
function fadeOut()
{
   var elem = document.getElementById(gsId);
   if (gnOpacity <= 0)
   {
      cancelFade();
      index = gnIndex;
      document.images[gsId].src = gaDiaporamas[gsId].images[index]["image"].src;
		document.getElementById(gsId+'DivClick').onclick = new Function('clickOnImage("'+index+'");');
		gnOpacity = 1;
      elem.style.opacity = gnOpacity;
      elem.style.MozOpacity = gnOpacity;
      elem.style.KhtmlOpacity = gnOpacity;
      elem.style.filter = "alpha(opacity=" + gnOpacity * 100 + ")";
		// elem.style.visibility='hidden';
      return false;
   }
   gnOpacity -= 0.04;
   elem.style.opacity = gnOpacity;
   elem.style.MozOpacity = gnOpacity;
   elem.style.KhtmlOpacity = gnOpacity;
   elem.style.filter = "alpha(opacity=" + gnOpacity * 100 + ")";
}

/**
 * @FunctionFR checkDiapoLoading » Vérifie que le diaporama est chargé
 * @FunctionEN checkDiapoLoading » Check that the slideshow is loaded
 */
function checkDiapoLoading( langue, codeOuest ) // GOTO
{
   imgload = 0;
   for (i=0 ; i<gaDiaporamas[gsId].slides.length ; i++) // Vérifie les images dont le chargement est complet
   {
      if (gaDiaporamas[gsId].images[i]['image'].complete)
      {
         imgload++;
      }
   }
   if(imgload == gaDiaporamas[gsId].slides.length)
   {
		document.getElementById('remplissage-progression').style.width = '280px';
		document.getElementById('remplissage-progression').style.marginLeft = '0px';
		document.getElementById('chargement-diaporama').style.display = 'none';
		gaDiaporamas[gsId].PlayNow();
		initLytebox( langue, codeOuest ); // GOTO
   }
   else
   {
		var imgtotal = gaDiaporamas[gsId].slides.length;
		var widthprogression = ( imgload * 280 ) / imgtotal;
		document.getElementById('remplissage-progression').style.width = widthprogression + 'px';
		document.getElementById('remplissage-progression').style.marginLeft = ( 140 - ( widthprogression / 2 ) ) + 'px';
      setTimeout("checkDiapoLoading('" + langue + "', '" + codeOuest + "')", 10);
   }
}

/**
 * @VarFR gaDiaporamas Array_of_Diaporama » Tableau de diaporamas
 * @VarEN gaDiaporamas Array_of_Diaporama » Array of slideshows
 */
var gaDiaporamas = new Array();


/**
 * @ClassFR Diaporama » Gestion d'un diaporama
 * @ClassEN Diaporama » Management of a slideshow
 */
 
/**
 * @FunctionFR Diaporama » Constructeur du diaporama
 * @FunctionEN Diaporama » Contructor of the slideshow
 * @ParamFR asName String / » Nom du diaporama 
 * @ParamEN asName String / » Name of the slideshow
 * @ParamFR anInterval Integer / » Intervalle de temps entre 2 diapositives
 * @ParamEN anInterval Integer / » interval of time between to slides
 */
function Diaporama(asName, anInterval)
{
   this.name     = asName;
   this.images   = new Array();
   this.slides   = new Array();
   this.imgIndex = 0;
   this.id       = null;
   this.interval = (anInterval == undefined ? 1000 : new Number(anInterval));

   /**
    * @FunctionFR Add » Ajoute une image au diaporama
    * @FunctionEN Add » Add an image to the slideshow
    * @ParamFR asImageUrl String / » Url de l'image 
    * @ParamEN asImageUrl String / » Url of the image
    * @ParamFR asImageTitle String / » Titre de l'image
    * @ParamEN asImageTitle String / » Title of the image
    */
   this.Add = function(asImageUrl, asImageTitle)
   {
      var index = this.slides.length;
      this.slides[index] = new Array();
      this.slides[index]["image"] = asImageUrl;
      this.slides[index]["title"] = asImageTitle;
   }

   /**
    * @FunctionFR Show » Affiche une diapositive
    * @FunctionEN Show » Show a slide
    * @ParamFR anIndex Integer / » Indice de la diapositive 
    * @ParamEN anIndex Integer / » Index of the slide
    */
   this.Show = function(anIndex)
   {
		index = anIndex;
      if(anIndex < 0) index = this.images.length - 1;
      if(anIndex > this.images.length - 1) index = 0;
      cancelFade();
      callFadeOut(this.name, index);
      this.imgIndex=index;
   }
   
   /**
    * @FunctionFR First » Affiche la première diapositive
    * @FunctionEN First » Show the first slide
    */
   this.First = function()
   {
      this.Show(0);
      if (this.id != null) {
         this.Pause();
         this.Play();
      }
   }
   
   /**
    * @FunctionFR Last » Affiche la dernière diapositive
    * @FunctionEN Last » Show the last slide
    */
   this.Last = function()
   {
      this.Show(this.images.length - 1);
      if (this.id != null) {
         this.Pause();
         this.Play();
      }
   }
   
   /**
    * @FunctionFR Previous » Affiche la diapositive précédente
    * @FunctionEN Previous » Show the previous slide
    */
   this.Previous = function()
   {
      if (this.imgIndex > 0) {
			this.Show(this.imgIndex - 1);
		} else {
			this.Show(this.slides.length - 1);
		}
      if (this.id != null) {
         this.Pause();
         this.Play();
      }
   }
   
   /**
    * @FunctionFR Next » Affiche la diapositive suivante
    * @FunctionEN Next » Show the next slide
    */
   this.Next = function()
   {
      if (this.imgIndex < this.images.length - 1) {
			   this.Show(this.imgIndex + 1);
		  } else {
			   this.Show(0);
      }
		if (this.id != null) {
         this.Pause();
         this.Play();
      }
   }
   
   /**
    * @FunctionFR PlayNow » Lance la lecture du diaporama
    * @FunctionEN PlayNow » Launch the play of the slideshow
    */
   this.PlayNow = function()
   {
      gaDiaporamas[this.name].Show(gaDiaporamas[this.name].imgIndex);
      this.Play();
   }
   
   /**
    * @FunctionFR Play » Lance la lecture du diaporama
    * @FunctionEN Play » Launch the play of the slideshow
    */
   this.Play = function()
   {
      this.Pause();
      this.id=setInterval("gaDiaporamas['" + this.name + "'].Show(gaDiaporamas['" + this.name + "'].imgIndex + 1);", this.interval);
   }
   
   /**
    * @FunctionFR Play » Met en pause la lecture du diaporama
    * @FunctionEN Play » Pause the play of the slideshow
    */
   this.Pause = function()
   {
      clearInterval(this.id);
      this.id=null;
   }
   
   /**
    * @FunctionFR Stop » Stoppe la lecture du diaporama
    * @FunctionEN Stop » Stop the play of the slideshow
    */
   this.Stop = function()
   {
      clearInterval(this.id);
      this.id=null;
      this.Show(0);
   }
   
   /**
    * @FunctionFR Stop » Affiche la diapositive choisie
    * @FunctionEN Stop » Show the choosen slide
    * @ParamFR anIndex Integer / » Indice de la diapositive 
    * @ParamEN anIndex Integer / » Index of the slide
    */
   this.Click = function(anIndex)
   {
      this.Show(anIndex);
      if (this.id != null)
      {
         this.Play();
      }
   }

   /**
    * @FunctionFR Build » Construit le diaporama
    * @FunctionEN Build » Build the slideshow
    */
   this.Build = function()
   {
      // document.write("<div class=\"diaporamaTitle\"><div class=\"numberDiapo\" id=\"" + this.name + "Number\"></div><div class=\"titleDiapo\" id=\"" + this.name + "Title\"></div></div>");
      document.write("<div id=\"" + this.name + "Div\">");
      document.write("	<img src=\"../images/vide.jpg\" id=\"" + this.name + "\" name=" + this.name + "\">");
		document.write("</div>");
		gsId = this.name;
		/*
      if (this.slides.length > 0)
      {
         this.images[0] = new Array();
         this.images[0]["image"]=new Image();
         this.images[0]["image"].src=this.slides[0]["image"];
         this.images[0]["image"].alt='';
         this.images[0]["title"]=this.slides[0]["title"];
         this.Show(0);
      }
		*/
   }

   /**
    * @FunctionFR LoadImages » Pré-chargement des images du diaporama
    * @FunctionEN LoadImages » Loading of the images of the slideshow
    */
   this.LoadImages = function( langue, codeOuest ) // GOTO
   {
      for (var i=0 ; i < this.slides.length ; i++)
      {
         this.images[i] = new Array();
         this.images[i]["image"]=new Image();
         this.images[i]["image"].src=this.slides[i]["image"];
         this.images[i]["image"].alt='';
         this.images[i]["title"]=this.slides[i]["title"];
		}
      checkDiapoLoading( langue, codeOuest ); // GOTO
   }
	
   gaDiaporamas[this.name] = this;
}
