/**
 * TeqMessageBox
 * Utile pour afficher une alerte ou un prompt en utilisant la librairie YUI.
 * 
 * @method AlertBox : affiche une dialog box modale de type Alert 
 * 		@param config :  {titre:"..", message:"..", valeur:"..", btnOk:".."}
 * 
 * @method PromptBox : affiche une dialog box modale de type prompt 
 * 		@param config :  {titre:"..", message:"..", valeur:"..", btnOk:"..", btnCancel:"..", callBackFunc:callBackFunc }
 *  	callBackFunc doit etre def comme suit :
 *  	function maCallBackFunc (btn, text) { ... } => btn : (OK|CANCEL) , text:valeur entre par l'utilisateur 	
 */
function TeqMessageBox() {	
	// Intrenal
	var yuiDialog = null;
	var PromptBoxCallbackFunc = '';	

	// On rajoute l'elt qui va contenir le dialog box
	if (!document.getElementById('TeqDialogBox')) {
		var AddDialogHolder = function () {
			var dialogHolder = document.createElement('div');
			dialogHolder.setAttribute('id', 'TeqDialogBox');
			document.body.appendChild(dialogHolder);
		}
		YAHOO.util.Event.addListener(window, 'load', AddDialogHolder);	
	}
		
	/* Handlers */
	var AlertBox_handleOK = function () {
		yuiDialog.hide();
	}
	var PromptBox_handleOK = function () {
		yuiDialog.hide();
		
		dialogBtn 	= 'OK';
		dialogText  = document.getElementById('TeqDialogBoxInput').value; 		
		if ((typeof PromptBoxCallbackFunc == 'function')) {
			eval('(' + PromptBoxCallbackFunc + '("'+ dialogBtn +'", "'+ dialogText +'")' +  ')');
		}
		else {
			alert('TeqMessageBox | Erreur : La fonction callback <'+PromptBoxCallbackFunc+'> est introuvable!');			
		}
	}
	var PromptBox_handleCANCEL = function () {
		yuiDialog.hide();
		
		dialogBtn 	= 'CANCEL';
		dialogText  = document.getElementById('TeqDialogBoxInput').value; 		
		if ((typeof PromptBoxCallbackFunc == 'function')) {
			eval('(' + PromptBoxCallbackFunc + '("'+ dialogBtn +'", "'+ dialogText +'")' +  ')');
		}
		else {
			alert('TeqMessageBox | Erreur : La fonction callback <'+PromptBoxCallbackFunc+'> est introuvable!');			
		}
	}

	
	/* Affiche un alert !!MODALE!! en utilisant une boite de dialog YAHOO */
	this.AlertBox = function (config) {
		
		// Initialisation de la boite de dialogue
		if (yuiDialog != null) yuiDialog = null;
		yuiDialog = new YAHOO.widget.Dialog("TeqDialogBox", {
			width:"300px",
			visible:false,
			fixedcenter:true,
			modal:true,
			constraintoviewport:true,
			buttons : [ { text:config.btnOk, handler:AlertBox_handleOK, isDefault:true}]
		} );
		
		yuiDialog.setHeader(config.titre);
		yuiDialog.setBody(config.message);
		yuiDialog.render();
		yuiDialog.show();
	};
	
	
	
	/* Affiche un prompt !!MODALE!! en utilisant une boite de dialog YAHOO, en remplit le champ avec une valeur initiale (valeur), le resultat */	
	this.PromptBox = function (config) {	

		// Fonctions call backs		
		PromptBoxCallbackFunc = config.callBackFunc;		
		
		// Initialisation de la boite de dialogue
		if (yuiDialog != null) yuiDialog = null;
		yuiDialog = new YAHOO.widget.Dialog("TeqDialogBox", {
			width:"300px",
			visible:false,
			fixedcenter:true,
			modal:true,
			constraintoviewport:true,
			buttons : [ { text:config.btnOk, handler:PromptBox_handleOK}, { text:config.btnCancel, handler:PromptBox_handleCANCEL, isDefault:true}]
		} );
		
		yuiDialog.setHeader(config.titre);
		yuiDialog.setBody(	'&raquo;&nbsp;'
							+ config.message 
							+ '<br><p style="margin-top:20px;">' 
							+ '<input style="width:250px;" id="TeqDialogBoxInput" type="text" value="'
							+ config.valeur 
							+ '" /></p>'
		);
		yuiDialog.render();
		yuiDialog.show();
		
		// text input focus
		document.getElementById('TeqDialogBoxInput').focus();
		document.getElementById('TeqDialogBoxInput').select();
	};
	
}
YAHOO.namespace("TEQ");
YAHOO.TEQ.MessageBox = new TeqMessageBox();


/**
 *
 * Supplant : remplace {name} par obj.name
 *	exp : str.supplant(id:5, color:'blue') ou str = '<span id="{id}" style="color:{color}">';
 */
String.prototype.supplant=function(o){return this.replace(/{([^{}]*)}/g,function(a,b){var r=o[b];return typeof r==='string'||typeof r==='number'?r:a;});};
String.prototype.trim=function(){return this.replace(/^\s*(\S*(\s+\S+)*)\s*$/,"$1");};


String.prototype.truncMe = function(length, truncation) {
    length = length || 35;
    truncation = truncation === undefined ? '...' : truncation;
    return this.length > length ?
      this.slice(0, length - truncation.length) + truncation : this;
}