$(function() {
	var $packages = $("ul.packages");
	var $packageChk = $("li.package>span>input:checkbox");

	// Format option content based upon package selection
	var formatPackage = function($chk, speed) {
		// Remember, ASP.net renders a CheckBox as:
		// <li><span><input type="checkbox" /><label /></span></li>
		// So we need the parent of the parent of the checkbox
		var $li = $chk.parent("span").parent("li");
		if ($chk.is(":checked")) {
			$("div.options", $li).show(speed);
		} else {
			$("div.options", $li).hide(speed);
		}
	};

	
	// Wire up the main package checkboxes
	$packageChk.click(function() {
		// Dynamic show/hide on selection
		formatPackage($(this), 1000);
	})
	.each(function() {
		// Set initial form state
		formatPackage($(this));
	});

	
	// Provide select/clear all functionality
	$("a.select", $packages).click(function(evt) {
		evt.preventDefault();
		var $link = $(this);
		var $container = $link.parent("p").parent("div");
		var $chkBoxes = $(":checkbox", $container);

		$chkBoxes.attr("checked", $link.hasClass("all"));		
		return false;
	});
});
