// GalleryList.js

var GalleryList = {
	currOpenedBranch:null,
	init: function()
	{
		var Galleries = document.getElementById("galleries").childNodes;

		GalleryList.currOpenedBranch = (getCookie('branch')) ? getCookie('branch') : null;
		for (var i = 0; i < Galleries.length; i++)
		{
			if (Galleries[i].tagName == "LI")
			{
				var item = Galleries[i].getElementsByTagName("a")[0];
				var subMenu;
				if (subMenu = item.parentNode.getElementsByTagName("ul")[0])
				{
					if (item.parentNode.id == GalleryList.currOpenedBranch)
					{
						item.className = 'opened';
						subMenu.className = 'opened';
					}

					for (var j = 0; j < subMenu.childNodes.length; j++)
					{
						if (subMenu.childNodes[j].tagName == "LI")
						{
							var subMenuItem = subMenu.childNodes[j].getElementsByTagName("a")[0];
							subMenuItem.onmouseover = GalleryList.toggleBackground;
							subMenuItem.onmouseout = GalleryList.toggleBackground;
							subMenuItem = null;
						}

					}
					submenu = null;
				}
				item.onclick = GalleryList.openBranch;
				item.onmouseover = GalleryList.toggleBackground;
				item.onmouseout = GalleryList.toggleBackground;
				item = null;
			}
		}
		// fixes mem leak in IE.
		Galleries = null;
	},
	openBranch: function()
	{
		if (GalleryList.currOpenedBranch != null)
		{
			var oldOpenedBranch = document.getElementById(GalleryList.currOpenedBranch).getElementsByTagName("a")[0]
			oldOpenedBranch.className = "closed";
			if (oldOpenedBranch.parentNode.getElementsByTagName("ul")[0])
			{
				oldOpenedBranch.parentNode.getElementsByTagName("ul")[0].className = "closed";
			}
			oldOpenedBranch = null;
		}
		if (this.parentNode.getElementsByTagName("ul")[0])
		{
			this.className = this.className == "opened" ? "closed" : "opened";
			this.parentNode.getElementsByTagName("ul")[0].className = this.parentNode.getElementsByTagName("ul")[0].className == "closed" ? "opened" : "closed";
			GalleryList.currOpenedBranch = this.parentNode.id;

			var expdate = new Date();
			expdate.setTime(expdate.getTime() + 265*24*60*60*1000);
			setCookie('branch',GalleryList.currOpenedBranch,expdate);
		}
		return false;
	},
	toggleBackground: function()
	{
		//alert(this.style.backgroundColor);
		var newColor = this.style.backgroundColor == "" ? "#57412e" : "";
		//alert(this.style.backgroundColor);
		this.style.backgroundColor = newColor;
		this.blur();
	}
};

addEventToObject(window,"onload",GalleryList.init);