var Class = {
   create: function() {
     return function() {
       if (this.initialize) {
         this.initialize();
       }
     }
   }
 }

var progressUpload = Class.create();

progressUpload.prototype = {
	initialize: function () {
	},
	initializeReal: function (elementCible, champForm) {
		//alert("PASS progress " + elementCible + " " + champForm) ;
		this.conteneurProgress	= "Progress" + elementCible;
		this.elementCible 		= elementCible;
		this.Id 				= elementCible;
		this.champForm 			= champForm;
		
		this.initProgress();
		this.definirStatus("En attente");
	},
	
	
	initProgress: function () {
		//alert("PASS init " + this.conteneurProgress + " " + $("#"+this.conteneurProgress));		
		if ($("#"+this.conteneurProgress).val() == undefined) {
			//alert("i if");
			codeProgress = '<div id="' + this.conteneurProgress + '" class="progressBloc Attente">' + "\n";
			codeProgress += '<a id="' + this.Id + 'Cancel" href="#" class="progressCancel">&nbsp;</a>' + "\n";
			codeProgress += '<div id="' + this.Id + 'Nom" class="progressNom"></div>' + "\n";
			codeProgress += '<div id="' + this.Id + 'Status" class="progressStatus"></div>' + "\n";
			codeProgress += '<div id="' + this.Id + 'LimitesBarre" class="progressLimitesBarre"><div id="' + this.Id + 'Barre" class="progressBarre" style="width: 0%"></div></div>' + "\n";
			codeProgress += '</div>' + "\n";
			$("#"+this.elementCible).append(codeProgress);
			$("#"+this.Id + 'LimitesBarre').hide();
		}
		//alert("PASS fin if");
	},
	
	forceInitProgress: function () {
		//alert("forceInitProgress start");
		if ($("#"+this.conteneurProgress).val() != undefined) {
			$("#"+this.conteneurProgress).remove();
			//alert("f2 " +  $("#"+this.conteneurProgress).val());
			this.initProgress();
		}
		//alert("forceInitProgress end");
	},
	
	definirStatus: function (nouveauStatus) {
		//alert("definirStatus start");
		$("#"+this.Id + 'Status').html(nouveauStatus);
		//alert("definirStatus end");
	},
	
	definirNomFichier : function (nomFichier) {
		//$("#"+this.Id + 'Nom').update(nomFichier);
		//alert("definirNomFichier" + this.Id + 'Nom');
		$("#"+this.Id + 'Nom').html(nomFichier);
		//alert("definirNomFichier end" );
	},
	
	definirErreur: function (messageErreur) {
		//alert("definirErreur start" );
		//$("#"+this.conteneurProgress).className = "progressBloc Erreur";
		$("#"+this.conteneurProgress).addClass("progressBloc Erreur");
		//$("#"+this.Id + 'Barre').setStyle({'width' : '0%'});
		$("#"+this.Id + 'Barre').css({'width' : '0%'});
		this.definirNomFichier("Erreur");
		this.definirStatus(messageErreur);
		this.toggleAnnuler(false);
		//alert("definirErreur end" );
	},
	
	majPourcentUpload: function (valeurPourcent) {
		//alert("majPourcentUpload start" );
		$("#"+this.Id + 'LimitesBarre').show();
		//$("#"+this.conteneurProgress).addClass("progressBloc enCours");
		//$("#"+this.Id + 'Barre').setStyle({'width' : valeurPourcent + '%'});
		$("#"+this.Id + 'Barre').css({'width' : valeurPourcent + '%'});
		//alert("majPourcentUpload end" );
	},
	
	traitementTermine: function (serverData) {
		//alert("traitementTermine start" );
		if ( serverData.Succes == true ) {
			//$("#"+this.conteneurProgress).addClass("progressBloc Attente");
			$("#"+this.Id + 'LimitesBarre').hide();
			this.definirNomFichier(serverData.progressNomFichier);
			this.definirStatus(serverData.progressSucces);
			this.activerSupprimerFichier();
			this.assignerValeurFormulaire(serverData.nomFichier);
		}
		else {
			this.definirErreur(serverData.messageErreur);
		}
		//alert("traitementTermine end" );
	},
	
	activerAnnulerUpload: function (SWFUploadInstance, fichierID) {
		//alert("activerAnnulerUpload" + this.Id + 'Cancel');
		this.toggleAnnuler(true);
		$("#"+this.Id + "Cancel").onclick = function () {
			SWFUploadInstance.cancelUpload(fichierID);
			return false;
		};
		//alert("fin activerAnnulerUpload");
	},
	
	activerSupprimerFichier: function () {
		//alert("activerSupprimerFichier start" );
		this.toggleAnnuler(true);
		var fichierProgress = this;
		$("#"+this.Id + "Cancel").onclick = function () {
			if ( confirm("Confirmer la suppression de ce fichier ? ") == true ) {
				fichierProgress.assignerValeurFormulaire("");
				fichierProgress.forceInitProgress();
				fichierProgress.definirStatus("Le fichier a été supprimé");
				fichierProgress.toggleAnnuler(false);
			}
			return false;
		};
		//alert("activerSupprimerFichier end" );
	},
	
	toggleAnnuler: function (afficherLien) {
		//alert("toggleAnnuler start" );
		if (afficherLien == true) {
			$("#"+this.Id + "Cancel").show();
		}
		else {
			$("#"+this.Id + "Cancel").hide();
		}
		//alert("toggleAnnuler end" );
	},
	
	assignerValeurFormulaire: function (valeurChamp) {
		//alert("assignerValeurFormulaire start" );
		$("#"+this.champForm).val( valeurChamp);
		//alert("assignerValeurFormulaire end" );
		return true;
	},
	
	initialiserAvecValeurForm: function (Status) {
		//alert("initialiserAvecValeurForm start" );
		this.definirNomFichier($("#"+this.champForm).val());
		this.definirStatus(Status);
		this.activerSupprimerFichier();
		//alert("initialiserAvecValeurForm end" );
	}
}