function hideSubMenus(slideSpeed) {
	
	// If there is no slide speed just hide the menus
	if(!slideSpeed) {
		
		// Hide the sub menus
		$('#menu ul.menu li ul.sub-menu').hide();
		
	// Slide the menus back up
	} else {
		
		// Hide the sub menus
		$('#menu ul.menu li ul.sub-menu').slideUp(slideSpeed);
		
	}
	
}

$(document).ready(function(){
	
	// Get the current parent link width
	var $parentLinkWidth = $('.touch #menu > ul.menu > li a').width();
	
	// Subtract 44 (the width of the disclosure) from it
	var $newParentLinkWidth = $parentLinkWidth - 44;
	
	// Resize the parent link
	$('.touch #menu > ul.menu > li a').width($newParentLinkWidth);
	
	// Add span tags to activate the dropdowns for touch enabled devices
	$('.touch #menu > ul.menu > li').append('<span><a href="#"></a></span>');
	
	// Add a click event handler to the new disclosure link
	$('#menu ul.menu li span a').click(function(e){
		
		// Check if the menu is currently hidden
		var $subMenu = $(this).parent('span').siblings('.sub-menu');
		
		subMenuVisibility = $subMenu.is(":visible");
		
		// If the menu is currently hidden show it and hide all other menus
		if(subMenuVisibility == false) {
			
			// Hide the sub menus
			hideSubMenus(400);
			
			// Show the sub menu
			$subMenu.slideDown(400);
			
		} else {
			
			// Hide the sub menu
			$subMenu.slideUp(400);
			
		}
		
		// Prevent default action
		e.preventDefault();
		
	});
	
	// Menus work on hover for non touch enabled devices
	$(".no-touch ul.menu li a").hover(function() {
		
		// Stop the animation
		$(this).parent().find("ul.sub-menu").stop(true, true);
		
		//Following events are applied to the subnav itself (moving subnav up and down)
		$(this).parent().find("ul.sub-menu").slideDown(400).show();
		
		$(this).parent().hover(function() {
			}, function(){
				$(this).parent().find("ul.sub-menu").slideUp(400); //When the mouse hovers out of the subnav, move it back up
		});
		
	});

});
