var documento;
var raiz = false;
var contador;
var cuentaDiv;
var elegido = false;
var parpadeo = false;
var miColor = "";
var resalte = "red";
var resaltado = -1;

function arboleda(elem)	{
	var yo = document.getElementsByTagName("div")[elem];
	var oculto = yo.visible == "none";
	var hijos = yo.childNodes;
	yo.visible = (oculto) ? "block" : "none";
	yo.firstChild.firstChild.nodeValue = (oculto) ? "-" : "+";

	for (var i = 0; i < hijos.length; i ++)
		if (hijos[i].nodeName == "DIV")
			hijos[i].style.display = yo.visible;
}

function alternar()	{
	elegido.style.backgroundColor = (elegido.style.backgroundColor == miColor) ? "red" : miColor; 
}

function estilos()	{
	var ventana = window.open("misEstilos.html?elemento=" + this.eso, "unaVentana", "width=400,height=500,status=yes");
	ventana.document.close();
	ventana.focus();
}

function estilizar(n)	{
	documento[n].estilos();
}

function pertenece(conjunto, elemento)	{
	return conjunto.join("|").split(elemento).length == 2;
}

function indicar()	{
	if (parpadeo)	{
		clearInterval(parpadeo);
		elegido.style.backgroundColor = miColor;
		parpadeo = false;
	}
	if (pertenece([" ", "HTML", "HEAD", "FRAMESET", "FRAME", "SCRIPT", "META", "STYLE", "TITLE", " "], "|" + this.nombre + "|"))
		alert(this.nombre);
	else
	{
		miColor = this.soy.style.backgroundColor;
		if (miColor == "undefined") miColor = "transparent";
		elegido = this.soy;
		if (!parpadeo)
			parpadeo = setInterval('alternar()', 500);
	}
}

function attrs(n)	{
	documento[n].atributos();
}

function modificar(elemento, atributo, valor)	{
	var modificando = "documento[" + elemento + "].soy." + atributo + " = '" + valor + "'";
	eval(modificando);
}

function atris()	{
	var datos	= "<html><head><title>Atributos de: ";
	datos 	+= this.nombre;
	datos	+= "</title></head>";
	datos	+= "<body><h1>Atributos de " + this.nombre + "</h1><table>";
	for (var i = 0; i < this.soy.attributes.length; i ++)	{
		datos +=	"<tr style='padding:0 2 0 2; background-color:";
		datos +=	(i % 2 == 0) ? "#eeeeee" : "#dddddd";
		datos +=	"'><form action='javascript: opener.modificar(";
		datos +=	this.lugar;// + ", ";
		datos +=	", " + "\"".toString() + this.soy.attributes[i].name;
		datos +=	"\"".toString() + ", document.forms[" + i + "].valor.value)'><td>";
		datos +=	"<input type='hidden' name='nombre' value=";
		datos +=	this.soy.attributes[i].name + " />";
		datos +=	this.soy.attributes[i].name + "</td><td>";
		datos +=	"<input type='text' name='valor' value='";
		datos +=	this.soy.attributes[i].value + "' />";
		datos +=	"</td><td><input type='submit' value='actualizar' /></td></form></tr>";
	}
	datos	+= "</table></body></html>";
	var ventana = window.open("", "atris", "width=450,height=300,scrollbars=yes");
	ventana.document.write(datos);
	ventana.document.close();
	ventana.focus();
}

function elemento(loQueSea, lugar)	{
	this.lugar = lugar;
	this.soy = eval(loQueSea);
	this.nombre = this.soy.nodeName;
	this.tipo = this.soy.nodeType;
	this.valor = this.soy.nodeValue;
	this.eso = loQueSea;
	this.atributos = atris;
	if (this.tipo == "1")	{
		this.indicar = indicar;
		this.estilos = estilos;
	}
	else	{
		if (parpadeo) clearInterval(parpadeo);
		parpadeo = false;
		this.indicar = function()	{
			if (parpadeo) clearInterval(parpadeo);
			parpadeo = false;
			if (elegido) elegido.style.backgroundColor = miColor;
			alert(this.nombre + " = " + this.valor);// + "(tipo: " + this.tipo + ")");
		}
	}
}

function mostrar(n)	{
	documento[n].indicar();
}

function ini()	{
	contador = 0;
	cuentaDiv = 0;
	resaltado = -1;
	documento = new Array();
	var borrar = document.getElementById("arbusto");
	if (borrar.hasChildNodes)
		for (var i = 0; i < borrar.childNodes.length; i ++)
			borrar.removeChild(borrar.childNodes[0]);
}

function limpiar()	{
	if (resaltado != -1)
		document.getElementsByTagName("div")[resaltado].style.backgroundColor = "transparent";
	resaltado = -1;
}

function resaltar()	{
	if (resaltado == -1)	{
		resaltado = this.Lugar();
		resaltado ++;
		eval('document.getElementsByTagName("div")[resaltado].style.backgroundColor = "#eeeeee"');
	}
}

function Lugar()	{
	return this.miLugar;
}

function leerNodo(elElemento, donde)	{
	documento[contador] = new elemento(elElemento, contador);
	
	var raiz = eval(elElemento);
	raiz.miLugar = contador;
	raiz.Lugar = Lugar;

	raiz.addEventListener("mouseover", resaltar, false);
	raiz.addEventListener("mouseout", limpiar, false);

	var ramaje = raiz.childNodes;
	var colgar = document.createElement("div");

	// gestión de visibilidad del árbol

	colgar.visible = "none";
	var acto = "arboleda(" + ++cuentaDiv + ")";
	colgar.style.display = (cuentaDiv == 1) ? "block" : "none";

	if (raiz.childNodes.length > 0	|| raiz.nodeName == "FRAME" || raiz.nodeName == "IFRAME")	{
		var visor = document.createElement("a");
		visor.href = "javascript: " + acto;
		visor.appendChild(document.createTextNode("+"));
		colgar.appendChild(visor);
	}

	colgar.appendChild(document.createTextNode(" "));
	acto = "mostrar(" + (contador) + ")";
	var voy = document.createElement("a");
	voy.href = "javascript: " + acto;
	var soy = document.createTextNode(raiz.nodeName);
	voy.appendChild(soy);
	colgar.appendChild(voy);

	acto = "attrs(" + (contador) + ")";
	voy = document.createElement("a");
	voy.href = "javascript: " + acto;
	soy = document.createTextNode("(atrs: " + raiz.attributes.length + ")");
	colgar.appendChild(document.createTextNode(" : "));
	voy.appendChild(soy);
	colgar.appendChild(voy);
	donde.appendChild(colgar);

	// ponemos un asterisco para los estilos


	acto = "estilizar(" + (contador) + ")";
	voy = document.createElement("a");
	voy.href = "javascript: " + acto;
	soy = document.createTextNode("*");
	colgar.appendChild(document.createTextNode(" "));
	voy.appendChild(soy);
	colgar.appendChild(voy);
	donde.appendChild(colgar);

	if (raiz.nodeName == "FRAME" || raiz.nodeName == "IFRAME")	{
		var marco = elElemento.split("document.documentElement")[0] + raiz.name + ".document.documentElement";
		contador ++;
		leerNodo(marco, colgar);
	}
	else contador ++;

	for (var i = 0; i < ramaje.length; i ++)
		if (ramaje[i].nodeType == 1)
			leerNodo(elElemento + ".childNodes[" + i + "]", colgar);
		else	{
			tag = document.createElement("div");
			tag.visible = "none";
			tag.style.display = "none";
			cuentaDiv ++;
			enlace = document.createElement("a");
			ponertxt = document.createTextNode(ramaje[i].nodeName);
			enlace.appendChild(ponertxt);
			tag.appendChild(enlace);
			documento[contador] = new elemento(ramaje[i]);
			var acto = "mostrar(" + (contador++) + ")";
			enlace.href = "javascript: " + acto;
			colgar.appendChild(tag);
		}
}
