﻿var aprovacao = {

	'instalaEventos' : function (conteiner, pai_padrao, filho_padrao) {

		aprovacao.alvos     = new Array();
		aprovacao.alvos     = conteiner;
		aprovacao.pai       = pai_padrao;
		aprovacao.filho     = filho_padrao;

		for ( var x in aprovacao.alvos) {

			aprovacao.conteiner = aprovacao.alvos[x];

			if ( !document.getElementById(aprovacao.conteiner) )
				continue;
	
			var pai   = document.getElementById(aprovacao.conteiner);
			var itens = pai.getElementsByTagName(filho_padrao);
			var total = itens.length;
	
			for (var k = 0; k < total; k++) {
	
				var alvos = itens.item(k).getElementsByTagName("a");

				for (var i = 0; i < alvos.length; i++) {
					if (alvos[i].href.match (/(\bcodProfile\b|idScrapbook|idTestimonial|idMessage|paramCase|idCard)/gm) != null) {
						lib.addEvent (alvos[i], "click", aprovacao.relacaoClique, false);
						alvos[i].onclick = lib.cancelaEventosSafari;
					}
				}
				
			}

		}

	},

	'relacaoClique' : function(e) {

		var t = window.event ? window.event.srcElement : e ? e.target : null;

		if ( t.nodeType == 3 )
			t = lib.ascendDOM (t, "a");

		var filho = t;

		var pai   = lib.ascendDOM (t, aprovacao.filho);

		var avo   = lib.ascendDOM (t, aprovacao.pai);

		/*if ( typeof http != "undefined" &&
			   http != null               ) {
			elid  = aprovacao.exibeAndamento (pai, "Alerta: Já existe uma operação em execução.", "alert");
			var c = "window.setTimeout ('animUOL.efeitoTransparencia (document.getElementById(\"" + elid.toString() + "\"), 25, 0, 50, \"\", function() { aprovacao.removeItem (\"" + elid.toString() + "\"); } );', 3000);";
			eval (c);
			return;
		}*/

		elid = aprovacao.exibeAndamento (pai, "Salvando alteração...", "info");

		var c = "window.setTimeout ('animUOL.efeitoTransparencia (document.getElementById(\"" + elid.toString() + "\"), 25, 0, 50, \"\", function() { aprovacao.removeItem (\"" + elid.toString() + "\"); } );', 30000);";
		eval (c);

		ajaxUOL = "var" + aprovacao.geraID();

		aprovacao.defineRelacionamento (filho, pai, avo, elid, ajaxUOL);

		lib.cancelaEventos (e);
			
	},

	'defineRelacionamento' : function (filho, pai, avo, elid, ajaxUOL) {

		lib.removeEvent (window, "resize", lib.zebradora, false);		

		var c = ajaxUOL.toString() + ' = lib.getHTTPObject();';
		eval (c);

		i = "filho-" + aprovacao.geraID();
		filho.id = i;

		i = "pai-" + aprovacao.geraID();
		pai.id = i;

		i = "avo-" + aprovacao.geraID();
		avo.id = i;

		var c = ajaxUOL.toString() + '.open ("GET", "' + filho.href + '&type=ajax", true);';
		eval (c);

		var c = ajaxUOL.toString() + '.onreadystatechange = function() { aprovacao.controlaAJAX ("' + filho.id.toString() + '", "' + pai.id.toString() + '", "' + avo.id.toString() + '", "' + elid.toString() + '", ' + ajaxUOL.toString() + '); }';
		eval (c);

		var c = ajaxUOL.toString() + '.send (null);';
		eval (c);
		
	},

	'controlaAJAX' : function (filho, pai, avo, elid, ajaxUOL) {

			if ( ajaxUOL.readyState == 4 ) {

				// lib.exibeMensagens (["Teste dos Webmasters",ajaxUOL.responseText,ajaxUOL.getAllResponseHeaders()], "hint", "copydiv");

				if (!ajaxUOL.responseXML) {
					aprovacao.exibeAndamento (pai, 'Erro ao tentar salvar sua alteração. XML inválido.', 'error', elid.toString());
					var c = "window.setTimeout ('animUOL.efeitoTransparencia (document.getElementById(\"" + elid.toString() + "\"), 25, 0, 50, \"\", function() { aprovacao.removeItem (\"" + elid.toString() + "\"); } );', 2000);";
					eval (c);
					if ( aprovacao.filho == "td" ) 
						window.setTimeout ('aprovacao.exibeAndamento ("' + pai.toString() + '", "Apagar.", "trash", "' + elid.toString() + '");', 2000);
					return;
				}

				var xml = ajaxUOL.responseXML;

				if (xml.getElementsByTagName("status").length < 1                            ||
					  !xml.getElementsByTagName("status").item(0).firstChild                   ||
					  xml.getElementsByTagName("status").item(0).firstChild.nodeValue != "ok"  ) {
					aprovacao.exibeAndamento (pai, "Não foi possível salvar a alteração, por favor tente mais tarde.", "error", elid.toString());
					var c = "window.setTimeout ('animUOL.efeitoTransparencia (document.getElementById(\"" + elid.toString() + "\"), 25, 0, 50, \"\", function() { aprovacao.removeItem (\"" + elid.toString() + "\"); } );', 2000);";
					eval (c);
				}
				else {
					aprovacao.exibeAndamento (pai, "Alteração salva com sucesso!", "ok", elid.toString());
					if ( document.getElementById(filho).innerHTML == "Apagar" ) {
						var c = "window.setTimeout ('animUOL.efeitoTransparencia (document.getElementById(\"" + avo.toString() + "\"), 25, 0, 100, \"\", function() { aprovacao.removeItem (\"" + avo.toString() + "\"); } );', 1500);";
						eval (c);
					}
					else if ( document.getElementById(filho).innerHTML == "Aprovar&nbsp;&nbsp;" || document.getElementById(filho).innerHTML == "Reprovar&nbsp;&nbsp;" ) {
						div = lib.ascendDOM(document.getElementById(avo), "div");
						var i = 'divTopic-' + aprovacao.geraID();
						div.id = i;
						var c = "window.setTimeout ('animUOL.efeitoTransparencia (document.getElementById(\"" + i.toString() + "\"), 25, 0, 100, \"\", function() { aprovacao.removeItem (\"" + i.toString() + "\"); } );', 1500);";
						eval (c);
					}
					else if ( document.getElementById(filho).innerHTML == "Aprovar" ) {
						var c = "window.setTimeout ('animUOL.efeitoTransparencia (document.getElementById(\"" + elid.toString() + "\"), 25, 0, 50, \"\", function() { aprovacao.removeItem (\"" + elid.toString() + "\"); } );', 2000);";
						eval (c);
						aprovacao.removeItem (filho);
					}
					else {
						var c = "window.setTimeout ('animUOL.efeitoTransparencia (document.getElementById(\"" + avo.toString() + "\"), 25, 0, 100, \"\", function() { aprovacao.removeItem (\"" + avo.toString() + "\"); } );', 1500);";
						eval (c);
					}
				}
			}

	},

	'removeItem' : function (alvo) {

		if ( document.getElementById (alvo) )
			alvo = document.getElementById (alvo);

		if ( alvo == null )
			return;

		alvo.style.display = "none";
		alvo.parentNode.removeChild (alvo);
		aprovacao.reescreveListagem();

	},

	'reescreveListagem' : function() {

		if ( aprovacao.alvos[0] == "topicWaiting" || aprovacao.alvos[0] == "replyWaitingTopic" ) {
			as = document.getElementById('dentro').getElementsByTagName('a');
			var temumpelomenos=false;
			for(var i=0;i<as.length;i++) { var a = as[i];
				if(a.href.indexOf('paramCase=publish') != -1) {
					temumpelomenos = true;
				}
			}
			if(!temumpelomenos) {
				document.location = document.location.href.replace(/page=[0-9]+&?/, '');
			}
		}


		for (var x in aprovacao.alvos) {

			aprovacao.conteiner = aprovacao.alvos[x];

			if ( !document.getElementById(aprovacao.conteiner) )
				continue;

			var pai   = document.getElementById(aprovacao.conteiner);
			var itens = pai.getElementsByTagName(aprovacao.pai);
			var total = itens.length;
			
			if ( aprovacao.conteiner == "listagem-mensagens" )
				total = total - pai.getElementsByTagName("tfoot").length;

			if (total == 0) {

				if ( document.getElementById("esperando")                                          &&
				     document.getElementById("esperando").nextSibling                              &&
					   document.getElementById("esperando").nextSibling.nodeType == 1                &&
						 document.getElementById("esperando").nextSibling.tagName.toLowerCase() == "p" )
					lib.removeItem ( document.getElementById("esperando").nextSibling );
					
				if ( aprovacao.conteiner == "listagem-mensagens" )
					lib.exibeMensagens ( ['Nenhuma mensagem restante nesta página'], 'info', 'dentro', 'msgs-msg' );

				lib.removeItem ( pai );
				continue;

			}


			var limite = (total < 2) ? 1 : 2;

			for (var k = 0; k < limite; k++) {
				itens.item(k).style.display = "";
			}

			lib.zebradora();
			
			if ( !document.all )
				lib.addEvent (window, 'resize', lib.zebradora, false);

		}
		
	},

	'exibeAndamento' : function (pai, mensagem, tipo, id_final) {

		if ( typeof pai != "object" ) {
			if ( document.getElementById (pai) )
				pai = document.getElementById (pai);
		}

		if ( aprovacao.filho == "td" ) {
			aprovacao.alteraIcone (pai, tipo);
			i = aprovacao.geraID();
			i = "msg-" + i;
			pai.id = i;
			return i;
		}
		else {

			var alvo = pai.getElementsByTagName("span");

			if (alvo.length == 0) {
				var span = document.createElement ("span");
				span.className = "ajax-status";
				pai.appendChild (span);
			}

			alvo = pai.getElementsByTagName("span").item( pai.getElementsByTagName("span").length - 1 );

			if ( alvo.className.match (/ajax-status/gim) == null ) {
				var span = document.createElement ("span");
				span.className = "ajax-status";
				pai.appendChild (span);
				alvo = pai.getElementsByTagName("span").item( pai.getElementsByTagName("span").length - 1 );
			}

			alvo.innerHTML = mensagem;
			
			alvo.className = "ajax-status msg-" + tipo;

			if ( typeof id_final != "undefined" ) {
				alvo.id = id_final;
				return id_final;
			}
			else {
				i = aprovacao.geraID();
				i = "msg-" + i;
				alvo.id = i;
				return i;
			}

		}

	},

	'alteraIcone' : function (alvo, classe) {

		alvo.className = "apagar del-" + classe;

	},

	'geraID' : function() {	

		var data   = new Date();
		var numero = Math.ceil ( Math.random() * 100000 );
		return (data.getTime() - numero);

	}

}

if ( document.getElementById              &&
	   document.getElementsByTagName        &&
		 document.createElement               ) {
	// lib.addEvent (window, "load", function() { aprovacao.instalaEventos (['esperando', 'recados', 'depoimentos'], "dl", "dd"); }, false);
	lib.addEvent (window, "load", function() { aprovacao.instalaEventos (instalar, aprovacao_avo, aprovacao_pai); }, false);
	// lib.addEvent (window, "load", function() { aprovacao.instalaEventos ("recados", "dl", "dd"); }, false);
}
