/*
Copyright (c) 2007, PMP Concept
version: 1
*/

/*****************************************************************************
 *
 * 								Classe pmpMsgBox
 *
 *	Fonction : 	affichage d'une boite de dialogue
 *
 *  Paramètres
 *		- message	 	: (string) le texte de la boite de dialogue
 *		- type		 	: (string) le type de la boite de dialogue, facultatif
 *  	- titre			: (string) le titre de la boite de dialogue
 *		- icone		 	: (string) l'url de l'icone de la boite de dialogue, facultatif
 *
*****************************************************************************/

/* inclusion des dépendances */
PMP.include("librairiesjs/popup.js");


PMP.util.pmpMsgBox = function(message, type, titleBar/*, icone, viewDefaultBouton, defaultBoutonCaption*/) 
{
	// type de message prédéfinis
	this.types = new Array("defaut", "information", "question", "avertissement", "erreur", "confirmation");
	// propriétés de la fenetre
	this.className = "pmpMsgBox";	// "pmpMsgBox " + this.type;
	this.keypress = null;			// la fenetre remplace l'action a effectue au document.keypress par l'action associée au bouton selectionné, 
									// on sauvegarde donc l'évènement afin de le restaurer à la fermeture de la fenêtre
	
	this.selectedBouton;			// Bouton actif de la fenetre
	this.modal = true;				// fenêtre modale ou non

	// propriétés attendues
	this.message = PMP.common.isString(message) ? message : "";
	this.type="defaut";
	if(PMP.common.isString(type))
	{
		this.type = PMP.common.isString(type) && type.length>0 && this.types.join(",").search(type) >= 0 ? type : "defaut";
	}
	else if(PMP.common.isObject(type))
	{
		this.type = PMP.common.isString(type.type) && type.type!= "" && this.types.join(",").search(type.type) >= 0 ? type.type : "defaut";
		
		if(PMP.common.isBoolean(type.modal))
			this.modal = type.modal;
	}
	

	// propriétés optionnelles de la barre de titre
	//initialisation des valeurs par défaut
	this.titre = this.type!="defaut" ? this.type : "";
	this.icone = "";
	if(PMP.common.isString(titleBar))
	{
		this.titre = titleBar;
	}
	else if(PMP.common.isObject(titleBar))
	{
		if(PMP.common.isString(titleBar.titre))
			this.titre = titleBar.titre;
		
		if(PMP.common.isString(titleBar.icone))
			this.icone = titleBar.icone;
	}
	

	// ajout de la fenetre dans la liste des fenetres du document
	//if( PMP.common.isUndefined(window.document.pmpMsgBoxes) )
	//	window.document.pmpMsgBoxes = new Array();
	this.index = PMP.util.pmpMsgBox.count;
	PMP.util.pmpMsgBox.count++;
	//window.document.pmpMsgBoxes[this.index] = this;
	
	
	// création du code HTML de la fenetre
	this.msgBoxContainer = document.createElement("div");
	this.msgBoxContainer.setAttribute("id", 'pmpMsgBox'+this.index);
	this.msgBoxContainer.setAttribute("class", this.className);

	this.msgBoxType = document.createElement("div");
	this.msgBoxType.setAttribute("class", this.className+"-"+this.type);
	
	this.titleBar = document.createElement("div");
	this.titleBar.setAttribute("id", 'titlebar'+this.index);
	this.titleBar.setAttribute("class", "titlebar");
	//this.titleBar.setAttribute("class", this.className+"-"+this.type+"-"+"titlebar");
	this.titleBar.innerHTML = '<div id="icon" class="icone">&nbsp;</div><div id="titre" class="titre">' + this.titre + '</div>';
	
	this.messageArea = document.createElement("div");
	this.messageArea.setAttribute("id", 'messagearea'+this.index);
	this.messageArea.setAttribute("class", this.className+"-"+this.type+"-"+"messagearea");
	this.messageArea.innerHTML = '<div id="message">' + this.message + '</div>';
	
	this.buttonsbar = document.createElement("div");
	this.buttonsbar.setAttribute("id", 'buttonsbar'+this.index);
	this.buttonsbar.setAttribute("class", "buttonsbar");
	
	// récupération des boutons en paramètres
	this.boutons = new Array();
	this.boutonDefaut = false;
	this.boutonCancel = false;
	this.boutonOk = false;
	var args = PMP.util.pmpMsgBox.arguments;
	for(var i=3; i<args.length; i++)
	{
		if(PMP.common.isObject(args[i]))
		{
			args[i].parent = this;
			var bouton = new PMP.util.pmpMsgBoxButton(args[i]);
			bouton.parent = this;
			this.boutons.push(bouton);

			// vérification du bouton par défaut
			if(args[i].selected)
				this.boutonDefaut = bouton;

			if(args[i].cancel)
				this.boutonCancel = bouton;

			if(args[i].ok)
				this.boutonOk = bouton;
		}
	}
	
	// si aucun bouton n'est passé en paramètre, on ajoute un bouton par défaut
	if(this.boutons.length == 0)
	{
		var bouton = new PMP.util.pmpMsgBoxButton("OK", "", true, "ESC", null);
		bouton.parent=this;
		this.boutons.push(bouton);
		this.boutonDefaut = bouton;
		this.boutonCancel = bouton;
		this.boutonOk = bouton;
	}

	// ajout des boutons a la fenetre
	for(var i=0; i < this.boutons.length; i++)
	{
		// si le bouton n'a pas d'id on en crée un par défaut
		if(this.boutons[i].content.id.length==0)
			this.boutons[i].content.id = "msgbox"+this.index+"_bouton"+i;
		//this.boutons[i].content.setAttribute("tabindex", this.index*1000+i);
	
		this.buttonsbar.appendChild(this.boutons[i].content);
	}

	this.msgBoxType.appendChild(this.titleBar);
	this.msgBoxType.appendChild(this.messageArea);
	this.msgBoxType.appendChild(this.buttonsbar);

	this.msgBoxContainer.appendChild(this.msgBoxType);


	this.show();
}


PMP.util.pmpMsgBox.prototype = {
	
	 setTitre : function(titre) {
		this.titre = titre;
	},

	getTitre : function() {
		return this.titre;
	},

	addBouton : function(caption, action, className, id, closeMsgBox) 
	{
		if( PMP.common.isUndefined(className) || className=="" )
			var className = "button";

		var bouton = new PMP.util.pmpMsgBoxButton(caption, action, closeMsgBox, "", className, PMP.common.isString(id) && id.length>0 ? id : "msgbox"+this.index+"_bouton"+(this.boutons-1), this);
		bouton.parent=this;
		if(this.buttonsbar)
			this.buttonsbar.appendChild(bouton.content);
		bouton.init();
		this.boutons.push(bouton);
	},

	show : function() 
	{
		if(PMP.common.isUndefined(this.popup))
			this.popup = PopupManager.createPopup(this.msgBoxContainer, null, this.modal);
		this.popup.focus = this.focus.bind(this);

		// récupération des pointeurs sur objet dans le DOM
		this.msgBoxContainer = document.getElementById(this.msgBoxContainer.id);
		this.titleBar = document.getElementById(this.titleBar.id);
		this.buttonsbar = document.getElementById(this.buttonsbar.id);
		this.content = document.getElementById(this.messageArea.id);
		
		/* DRAG AND DROP SUR LA BARRE DE TITRE */
		this.popup.setDraggable(this.titleBar);
		
		/* GESTION DES BOUTONS : RACCOURCIS CLAVIER ET INITIALISATION */
		for(var i=0; i < this.boutons.length; i++)
		{
			// RAFFRAICHISSEMENT DU BOUTON
			//this.boutons[i].content = document.getElementById(this.boutons[i].content.id);
			this.boutons[i].init();
		}

		
		// sauvegarde de l'évenement onkeypress de la page
		this.keydown = window.document.onkeydown;
		this.keyup = window.document.onkeyup;
		this.keypress = window.document.onkeypress;
		document.onkeydown = this.onkeydown.bind(this);
		document.onkeyup = this.onkeyup.bind(this);
		document.onkeypress = this.onkeypress.bind(this);

		// affichage de la popup
		this.popup.show();
	},

	focus : function() 
	{
		if(this.boutonDefaut)
		{
			this.boutonDefaut.focus();
		}
	},

	hide : function() 
	{
		window.document.onkeydown = this.keydown;
		window.document.onkeyup = this.keyup;
		window.document.onkeypress = this.keypress;
		
		//this.popup.hide();
		PopupManager.removePopup(this.popup);

		PMP.util.pmpMsgBox.count--;
	},

	previousBouton : function() 
	{
		if(!this.selectedBouton)
			return;

		/* GESTION DES BOUTONS : RACCOURCIS CLAVIER ET INITIALISATION */
		for(var i=0; i < this.boutons.length; i++)
		{
			if(this.selectedBouton == this.boutons[i] && i > 0)
			{
				this.boutons[i-1].focus();
				break;
			}
		}
	},

	nextBouton : function() 
	{
		if(!this.selectedBouton)
			return;

		/* GESTION DES BOUTONS : RACCOURCIS CLAVIER ET INITIALISATION */
		for(var i=0; i < this.boutons.length; i++)
		{
			if(this.selectedBouton == this.boutons[i] && i < this.boutons.length-1)
			{
				this.boutons[i+1].focus();
				break;
			}
		}
	},

	onkeypress : function (evt)
	{
		var code;

		var pmpEvt = new PMP.util.pmpEvent();
		pmpEvt.getEvent(evt);

		if (pmpEvt.event.keyCode) 
			code = pmpEvt.event.keyCode;
		else if (pmpEvt.event.which) 
			code = pmpEvt.event.which;
		
		for(var i=0; i < this.boutons.length; i++)
		{
			if( code == this.boutons[i].keyCode )
			{
				this.boutons[i].focus();
				this.boutons[i].click();
				pmpEvt.cancelEvent();
				return;
			}
		}


		// si le raccourci ne correspond à aucun bouton, on vérifie les boutons OK / CANCEL
		if(code==13 && this.boutonOk) 				// si la touche entrée est pressé et qu'un bouton est défini comme le bouton de validation
		{
			this.boutonOk.focus();
			this.boutonOk.click();
		}
		else if(code==27 && this.boutonCancel)	// si la touche escape est pressé et qu'un bouton est défini comme le bouton d'annulation
		{
			this.boutonCancel.focus();					
			this.boutonCancel.click();					
		}
	},

	onkeydown : function (evt)
	{
		
	},

	onkeyup : function (evt)
	{
		
	}
}

PMP.util.pmpMsgBox.count = 0;



PMP.util.pmpMsgBoxButton = function(caption, action, close, keyCode, upClass, id, parent) 
{
	// Initialisation des attributs
	this.hasFocus = false;

	var args = PMP.util.pmpMsgBoxButton.arguments;
	if(args.length==1 && PMP.common.isObject(args[0]))
	{
		this.id = PMP.common.isString(args[0].id) ? args[0].id : "";
		this.caption = PMP.common.isString(args[0].caption) ? args[0].caption : "";
		this.action = PMP.common.isString(args[0].action) ? args[0].action : "";
		this.upClass = PMP.common.isString(args[0].upClass) ? args[0].upClass : "button";
		this.overClass = PMP.common.isString(args[0].overClass) ? args[0].overClass : "buttonOver";
		this.downClass = PMP.common.isString(args[0].downClass) ? args[0].downClass : "buttonDown";
		this.close = PMP.common.isBoolean(args[0].close) ? args[0].close : PMP.common.isUndefined(args[0].close) ? true : false;
		this.keyCode = PMP.common.isString(args[0].keyCode) || PMP.common.isNumber(args[0].keyCode) ? args[0].keyCode : "";
		this.parent = !PMP.common.isNull(args[0].parent) ? args[0].parent : null;
	}
	else
	{
		this.id = PMP.common.isString(id) ? id : "";
		this.caption = PMP.common.isString(caption) ? caption : "";
		this.action = PMP.common.isString(action) ? action : "";
		this.upClass = PMP.common.isString(upClass) ? upClass : "button";
		this.overClass = "buttonOver";
		this.downClass = "buttonDown";
		this.close = PMP.common.isBoolean(close) ? close : PMP.common.isUndefined(close) ? true : false;
		this.keyCode = PMP.common.isString(keyCode) || PMP.common.isNumber(keyCode) ? keyCode : "";
		this.parent = !PMP.common.isNull(parent) ? parent : null;
	}	

	
	if(PMP.common.isString(this.keyCode))
	{
		if(this.keyCode=="ESC" || this.keyCode=="ESCAPE")
			this.keyCode = 27;
		else if(this.keyCode=="ENTER" || this.keyCode=="RETURN")
			this.keyCode = 13;
		else
		{
			this.keyCode = this.keyCode.charCodeAt(0);
			if(!PMP.common.isNumber(this.keyCode))
				this.keyCode = -1;
		}
	}


	this.content = document.createElement("span");
	this.content.setAttribute("id", this.id);
	this.content.setAttribute("class", this.upClass);
	this.content.setAttribute("tabindex", 1000);
	this.content.setAttribute("onclick", "alert('onclick');");
	this.content.innerHTML = this.caption;
	
	/*
	this.onclick = function (evt)
	{
		var val = eval(this.action + (PMP.common.isString(this.parent) && this.close ? this.parent+".hide();" : "" ));

		if(this.close && PMP.common.isObject(this.parent))
			this.parent.hide();

		return val;
	}*/
	
	this.over = function (evt)
	{
		this.content.className = this.overClass;
	}
	
	this.out = function (evt)
	{
		if(!this.hasFocus)
			this.content.className = this.upClass;
	}
	
	this.mouseDown = function (evt)
	{
		this.content.className = this.downClass;
	}
	
	this.mouseUp = function (evt)
	{
		this.content.className = this.upClass;
	}
	
	this.keyDown = function (evt)
	{
		var pmpEvt = new PMP.util.pmpEvent();
		pmpEvt.getEvent(evt);
	
		var code;
		if (pmpEvt.event.keyCode) 
			code = pmpEvt.event.keyCode;
		else if (pmpEvt.event.which) 
			code = pmpEvt.event.which;
		
		switch(code)
		{
			// entrée
			case 13:
				this.click();
				break;

			// fleche gauche
			case 37:
				this.parent.previousBouton();
				break;

			// fleche haut
			case 38:
				break;

			// fleche droite
			case 39:
				this.parent.nextBouton();
				break;

			// fleche bas
			case 40:
				break;
		}
		
	}
	
	this.keyUp = function (evt)
	{
	
	}

	this.click = function (evt)
	{
		var val = eval(this.action + (PMP.common.isString(this.parent) && this.close ? this.parent+".hide();" : "" ));
		
		if(this.close && PMP.common.isObject(this.parent))
			this.parent.hide();

		return val;
	}

	this.focus = function ()
	{
		this.parent.selectedBouton = this;
		this.hasFocus = true;
		this.content.focus();
		//setTimeout("document.getElementById('"+this.content.id+"').focus();",50);
	}

	this.blur = function ()
	{
		this.hasFocus = false;
		this.out();
	}

	/* GETTER / SETTER */
	//this.__defineGetter__("value", function(){ return value; });
	//this.__defineSetter__("value", function(val){ value = val; });
}


PMP.util.pmpMsgBoxButton.prototype = {
	
	/*
	// getter 
	get value : function()
	{
		return this._value;
	},

	// setter
	set value : function(val)
	{
		this._value = val;
	},
	*/
	init : function() {
		this.content = document.getElementById(this.content.id);
		
		this.content.onclick = this.click.bind(this);
		this.content.onmouseover = this.over.bind(this);
		this.content.onmouseout = this.out.bind(this);
		this.content.onmousedown = this.mouseDown.bind(this);
		this.content.onmouseup = this.mouseUp.bind(this);
		this.content.onkeydown = this.keyDown.bind(this);
		this.content.onkeyup = this.keyUp.bind(this);
		this.content.onfocus = this.over.bind(this);
		this.content.onblur = this.blur.bind(this);
	}
}


function pmpConfirm(message, titre, fonction)
{
	var msgbox = new PMP.util.pmpMsgBox(message, "question", titre, {caption:"NON", keyCode:"n", selected:true, cancel:true}, {caption:"OUI", action:fonction, keyCode:"o"});
}


function pmpAlert(message, titre, type)
{
	var msgbox = new PMP.util.pmpMsgBox(message, type, titre, {caption:"OK", keyCode:"O", selected:true, cancel:true, ok:true});
}
