//class qui crÃ©e une galerie simple et accessible depuis d'autres classes JS
function simpleLoadingGal(_objet)
{
	//utile
	var i;
	var current=this;
	//affectation
	var objet;//objet contenant les paramÃ¨tres
	if (_objet == null) 
	{
		objet = {
					_nomInstance: "nomInstance",
					_largeur: "largeur",
					_hauteur: "hauteur",
					_idParent: "idParent",
					_waiter: "linkWait",
					_dureeTransition:5,
					_changeFunction:'a function',
					_transitionType:'a string',
					_isFull:'boolean'
				};
	}
	else//sinon on affecte avec l'objet passÃ© en paramÃ¨tre
	{
		objet = _objet;
	}
	this.nomInstance=objet._nomInstance;
	this.largeur=objet._largeur;
	this.hauteur=objet._hauteur;
	this.idParent=objet._idParent;
	this.waiter=objet._waiter;
	this.dureeTransition=objet._dureeTransition;
	this.changeFunction=objet._changeFunction;
	this.transitionType=objet._transitionType;
	if(objet.isFull==true){this.isFull=true;}else{this.isFull=false;}
	
	//schmilblik
	this.arrayDataImages;//tableau contenant les donnÃ©es des images 1 entrÃ©e => {infos:"info1",image:"img/1.jpg"}
	this.arrayImages=Array();//tableau contenant les images ... (utilisÃ© dans le cadre d'une chargement total prÃ©alable)
	this.imageWait;//premiÃ¨re image chargÃ©e
	this.imageWaitOther;//niÃ¨me image chargÃ©e
	this.indice=0;//indice de dÃ©part
	this.indiceOther;//indice de la prochaine image affichÃ©e( stockage )
	this.etatChargement;//variable qui stocke les setInterval
	this.divCont;//div qui contient notre galerie
	this.idDivCont;//son id en mode string
	this.divContImg;//div qui contient vraiment notre galerie
	this.idDivContImg;//son id
	this.hasBeenlaunched=false;//boolean qui indique si on a initialisÃ© une galerie
	this.nbreImages;//nbre d'images courant
	this.FIRST_TIME="first_time";
	this.BALAYAGE="balayage";
	this.FROM_RIGHT="from_right";//constante
	this.FROM_LEFT="from_left";//constante
	this.FONDUE="fondue";
	this.canDoTransition=true;//boolean qui indique si les transitions sont possibles
	
	
	//constructeur dÃ©diÃ© Ã  la crÃ©ation grace Ã  un array de donnÃ©es
	this.loadWithArrayData=function(_arrayData,_firstImage)
	{
		if(_firstImage==null){_firstImage=0;}
		//si c'est pas la premiÃ¨re fois
		if (this.hasBeenlaunched) 
		{
			this.arrayDataImages=null;
		}
		//sinon on crÃ©e nos divs
		else
		{
			this.divCont=document.createElement("div");
			this.idDivCont=this.nomInstance+"Div";
			this.divCont.setAttribute("id",this.idDivCont);
			$(this.divCont).appendTo("#"+this.idParent+"");	
			$(this.divCont).css("position","relative");
			$(this.divCont).css("overflow","hidden");
			$(this.divCont).css("width",this.largeur+"px");
			$(this.divCont).css("height",this.hauteur+"px");
			//$(this.divCont).css("background","blue");
			
			this.divContImg=document.createElement("div");
			this.idDivContImg=this.nomInstance+"contImg";
			this.divContImg.setAttribute("id",this.idDivContImg);
			$(this.divContImg).appendTo("#"+this.idDivCont+"");	
			$(this.divContImg).css("width",this.largeur+"px");
			$(this.divContImg).css("height",this.hauteur+"px");
			$(this.divContImg).css("position","relative");
			
			this.hasBeenlaunched=true;
			
		}
		//affectation
		this.arrayDataImages = _arrayData;
		this.nbreImages=this.arrayDataImages.length;
		
		//crÃ©ation et chargement de la premiÃ¨re image
		this.imageWait=null;
		this.imageWait = document.createElement("img");
		this.imageWait.setAttribute("id", "imageFocusWait");
		this.imageWait.setAttribute("src",this.arrayDataImages[_firstImage].image);
		this.indice=_firstImage;
		if(this.waiter!=null)
		{this.waiter.show(this.idDivCont);}
		this.etatChargement=setInterval(""+this.nomInstance+".verifLoad('first_time')",1);
		
	};
	//function qui vÃ©rifie le chargement de l'image
	this.verifLoad=function(_caller)
	{
		var coors;//variable qui stockera les coordonnÃ©ees
		switch(_caller)//selon qu'on vienne de droite ou de gauche
		{
			case this.FIRST_TIME:
				if(this.imageWait.complete==true)//si l'image est chargÃ©e
				{
					$('#galeriecontImg').children().remove();
					clearInterval(this.etatChargement);//on coupe la vÃ©rification
					if(this.waiter!=null)
					{this.waiter.hide();}
					this.imageWait.setAttribute("width", this.largeur);
					this.imageWait.setAttribute("height", this.getNewHeightRelativeToFormat(1260,960));
					$(this.imageWait).appendTo("#"+this.idDivContImg+"");
					this.imageWait.setAttribute("id", "imageFocus");
					$(this.imageWait).css("position","absolute");
					$(this.imageWait).css("top","0px");
					$(this.imageWait).css("left","0px");
					$(this.imageWait).css("opacity","0");
					//on commente pour cotec
					$(this.imageWait).animate({opacity:1},{duration:this.dureeTransition});
					coors=this.centrage($(this.imageWait).width(),$(this.imageWait).height());
					//$(this.imageWait).css("top",coors.margeT+"px");
					$(this.imageWait).css("left",coors.margeG+"px");
					
				}
			break;
			
			case this.FROM_RIGHT:
				if(this.imageWaitOther.complete==true)//si l'image est chargÃ©e
				{
					clearInterval(this.etatChargement);//on coupe la vÃ©rification
					if(this.waiter!=null)
					{this.waiter.hide();}
					//on bouge image focus
					$("#imageFocus").animate(
												{left: -$("#imageFocus").width()},
												{duration:this.dureeTransition}
											);
					//on fait apparaire l'autre et on met Ã  jour id , indices etc ....
					$(this.imageWaitOther).appendTo("#"+this.idDivContImg+"");
					
					
					
					coors=this.centrage($(this.imageWaitOther).width(),$(this.imageWaitOther).height());
					
					$(this.imageWaitOther).css("position","absolute");
					//$(this.imageWaitOther).css("top",coors.margeT+"px");
					$(this.imageWaitOther).css("left",this.largeur+"px");
					$(this.imageWaitOther).animate(
													  {left:coors.margeG},
													  {
													  	duration:this.dureeTransition,
														complete:function()//cette function est un callback pour mettre Ã  jour les donnÃ©es etc
																			{
																				$("#imageFocus").remove();
																				current.imageWaitOther.setAttribute("id", "imageFocus");
																				current.indice=current.indiceOther;
																				current.canDoTransition=true;
																				if(current.changeFunction!=null)
																				{
																					current.changeFunction(current.indice);
																				}
																			}
													  }
												  );
				}
			break;
			
			case this.FROM_LEFT:
				if(this.imageWaitOther.complete==true)//si l'image est chargÃ©e
				{
					clearInterval(this.etatChargement);//on coupe la vÃ©rification
					if(this.waiter!=null)
					{this.waiter.hide();}
					//on bouge image focus
					$("#imageFocus").animate(
												{left: this.largeur},
												{duration:this.dureeTransition}
											);
					//on fait apparaire l'autre et on met Ã  jour id , indices etc ....			
					$(this.imageWaitOther).appendTo("#"+this.idDivContImg+"");
					coors=this.centrage($(this.imageWaitOther).width(),$(this.imageWaitOther).height());
					$(this.imageWaitOther).css("position","absolute");
					//$(this.imageWaitOther).css("top",coors.margeT+"px");
					$(this.imageWaitOther).css("left",-$(this.imageWaitOther).width()+"px");
					$(this.imageWaitOther).animate(
													  {left:coors.margeG},
													  {
													  	duration:this.dureeTransition,
														complete:function()//cette function est un callback pour mettre Ã  jour les donnÃ©es etc
																			{
																				$("#imageFocus").remove();
																				current.imageWaitOther.setAttribute("id", "imageFocus");
																				current.indice=current.indiceOther;
																				current.canDoTransition=true;
																				if(current.changeFunction!=null)
																				{
																					current.changeFunction(current.indice);
																				}
																			}
													  }
												  );
				}
			break;
			
			
			case this.FONDUE:
				if(this.imageWaitOther != undefined && this.imageWaitOther.complete==true)//si l'image est chargÃ©e
				{
					clearInterval(this.etatChargement);//on coupe la vÃ©rification
					if(this.waiter!=null)
					{this.waiter.hide();}
					//on bouge image focus
					$("#imageFocus").animate(
												{opacity:0},
												{duration:this.dureeTransition}
											);
					//on fait apparaire l'autre et on met Ã  jour id , indices etc ....		
					this.imageWaitOther.setAttribute("width", this.largeur);
					this.imageWaitOther.setAttribute("height", this.getNewHeightRelativeToFormat(1260,960));
					$(this.imageWaitOther).appendTo("#"+this.idDivContImg+"");
					coors=this.centrage($(this.imageWaitOther).width(),$(this.imageWaitOther).height());
					$(this.imageWaitOther).css("position","absolute");
					
					//$(this.imageWaitOther).css("top",coors.margeT+"px");
					$(this.imageWaitOther).css("left",coors.margeG+"px");
					
					
					$(this.imageWaitOther).css("opacity",0);
					$(this.imageWaitOther).animate(
													  {opacity:1},
													  {
													  	duration:this.dureeTransition,
														complete:function()//cette function est un callback pour mettre Ã  jour les donnÃ©es etc
																			{
																				$("#imageFocus").remove();
																				current.imageWaitOther.setAttribute("id", "imageFocus");
																				current.indice=current.indiceOther;
																				current.canDoTransition=true;
																				if(current.changeFunction!=null)
																				{
																					current.changeFunction(current.indice);
																				}
																			}
													  }
												  );
				}
			break;
		}
	};
	//function qui dÃ©clenche l'apparition de l'image suivante dans la liste
	this.appaNext=function()
	{
		if (this.canDoTransition) //si on peut le faire
		{
			this.redefineOtherImage();
			//on dÃ©fini le nouvel indice
			if (this.indice == this.nbreImages - 1) {
				this.indiceOther = 0;
			}
			else {
				this.indiceOther = this.indice + 1;
			}
			//on lance son chargement
			
			this.imageWaitOther.setAttribute("src", this.arrayDataImages[this.indiceOther].image);
			//this.imageWaitOther.setAttribute("width", this.largeur);
			
			if(this.waiter!=null)
			{this.waiter.show(this.idDivCont);}
			switch(this.transitionType)
			{
				case this.BALAYAGE:
					this.etatChargement = setInterval("" + this.nomInstance + ".verifLoad('" + this.FROM_RIGHT + "')", 1);
				break;
				
				case this.FONDUE:
					this.etatChargement = setInterval("" + this.nomInstance + ".verifLoad('" + this.FONDUE + "')", 1);
				break;
			}
			
			
			
		}
		
	};
	//function qui dÃ©clenche l'apparition de l'image prÃ©cÃ©dente dans la liste
	this.appaPrev=function()
	{
		
		if (this.canDoTransition) //si on peut le faire
		{
			this.redefineOtherImage();
			//on dÃ©fini le nouvel indice
			if (this.indice == 0) {
				this.indiceOther = this.nbreImages-1;
			}
			else {
				this.indiceOther = this.indice - 1;
			}

			//on lance son chargement
			this.imageWaitOther.setAttribute("src", this.arrayDataImages[this.indiceOther].image);
			if(this.waiter!=null)
			{this.waiter.show(this.idDivCont);}
			
			switch(this.transitionType)
			{
				case this.BALAYAGE:
					this.etatChargement = setInterval("" + this.nomInstance + ".verifLoad('" + this.FROM_LEFT + "')", 1);
				break;
				
				case this.FONDUE:
					this.etatChargement = setInterval("" + this.nomInstance + ".verifLoad('" + this.FONDUE + "')", 1);
				break;
			}
		}
	};
	//function qui prÃ©pare pour une nouvelle initialisation
	this.initAndBeginAgain=function(_newData)
	{
		$("#imageFocus").animate(
									{opacity:0},
									{
										duration:this.dureeTransition,
										complete:function()
															{
																$("#imageFocus").remove();
																current.imageWait=null;
																current.imageWaitOther=null;
																current.indice=0;
																current.loadWithArrayData(_newData);
															}
									}
								);

		
	};
	
	//function qui dÃ©clenche l'apparition d'une image selon son id et d'ou elle vient
	this.appaWithIndice=function(_indice,_dou)
	{
		if(this.canDoTransition) //si on peut le faire
		{
			this.redefineOtherImage();
			//on dÃ©fini le nouvel indice
			this.indiceOther=_indice;
			this.imageWaitOther.setAttribute("src", this.arrayDataImages[this.indiceOther].image);
			if(this.waiter!=null)
			{this.waiter.show(this.idDivCont);}
			this.etatChargement = setInterval("" + this.nomInstance + ".verifLoad('" + _dou + "')", 1);
		}
	};
	//function qui mets des trucs Ã  0 pour afficher une autre image
	this.redefineOtherImage=function()
	{
		this.canDoTransition=false;//on bloque la navigation
		this.imageWaitOther = null;//on redÃ©fini l'objet qui stocke la prochaine image Ã  Ãªtre affichÃ©e
		this.imageWaitOther = document.createElement("img");
		this.imageWaitOther.setAttribute("id", "imageFocusWait");
	};
	
	//function qui retourne des coordonnées de proporion
	this.getNewHeightRelativeToFormat=function(_width,_height)
	{
		var scale=this.largeur/_width;
		return _height*scale;
	};
	//function qui calcule la position centrÃ©e de nos images
	this.centrage=function(_largeur,_hauteur)
	{
		//pour l'image
		var _margeG=(this.largeur/2)-(_largeur/2);
		var _margeT=(this.hauteur/2)-(_hauteur/2);
		return {margeG:_margeG,margeT:_margeT};
	}
	this.minimize=function(_duree)
	{
		$("#"+this.idParent+"").animate({height:0},{duration:_duree});
		$(this.divCont).animate({height:0},{duration:_duree});
	};
	
	this.getIdContainer=function()
	{
		return current.idParent;
		//return "huhu";
	};
	
	this.maximize=function(_duree)
	{
		$("#"+this.idParent+"").animate({height:current.hauteur},{duration:_duree});
		$(this.divCont).animate({height:current.hauteur},{duration:_duree});
	};
}
