jQuery(document).ready(function()
{
	// Initialize the primary navigation menu
	jQuery('.primaryNav ul').slideMenu();
});

jQuery.fn.slideMenu = function()
{
	// Get all li elements that contain a nested list
	var nestedItems = jQuery(this).find('li:has(ul)');

	// Get all nested ul elements and hide them
	var nestedLists = nestedItems.children('ul').hide();

	// Bind click handler to the a elements in the li elements
	nestedItems.children('a').click(function(event)
	{
		// Get the next ul element in DOM after the clicked a element
		var list = jQuery(this).next('ul');

		// Check if the ul element is hidden
		if (list.is(':hidden'))
		{
			// Hide all other ul elements
			nestedLists.not(this).hide('slow');

			// Show the ul element
			list.animate({opacity:'toggle',height:'toggle'},'slow');
		}

		// Disable default a action
		return false;
	});

	// Show all ul elements traversing upwards from the current page item
	nestedLists.children('.current_page_item').parents('ul').show();
}
