﻿
var progressUpload = Class.create();

progressUpload.prototype = {
	initialize: function (elementCible, champForm) {
		this.conteneurProgress	= "Progress" + elementCible;
		this.elementCible 		= elementCible;
		this.Id 				= elementCible;
		this.champForm 			= champForm;
		
		this.initProgress();
		this.definirStatus("En attente");
	},
	
	initProgress: function () {
		if ($(this.conteneurProgress) == null) {
			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).insert({bottom: codeProgress});
			$(this.Id + 'LimitesBarre').hide();
		}
	},
	
	forceInitProgress: function () {
		if ($(this.conteneurProgress) != null) {
			$(this.conteneurProgress).remove();
			this.initProgress();
		}
	},
	
	definirStatus: function (nouveauStatus) {
		$(this.Id + 'Status').update(nouveauStatus);
	},
	
	definirNomFichier : function (nomFichier) {
		$(this.Id + 'Nom').update(nomFichier);
	},
	
	definirErreur: function (messageErreur) {
		$(this.conteneurProgress).className = "progressBloc Erreur";
		$(this.Id + 'Barre').setStyle({'width' : '0%'});
		this.definirNomFichier("Erreur");
		this.definirStatus(messageErreur);
		this.toggleAnnuler(false);
	},
	
	majPourcentUpload: function (valeurPourcent) {
		$(this.Id + 'LimitesBarre').show();
		$(this.conteneurProgress).className = "progressBloc enCours";
		$(this.Id + 'Barre').setStyle({'width' : valeurPourcent + '%'});
	},
	
	traitementTermine: function (serverData) {
		if ( serverData.Succes == true ) {
			$(this.conteneurProgress).className = "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);
		}
	},
	
	activerAnnulerUpload: function (SWFUploadInstance, fichierID) {
		this.toggleAnnuler(true);
		$(this.Id + "Cancel").onclick = function () {
			SWFUploadInstance.cancelUpload(fichierID);
			return false;
		};
	},
	
	activerSupprimerFichier: function () {
		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;
		};
	},
	
	toggleAnnuler: function (afficherLien) {
		if (afficherLien == true) {
			$(this.Id + "Cancel").show();
		}
		else {
			$(this.Id + "Cancel").hide();
		}
	},
	
	assignerValeurFormulaire: function (valeurChamp) {
		$(this.champForm).value = valeurChamp;
		return true;
	},
	
	initialiserAvecValeurForm: function (Status) {
		this.definirNomFichier($(this.champForm).value);
		this.definirStatus(Status);
		this.activerSupprimerFichier();
	}
}
