$(document).ready(function() {
	
	$("li.p1:has(ul)").click(function(event){
		if (this == event.target) {
			var current = this;
			$("#nav li:has(ul)").each(function() {
				if (this != current) $(this).children().slideUp(400);
			});
			$("ul:first", $(this)).slideToggle(400);
		}
	});
});


function expand(id){
	var current = null;
	var link = null;
	
	$("#nav a").each(function(){
		if($(this).attr("href") == "/ws/id:"+id){
			link = $(this);
			if($(this).parent().parent().parent().attr("class") == "p1 down"){
				current = $(this).parent().parent().parent();
			}
			else {
				current = $(this);
			}
		}
	});
	
	if(current == null) return;

	// Slides down the element
	$("ul:first", $(current)).slideToggle(0);
	
	// unselects all the other links
	$("#nav a").each(function(){
		if($(this).hasClass("selected")){
			$(this).removeClass("selected");
		}
	});
	
	// selects the active link
	link.addClass("selected");
	link.removeAttr("href");
}
