/**
 * This class contains static utility methods.
 *
 * This class is dependent of the following JS classes:
 *		- YAHOO.util.Dom.get
 *		- YAHOO.util.Connect
 *		- YAHOO.widget.SimpleDialog
 *		- YAHOO.widget.Panel
 * 
 * @since 16th Jan 2007
 * @copyright Copyright (c) 2007 Complinet UK Ltd. (http://www.complinet.com)
 * @author Alexandre Arica <alexandre.arica@complinet.com>
 */
function WebUtility(){
}


/**
 * Set the style property 'display'  
 * of an element to 'block'.
 * @access public
 * @param string sElId An HTML element's id
 * @return null
 */
WebUtility.displayByElId = function (sElId){
	var oEl = null;
	if(oEl = YAHOO.util.Dom.get(sElId)){
		oEl.style.display = 'block';
	}
}


/**
 * Set the style property 'display'  
 * of an element to 'none'.
 * @access public
 * @param string sElId An HTML element's id
 * @return null
 */
WebUtility.hideByElId = function (sElId){
	var oEl = null;
	if(oEl = YAHOO.util.Dom.get(sElId)){
		oEl.style.display = 'none';
	}
}


/**
 * This method displays or hides an HTML element.
 * @access public
 * @param string sElId An HTML element's id
 * @return null
 */
WebUtility.displayHideByElId = function (sElId){

	var oEl = YAHOO.util.Dom.get(sElId);
	if(oEl.style.display == 'block'){
		oEl.style.display = 'none';
	}else{
		oEl.style.display = 'block';
	}
}


/**
 * Send a request through an XMLHttpRequest.
 * Eg.
 * WebUtility.sendRequest (sUriServerSideScript, { success: function(o){ WebUtility.innerHtml(sElId, o.responseText); }, failure: function(o){ alert('Failure innerHtmlAjax with the HTML\'s element-id '+sElId+' and the script\'s path '+sUriServerSideScript); } });
 * @access public
 * @param string sUriServerSideScript Url of the server side script
 * @param object oCallback Callback object which contains the function to call if success or failure
 *			     Eg. oCallback = { success: function(o){ alert('ok'); }, failure: function(o){ alert('Failure'); } }
 * @param string sMethod 'POST' or 'GET'
 * @return null
 */
WebUtility.sendRequest = function (sUriServerSideScript, oCallback, sMethod){
	if(!sMethod){
		sMethod = 'GET';
	}
	YAHOO.util.Connect.asyncRequest(sMethod, sUriServerSideScript, oCallback, null);
}


/**
 * Refrech a current page.
 * @access public
 * @param null
 * @return null
 */
WebUtility.refreshPage = function (){
	window.location.href = window.location.href;
}


/**
 * InnerHTML a string into an HTML element.
 * @access public
 * @param string sElId An HTML element's id
 * @param string sContent Content to add in an HTML element
 * @return null
 */
WebUtility.innerHtml = function (sElId, sContent){
	YAHOO.util.Dom.get(sElId).innerHTML = sContent;
}


/**
 * InnerHTML into an HTML element 
 * data from a server-side script.
 * @access public
 * @param string sElId An HTML element's id
 * @param string sUriServerSideScript Url of the server side script
 * @return null
 */
WebUtility.innerHtmlAjax = function (sElId, sUriServerSideScript){
	WebUtility.sendRequest (sUriServerSideScript, { success: function(o){ WebUtility.innerHtml(sElId, o.responseText); }, failure: function(o){ alert('Failure innerHtmlAjax with the HTML\'s element-id '+sElId+' and the script\'s path '+sUriServerSideScript); } });
}


/**
 * Display a message to a client in a 
 * dialog with an 'Ok' button and a title 
 * and an Icon. 
 * This static method has for aim to replace 
 * the conventionnal javascript's function 'alert()'.
 * @access public
 * @param string sMessage		Message to display in the dialog.
 * @param string sDialogIcon	Icon to display in the dialog.
 *										Possible values: 'ICON_ALARM', 'ICON_BLOCK', 'ICON_HELP', 'ICON_INFO', 'ICON_TIP', 'ICON_WARN'.
 *										The default value is 'ICON_INFO'.
 * @param string sTitle			Title to display in the dialog.
 * @param string sHandler  	Contains the handler function which 
 *										will be executed if a user click on 'Yes'
 *                         	(we will exec 'eval' on this string).
 * @return null
 */
WebUtility.alert = function (sMessage, sDialogIcon, sTitle, sHandler){

	// We select the icon to display in the dialog
	switch(sDialogIcon){
		case 'ICON_ALARM': {
			oDialogIcon = YAHOO.widget.SimpleDialog.ICON_ALARM;
			if(!sTitle){
				sTitle = 'Alarm';
			}
			break;
		}
		case 'ICON_BLOCK': {
			oDialogIcon = YAHOO.widget.SimpleDialog.ICON_BLOCK;
			if(!sTitle){
				sTitle = 'Blocked';
			}
			break;
		}
		case 'ICON_HELP': {
			oDialogIcon = YAHOO.widget.SimpleDialog.ICON_HELP;
			if(!sTitle){
				sTitle = 'Help';
			}
			break;
		}
		case 'ICON_TIP': {
			oDialogIcon = YAHOO.widget.SimpleDialog.ICON_TIP;
			if(!sTitle){
				sTitle = 'Tip';
			}
			break;
		}
		case 'ICON_WARN': {
			oDialogIcon = YAHOO.widget.SimpleDialog.ICON_WARN;
			if(!sTitle){
				sTitle = 'Warning';
			}
			break;
		}
		default: {
			oDialogIcon = YAHOO.widget.SimpleDialog.ICON_INFO;
			if(!sTitle){
				sTitle = 'Info';
			}
		}
	}

	// Instanciation of the YUI class 'YAHOO.widget.SimpleDialog'
	var oSimpleDialog = new YAHOO.widget.SimpleDialog("simpleDialog", 
															   {width: "300px", 
															    zIndex: 99,
															  	 fixedcenter: true, 
															  	 visible: false, 
															  	 draggable: false, 
															  	 close: false, 
															  	 text: sMessage,
															  	 icon: oDialogIcon,
															  	 constraintoviewport: true, 
															  	 buttons: [ { text:"OK", handler:function(){ oSimpleDialog.hide(); if(sHandler){ eval(sHandler); } }, isDefault:true } ] 
															  	 }
															  	);
	oSimpleDialog.setHeader(sTitle);
	oSimpleDialog.render(document.body);
	oSimpleDialog.show();
}


/**
 * Display a confirmation dialog to a client.
 * A dialog contains a title, a message and the
 * button 'Yes' and 'No'.
 * This static method has for aim to replace 
 * the conventionnal javascript's function 'confirm()'.
 * @access public
 * @param string sTitle		Title to display.
 * @param string sMessage	Message to display.
 * @param string sHandler  Contains the handler function which 
 *									will be executed if a user click on 'Yes'
 *                         (we will exec 'eval' on this string).
 * @return null
 */
WebUtility.confirm = function (sTitle, sMessage, sHandler){
	var oSimpleDialog = new YAHOO.widget.SimpleDialog("simpleDialog", 
															   {width: "300px", 
															    zIndex: 99,
															  	 fixedcenter: true, 
															  	 modal: true,
															  	 visible: false, 
															  	 draggable: false, 
															  	 close: true, 
															  	 text: sMessage,
															  	 icon: YAHOO.widget.SimpleDialog.ICON_HELP,
															  	 constraintoviewport: true, 
															  	 buttons: [ { text:"Yes", handler:function(){ oSimpleDialog.hide(); eval(sHandler); } }, 
															  	 				{ text:"No",  handler:function(){ oSimpleDialog.hide(); }, isDefault:true } ]
															  	 }
															  	);													  	
	oSimpleDialog.setHeader(sTitle);
	oSimpleDialog.render(document.body);
	oSimpleDialog.show();
}


/**
 * This class allows to display or to hide a loading panel.
 *
 * Usage:
 *
 * // We create an object (the argument is an HTML element's id 
 * // inside which we will add the loading panel's content)
 * oWebLoadingPanel = new WebLoadingPanel ('HtmlElementId');
 *
 * // We display the panel
 * oWebLoadingPanel.display();
 *
 * // We hide the panel
 * oWebLoadingPanel.hide();
 * 
 * @since 16th Jan 2006
 * @version 1.0 modified the 18h Jan 2007
 * @author Alexandre Arica <alexandre.arica@complinet.com>
 * @param string sElId An HTML element's id inside which we will add the loading panel's content
 */
function WebLoadingPanel(sElId){
	// Initialize the temporary Panel to display while waiting for external content to load 
	this._oWidgetPanel = new YAHOO.widget.Panel("webLoadingPanel_"+sElId,
															  {width:"240", 
	 															draggable: false,
															  	zIndex: 0,
															 	visible: false,
															 	underlay:"none"}
															 );
	this._oWidgetPanel.setHeader("Loading, please wait..."); 
	this._oWidgetPanel.setBody('<img src="'+WebUtility.sImgPath+'/loading_bar.gif" />');
	this._oWidgetPanel.render(sElId);
	
	// We modify the style of a current loading panel
	oEl = YAHOO.util.Dom.get("webLoadingPanel_"+sElId);
	oEl.style.margin = '3px';
	oEl.style.marginLeft = '70px';
}

WebLoadingPanel.prototype.display = function (){
	this._oWidgetPanel.show();
}

WebLoadingPanel.prototype.hide = function (){
	this._oWidgetPanel.hide();
}

