/*
	var request = new classEnviaRequisicao({
		url: 'modulos/conteudo/ajax_conteudo.php',
		funcaoRetorno: funcaoRetorno,
		fucaoErro: funcaoErro
		variaveis: vars
	});
*/

var classEnviaRequisicao = new Class({
	options: {
		funcaoRetorno: null,
		funcaoErro: null
	},
	/* inicialização da Classe */
	initialize: function(options)
	{ 
		this.setOptions(options);
		
		var background = this.options.background ? this.options.background : '#CCCCCC'
		this.janelaProcessando = new pixelWindowClass({box_css: {"background": "url(img/processando.gif) center center no-repeat", "border": "none"} });
		
		this.method = this.options.method ? this.options.method : 'post';
		
		if(this.options.url)
		{
			this.janela_processando(' ');
			new Request({
				url: this.options.url,
				method: this.method, 
				data: this.options.variaveis,
				onSuccess: function(obj_text)
				{
					this.janela_processando(false);
					if(obj_text && obj_text != '')
					{
						// Verificando a existência de erros na estrutura
						try
						{
							eval('var obj = ' + obj_text + ';');
						}
						catch(err)
						{
							if(this.options.funcaoErro)
								setTimeout(function(){ this.options.funcaoErro() }.bind(this), 100);
							return false;
						}
						if(this.options.funcaoRetorno)
							setTimeout(function(){ this.options.funcaoRetorno(obj) }.bind(this), 100);
					}
					// Script server side não devolveu resposta
					else
					{
						alert("ERRO na comunicação com o servidor de dados.\n\nPor favor tente novamente mais tarde!");
						if(funcaoErro)
							setTimeout(function(){ this.options.funcaoErro(1) }.bind(this), 100);
					}
				}.bind(this),
				
				onFailure:  function()
				{
					this.janela_processando(false);
					alert("Falha na comunica&ccedil;&atilde;o com o servidor de dados.\n\nPor favor tente novamente mais tarde!");
					if(funcaoErro)
						setTimeout(function(){ this.options.funcaoErro(1) }.bind(this), 100);
				}
			}).send();
		}
	},
	
	janela_processando: function(type){ this.janelaProcessando.show(type); }
});
classEnviaRequisicao.implement(new Options, new Events);