// mettre un minimum de 4 images !!!

var coef = 0.05 ; // avancement de l'opacité
var temps = 50 ; // temps entre chaque changement d'opacité
var temps_pause = 2000 ; // temps d'attente entre 2 changements d'images
//var nombre_image = 5 ; // nombre d'images a faire bouger
var prefix_image = 'images/coverflow_home/'; // chemin + prefix du nom des images
var suffix_image = '.jpg' ; // suffix + '.extension' du nom des images

// pas touche
var indice = 2; // les 2 premiere image sont deja charger dans le HTML, on commence a la 3eme
var isIE = !!document.all ;
var img1 = null;
var img2 = null ;
var sens = 1;
var tabImg; // tab contenant les images

function prechargerImg(nombre_image){
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 init(nb){
  img1 = document.getElementById("defilement1") ;
  img2 = document.getElementById("defilement2") ;
  
  prechargerImg(nb);
  change_opacity();
}

function change_opacity(){
  var opacity1 = 0 ;
  var opacity2 = 0 ;
  if (isIE) // for IE
  {
    opacity1 = Math.round((document.getElementById("result1").value)*100)/100;
    opacity2 = Math.round((document.getElementById("result2").value)*100)/100;
  }
  else // for mozilla
  {
    opacity1 = Math.round((document.getElementById("result1").value)*100)/100;
    opacity2 = Math.round((document.getElementById("result2").value)*100)/100;  
  }

  if (sens){
    if (isIE) // for IE
    {
      img1.filters[0].opacity = Math.round((opacity1 * 100 + coef * 10)*100)/100;
      img2.filters[0].opacity = Math.round((opacity2 * 100 - coef * 10)*100)/100;
      document.getElementById("result1").value = Math.round((opacity1 + coef)*100)/100;
      document.getElementById("result2").value = Math.round((opacity2 - coef)*100)/100;
    }
    else // for Mozilla
    {
      img1.style.opacity = Math.round((opacity1 + coef)*100)/100;
      img2.style.opacity = Math.round((opacity2 - coef)*100)/100;
      document.getElementById("result1").value = Math.round((opacity1 + coef)*100)/100;
      document.getElementById("result2").value = Math.round((opacity2 - coef)*100)/100; 
    }
  } else {
    if (isIE) // for IE
    {
      img1.filters[0].opacity = Math.round((opacity1 * 100 - coef * 10)*100)/100;
      img2.filters[0].opacity = Math.round((opacity2 * 100 + coef * 10)*100)/100;
      document.getElementById("result1").value = Math.round((opacity1 - coef)*100)/100;
      document.getElementById("result2").value = Math.round((opacity2 + coef)*100)/100;
    }
    else // for Mozilla
    {
      img1.style.opacity = Math.round((opacity1 - coef)*100)/100;
      img2.style.opacity = Math.round((opacity2 + coef)*100)/100;
      document.getElementById("result1").value = Math.round((opacity1 - coef)*100)/100;
      document.getElementById("result2").value = Math.round((opacity2 + coef)*100)/100;
    }
  }

  if (isIE) // for IE
  {
    opacity1 = Math.round((document.getElementById("result1").value)*100)/100;
    opacity2 = Math.round((document.getElementById("result2").value)*100)/100;
  }
  else // for mozilla
  {
    opacity1 = Math.round((document.getElementById("result1").value)*100)/100;
    opacity2 = Math.round((document.getElementById("result2").value)*100)/100; 
  }

  // on fait varié le sens d'opacité du bazar
  if (opacity2 <= 0){
    //img2.src=tabImg[indice++].src;
    img2.style.background = "url(" + tabImg[indice++].src + ")";
    sens = 0;    
    if (indice == (tabImg.length)) indice=0;
    window.setTimeout("change_opacity()",temps_pause) ; // attente
    return 0;
  } else if (opacity1 <= 0){
    //img1.src=tabImg[indice++].src;
    img1.style.background = "url(" + tabImg[indice++].src + ")";
    sens = 1;
    if (indice == (tabImg.length)) indice=0;
    window.setTimeout("change_opacity()",temps_pause) ; // attente
    return 0;
  }
  
  window.setTimeout("change_opacity()",temps) ; // recursion toutes les x millisec
}
