
function resetMenu()
{
   	$('.parent_menu').each(function() {
		var id 		= this.id.substring(3);
		var child   = $("#cm_" + id);

		child.hide();
		$.cookie("cm_" + id, 'hide', {path: "/"});
	});
}


$(document).ready(function() {

	var pid 	= $.jqURL.get('p_id');
	var child   = $("#cm_" + pid);	

	if (pid)
	{
		if (child.is(":visible")) {
			resetMenu();
			child.hide();
			$.cookie("cm_" + pid, 'hide', {path: "/"});
		} 
		else 
		{
			resetMenu();
			child.show();
			$.cookie("cm_" + pid, 'show', {path: "/"});
		}		
	}


	// Click function for all the categories that have children
	$('.parent_menu').click(function(e) {   

		var clicked = $(e.target);   		
		var id 		= clicked.attr("id").substring(3);
		var child   = $("#cm_" + id);
		
		if (id)
		{
			if (child.is(":visible")) {
				resetMenu();
				child.hide();
				$.cookie("cm_" + id, 'hide', {path: "/"});
			} 
			else 
			{
				resetMenu();
				child.show();
				$.cookie("cm_" + id, 'show', {path: "/"});
			}
			return false;		
		}
	});  


	// Click function for all the categories that have children
	$('.noparent_menu').click(function(e) {   
				resetMenu();
	});  
	
	
	// Go through all the parents, read the cookies and decide to show or hide
   	$('.parent_menu').each(function() {
		var id 		= this.id.substring(3);
		var child   = $("#cm_" + id);
		var c 		= $.cookie("cm_" + id);
 		
		if (c == 'show')
		{
			child.show();			
		}
		else
		{
			child.hide();
		}
	});
});
