/**
 *
 *
 */

EMODE.ajaxContext = function( config )
{
	var context = {	
		
	displayedDivId : '',	

	init : function()
	{
		// Ajax Setup
		$.ajaxSetup( {
			   type: "POST",
			   dataType: "json",
			   async : true,
			   timeout:   30000,
			   beforeSend: function(){context.showAll();},
			   error: function(){context.errorAjax();}
		} );
		context.createAjaxMasque(); 
		context.createAjaxDiv();
	},		
		
	createAjaxMasque : function()
	{
		var ajaxMasque = '';
		ajaxMasque += '<div id="ajaxMasque" style="display:none;width:100%;height: 100%;position:absolute;top:0;left:0;background-color: #000000;">';
		ajaxMasque += '&nbsp;';
		ajaxMasque += '</div>';
		
		$(document.body).append( ajaxMasque );
		$('#ajaxMasque').height( $(document).height() );
		//$('#ajaxMasque').width( $(document).width() );
		$('#ajaxMasque').css( 'opacity', '0.6' ); // make sure
	},

	createAjaxDiv : function ()
	{
		var ajaxDiv = '<div id="ajaxDiv" style="display:none;overflow:hidden;z-index:10;width:150px;height:150px;border:3px solid #666666;background-color:#FFFFFF;text-align:center;">';
		ajaxDiv += '<a href="javascript:void(0);" id="btnFermer" title="fermer" style="display:none;position:absolute;top:0;right:0;font-size:18px;color:#999999;font-weight:bold;"><img src="'+IMAGES_URL+'/btn_popup_fermer.jpg" alt="fermer" /></a>';
		ajaxDiv += '<img src="'+IMAGES_URL+'/action_icons/ajax_loader.gif" alt="ajax loader" id="ajaxLoader" style="z-index:100" />';
		ajaxDiv += '<div id="messageBox" style="display:none;">';
		ajaxDiv += '	<p class="responseTexte" style="color: #999999;padding: 67px 30px 0 30px;font-size:12px;">&nbsp;</p>';
		ajaxDiv += '</div>';
		ajaxDiv += '</div>';
		
		$( document.body ).append( ajaxDiv );
		
		$('#ajaxDiv').center();
		
		$('#btnFermer').bind( 'click', {}, function(){context.closeAll();} );
		
		$('#ajaxMasque').bind( 'click', {}, function(){context.closeAll();} );
	},
	
	showMasque : function()
	{
		$('#ajaxMasque').show();
	},
	
	hideMasque : function()
	{
		$('#ajaxMasque').fadeOut();	
	},

	showAjaxDiv : function()
	{
		$('#ajaxDiv').show();
		$( '#ajaxDiv' ).center();
	},
	
	animAjaxDiv : function( callback, w, h )
	{
		if( !w ) w = 350;
		if( !h ) h = 200;
		
		/* 150 taille initiale centree avt anim */
		
		var mgLeft = -( (w - 150) / 2 );
		var mgTop = -( (h- 150 + 100) / 2 );
				
		$("#ajaxDiv").animate({ width: w+"px", height: h+"px" , marginLeft: mgLeft+"px", marginTop: mgTop+"px"}, 500, '', callback );
	},
		
	hideAjaxDiv : function()
	{				
		$('#ajaxDiv').hide();
		$('#ajaxDiv').width( '150px' );
		$('#ajaxDiv').height( '150px' );
		$('#ajaxDiv').css( 'margin-left', 0 );
		$('#ajaxDiv').css( 'margin-top', 0 );
		$( '#ajaxDiv' ).center();
	},
	
	showLoader: function()
	{
		$('#ajaxLoader').show();
		//$( '#ajaxLoader' ).center();
	},
	
	hideLoader : function()
	{
		$('#ajaxLoader').hide();	
	},	
	
	showBtnClose: function()
	{
		$('#btnFermer').show();
	},
	
	hideBtnclose : function()
	{
		$('#btnFermer').hide();	
	},
	
	showMessageBox : function()
	{
		$('#messageBox').show();
	},
	
	hideMessageBox : function()
	{
		$('#messageBox').hide();
	},
	
	htmlMessagebox : function( html )
	{
		$("#messageBox p:first").html( html );
	},
	
	messageBox : function( html )
	{		
		this.htmlMessagebox( html );
		this.hideLoader();
		this.showMessageBox();
	},
	
	showAll : function()
	{
		this.showMasque();
		this.showLoader();
		this.showAjaxDiv();
		$('#btnFermer').fadeIn();
		
		$('embed, object, select').css({ 'visibility' : 'hidden' });
	},
	
	closeAll : function ( event )
	{
		this.hideMasque();
		this.hideAjaxDiv();
		this.hideMessageBox();
		
		$('embed, object, select').css({ 'visibility' : 'visible' });
		
		if( this.displayedDivId )
		{ 
			$('#'+this.displayedDivId).hide();
			this.displayedDivId ='';
		}
	},
	
	errorAjax : function ( XMLHttpRequest )
	{
		this.animAjaxDiv( function(){} );
		this.messageBox( '<p class="error">Une erreur est survenue.<br />Si l’erreur persite, vous pouvez contacter la hotline au 09.51.29.29.00 </p>' );
		
		if( this.displayedDivId )
		{ 
			$('#'+this.displayedDivId).hide();
			this.displayedDivId ='';
		}
	}
	
	/*errorAjax : function ( XMLHTTPREQUEST )
	{
		alert( XMLHTTPREQUEST.responseText );
	}*/
 }
 return context;
}
