	Ext.namespace('Nca.Proxy');
 
	Nca.Proxy.ProxyProvider = Ext.extend(Ext.util.Observable,{
		init : function(){
			this.ajax = Ext.Ajax;
			this.messageStack = new Array();
		
			this.call = this.call.createInterceptor(this.beforeRequest, this);
			this.loadScript = this.loadScript.createInterceptor(this.beforeRequest, this);
		},
	
		setWaitMsg : function(aMsg){
			Ext.get('loading-msg').dom.innerHTML = aMsg;
			var el = Ext.get('loading').center(Ext.get('loading-mask'));
		},
	
		call : function(aMsg, anURL, aParams, aSuccessFn, aFailureFn, asyncMode, aScope){		
			this.successFn = aSuccessFn;
			this.failureFn = aFailureFn;
			this.scopeFn = aScope;
			
			if (asyncMode == undefined){
				asyncMode = true;
			}
			
			anURL = anURL;
			
			this.ajax.request({
				method	: 'POST',
				url		: anURL,
				params	: aParams,
				success	: this.processResponse,
				failure	: this.failCall,
				async	: asyncMode,
				scope	: this
			});
		},
	
		failCall : function(response, options){
			
		},
	
		loadScript : function(script){
			this.buildScript = this.buildScript.createSequence(this.afterRequest,this);
			this.failLoadScript = this.failLoadScript.createSequence(this.afterRequest,this);
			
			script = Ext.nce.appPath + script;
			
			this.ajax.request({
				url		: script,
				method	:'POST',
				headers	: {content: "text/javascript", charset: "iso-8859-1"}, 			
				success	: this.buildScript,
				failure	: this.failLoadScript,
				async	: false,
				scope	: this
			});
		},
		
		buildScript : function(response, options){
			eval(response.responseText);
		},

		failLoadScript : function(response, options){		
			Ext.MessageBox.show({
			   title	: 'ATENÇÃO',
			   msg		: 'ATENÇÃO NÃO FOI POSSÍVEL CARREGAR O SCRIPT:<br/> ' + options.url,
			   buttons	: Ext.MessageBox.OK,
			   icon		: Ext.MessageBox.ERROR
		   });

		},
		
		beforeRequest : function(msg){
			if (msg != undefined){
				this.messageStack.push(msg);
				var last = this.messageStack.length;
				/*Ext.get('loading-msg').dom.innerHTML = this.messageStack[last-1];
				var el = Ext.get('loading').center(Ext.get('loading-mask'));
				if (this.messageStack.length <= 0){						
					Ext.get('loading').fadeIn();	
				}*/
			}
		},
		
		afterRequest : function(){
			this.messageStack.pop();
			
			if (this.messageStack.length == 0){
				Ext.get('loading').fadeOut({remove: false});
				Ext.get('loading-mask').fadeOut({remove: false});		
			}else{
				var msg = this.messageStack[this.messageStack.length-1];
				Ext.get('loading-msg').dom.innerHTML = msg;
				var el = Ext.get('loading').center(Ext.getBody());
			}
		},

		processResponse : function(response, options){
			var responseObject = Ext.decode(response.responseText);			
			
			try {
				var data = responseObject.data;
			}catch(e){
				var data = null;
			}

			if (responseObject.action == 'success'){
				this.successFn = this.successFn.createSequence(this.afterRequest,this);
				this.successFn.call(this.scopeFn, data, response, options);
			}else{
				this.failureFn = this.failureFn.createSequence(this.afterRequest,this);
				this.failureFn.call(this.scopeFn, data, response, options);
			}					
		},
		
		getProxy : function(anUrl, params){
			anUrl = anUrl;
			asyncMode = true;
			
			if (params){
				if (params.sync)
					asyncMode = false;
			}
			var proxy =  new Ext.data.HttpProxy({url:anUrl, method: 'POST', async: asyncMode});
			return proxy;
		}
	});
