/* Global variable holding the expand/collaps status of the sidebar menu */
sidebar_state = {}

/* This function sets the visibility of menu items in the left sidebar based
 *  on the values stored in the sidebar_state global variable.
 *
 *  Args:
 *      None
 */
function set_sidebar_state() {
	$.each(sidebar_state.cookieObject, function(index, value) {
		if (value == 'none') {
			$('#'+index).children("ul").hide()
		} else {
			$('#'+index).children("ul").show()
		}
	});
}

$(document).ready(function(){
	/* Global variable that holds the sidebar tree structure state */
	sidebar_state = $.cookieJar('tree-menu')
	set_sidebar_state()

	/* This function makes all sidebar menu items to be collapsible.
	 *
	 * Args:
	 *     None
	 */
	$("ul li.collapsible span").click(function(){
		/* Toogle menu item visibility */
		var menu_id = $(this).parent('li').attr('id')
		var menu = $(this).parent().children("ul")
		menu.toggle()

		/* Save the menu status */
		sidebar_state.set(menu_id, menu.get(0).style.display)

		/* Find the default link */
		var link = $(this).parent('li').find('a.default')
		if (link.length > 0) {
			var target = link.attr('href')
			if (window.location.pathname != target) {
				window.location = link.attr('href')
			}
		}
	});
})