﻿/************************************************************************************************************************************************************************

								GESTIONNAIRE DE L'APPLICATION

*************************************************************************************************************************************************************************/
function MasterApplication(  )
{
	this.oZonesApplication = new Array(  );
	
	
	this.oModalDialogBox = null;
	this.oDialogBoxsOpening = new Array();
	
	this.oLoading = null;
	
	
}

MasterApplication.prototype.addZoneApplication = function( p_oNode )
{
	var oZone = new ZoneApplication( p_oNode );
	this.oZonesApplication[ this.oZonesApplication.length ] = oZone;
	return oZone;
}



MasterApplication.prototype.setPage  = function( p_oNodeZone, p_sUrl )
{
	if( p_oNodeZone==null )	return;
	//this.removeAllDialogBox(  );
	
	
	//A chaque chargement d'une page l'ensemble des boites se ferme
	//this.removeAllDialogBox(  );
	
	var oCurrentZone = this.getZone( $( p_oNodeZone).attr( "id" ) );
	if( oCurrentZone == null )
		oCurrentZone = this.addZoneApplication( p_oNodeZone ); 
	
	
	oCurrentZone.loadUrl( { url : p_sUrl } );
	return oCurrentZone;
}

MasterApplication.prototype.openWindow  = function( options )
{
	//Si modal on affiche la fenetre en fin de chargement
	//Sinon on affiche la fenetre tout de suite avec un loading à l'intérieur
	var p_url = "";
	var p_height = null;
	var p_width = null;
	var p_title = null;
	var p_onButtonYes = null;
	var p_buttonYesDisabled  = false;
	var p_buttonYesTitle  = "Valider";
	var p_buttonNoTitle  = "Annuler";
	var p_disabledModal = false;
	var p_iconeSrc = "";
	var _this = this;
	
	if( typeof options != "undefined" )
	{
		if( typeof options.disabledModal != "undefined" )			p_disabledModal = options.disabledModal;
		if( typeof options.url != "undefined" )					p_url = options.url;
		if( typeof options.height != "undefined" )				p_height = options.height;
		if( typeof options.width != "undefined" )				p_width = options.width;
		if( typeof options.title != "undefined" )				p_title = options.title;
		if( typeof options.onButtonYes != "undefined" )				p_onButtonYes = options.onButtonYes;
		if( typeof options.buttonYesTitle != "undefined" )			p_buttonYesTitle = options.buttonYesTitle;
		if( typeof options.buttonYesDisabled != "undefined" )			p_buttonYesDisabled = options.buttonYesDisabled;
		if( typeof options.buttonNoTitle != "undefined" )			p_buttonNoTitle = options.buttonNoTitle;
		if( typeof options.iconeSrc != "undefined" )				p_iconeSrc = options.iconeSrc;
	}
	
	var oCurrentDialogBox = new dialogBox({
						parent :		document.body,
						title :			p_title,
						height :		p_height,
						width :			p_width,
						templateHTML : 		DEFAULT_DIALOGBOX_TEMPLATE_EDIT_HTML,
						templateSrc : 		DEFAULT_DIALOGBOX_TEMPLATE_EDIT,
						isModal :		!p_disabledModal,
						iconeSrc :		p_iconeSrc
						});
	
	
	//pour éviter d'afficher le loading si la fenetre est modal
	var oZoneApplication = new ZoneApplication( oCurrentDialogBox.oDialogboxContent, { disabledLoading : !p_disabledModal } );
	this.addZoneApplication( oZoneApplication );	
	
	//Buttons
	var oFnClose = function(  )
			{
				oZoneApplication.EndLoading();
				oCurrentDialogBox.remove();
				//_this.unlock;
			};
			
	var oFnConfirm = function(  )
			{
				p_onButtonYes.call();
				oZoneApplication.EndLoading();
				oCurrentDialogBox.remove();
			}

	oCurrentDialogBox.setButton({
				position : "left",
				value : p_buttonNoTitle,
				click : oFnClose
				});
	oCurrentDialogBox.setClose( oFnClose );	
	
	

	
	oCurrentDialogBox.setButton({
				position : "right",
				value : p_buttonYesTitle,
				click : oFnConfirm
				});
	
	
	
	this.oDialogBoxsOpening[ this.oDialogBoxsOpening.length ] = oCurrentDialogBox;
	
	//Si pas modal on affiche la boite dès le départ
	if( p_disabledModal )
	{
		oCurrentDialogBox.show(  );
	}
	//Sinon on affiche à la fin du chargement
	else	
		this.lock(  );
	
	
	if( p_buttonYesDisabled )
	{
		oCurrentDialogBox.getButton( "right" ).remove();
	}
	
	//Chargement de l'URL
	var oOptions = {
				url :  p_url,
				responseExecBegin : 	function()
							{
								//oCurrentDialogBox.show(  );
								//oCurrentDialogBox.hide(  );
							},
				responseExecFinish : 	function()
							{
								
								if( !p_disabledModal )
								{
									_this.unlock()
									oCurrentDialogBox.show();
								}
							},

				responseErrorBegin : function(  )
							{
									_this.unlock();
									oCurrentDialogBox.remove();
							},
				errorButtonNo : 	function(  )
							{
								oCurrentDialogBox.remove();
								_this.unlock();
							},
				errorButtonYes : 	function(  )
							{
								oCurrentDialogBox.remove();
								_this.unlock();
							}	
	}
	
	oZoneApplication.loadUrl( oOptions );
	return oCurrentDialogBox;

}
//Gère l'ensemble du process de sauvegarde
MasterApplication.prototype.save = function( params )
{
	var sMessageLoad = DEFAULT_MESSAGE_SAVE_LOAD;
	var sMessageSuccess = DEFAULT_MESSAGE_SAVE_SUCCESS;
	var sMessageError = DEFAULT_MESSAGE_SAVE_ERROR;
	
	if( params.action == "delete" )	
	{
		sMessageLoad 	= DEFAULT_MESSAGE_DELETE_LOAD
		sMessageSuccess = DEFAULT_MESSAGE_DELETE_SUCCESS;
		sMessageError 	= DEFAULT_MESSAGE_DELETE_ERROR;
	}
	
	var oParentContainer = document.body;
	if( typeof params.parent != "undefined" )
		oParentContainer = params.parent;
	
	//Affichage de la boite de chargement de la sauvegarde
	var oDialogBoxSave = new dialogBox({
			parent : oParentContainer,
			content : sMessageLoad,
			height : 50,
			width : 370,
			templateHTML : DEFAULT_DIALOGBOX_TEMPLATE_LOADING_HTML,
			templateSrc : DEFAULT_DIALOGBOX_TEMPLATE_LOADING
		});
		
	if( typeof params.disabledDialogBox != "undefined" && !params.disabledDialogBox )	
	{
		oDialogBoxSave.show(  );
	}
	
	var oZoneExecSave = new ZoneApplication( oParentContainer );	
	
	var bAsynchrone = true;
	if( params.async == false )	bAsynchrone = false;
	
	
	var oCurrentEvent = null;
	var options = {
		url : params.url,
		data : params.data,
		async : bAsynchrone,
		isPostForm : true,
		
		//Dans tous les cas on la boite de Save Loading 
		responseErrorFinish : function(  )
		{
			oDialogBoxSave.remove(  );			
		},
		
		successExecFinish : function(  )
		{	
			oCurrentEvent = oZoneExecSave.getLastEvent(  )
			
			
			var isSave = false;
			if( typeof oCurrentEvent.result == "string" ) //Text
			{
				if( oCurrentEvent.result.indexOf( "<result>1</result>" ) > 0 )	isSave = true;
			}
			else //Xml 
			{
				try
				{
					var sResult = $( oCurrentEvent.result ).find( "result" ).text();
					if( sResult == "1" )	isSave = true;
				}
				catch( e )
				{
					
				}
				
			}
			
			
			oDialogBoxSave.remove(  );
			if( isSave  ) //Confirmation
			{
				//boite de dialog de success
				var oDialogBoxConfirm = new dialogBox({
						parent : oParentContainer,
						content : sMessageSuccess,
						height : 50,
						width : 370,
						title : "Confirmation",
						templateHTML : DEFAULT_DIALOGBOX_TEMPLATE_EDIT_HTML,
						templateSrc : DEFAULT_DIALOGBOX_TEMPLATE_EDIT
					});
				
				var fnClose = function(){
								oDialogBoxConfirm.remove();
								
								if( typeof params.success != "undefined" )	
									params.success.call(  );
								
								//oMASTER_APPLICATION.getZone( "MasterContent" ).reload(  );
							}
				
				oDialogBoxConfirm.setClose( fnClose );
				oDialogBoxConfirm.setButton( { position:"left", value:"Fermer", click :fnClose } );
				
				if( typeof params.disabledDialogBox == "undefined" || !params.disabledDialogBox )	
				{
					oDialogBoxConfirm.show();
				}
				return;	
			}
			else//Error
			{
				//boite de dialog en cas d'erreur
				var oDialogBoxConfirm = new dialogBox({
						parent : oParentContainer,
						content : sMessageError,
						height : 50,
						width : 370,
						title : "Erreur save Edit",
						templateSrc : DEFAULT_DIALOGBOX_TEMPLATE_EDIT_HTML,
						templateSrc : DEFAULT_DIALOGBOX_TEMPLATE_EDIT
					});
				oDialogBoxConfirm.setButton( { 
								position:"left", 
								value:"Fermer", 
								click : function(){ 
											
											oDialogBoxConfirm.remove();
											if( typeof params.error != "undefined" )	params.error.call(  );
										} 
								} );
								
				if( typeof params.disabledDialogBox == "undefined" || !params.disabledDialogBox )	
				{
					oDialogBoxConfirm.show();
				}
			}
		},
		errorButtonYes : function(  ){},
		errorCallBackNo : function(  ){}		
		
	}
	
	if( typeof params.dataType != "undefined" )
	{
		options.dataType = params.dataType;
	}
	
	oZoneExecSave.loadUrl( options );
	return oCurrentEvent;
}

MasterApplication.prototype.lock = function(  )
{
	this.oLoading = new dialogBox({
					parent : this.oZone,
					content : DEFAULT_MESSAGE_LOAD, 
					height : 40, 
					width : 250, 
					templateSrc : DEFAULT_DIALOGBOX_TEMPLATE_LOADING_HTML,
					templateSrc : DEFAULT_DIALOGBOX_TEMPLATE_LOADING
					});
	this.oLoading.show(  );
	return;
	
}
MasterApplication.prototype.unlock = function(  )
{
	if( this.oLoading )
		this.oLoading.hide(  );
}


MasterApplication.prototype.removeCurrentModalDialogBox = function(  )
{
	if( this.oModalDialogBox )
	{
		this.oModalDialogBox.remove(  );
		this.oModalDialogBox = null;
	}
}
MasterApplication.prototype.removeAllDialogBox = function()
{
	
	//Suppression des boites de dialog liés à l'appli master
	for( var i=0; i<this.oDialogBoxsOpening.length; i++ )
	{
		if( this.oDialogBoxsOpening[ i ] )
		{
			this.oDialogBoxsOpening[ i ].remove();
		}
	}
	this.oDialogBoxsOpening = new Array();
	
	//Suppression des boites de dialog liés aux zones
	for( var i=0; i<this.oZonesApplication.length; i++ )
	{
		this.oZonesApplication[ i ].removeAllDialogBox(  );
	}
}

MasterApplication.prototype.getZone = function( p_sNodeId )
{
	for( var i=0; i<this.oZonesApplication.length; i++ )
	{
		if( $( this.oZonesApplication[ i ].oZone).attr("id") == p_sNodeId )
		{
			return this.oZonesApplication[ i ];
		}
	}
	return null;
}