// JavaScript Document

var IH = Class.create(
{
	initialize:function()
	{
		var arr_lis =  $("MENU_ARTICULOS").getElementsByTagName("li");
		for(var  i = 0 ; i < arr_lis.length ; i++)
		{
			arr_lis[i].categoria = i;
			arr_lis[i].handler = this;
			arr_lis[i].onclick = function()
			{
				this.handler.setContent(this.categoria);
			}
		}
		
		this.ArregloCategorias = Array(
		"xml_catg_1.php",
		"xml_catg_2.php",
		"xml_catg_3.php",
		"xml_catg_4.php",
		"xml_catg_5.php",
		"xml_catg_6.php"
		);
	},
	setContent:function(catg)
	{
		
		new Ajax.Request(this.ArregloCategorias[catg],
		{
			method:'post',
			onSuccess:function(transport)
			{
				this.ih.doLayOut(transport.responseXML);
				
			}
		});
	},
	doLayOut:function(xmldoc)
	{
		this.arr_items = xmldoc.getElementsByTagName("articulos")[0].getElementsByTagName("articulo");
		
		this.removeAllElements("thumbs");
		
		for(var i = 0 ; i < this.arr_items.length ; i++)
		{
			if(i == 0)
			{
				this.insertarInformacion(i);
			}
			this.insertarThumbs(this.arr_items[i],i);
		}
	},
	insertarThumbs:function(nodo,indice)
	{
		for(var j = 0 ; j < nodo.childNodes.length ; j++)
		{
			
			if(nodo.childNodes[j].nodeType != 3)
			{
				
				if(nodo.childNodes[j].nodeName == "thumb")
				{
					var url = this.returnElement(nodo.childNodes[j]);
					$("thumbs").appendChild(this.doThumbDIV(url,indice));
				}
			}
		}
	},
	insertarInformacion:function(indice)
	{
		
		var nodo_temp = this.arr_items[indice];
		
		var logo_temp;
		var a_temp;
		
		for(var j = 0 ; j < nodo_temp.childNodes.length ; j++)
		{
			if(nodo_temp.childNodes[j].nodeType != 3)
			{
				
				if(nodo_temp.childNodes[j].nodeName == "logo")
				{
					var url = this.returnElement(nodo_temp.childNodes[j]);
					this.removeAllElements("logo_visor");
					//$("logo_visor").appendChild(this.insertarIMGLogo(url));
					logo_temp = this.insertarIMGLogo(url);
				}
				if(nodo_temp.childNodes[j].nodeName == "titulo")
				{
					var tit = this.returnElement(nodo_temp.childNodes[j]);
					this.removeAllElements("titulo_visor");
					$("titulo_visor").appendChild(this.insertarTexto(tit));
				}
				if(nodo_temp.childNodes[j].nodeName == "contenido")
				{
					var ctd = this.returnElement(nodo_temp.childNodes[j]);
					$("contenido_visor").innerHTML = ctd;
				}
				if(nodo_temp.childNodes[j].nodeName == "webpage")
				{
					var url = this.returnElement(nodo_temp.childNodes[j]);
					this.removeAllElements("web_site_visor");
					
					var aref = this.insertarWEBPage(url);
					$("web_site_visor").appendChild(aref);
					
					a_temp = this.insertarWEBIMGPage(url);
					//a_clone.appendChild(logo_temp);
					//$("logo_visor").appendChild(logo_temp);
					
				}
				if(nodo_temp.childNodes[j].nodeName == "backgroundimage")
				{
					var url = this.returnElement(nodo_temp.childNodes[j]);
					$("content_visor").style.backgroundImage = "url("+url+")";
				}
			}
			
			
		}
		//alert(logo_temp);
		a_temp.appendChild(logo_temp);
		$("logo_visor").appendChild(a_temp);
	},
	returnElement:function(nod)
	{
		for(var j = 0 ; j < nod.childNodes.length ; j++)
		{
			if(nod.childNodes[j].nodeType == 3)
			{
				return nod.childNodes[j].nodeValue;
			}
		}
		return null;
	},
	removeAllElements:function(id)
	{
		var num = $(id).childNodes.length;
		for(var j = 0 ; j < num ; j++)
		{
			$(id).removeChild($(id).childNodes[0]);
		}
	},
	insertarIMGLogo:function(url)
	{
		var imagen = document.createElement("img");
		imagen.src = url;
		imagen.alt = "";
		return imagen;
	},
	insertarTexto:function(txt)
	{
		var txt = document.createTextNode(txt);
		return txt;
	},
	insertarWEBPage:function(url)
	{
		var aref = document.createElement("a");
		var txt = document.createTextNode(url);
		aref.href = url;
		aref.target = "_blank";
		aref.appendChild(txt);
		return aref;
	},
	insertarWEBIMGPage:function(url)
	{
		var aref = document.createElement("a");
		aref.href = url;
		aref.target = "_blank";
		return aref;
	},
	doThumbDIV:function(url,indice)
	{
		var division = document.createElement("div");
		var imagen = document.createElement("img");
		var tabla = document.createElement("table");
		var tbody = document.createElement("tbody");
		var tr = document.createElement("tr");
		var td = document.createElement("td");
		
		imagen.alt = "";
		imagen.src = url;
		
		
		td.appendChild(imagen);
		
		td.valign = "middle";
		
		tr.appendChild(td);
		tbody.appendChild(tr);
		tabla.appendChild(tbody);
		
		tabla.width = "100%";
		tabla.style.height = "100%";
		
		division.appendChild(tabla);
		division.className = "THUMB";
		
		division.index = indice;
		division.ih = this;
		
		division.onclick = function()
		{
			this.ih.insertarInformacion(this.index);
		}
		
		return division;
	}
});

var ih = new IH();
window.ih = ih;

window.onload = function()
{
	$("MENU_ARTICULOS").getElementsByTagName("li")[0].onclick();
}