/**
* Ce fichier fait parti d'un package, toute modification doit entrainer la génération
* d'un nouveau package pour être pris en compte.
* voir l'objet ObjFichierPackage pour connaitre la marche à suivre pour regénérer un package
* @since 20/05/2009 Olivier REYT <reyt@benchmark.fr>
*/

/**
 * @package bibliotheque
 * @subpackage benchmark
 * @filesource
 */

if(typeof benchmark == "undefined") { var benchmark = new Object(); }
if(typeof benchmark.framework == "undefined") { benchmark.framework = new Object(); }
if(typeof benchmark.framework.ihm == "undefined") { benchmark.framework.ihm = new Object(); }
if(typeof benchmark.framework.ihm.popup == "undefined") { benchmark.framework.ihm.popup = new Object(); }

/**
 * constructeur :
 * @param configuration objet de configuration (optionnel )
 * @return void
 */
benchmark.framework.ihm.popup.ObjPopupFabrique = function(){};

/**
* fonction static creerPopUpConfirmation permet de creer une popup avec un texte et deux boutons
*<code>
* 		benchmark.framework.ihm.popup.ObjPopupFabrique.creerPopUpConfirmation(
*		 	'Voulez-vous supprimer les parties sélectionnées',
*		 	function(event){console.log('VALIDER'},
*			function(event){console.log('ANNULER'}
*		);
*</code>
* @param message string le message a afficher
* @param fonctionEcouteurValider function la fonction a executer quand l'action est validée
* @param fonctionEcouteurAnnuler function la fonction a executer quand l'action est annulée
* @param configuration l'objet de configuration
* @param object donnee supplémentaires optionnelles et seront donc disponibles dans l'evenement target.donnee des méthodes appelées
* @return un ObjPopUpConfirmation
* @since 21/04/2009 correction syntaxe (; oubliés) et ajout parametre donnee.
*/
benchmark.framework.ihm.popup.ObjPopupFabrique.creerPopUpConfirmation = function(
	message,
	fonctionEcouteurValider,
	fonctionEcouteurAnnuler,
	configuration,
	donnee
){

	var popup = new benchmark.framework.ihm.popup.ObjPopupConfirmation(configuration);
	popup.donnee = donnee;
	if(!popup.hasEventListener(benchmark.framework.ihm.popup.ObjPopupConfirmation.VALIDER)){
		popup.addEventListener(benchmark.framework.ihm.popup.ObjPopupConfirmation.VALIDER,fonctionEcouteurValider);
	}
	if(!popup.hasEventListener(benchmark.framework.ihm.popup.ObjPopupConfirmation.ANNULER)){
		popup.addEventListener(benchmark.framework.ihm.popup.ObjPopupConfirmation.ANNULER,fonctionEcouteurAnnuler);
	}
	popup.ouvre(message);
	return popup;
};

/**
* fonction static ceerPopupInformation permet de creer une popup avec un texte et un bouton de validation
*<code>
* 		benchmark.framework.ihm.popup.ObjPopupFabrique.ceerPopupInformation(
*		 	'Votre photo a bien été enregistrée.'
*		);
*</code>
* @param message string le message a afficher
* @param fonctionEcouteurValider function la fonction a executer quand l'action est validée
* @param configuration l'objet de configuration
* @param object donnee supplémentaires optionnelles et seront donc disponibles dans l'evenement target.donnee des méthodes appelées
* @return un ObjPopUpConfirmation
*/
benchmark.framework.ihm.popup.ObjPopupFabrique.creerPopUpInformation = function(
	message,
	fonctionEcouteurValider,
	configuration,
	donnee
){
	configuration = benchmark.framework.util.objet.fusion({uniquementValidation : true}, configuration);
	var popup = new benchmark.framework.ihm.popup.ObjPopupConfirmation(configuration);
	popup.donnee = donnee;
	if(!popup.hasEventListener(benchmark.framework.ihm.popup.ObjPopupConfirmation.VALIDER)){
		popup.addEventListener(benchmark.framework.ihm.popup.ObjPopupConfirmation.VALIDER,fonctionEcouteurValider);
	}
	popup.ouvre(message);
	return popup;
};

/**
 * objet js permettant l'affichage d'une popup de confirmation en JS (équivalent skinnable de la fonction confirm() )
 *
 *	var popup = new benchmark.framework.ihm.ObjPopupConfirmation();
 * 	popup.addEventListener(benchmark.framework.ihm.ObjPopupConfirmation.VALIDER,function(event){console.log('VALIDER')});
 * 	popup.addEventListener(benchmark.framework.ihm.ObjPopupConfirmation.ANNULER,function(event){console.log('ANNULER')});
 *	popup.ouvre('Voulez-vous supprimer les parties sélectionnées');
 *
 *
 * @author Pierre Chabiland <chabiland@benchmark.fr>
 * @package bibliotheque
 * @subpackage html_include
 * @version 1.00
 * @since 25/03/2009 Pierre Chabiland <chabiland@benchmark.fr> creation
 */
/**
 * constructeur :
 * @param configuration objet de configuration (optionnel )
 * @return void
 */
benchmark.framework.ihm.popup.ObjPopupConfirmation= function(configuration){

	/**
	* @since 22/05/09 Damien BENOIT <benoit@benchmark.fr>
	* redéclaré ici car cet objet hérite de l'eventDispatcher et sans celà la liste reste mutualisée entre les instances
	* cf eventDispatcher.js pour de plus amples explications
	*/
	this._listeEvenenement = {};

	// @var array Configuration par défaut
	this.configurationDefaut = {
		boutonTexte1: 'Valider',
		boutonTexte2: 'Annuler',
		fermetureSurClic: false,
		boutonLargeur1: 53,
		boutonLargeur2: 56,
		uniquementValidation: false
	};

	// @var array configuration de la popup
	this.configuration = configuration;

	// @var object données supplémentaires optionnelles
	this.donnee = {};

	/**
	 * ouvre une popup avec un message particulier
	 * les lignes 1 et 2 ont été inverser pour avoir le bouton annuler en premier
	 */
	this.ouvre = function(message){

		var t = new Date().getTime();
		var id_valider = 'ObjPopupConfirmation_valider_'+t;
		var id_annuler = 'ObjPopupConfirmation_annuler_'+t;
		var contenu = '<div class="zone_bouton_genere" style="margin-bottom: 15px; font-size: 12px; text-align: center;">'+message+'</div>'+
		'<table class="tableau_bouton_genere" align="center"><tr>';
		if(!this.configuration.uniquementValidation){
			contenu += '<td><span class="bouton_genere_blanc" style="width: '+this.configuration.boutonLargeur2+'px;">'+
				'<a id="'+id_annuler+'">'+this.configuration.boutonTexte2+'</a>'+
				'<span class="bouton_genere_blanc_gauche"></span>'+
				'<span class="bouton_genere_blanc_droit"></span>'+
			'</span></td>';
		}
		contenu += '<td><span class="bouton_genere" style="width: '+this.configuration.boutonLargeur1+'px;">'+
		      '<a id="'+id_valider+'">'+this.configuration.boutonTexte1+'</a>'+
		      '<span class="bouton_genere_gauche"></span>'+
		      '<span class="bouton_genere_droit"></span>'+
		'</span></td>'+
		'</tr></table>';
		this.objpopup.ouvreHtml(contenu,this.configuration);
		$('#'+id_valider).bind('click',benchmark.framework.util.delegate(this,this._valider));
		if(!this.configuration.uniquementValidation){
			$('#'+id_annuler).bind('click',benchmark.framework.util.delegate(this,this._annuler));
		}
	};

	/**
	 * methode privée
 	 * ferme la popup et diffuse un evenement "Clique sur bouton valider"
 	 * @see benchmark.framework.ihm.popup.ObjPopupConfirmation.VALIDER
 	 * @return void
 	 */
	this._valider = function(){
		this.objpopup.ferme();
		this.dispatchEvent(new benchmark.framework.util.Event(benchmark.framework.ihm.popup.ObjPopupConfirmation.VALIDER));
	};

	/**
	 * methode privée
 	 * ferme la popup et diffuse un evenement "Clique sur bouton annuler"
 	 * @see benchmark.framework.ihm.popup.ObjPopupConfirmation.ANNULER
 	 * @return void
 	 */
	this._annuler = function(){
		this.objpopup.ferme();
		this.dispatchEvent(new benchmark.framework.util.Event(benchmark.framework.ihm.popup.ObjPopupConfirmation.ANNULER));
	};

	// Constructeur
	// configuration de la popup
	this.configuration = benchmark.framework.util.objet.fusion(configuration, this.configurationDefaut);
	this.objpopup = new benchmark.framework.ihm.ObjPopup(this.configuration);
};

benchmark.framework.ihm.popup.ObjPopupConfirmation.VALIDER = 'ObjPopupConfirmation_valider';
benchmark.framework.ihm.popup.ObjPopupConfirmation.ANNULER = 'ObjPopupConfirmation_annuler';
benchmark.framework.ihm.popup.ObjPopupConfirmation.prototype = new benchmark.framework.util.EventDispatcher;