// 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 and determine which top nav item should be highlighted */
	if ( $('body').is('.home') ) {
		$("div#navbartop li#home-section").attr('id', 'selected');
	}
	
	if ( $('body').is('.about') ) {
		$("div#navbartop li#about-section").attr('id', 'selected');
	}
	
	if ( $('body').is('.research') ) {
		$("div#navbartop li#research-section").attr('id', 'selected');
	}
	
	if ( $('body').is('.news') ) {
		$("div#navbartop li#news-section").attr('id', 'selected');
	}

	if ( $('body').is('.resources') ) {
		$("div#navbartop li#resources-section").attr('id', 'selected');
	}
	
	
	
	
	
	
	
	
	/* 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('.facultysearch') ) {
        // expand the proper menu when the page loads
		$('ul#mininav li a.facultysearch').addClass('selected');
	}
	
	
	
			if ( $('body').is('.currentstudents') ) {
        // expand the proper menu when the page loads
		$('ul#mininav li a.currentstudents').addClass('selected');
	}
	
	
	
				if ( $('body').is('.departmentlist') ) {
        // expand the proper menu when the page loads
		$('ul#mininav li a.departmentlist').addClass('selected');
	}
	
	
					if ( $('body').is('.byresearch') ) {
        // expand the proper menu when the page loads
		$('ul#mininav li a.byresearch').addClass('selected');
	}
	
			if ( $('body').is('.careeradvisory') ) {
        // expand the proper menu when the page loads
		$('ul#mininav li a.careeradvisory').addClass('selected');
	}
	
	
			if ( $('body').is('.clinicalmentor') ) {
        // expand the proper menu when the page loads
		$('ul#mininav li a.clinicalmentor').addClass('selected');
	}
	
	
		if ( $('body').is('.events') ) {
        // expand the proper menu when the page loads
		$('ul#mininav li ul#events').prev().addClass('toplevel');
		$('ul#mininav li ul#events').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');
	
	
	
	
	/* 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;
	});
});