$(document).ready(function(){

	$("div#utilities ul.section_links li a").click(function(e){
		var link = $(this).attr("href");
		link = link.substring(1);
		toggleSectionBody(link);
	});

	$("div#main div.section").each(function(){
		var mthis = this;
		$("div.section_body", this)
			.hide()
			.parent()
			.append('<p class="show_section"><a href="#">read more...</a></p>');
		
		$("p.show_section, h2", this).click(function(e){
			e.preventDefault();
			var id = $(mthis).attr("id");
			toggleSectionBody(id);
			return false;
		});
	});
	
	$('div#main sup').hover(
		function(){
			var id = $(this).text();
			var target = $("div.references sup:contains("+id+")").parent();
			target.addClass("hovered");
		},
		function(){
			var id = $(this).text();
			var target = $("div.references sup:contains("+id+")").parent();
			target.removeClass("hovered");
		}
	);

});

function toggleSectionBody(sectionId){
 	if(!$("div#" + sectionId).find("div.section_body").is(":visible")){
 		$("div.body_selected").removeClass("body_selected").find("div.section_body").hide().parent().find("p.show_section a").text("read more...");
		var theSection = $("div#" + sectionId).addClass("body_selected");
		$("div.section_body", theSection).show("slow");
		var togbtn = theSection.find("p.show_section a");
		togbtn.text((togbtn.text() == "read more..." ? "close" : "read more..."));
	} else {
		$("div.body_selected").removeClass("body_selected").find("div.section_body").hide().parent().find("p.show_section a").text("read more...");
	}
}