// 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('.first-load') ) {
		$("ul#mininav li.first-home").attr('class', 'selected');
	}
	
	if ( $('body').is('.about_us') ) {
        // expand the proper menu when the page loads
		/*$('ul#mininav li ul#applications').show().prev().addClass('selected');*/
		$('ul#mininav li ul#about_us').prev().addClass('toplevel');
		$('ul#mininav li ul#about_us').show().addClass('selected');
		
	} 
	
	
	if ( $('body').is('.resources') ) {
        // expand the proper menu when the page loads
		$('ul#mininav li ul#resources').prev().addClass('toplevel');
		$('ul#mininav li ul#resources').show().addClass('selected');
		
	
	} 
	
	if ( $('body').is('.programs') ) {
        // expand the proper menu when the page loads
		$('ul#mininav li ul#programs').prev().addClass('toplevel');
		$('ul#mininav li ul#programs').show().addClass('selected');
	} 

	if ( $('body').is('.towsley') ) {
        // expand the proper menu when the page loads
		$('ul#mininav li ul#towsley').prev().addClass('toplevel');
		$('ul#mininav li ul#towsley').show().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');*/
	/*alert(path);*/
	
	/*$('ul#mininav ul li a[@href$="http://med.umich.edu/medschool/vitals-OTT/"]').attr('class', 'selected');*/
	

	
	
	/* do it to the ones with submenus */
  	$('ul#mininav li 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');
	
	
		/* do it to the ones with submenus */
  	$('ul#mininav li ul li a[@href$="http://www.med.umich.edu/' + path + '"]').attr('class', 'selected');
	/* do it to the ones that do NOT have submenus */
	$('ul#mininav li a[@href$="http://www.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");*/

	$('ul#mininav li ul li a').click(function(){
			/*alert("hello");*/
			/*$('ul#mininav li a').toggleClass('selected');*/
			/*$("ul#mininav").find("span").css('color','red');*/
			
			
			/*$('ul#mininav').find('a').filter('[class=menuhead selected]').attr('class', 'boot');*/

			

			
		})
	
	// return false to stop link following the 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;
	});
});