/*  
- Based on "Simple jQuery Collapsing menu", copyright 2007 by Marco van Hylckama Vlieg
- NWS modifications © 2008
- HTML structure to use:
   <ul id="menu">
     <li><a href="#">Sub menu heading</a>
     <ul>
       <li><a href="http://site.com/">Link</a></li>
       <li><a href="http://site.com/">Link</a></li>
     </ul>
     <li><a href="#">Sub menu heading</a>
   </ul>
*/

function initMenu() {
  var $ = jQuery;

  // OLD: $('#menu ul').hide();
  // OLD: $('#menu li a').click(
  $('#menu li:has(ul)').children('a').addClass('expandable').click(
    function(e) {
				// toggle sub-menu that follows clicked link (using basic/pretty toggle, depending on flag)
				Toggle($(this), !window.bBasicToggle);
				// cancel link default action (jQuery bug means if page has BASE href, isn't auto cancelled as should be)
				e.preventDefault();
      }
    );

	// get page path (without SITE_PATH)
	var sPagePath = location.pathname.replace(SITE_PATH, "");

	// DBG: alert(sPagePath);
	if((sPagePath != "") && (sPagePath != "/")) {
		// get pointer to first menu link with href matching current URL
		var aActiveLink = $("#menu a[href="+ sPagePath +"]").eq(0);
		
		// add "activeLink" CSS class to current page link
		aActiveLink.addClass("activeLink");
		// display menu tree that current page link is part of
		Toggle( aActiveLink.parent().parent().prev(), !window.bBasicToggle );
	}
	
	$('#menu a[href$=.pdf]').addClass('pdfLink');		// add "pdfLink" classes to PDF links
	$('#menu a[href=#]').addClass('blankLink');			// add "blankLink" classes to "#" links
	
	// OLD: $('#menu').show();															// reveal menu, in case not already revealed
}


// show/hide UL sub-menu that follows specified link (jQuery object), plus all parent menus
function Toggle(jqLink, bPretty) {
	if(typeof jqLink != "object") jqLink = $(this);
	if(typeof bPretty == "undefined") bPretty = true;
	
	var ulSubMenu = jqLink.next();
	// if sub-menu after clicked link is currently closed, open it
	if(!ulSubMenu.is(':visible')) {
		// close menus that are already open
		var ulOpenMenus = jqLink.parent().parent().find('ul:visible');
		(bPretty? ulOpenMenus.slideUp('normal') : ulOpenMenus.css('display', 'none'))
			.prev().removeClass("expanded");

		// add "expanded" CSS class to clicked link, so 'open' bullet icon shown
		jqLink.addClass("expanded");
		// display the sub-menu after the clicked link (+ its parent menus)
		var ulSelfAndParents = ulSubMenu.parents("ul").andSelf();
		if(bPretty) 
			ulSelfAndParents.slideDown('normal');
		else
			ulSelfAndParents.css('display', '');
	}
	// sub-menu following clicked link is currently open, so close it
	else {
		if(bPretty) 
			ulSubMenu.slideUp('normal'); 
			else 	ulSubMenu.css('display', 'none');

		jqLink.removeClass("expanded");
	}
}

jQuery(document).ready(function($) {
		if(document.all) {
			// only display top-level initially in IE, as IE6 displays page very slowly if try to load menu with rest of page
			// OLD: $('#menu ul').hide();
			// OLD: $('#menu').show();
			setTimeout(initMenu, 10);
		}
		else
			initMenu();
	});
