XfadeInstance=1;
var Xfade = function(id,name,dossierImg,WaitImgXfade,TempAnim,TempFirstAnim,ListIMG){

  this.id=id;
  this.name=name;
  this.dossierImg=dossierImg;
  this.WaitImgXfade=WaitImgXfade;
  this.TempAnim=TempAnim;
  this.TempFirstAnim=TempFirstAnim;
  this.ListIMG=ListIMG;
  this.NbAnim=id.length;
  this.CurrentAnim=0;
  
  this.MyCurrent = new Array();
  this.d=document;
  this.imgs = new Array();
  this.zInterval = null;
  this.current=new Array();
  this.nIndex=new Array();
  this.pause=false;
  this.inst=XfadeInstance;
  XfadeInstance++;
  
	if(!this.d.getElementById || !this.d.createElement) return;
  for(j=0;j<this.NbAnim;j++){
  
    this.current[j]=0;
    var ListIMG = this.ListIMG[j]; // Liste des images
    var ID = this.id[j]; // ID Block xFade
    
    var el=$("#"+ID); // Block xFade
    var height = el.height(); // Récupère la hauteur
    el.empty().height(height); // Vide le block et défini la hauteur
    
    // INSERE LES IMAGES
  	for(var i=1;i<=ListIMG.length;i++){
  	
  		if(i<=2) url=this.dossierImg[j]+ListIMG[i-1].fichier; // Image pour les deux premiers
      else url=this.WaitImgXfade[j]; // Image wait
  		el.append('<img id="ImgXfade'+this.inst+'_'+j+'_'+i+'" src="'+url+'" alt="" />'); // Insere l'image
  		
  	}
  	
  	var els = $("#"+ID+" > img"); // Les images du block xFade
  	els.hide().css({position:'absolute',top:0,left:0}); // Cache les images et les défini en position absolute
  	
  	this.imgs[j] = new Array(); // Défini un tableau pour les images
  	this.imgs[j] = els; // Insere les images au tableau
  	els.each(function(){ this.xOpacity = 0; }); // Défini variable xOpacity

  	$( this.imgs[j][0] ).show(); // Affiche la première image
	this.imgs[j][0].xOpacity=.99;
	  
  }

  var el=this; var name=this.name;
  var test = setTimeout(function(){ el.start(el.name); }, el.TempFirstAnim);
  
};

Xfade.prototype.start = function(name){
  var el=eval(name); 
  var CurrentAnim = el.CurrentAnim;
  var current = el.current[el.CurrentAnim];

	
	var cOpacity = el.imgs[CurrentAnim][current].xOpacity; // Opacité de l'image actuel
	el.nIndex[CurrentAnim] = el.imgs[CurrentAnim][current+1]?current+1:0; // Image suivante ou première
	
	var nIndex = el.nIndex[CurrentAnim];
	var nOpacity = el.imgs[CurrentAnim][nIndex].xOpacity; // Opacité de l'image suivante

	cOpacity-=.05; // Décremente image actuel
	nOpacity+=.05; // Incrémente image suivante

  var imgs = el.imgs[el.CurrentAnim];
	imgs[nIndex].style.display = "block"; // Affichage l'image suivante
	imgs[current].xOpacity = cOpacity; // donne l'opacité à l'image actuel
	imgs[nIndex].xOpacity = nOpacity; // done l'opacité à l'image suivante

	setOpacity(imgs[current]); // Applique l'opacité image actuel
	setOpacity(imgs[nIndex]); // Applique l'opacité image suivante

	if( cOpacity<=0 ){ // Si l'opacité de l'image actuel <= 0

	    $( imgs[current] ).hide(); // Cache l'image actuel
	    el.current[CurrentAnim] = nIndex; // Défini que l'image actuel est la suivante

      // Vérifie que l'animation suivante existe, sinon retourne à la premiere
      if( CurrentAnim+1 < el.NbAnim ) el.CurrentAnim+=1;
	    else el.CurrentAnim=0;
	    
	    // Prochaine animation
	    setTimeout(function(){ el.start(el.name); }, el.TempAnim[el.CurrentAnim]);
	    
	    // Preload de l'image suivante
	    var CurrentAnim = el.CurrentAnim;
	    var current = el.current[CurrentAnim];
	    el.MyCurrent[CurrentAnim]=current; // Défini l'image courante
	    var MyCurrent = el.MyCurrent[CurrentAnim];
	    
	    
	    var next = imgs[ MyCurrent+1 ]? MyCurrent+1 : false;
	    if( next!=false ){ // Si Existe, precharge la suivante
			  var img = this.ListIMG[CurrentAnim][next].fichier;
	    	$("#ImgXfade"+el.inst+"_"+el.CurrentAnim+"_"+(next+1)).attr("src", el.dossierImg[CurrentAnim]+img );
	    }
			
	}else{ // Sinon continue a décrémenter
	
		setTimeout(function(){ el.start(el.name); },50);
		
	}

	function setOpacity(obj){
		if(obj.xOpacity>.99){
			obj.xOpacity = .99;
			return;
		}
		obj.style.opacity = obj.xOpacity;
		obj.style.MozOpacity = obj.xOpacity;
		obj.style.filter = "alpha(opacity=" + (obj.xOpacity*100) + ")";
	}

};
