$(document).ready(function(){

        // Display the Take Action Menu
	$("ul#takeaction").show();

	// Add last class to the last li in each navigation list.
	$("div#nav-realm ul li:last, div#nav-utilities ul.user li:last, div#nav-utilities ul.utilities li:last, div#nav-primary ul li:last").addClass("last");

	// Add identifying character to external links
	$("div#nav-realm ul li a[rel='external']").each(function() {
		$(this).addClass("external");
	});

	$("div.nav-allgroups ul li ul").parent("li").each(function() {
		$(this).prepend("<span>Expand</span>");
		$(this).addClass("collapse");
		$(this).addClass("parent");
	});
	
	$("div.nav-allgroups h4").each(function() {
		$(this).append("<span class='togglelist'>[Expand All]</span>");
	});
	
	$("div.nav-allgroups span.togglelist").click(function() {
		if ( $(this).text() == "[Expand All]" ) {
			$(document.body).find("div.nav-allgroups li.parent.collapse").each(function() {
				$(this).removeClass("collapse");
			});
			$("div.nav-allgroups span.togglelist").text("[Collapse All]");
		} else if ( $(this).text() == "[Collapse All]" ) {
			$(document.body).find("div.nav-allgroups li.parent").not(".collapse").each(function() {
				$(this).addClass("collapse");
			});
			$("div.nav-allgroups span.togglelist").text("[Expand All]");
		}
	});
	
	$("div.nav-allgroups ul li.parent span").click(function() {
		$(this).parent("li").toggleClass("collapse");
		return false;
	});

	// What was clicked?
	$(document.body).click(function(e){
		
		// If this is a child of the Groups Menu
		if ( $(e.target).parents().is("li.groups") ) {
			collapseMenus(".groups");
			if ( $(e.target).is(".toggle") ) {
				$(document.body).find("li.groups").toggleClass("expand");
				$("div.nav-summary form input#search_all_val").focus();
				return false;		
			}
		
		// If this is a child of the User Menu
		} else if ( $(e.target).parents().is("li.username") ) {
			collapseMenus(".username");
			if ( $(e.target).is(".toggle") ) {
				$(document.body).find("li.username").toggleClass("expand");
				return false;		
			}
			
		// If this is a child of the Administration Menu
		} else if ( $(e.target).parents().is("li.administration") ) {
			collapseMenus(".administration");
			if ( $(e.target).is(".toggle") ) {
				$(document.body).find("li.administration").toggleClass("expand");
				return false;		
			}

		// If this is a child of the Take Action Menu
		} else if ( $(e.target).parents().is("li.take_action") ) {
			collapseMenus(".take_action");
			if ( $(e.target).is(".toggle") ) {
				$(document.body).find("li.take_action").toggleClass("expand");
				return false;		
			} 
			
		// If this is a child of the Summary Filter Menu
		} else if ( $(e.target).parents().is("li.filter") ) {
			collapseMenus(".filter");
			if ( $(e.target).is(".toggle") ) {
				$(document.body).find("li.filter").toggleClass("expand");
				return false;		
			} 

		// Something else was clicked
		} else {
			collapseMenus();						
		}
		
	});
	
});

function collapseMenus(omitEl) {
	if (omitEl) {
		$(document.body).find(".expand").not(omitEl).removeClass("expand");
	} else {
		$(document.body).find(".expand").removeClass("expand");
	}		
}
