/*
 * Door Mark van der Tol
 */
$(function() {
	function nextNode(start)
	{
		var n = start.nextSibling;
		
		if (!n)
			return undefined;
		if (n.nodeName == "#text")
			return nextNode(n);
	
		return n;
	}
	
	function previousNode(start)
	{
		var n = start.previousSibling;
		if (!n)
			return undefined;
		if (n.nodeName == "#text")
			return previousNode(n);
	
		return n;
	}
	
	function nodeCookieName(node)
	{
		if (!node)
			return "";
		var name = node.innerHTML;
		name = name.replace(" &gt;","");
		
		return nodeCookieName(previousNode(node.parentNode.parentNode)) + name + "-";
	}
	
	function reopen(event)
	{
		var cookieName =  "menu_" + nodeCookieName(this);
		var cookieOptions = { path: '/' };
	
		$(nextNode(this)).each(
			function (i) {
				if (this.style.display == "none") {
					$.cookie(cookieName, "open", cookieOptions);
					this.style.display = "block";
				} else {
					$.cookie(cookieName, null, cookieOptions);
					this.style.display = "none";
				}
			}
		);	
		event.preventDefault();
	}
	
	$("#left_menu a").each(
		function(i) {
			var next = nextNode(this);
			var cookieName =  "menu_" + nodeCookieName(this);
			
			if (next && next.tagName == "UL")
			{
				if ($.cookie(cookieName)) {
					next.style.display = "block";
				} else {
					next.style.display = "none";
				}
				$(this).bind("click", reopen);
			}
		}
	);
});


