/**
 *	Menu
 *	@library jQuery
 *	@author Phil Thompson [http://philthompson.co.uk/]
 */

 
 /** 
  *	document.ready (onload)
  */
 $(document).ready(function(){  	
 	
 	autoOpen();
 	
 	/**
 	 *	@var boolean
 	 *	set to true if you don't want a parent
 	 *	link to return:true; after onclicking
 	 */
 	var noReturn = true;
 	showHideOnClick(noReturn); 	
 		
 });


/**
 *	autoOpen()
 *	If a menu item link is the current page 
 *	automatically open it up
 */ 
function autoOpen(){
	/* Current page URL */
	var currentPage = window.location.pathname;
	
	/** 
	 *	Look through menu links
	 *	check links match the curent URL
	 *	if they do open up that item's parent (li) or children <ul>
	 */
	$('#secondary-menu ul li a').each(function(){
		var href = $(this).attr('href');

		if(currentPage == href){
			$(this).parent().addClass('selected');
			/* Open up the parent menu */
			var parent_li = $(this).parent('li').parent('ul').parent('li');
			if(parent_li.length > 0){
				$(parent_li).addClass('selected');
			}
		}
		
	});
}
 
/**
 *	showHideOnClick
 *	Show/hide menu items based on clicking.
 *	Only one menu item at a time.
 *	CSS does the showing/hiding, JavaScript here just to
 *	add/remove class names for CSS hooks.
 *	UPDATE: this now slides the mneu items up and down.
 */
function showHideOnClick(noReturn){

 	$('#secondary-menu ul li a').click(function(){
 	
 		/* Has children */
 		$children = false; 		

 		if($(this).parent().find('ul').length > 0){
 			$children = true; 			
 		}
 		if($children == false){
 			return true;
 		}
 		/* Children showing? Show/hide */
 		if($(this).parent().hasClass('selected')){
 			/* Hide children */
 			$(this).parent().find('ul').slideUp('slow');
 			$(this).parent().removeClass('selected');
 			
 		} else{
 			/* Show children */
 			hideAll();
 			$(this).parent().find('ul').slideDown('slow');
 			$(this).parent().addClass('selected');
 		}

 		/* Links that are hashes shouldn't be returned */
 		if($(this).attr('href') == '#' || noReturn == true){
 			return false;	
 		}
 			
 	});
 	
 }

/**
 *	hideAll()
 */
function hideAll(){
	$('#secondary-menu ul li ul').slideUp('slow');
	$('#secondary-menu ul li').removeClass('selected');
}
