/************************************************************************************************************************************************************************

								ZONE DE L'APPLICATION

*************************************************************************************************************************************************************************/
function ZoneApplication( p_oZone , options )
{
	this.oZone = p_oZone;
	this.oEventHandler = new ApplicationEventHandler(  );
	
	this.oListDialogBoxOpening = new Array();
	this.disabledLoading = false;
	this.locked = false;
	
	//Dernier évènement réussi
	this.lastEventSuccessfull = null;
	//Dernier évènement appelé
	this.lastEvent = null;
	
	
	//Fonction executer à chaque evènement sur la zone --> BIDOUILLE pour gérer la liste de choix simple
	this.responseExecFinishRecurrent = function(){};
	
	
	this.oLoading = null;
	
	
	/*
	this.oZoneMask = null;
	this.oZoneMaskLoading = null;	
	*/
	
	if( typeof options != "undefined" )
	{
		if(  typeof options.disabledLoading != "undefined" )	this.disabledLoading = options.disabledLoading;
	}
	
}
ZoneApplication.prototype.loadUrl = function( options )
{	
	if( typeof options != "undefined" )
	{	
		var oEventApplication = new EventApplication(  );
		
		if(  typeof options.url != "undefined" )	oEventApplication.url = options.url;
		if(  typeof options.data != "undefined" )	oEventApplication.data = options.data;
		if(  typeof options.dataType != "undefined" )	oEventApplication.dataType = options.dataType;
		if(  typeof options.async != "undefined" )	oEventApplication.async = options.async;
		if(  typeof options.isPostForm != "undefined" )	oEventApplication.isPostForm = options.isPostForm;
		
		if(  typeof options.beforeloadExec != "undefined" )	oEventApplication.beforeloadExec = options.beforeloadExec;
		if(  typeof options.loadExec != "undefined" )		oEventApplication.loadExec = options.loadExec;
		if(  typeof options.alreadyExec != "undefined" )	oEventApplication.alreadyExec = options.alreadyExec;
		if(  typeof options.lockedExec != "undefined" )		oEventApplication.lockedExec = options.lockedExec;
		
		
		if( typeof options.responseExecBegin != "undefined" )	oEventApplication.responseExecBegin = options.responseExecBegin;
		if( typeof options.responseErrorBegin != "undefined" )	oEventApplication.responseErrorBegin = options.responseErrorBegin;
		if( typeof options.responseErrorFinish != "undefined" )	oEventApplication.responseErrorFinish = options.responseErrorFinish;
		if( typeof options.successExecBegin != "undefined" )	oEventApplication.successExecBegin = options.successExecBegin;
		if( typeof options.successExecFinish != "undefined" )	oEventApplication.successExecFinish = options.successExecFinish;
		if( typeof options.errorButtonNo != "undefined" )	oEventApplication.errorButtonNo = options.errorButtonNo;
		if( typeof options.errorButtonYes != "undefined" )	oEventApplication.errorButtonYes = options.errorButtonYes;
		if( typeof options.responseExecFinish != "undefined" )	oEventApplication.responseExecFinish = options.responseExecFinish;
		
		//fonction executé à chaque appel de la zone en cas de success
		if( typeof options.responseExecFinishRecurrent != "undefined" )	this.responseExecFinishRecurrent = options.responseExecFinishRecurrent;
		
		this.invokeEvent( oEventApplication );
	}
	else
	{
		alert( "ZoneApplication.prototype.loadUrl : aucun argument" );
	}
	
} 

ZoneApplication.prototype.reload = function( options )
{
	if( this.lastEventSuccessfull )
	{
		if( typeof options != "undefined" && typeof options.args != "undefined" )
		{
			this.lastEventSuccessfull.data = this.lastEventSuccessfull.data + "&" + options.args;
		}
		this.invokeEvent( this.lastEventSuccessfull );
	}
}
ZoneApplication.prototype.getLastEventSuccessfull = function(  )
{
	return this.lastEventSuccessfull;
}


//Appel
ZoneApplication.prototype.invokeEvent = function( p_oEventApplication )
{
	this.lastEvent = p_oEventApplication;
	var _this = this;
	var iStateEvent = this.oEventHandler.attachEvent( p_oEventApplication );
	
	switch( iStateEvent )
	{
	
		case EVENT_STATE_ONLOAD : 
			this.invokeLoad( p_oEventApplication );
			break;
			
		case EVENT_STATE_ALREADY_EXEC : 
			var oCurrentEvent = this.oEventHandler.getEvent( p_oEventApplication, { url:true } );	
			this.invokeAlready( oCurrentEvent );
			break;
			
		case EVENT_STATE_APPLI_LOCKED : 
			this.invokeLocked( p_oEventApplication )
			break;			
	}
}
//Retour
ZoneApplication.prototype.responseEvent = function( p_oEventApplication, p_iStateEvent )
{	
	this.oEventHandler.responseEvent( p_oEventApplication, p_iStateEvent );

	if( p_oEventApplication.responseExecBegin )
		p_oEventApplication.responseExecBegin.call();
		
	switch( p_iStateEvent )
	{
		case EVENT_STATE_SUCCESS : 
						this.invokeSuccess( p_oEventApplication );
						break;
						
		case EVENT_STATE_ERROR : 
						this.invokeError( p_oEventApplication );
						break;
						
		case EVENT_STATE_CANCEL : 
						this.invokeCancel( p_oEventApplication );
						break;
	}
	
	if( p_oEventApplication.responseExecFinish )
		p_oEventApplication.responseExecFinish.call(  );
		
	if( this.responseExecFinishRecurrent )
		this.responseExecFinishRecurrent.call(  );
}








//====================================================================================================================================================//
//					Fonctions executées selon l'état de l'évènement renvoyé
//====================================================================================================================================================//
ZoneApplication.prototype.invokeLoad = function( p_oEventApplication )
{
	var _this = this;
	this.StartLoading(  );
	
	
	var sDataType = "html";
	if( typeof p_oEventApplication.dataType != "undefined" )	
	{
		sDataType = p_oEventApplication.dataType;
		//alert( 'invokeLoad = ' + sDataType );
	}
	
	p_oEventApplication.request = $.ajax({
		type : 'POST', 
		url : p_oEventApplication.url, 
		async : p_oEventApplication.async, 
		cache : false , 
		dataType : sDataType,
		data : p_oEventApplication.data,
		contentType: "application/x-www-form-urlencoded; charset=UTF-8",
		processData: true,
		error: function( e ) {
			alert( e.responseText );
			p_oEventApplication.error = e.responseText;
			//alert( p_oEventApplication.error );
			_this.responseEvent( p_oEventApplication, EVENT_STATE_ERROR );
			
		},
		success: function( xml ) {
			//alert( xml );
			p_oEventApplication.result = xml;
			_this.responseEvent( p_oEventApplication, EVENT_STATE_SUCCESS );
	   }
	 });
}

ZoneApplication.prototype.invokeAlready = function( p_oEventApplication )
{
	if( this.oLoading )
		this.oLoading.active(  );
}
ZoneApplication.prototype.invokeLocked = function( p_oEventApplication )
{	
	this.locked = true;
	var oCurrentEventLocked = this.oEventHandler.getEvent( p_oEventApplication );	
	
	var _this = this;
	
	this.EndLoading(  );
	var oCurrentDialogBox = new dialogBox({
						parent :		this.oZone,
						content : 		p_oEventApplication.invokeLockedContent,
						title :			DEFAULT_MESSAGE_LOAD,
						height :		90,
						width :			300,
						templateSrc : 		DEFAULT_DIALOGBOX_TEMPLATE_EDIT,
						templateHTML : 		DEFAULT_DIALOGBOX_TEMPLATE_EDIT_HTML
						});
	
	//Buttons
	var oFnClose = function()
			{
				_this.locked = false;
				oCurrentDialogBox.hide();
				if( oCurrentEventLocked.result != null )
					_this.responseEvent( oCurrentEventLocked, EVENT_STATE_SUCCESS );				
			};
	var oFnConfirm = function()
			{
				_this.locked = false;
				oCurrentDialogBox.hide();
				_this.responseEvent( oCurrentEventLocked , EVENT_STATE_CANCEL );
				_this.invokeEvent( p_oEventApplication );
			}
			
			
	oCurrentDialogBox.setButton({
				position : "left",
				value : "Annuler",
				click : oFnClose
				});
	oCurrentDialogBox.setClose( oFnClose );						
	oCurrentDialogBox.setButton({
				position : "right",
				value : "Valider",
				click : oFnConfirm
				});	
	
	oCurrentDialogBox.show(  );
	return;
}

ZoneApplication.prototype.invokeSuccess = function( p_oEventApplication )
{
	if( this.locked )	return;
	
	if( p_oEventApplication.successExecBegin )
		p_oEventApplication.successExecBegin.call();
	
	if( !p_oEventApplication.isPostForm )
	{
		$( this.oZone ).html( p_oEventApplication.result );
		
		
		//Important : Bidouille By Rob car le form n'est pas reconnu lorqu'on le charge en AJAX dans un noeud
		//Solution temporaire
		var oForms = $( this.oZone ).find( "form" );
		for( var i=0; i<oForms.length; i++ )
		{
			eval( "var Test = document." + $( oForms[i] ).attr( "name" ) );
		}
		
	}
	
	/*IMPORTANT*/
	//Début Appel de la fonction qui initialise les composants AMCMS de Alex
	var sPageName = p_oEventApplication.url.split( "?" )[ 0 ];
	sPageName = sPageName.split( "/" )[ sPageName.split( "/" ).length-1 ];
	sPageName = sPageName.replace(  ".", "_" );
	var JsInit_System = "JsInit_System" + sPageName;
	eval( "if( typeof " + JsInit_System + " != \"undefined\" ){ " + JsInit_System + "() }" );
	//Fin
	
	
	this.EndLoading(  );
	
	if( p_oEventApplication.successExecFinish )
		p_oEventApplication.successExecFinish.call();
		
	//On conserve le dernier évènement chargé pour pouvoir effectuer un reload
	this.lastEventSuccessfull = p_oEventApplication;
}

ZoneApplication.prototype.invokeError = function( p_oEventApplication )	
{	
	if( p_oEventApplication.responseErrorBegin )
		p_oEventApplication.responseErrorBegin.call();	
		
	this.EndLoading(  );
	var _this = this;
	
	
	this.EndLoading(  );
	var oCurrentDialogBox = new dialogBox({
						parent :		this.oZone,
						content : 		p_oEventApplication.invokeErrorContent,
						title :			"Erreur",
						height :		70,
						width :			300,
						templateHTML : 		DEFAULT_DIALOGBOX_TEMPLATE_EDIT_HTML,
						templateSrc : 		DEFAULT_DIALOGBOX_TEMPLATE_EDIT
						});
	
	//Buttons
	var oFnClose = function(  )
			{
				oCurrentDialogBox.remove(  );
				
				
				_this.responseEvent( p_oEventApplication , EVENT_STATE_CANCEL );
				if( p_oEventApplication.errorButtonNo )
				{
					p_oEventApplication.errorButtonNo.call();
				}
				
			};
	var oFnConfirm = function()
			{
				oCurrentDialogBox.remove(  );
				_this.responseEvent( p_oEventApplication , EVENT_STATE_CANCEL );
				_this.invokeEvent( p_oEventApplication );
				
				if( p_oEventApplication.errorButtonYes )
				{
					p_oEventApplication.errorButtonYes.call();
				}
			}
			
			
	oCurrentDialogBox.setButton({
				position : "left",
				value : "Annuler",
				click : oFnClose
				});
	oCurrentDialogBox.setClose( oFnClose );						
	oCurrentDialogBox.setButton({
				position : "right",
				value : "Valider",
				click : oFnConfirm
				});	
	
	
	oCurrentDialogBox.show(  );
	
	
	if( p_oEventApplication.responseErrorFinish )
		p_oEventApplication.responseErrorFinish.call();	
	
	return;

}

//Stop l'appel en cours
ZoneApplication.prototype.invokeCancel = function( p_oEventApplication )	
{
	this.EndLoading(  );
	p_oEventApplication.request.abort(  );
}

ZoneApplication.prototype.StartLoading = function(   )
{
	if( this.disabledLoading )	return;

	var oLoading = new dialogBox({
					parent : this.oZone,
					content : DEFAULT_MESSAGE_LOAD, 
					height : 20, 
					width : 250, 
					templateHTML : DEFAULT_DIALOGBOX_TEMPLATE_LOADING_HTML,
					templateSrc : DEFAULT_DIALOGBOX_TEMPLATE_LOADING
					});
	

	oLoading.show(  );
	this.oLoading = oLoading;
	return;
}
ZoneApplication.prototype.EndLoading = function(   )
{
	if( this.oLoading )
		this.oLoading.hide(  );
}
	
	


//====================================================================================================================================================//
//					Fonctions executées pour créer une fenetre de message
//====================================================================================================================================================//

ZoneApplication.prototype.attachDialogBox  = function( p_oEventApplication, p_iStateEvent, p_oDialogBox )
{		
	this.oListDialogBoxOpening.push( { event : p_oEventApplication.id, state : p_iStateEvent, dialogBox : p_oDialogBox } );
	//alert( "attachDialogBox : " +  p_oEventApplication.id )
}

ZoneApplication.prototype.removeDialogBox = function( p_oDialogBox )
{
	p_oDialogBox.remove(  );
}
ZoneApplication.prototype.removeAllDialogBox = function( p_oEventApplication )
{
	for( var i=0; i<this.oListDialogBoxOpening.length; i++ )
	{
		this.oListDialogBoxOpening[ i ].dialogBox.remove( );
	}
	this.oListDialogBoxOpening = new Array();
}
ZoneApplication.prototype.getDialogBox = function( p_oEventApplication, p_iStateEvent )
{
	if( this.oListDialogBoxOpening.length == 0 )	return null;
	
	for( var i=this.oListDialogBoxOpening.length; i>0; i-- )
	{
		if( this.oListDialogBoxOpening[ i-1 ].event == p_oEventApplication.id && ( typeof p_iStateEvent == "undefined" || this.oListDialogBoxOpening[ i-1 ].state == p_iStateEvent ) )
		return this.oListDialogBoxOpening[ i-1 ].dialogBox;
	}
	return null;
}

ZoneApplication.prototype.getLastEvent = function( )
{
	return this.lastEvent;
}
	

