/**************************************
Dependencias con Events.js y jQuery1.1.3.js
*/
var ContentLists = 
{
	_ItemsToShow : 3
	,
	Init : function ()
	{
		var divs = document.getElementsByTagName("DIV");
		//alert(uls.length);
		for (var i=0;i<divs.length;i++)
		{
			var div = divs[i];
			if (div.className == "ContentList")
			{
				//Plegar ULS
				uls = div.getElementsByTagName("UL");
				if (uls.length > 0)
				{
					var ul = uls[0];
					var lis = ul.getElementsByTagName("LI");
					
					
					if (lis.length <= ContentLists._ItemsToShow)
					{
						//Quito 1px a la altura
						ul.style.height = (ul.offsetHeight - 3) + "px";
					}
					else
					{
						ContentLists._HideItems(ul, lis, false);
					}
					//Configurar botones
					var divsTemp = div.getElementsByTagName("DIV");
					if (divsTemp.length > 0)
					{
						var divBoton = divsTemp[0];
						if (lis.length > ContentLists._ItemsToShow)
						{
							divBoton.style.display = "";
							var as = divBoton.getElementsByTagName("A");
							if (as.length > 0)
							{
								var a = as[0];
								Events.Add(a, "click", ContentLists.ShowAll);
							}
						}
					}
				}
				
				//Mostrar el div contenedor, esta oculto
				div.style.visibility = "visible";
				
			}
		}
	}
	,
	ShowAll : function (e)
	{
		var a;
		e = e ? e : event;
		if (e.target)
		{
			a = e.target;
		}
		else
		{
			a = e.srcElement;
		}
		
		//Remover el A
		var divButton = a.parentNode;
		divButton.removeChild(a);
		var aNew = document.createElement("A");
		Events.Add(aNew, "click", ContentLists.HideAll);
		divButton.appendChild(aNew);
		
		//Cambiar la clase del div.Button
		divButton.className = "ButtonHide";
		
		//Mostrar los elementos
		var uls = divButton.parentNode.getElementsByTagName("UL");
		if (uls.length > 0)
		{
			var ul = uls[0];
			ContentLists._ShowItems(ul);
		}
		return false;
	}
	,
	HideAll : function (e)
	{
		var a;
		e = e ? e : event;
		if (e.target)
		{
			a = e.target;
		}
		else
		{
			a = e.srcElement;
		}
		//Remover el A
		var divButton = a.parentNode;
		divButton.removeChild(a);
		var aNew = document.createElement("A");
		Events.Add(aNew, "click", ContentLists.ShowAll);
		divButton.appendChild(aNew);
		
		//Cambiar la clase del div.Button
		divButton.className = "ButtonShow";
		
		//Mostrar los elementos
		var uls = divButton.parentNode.getElementsByTagName("UL");
		if (uls.length > 0)
		{
			var ul = uls[0];
			ContentLists._HideItems(ul, ul.getElementsByTagName("LI"), true);
		}
		return false;
	}
	,
	_ShowItems : function (ul)
	{
		var height = -1;
		var lis = ul.getElementsByTagName("LI");
		for (var j=0;j<lis.length;j++)
		{
			var li = lis[j];
			height += li.offsetHeight;
		}
		var hash = new Object();
		hash.height = height;
		$(ul).animate(hash, "slow");
	}
	,
	_HideItems : function (ul, lis, animate)
	{
		//Oculto mas alla del tercero
		var height = -1; //El ultimo separador de la lista mide un pixel
		for (var j=0;j<ContentLists._ItemsToShow;j++)
		{
			var li = lis[j];
			height += li.offsetHeight;
		}
		if (animate)
		{
			var hash = new Object();
			hash.height = height;
			$(ul).animate(hash, "slow");
		}
		else
		{
			ul.style.height = height+ "px";
		}
	}
}

Events.Add(window, "load", ContentLists.Init);