// execute only when the whole document is ready
$(document).ready(function() {
	
	// hide all sub heading lists
	$('ul#mininav li ul').hide();
	
	/* Look at body tag class to see which menu to expand initially */
	
	if ( $('body').is('.admissions') ) {
        // expand the proper menu when the page loads
		$('ul#mininav li ul#admissions').show().prev().addClass('selected');
	} 
	
	if ( $('body').is('.application') ) {
        // expand the proper menu when the page loads
		$('ul#mininav li ul#application').show().prev().addClass('selected');
	} 
	
	if ( $('body').is('.student-life') ) {
        // expand the proper menu when the page loads
		$('ul#mininav li ul#student-life').show().prev().addClass('selected');
	} 
	
	if ( $('body').is('.career') ) {
        // expand the proper menu when the page loads
		$('ul#mininav li ul#career').show().prev().addClass('selected');
		
	} 
	
	if ( $('body').is('.curriculum') ) {
        // expand the proper menu when the page loads
		$('ul#mininav li ul#curriculum').show().prev().addClass('selected');
		
	}
	

	/* Grab the filepath - use this to decide which sublink to get the selected class */
	
	var path = location.pathname.substring(1);
	/*$('#subnav a[@href$="http://med.umich.edu/ ' + path + '"]').addClass('active');*/
	
	/* do it to the ones with submenus */
  	$('ul#mininav ul li a[@href$="http://med.umich.edu/' + path + '"]').attr('class', 'selected');
	/* do it to the ones that do NOT have submenus */
	$('ul#mininav li a[@href$="http://med.umich.edu/' + path + '"]').attr('class', 'selected');
	
	/* Remove the link from the selected link so it is not clickable */
	/* do it to the ones with submenus */
	$("ul#mininav ul li a.selected").removeAttr("href");
	
	/* Remove the link from the selected link so it is not clickable */
	/* do it to the ones that do NOT have submenus */
	$("ul#mininav li a.selected").removeAttr("href");
	
	
	
	// add a click handler to the heading links
	$('ul#mininav li a.menuhead').click(function(){
										
		// if the current sub heading list is already open
		if($(this).next('ul:visible').length) {
			// close the sub heading list
			$(this).next('ul:visible').slideUp();
		} else {
			// close all open sub heading lists
			$('#nav li ul:visible').slideUp();
			// slide open the next list
			$(this).next('ul').slideToggle('normal');
			

		}
	
	// return false to stop link following the href
	return false;
	});
});