$(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");
		}
	);
	
	
// Clinic search and answer pages ////////////////////////////////////
	
	$('head').append(plusMinusCSS);
	
	// If any questions are answered no and have dependents,
	// make sure the dependents are removed. In case someone
	// answered them anyway.
	$dependentParents = $('ul.question-list li p span.dependent_parent');
	$dependentParents.each(function(){
		var text = $(this).text();
		text = $.trim(text);
		if(text.match(/.*[No|N\/A]+.*/)){
			$(this).parent().next('div.dependent_child').remove();
		}
	});
	
	
	// Check if divs that need answers have answers in them
	// If they don't, remove them
	$answerDivs = $('div.needs_answers');
	$answerDivs.each(function(){
		if ($('span.answer', this).length == 0){
			$(this).remove();
		}
			
	});
	
	// Check if lis that need answers have answers in them
	// If they don't, remove them
	$answerLis = $('li.needs_answers');
	$answerLis.each(function(){
		if ($('span.answer', this).length == 0){
			$(this).remove();
		}
			
	});

	$('h3.question_list_head').hover(
		function(){
			$(this).fadeTo(100, 0.8);
		},
		function(){
			$(this).fadeTo(100, 1);
		}
	);

	// Alternate the colour scheme on lis and hide the last two sections
	
	$uls = $("ul.question-list");
	$uls.each(function(index){
		if (index != 0 && index != 1){
			$(this).hide();
			$(this).addClass('closed');
		} else {
			$(this).addClass('open');
		}
	});
	
	$h3s = $("h3.question_list_head");
	$h3s.each(function(index){
		$(this).click(function(){
			$(this).next().toggle("slow");
			$(this).toggleClass('open closed');
		});
		if ($(this).next().css("display") == "none"){
			$(this).addClass("closed");
		} else {
			$(this).addClass("open");
		}
	});
	
	$lis = $("ul.question-list li");
	$lis.each(function(index) {
		$(this).addClass(
			"alternator_" + (index % 2 == 0 ? 1 : 2) 
		);
	});
	
	$ans = $("ul.question-list li p span.answer");
	$ans.each(function(index){
		  var text = $(this).text();
		  text = $.trim(text);
		  if ( text == "Yes" || text == "yes"){
			$(this).replaceWith("<span class='answer tick'></span>");
		  } else if (text == "No" || text == "no"){
			$(this).replaceWith("<span class='answer cross'></span>");
		  }
	});

});

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...");
	}
}

var hn = "";//window.location.hostname;
var plusMinusCSS = "<style type='text/css'>\
	h3.open span{\
		padding-left:2em;\
		background:url("+hn+"/media/scripts/images/minus-icon.gif) left no-repeat;\
	}\
	h3.closed span{\
		padding-left:2em;\
		background:url("+hn+"/media/scripts/images/plus-icon.gif) left no-repeat;\
	}\
</style>";


