/*
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
 *
*****************************************************************************/


PMP.util.pmpMsgBox = function(message, type, titre, icone, defaultBoutonCaption, viewDefaultBouton) 
{
	this.typeArray = new Array("information", "question", "avertissement", "erreur", "confirmation");

	this.message = message;
	this.type = PMP.common.isString(type)  && this.typeArray.join(",").search(type) >= 0 ? type : "information";
	this.titre = PMP.common.isString(titre) ? titre : type;
	this.icone = icone;
	this.defaultBoutonCaption = PMP.common.isString(defaultBoutonCaption) ? defaultBoutonCaption : "OK";
	this.viewDefaultBouton = PMP.common.isBoolean(viewDefaultBouton) && !viewDefaultBouton ? false : true;
	this.className = "pmpMsgBox";
	this.keypress = null;	

	this.popup = new Popup();
	
	// 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 = window.document.pmpMsgBoxes.length;
	window.document.pmpMsgBoxes[this.index] = this;
	
	this.template =	'<div id="fenetre" class="'+this.className+'">'
				  +		'<div id="titlebar"></div>'
				  +		'<div id="messagearea">'
				  +		'<div id="icon"></div><div id="titre">' + this.titre + '</div>'
				  +			'<div id="message" class="' + this.type + '">' + this.message + '</div>'
				  +		'</div>'
				  +		'<div id="buttonsbar">'
				  //+			'<input type="image" id="bouton_close" class="button" alt="Fermer" onclick="document.pmpMsgBoxes['+this.index+'].hide();" />'
				  +		(this.viewDefaultBouton ? '<span id="bouton_default" class="button" onclick="window.document.pmpMsgBoxes['+this.index+'].hide();">' + this.defaultBoutonCaption +'</span>' : "")
				  +		'</div>'
				  +		'<div id="bottombar"></div>'
				  +	'</div>';
	
	this.popup.addContent(this.template);
	this.show();
	this.dragdrop = new PMP.util.pmpDragDrop("outerImageContainer", "titlebar");
}


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

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

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

		var bouton = new PMP.util.pmpMsgBoxButton(caption, action, className, id, "window.document.pmpMsgBoxes["+this.index+"]");
		var buttonsbar = document.getElementById("buttonsbar");
		if(buttonsbar)
			buttonsbar.innerHTML = bouton.template + buttonsbar.innerHTML;
	},

	show : function() {
		this.popup.show();
		
		this.bouton_close = document.getElementById("bouton_close");
		if( this.bouton_close )
		{
			/*var me = this;
			this.bouton_close.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;
				
				if( code == 13 || code == 27 )
				{
					me.hide();
					pmpEvt.cancelEvent();
				}
			}
			this.bouton_close.focus();
			*/
			
			this.keypress = window.document.onkeypress;
			var me = this;
			document.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;
				
				if( code == 13 || code == 27 )
				{
					me.hide();
					pmpEvt.cancelEvent();
				}
			}
			
		}
	},

	hide : function() 
	{
		if(!PMP.common.isNull(this.keypress))
			window.document.onkeypress = this.keypress;
		this.popup.hide();
	}

}



PMP.util.pmpMsgBoxButton = function(caption, action, className, id, parent) 
{
	this.id = PMP.common.isString(id) ? id : "";
	this.caption = PMP.common.isString(caption) ? caption : "";
	this.action = PMP.common.isString(action) ? action : "";
	this.className = PMP.common.isString(className) ? className : "";
	
	this.template =	'<span id="' + this.id + '" class="' + this.className + '" onclick="' + (!PMP.common.isUndefined(parent) ? parent+".hide();" : "" ) + this.action + '">' + this.caption + '</span>';
					//'<input type="image" id="bouton_close" class="button" alt="Fermer" onclick="document.pmpMsgBoxes['+this.index+'].hide();" />'
}


function pmpConfirm(message, titre, fonction)
{
	var msgbox = new PMP.util.pmpMsgBox(message, "question", titre, null, "NON");
	msgbox.addBouton("OUI", fonction);
}