/*
Ajax Sem Dor, Fácil
autor: Renato
resumo:
data de criação: 13/08/2007 02:19
prazo: 13/08/2007 10:00
modificações:
		executar
		abrirDiv
		fecharDiv
	08/09/2007
		alterado a forma de construção
	27/09/2007
		abrir
		acrescentados comportamentos padrões

pendencias:
	addFuncao
	executarFuncao
*/
var Asdf = Class.create();
Asdf.prototype = {
	initialize: function () {
		this.nomesFuncoes = new Array();
		this.funcoes = new Array();
	},	
	carregar: function(url, alvo, metodo, informacoes) {
		var ret = false;
		if (metodo == null) {
			metodo = "get";
		}
		new Ajax.Request(url, {
			method: metodo,
			parameters: informacoes,
			onSuccess: function(req) {
				$(alvo).innerHTML = req.responseText; //talvez precise de uma referência do documento
				ret = true;
			},
			onFailure: function() {
				alert("URL não encontrada!");
				ret = false;
			}
		});
		
		return ret;
	},	
	executar: function(url, metodo, informacoes) {
		var ret;// = null;
		if (metodo == null) {
			metodo = "get";
		}
		new Ajax.Request(url, {
			method: metodo,
			parameters: informacoes,
			asynchronous: false,
			onComplete: function(req) {
				ret = req.responseText;
			}
		});
		return ret;
	},	
	addJS: function(id, url) {
		var e = $(id);
		if (e == null) {
			var e = document.createElement("script");
			e.id = id;
			e.src = url;
			e.type="text/javascript";
			document.getElementsByTagName("head")[0].appendChild(e);
		}
		return e;
	},
	/*
	addFuncao: function(id, funcao) {
		/*
		var e = document.getElementById(id);
		if (e == null) {
			var e = document.createElement("script");
			e.id = id;
			e.type = "text/javascript";
			e.text = texto;
			document.getElementsByTagName("head")[0].appendChild(e);
			alert(e.text);
		}
		return e;
		* /
	alert(this.funcoes.length + ", " + id + ", " + funcao);
		this.nomesFuncoes.push(id);
		this.funcoes.push(funcao);
	alert(this.funcoes.length);
	}
	
	executarFuncao: function(id) {
	alert(this.funcoes.length);
		for (var i = 0; i < this.nomesFuncoes.length; i++) {
			if (this.nomesFuncoes[i] == id) {
	alert("ok");
				var c = this.funcoes[id];
				alert(c);
				c.executar();
			}
		}
	},*/
	
	add: function(tipo, nome, alvo, propriedades, repetir) {
		var o = $(nome);
		if (alvo == null) {
			alvo = document.body;
		}
		if (o == null || repetir) {
			o = document.createElement(tipo);
			o.id = nome;
			if (propriedades != null) {
				for (p in propriedades) {
					o.setAttribute(p, propriedades[p]);
				}
			}
			alvo.appendChild(o);
		}
		return o;
	},	
	del: function(nome) {
		var o = $(nome);
		if (o != null) {
			var pai = o.parentNode;
			pai.removeChild(o);
			return true;
		}
		else {
			return false;
		}
	},	
	mostrar: function(nome) {
		var o = $(nome);
		if (o == null) {
			return null;
		}
		o.style.display = "";
		return true;
	},	
	esconder: function(nome) {
		var o = $(nome);
		if (o == null) {
			return null;
		}
		o.style.display = "none";
		return true;
	},	
	ver: function(nome) {
		var o = $(nome);
		if (o == null) {
			return null;
		}
		o.style.display = (o.style.display == "none")?"":"none";
		return true;
	},	
	set: function(nome, conteudo) {
		var o = $(nome);
		if (o == null) {
			return null;
		}
		o.innerHTML = conteudo;
		return true;
	},
	abrir: function(url, metodo, informacoes, novaJanela, nomeJanela, opcoesJanela) {
		if (metodo == null) {
			metodo = "get";
		}
		if (metodo.toLowerCase() == "get") {
			if (url.indexOf("?") == -1) {
				url += "?";
			}
			if (informacoes != null) {
				for (i in informacoes) {
					url += "&" + i + "=" + informacoes[i];
				}
			}
			if (novaJanela) {
				window.open(url, nomeJanela, opcoesJanela);
			}
			else {
				window.location = url;
			}
		}
		else {
			var idForm = "form-" + url;
			var form = $(idForm);
			if (form == null) {
				form = this.add("form", idForm, document.body, {action: url, method: "post", target: (novaJanela)?"_blank":""});
			}
			if (informacoes != null) {
				for (i in informacoes) {
					var campo = $(i);
					if (campo == null || campo.parentNode != form) {
						campo = this.add("input", i, form, {type: "hidden", name: i}, true);
					}
					campo.value = informacoes[i];
				}
			}
			form.submit();
		}
	}
};
