$(document).ready(function(){
 	// Defuscate email addresses.
 	$('a').defuscate();
 	
 	// Use the date picker.
	$('.date-pick').datePicker();

	 // Open external links in a new window.
	 $("a").filter(".link").click(function(){
	   return !window.open(this.href);
	 });

	 // Warn before deleting messages.
	 $("a").filter(".deleteMessage").click(function(){
		return confirm("Are you sure you want to delete this message?");
	 });
	 
	 // Warn before deleting requirements.
	 $("a").filter(".deleteRequirement").click(function(){
	 	return confirm("Are you sure you want to delete this requirement?");
	 });
	 
	 // Hide checkboxes.
	 $("div:not(:has(:checked))").filter(".treatments").css("display", "none");
	 
	 // Display treatments that were searched for.
	 $("div:has(.searchedFor)").filter(".treatments").css("display", "block");
	 
	 // Toggle checkbox visibility when the treatment type is clicked on.
	$("a").filter(".type").click(function(){
		var div = $(this).parent().next(".treatments");
		if (div.css("display") == undefined)
		{
			div = $(this).parent().parent().parent().parent().next(".treatments");
		}
		if (div.css("display") == "none")
		{
			div.slideDown("slow");
		}
		else
		{
			div.slideUp("slow");
		}
		return false;
	});
	
	// Display the label for selected checkboxes with bold purple text.
	$("div:has(:checked)").filter(".wwctrl").next(".wwlbl").addClass("checked");
	
	// Toggle checkbox label colour.
	$("input").filter(":checkbox").click(function(){
		if ($(this).attr("checked"))
		{
			$(this).parent().next(".wwlbl").addClass("checked");
		}
		else
		{
			$(this).parent().next(".wwlbl").removeClass("checked");
		}
	});
	
	// Handle the "select all" link.
	$("a").filter(".selectAll").click(function(){
		var div = $(this).parent().parent().parent().parent().parent().next(".treatments");
		div.children(".treatment").children(".wwgrp").children(".wwctrl").children(":checkbox").attr("checked", true);
		div.children(".treatment").children(".wwgrp").children(".wwlbl").addClass("checked");
		if (div.css("display") == "none")
		{
			div.slideDown("slow");
		}
		return false;
	});
	
	// Handle the "select none" link.
	$("a").filter(".selectNone").click(function(){
		var div = $(this).parent().parent().parent().parent().parent().next(".treatments");
		div.children(".treatment").children(".wwgrp").children(".wwctrl").children(":checkbox").attr("checked", false);
		div.children(".treatment").children(".wwgrp").children(".wwlbl").removeClass("checked");
		if (div.css("display") == "block")
		{
			div.slideUp("slow");
		}
		return false;
	});
	
	// FAQ animation.
	$("a").filter(".question").click(function(){
		$(".answer").slideUp("slow");
		$(this).parent().next(".answer").slideDown("slow");
		return false;
	});
	
	// Hide FAQ answers until clicked on.
	$(".answer").css("display", "none");

	// Do nothing with null links.
	$("a").filter(".null").click(function(){
		return false;
	});

    // Use Facebox for popup images.
	jQuery(document).ready(function($) {
	  $('a[rel*=facebox]').facebox(); 
	});
	
	// navigation items.
    $(document).ready(function(){ 
        $("ul.sf-menu").supersubs({ 
            minWidth:    2,   // minimum width of sub-menus in em units 
            maxWidth:    12,   // maximum width of sub-menus in em units 
            extraWidth:  3     // extra width can ensure lines don't sometimes turn over 
                               // due to slight rounding differences and font-family 
        }).superfish({
	        hoverClass    : "sfHover", 
	        delay        : 750, 
	        animation    : {opacity:"show"}, 
	        speed        : 250 
    	});
    });
});
