$(function() {
	// Utilities
	// Button to expand/collapse all the books
	$('#expandall').toggle(
		function() {
			$('.lab').fadeIn();
			$('.batch img.arrow').attr("src", "images/down_arrow.gif");
			$(this).text('Collapse All Books');
		},
		function() {
			$('.lab').fadeOut();
			$('.batch img.arrow').attr("src", "images/right_arrow.gif");
			$(this).text('Expand All Books');
		}
	);

	// Clear all selection
	$('#reset').click(function() {
		$('.batch img.multiselect').attr('src', 'images/checkbox_off.gif');
		$('.lab input').attr('checked', false);
		$('.batch').animate({backgroundColor: "#fffd92"}, "fast").animate({backgroundColor: "white"}, 1500);
	});

	// Select default 100 labs
	$('#default').click(function() {
		// Clear existing selection
		$('.batch').animate({backgroundColor: "#fffd92"}, "fast");
		$('.batch img.multiselect').attr('src', 'images/checkbox_off.gif');
		$('.lab input').attr('checked', false);

		var default_labs = new Array(
			"BIO-A-VST0040",
			"BIO-A-VST0043",
			"BIO-A-VST0377",
			"BIO-A-VST0046",
			"BIO-A-VST0429",
			"CHEM-A-VST0004",
			"CHEM-A-VST0006",
			"CHEM-A-VST0009",
			"CHEM-A-VST0017",
			"CHEM-A-VST0022",
			"CHEM-A-VST0033",
			"CHEM-A-VST0035",
			"BWV-VST0039",
			"BWV-VST0040",
			"BWV-VST0041",
			"BWV-VST0376",
			"BWV-VST0377",
			"BWV-VST0056",
			"BWV-VST0057",
			"BWV-VST0065",
			"BWV-VST0066",
			"BWV-VST0068",
			"CWV-VST0071",
			"CWV-VST0075",
			"CWV-VST0076",
			"CWV-VST0078",
			"CWV-VST0080",
			"CWV-VST0083",
			"CWV-VST0087",
			"CWV-VST0091",
			"CWV-VST0093",
			"ESV-VST0108",
			"ESV-VST0363",
			"ESV-VST0120",
			"ESV-VST0121",
			"ESV-VST0122",
			"ESV-VST0124",
			"ESV-VST0127",
			"EWV-VST0151",
			"EWV-VST0154",
			"EWV-VST0155",
			"EWV-VST0158",
			"EWV-VST0161",
			"EWV-VST0165",
			"EWV-VST0171",
			"EWV-VST0172",
			"EWV-VST0178",
			"EWV-VST0182",
			"ESI-VST0403",
			"ESI-VST0408",
			"ESI-VST0413",
			"ESI-VST0415",
			"ESI-VST0418",
			"ESI-VST0421",
			"FWV-VST0368",
			"FWV-VST0321",
			"FWV-VST0370",
			"FWV-VST0371",
			"FWV-VST0336",
			"HP-A-VST0186",
			"HP-A-VST0189",
			"HP-A-VST0197",
			"HP-A-VST0201",
			"HP-A-VST0203",
			"HP-A-VST0208",
			"MSV-VST0210",
			"MSV-VST0213",
			"MSV-VST0263",
			"MSV-VST0216",
			"MSV-VST0222",
			"MSV-VST0228",
			"MSV-VST0259",
			"MSV-VST0232",
			"MSV-VST0275",
			"MSV-VST0280",
			"NRV-VST0237",
			"NRV-VST0238",
			"NRV-VST0029",
			"PSV-VST0243",
			"PSV-VST0257",
			"PSV-VST0262",
			"PSV-VST0263",
			"PSV-VST0268",
			"PSV-VST0275",
			"PWV-VST0281",
			"PWV-VST0283",
			"PWV-VST0285",
			"PWV-VST0286",
			"PWV-VST0289",
			"PWV-VST0291",
			"PWV-VST0293",
			"PWV-VST0304",
			"PWV-VST0311",
			"PWV-VST0312",
			"WQV-VST0353",
			"WQV-VST0354",
			"WQV-VST0356",
			"WQV-VST0359",
			"WQV-VST0363",
			"XAVL-VST0450",
			"XAVL-VST0451",
			"XAVL-VST0452"
		);

		for (lab_id in default_labs) {
			$('#' + default_labs[lab_id]).attr('checked', true);
		}

		// ASSUMPTION: Labs from each book are selected in default 100, so all books are marked as some selected
		$('.batch img.multiselect').attr('src', 'images/checkbox_some.gif');

		// The Additional Vernier Labs section includes all its labs when default 100 is clicked
		$('#XAVL').prev('.batch').children('.multiselect').attr('src', 'images/checkbox_on.gif');

		// Fade the labs
		$('.batch').animate({backgroundColor: "white"}, 1500);
	});


	// Expand list of labs for a book
	$('.batch img.arrow').click(function() {
		if ($(this).attr("src") == "images/down_arrow.gif") {
			$(this).attr("src", "images/right_arrow.gif");
			$(this).parent('div').next('div').slideUp("fast");
		} else {
			$(this).attr("src", "images/down_arrow.gif");
			$(this).parent('div').next('div').slideDown("slow");
		}
	});

	// Check all labs in a book
	$('.batch img.multiselect').click(function() {
		if (($(this).attr('src') == 'images/checkbox_off.gif') || ($(this).attr('src') == 'images/checkbox_some.gif')) {
			$(this).parent('div').next('div').find('input:checkbox').each(
				function() {
					$(this).attr('checked', true);
				}
			);
			$(this).attr('src', 'images/checkbox_on.gif');
		} else {
			$(this).parent('div').next('div').find('input:checkbox').each(
				function() {
					$(this).attr('checked', false);
				}
			);
			$(this).attr('src', 'images/checkbox_off.gif');
		}
	});


	// Setting the multiselect state when clicking on a lab checkbox
	$('.lab input').click(function() {
		if ($(this).attr('checked') == true) {
			// Scenario 1: Checking a box. Assume all are checked until we find one that isn't.

			var state = 'checkbox_on.gif';
			var target = $(this).parent('li').parent('ul').parent('div.lab').prev('div.batch').children('img.multiselect');

			// Check all checkboxes, if we find one that's not checked, then we're in the "some" state
			$(this).parent('li').parent('ul').find('input:checkbox').each(
				function() {
					if ($(this).attr('checked') == false) {
						state = 'checkbox_some.gif';
						return false;
					}
				}
			);
			$(target).attr('src', 'images/' + state);
		} else {
			// Scenario 2: Unchecking a box. Assume all are unchecked until we find one checked.

			var state = 'checkbox_off.gif';
			var target = $(this).parent('li').parent('ul').parent('div.lab').prev('div.batch').children('img.multiselect');

			// Check all checkboxes, if we find one that is checked, then we're in the "some" state
			$(this).parent('li').parent('ul').find('input:checkbox').each(
				function() {
					if ($(this).attr('checked') == true) {
						state = 'checkbox_some.gif';
						return false;
					}
				}
			);
			$(target).attr('src', 'images/' + state);
		}
	});


	// Show lab objectives
	$('img.showpreview').toggle(
		function() {
			$(this).siblings(".preview").slideDown();
			$(this).attr("src", "images/collapse.gif");
		},
		function() {
			$(this).siblings(".preview").slideUp();
			$(this).attr("src", "images/expand.gif");
		}
	);


	// Scroll to an inline anchor
	$("a.shortcut").click(function() {
		if (location.pathname.replace(/^\//,"") == this.pathname.replace(/^\//,"")
		&& location.hostname == this.hostname) {
			var $target = $(this.hash);
			$target = $target.length && $target || $("[name=" + this.hash.slice(1) +"]");
			if ($target.length) {
				var targetOffset = $target.offset().top;
				$("html,body").animate({scrollTop: targetOffset}, 1000);
				return false;
			}
		}
	});

	// Toggle Grid
	$('#showgrid').toggle(
		function() {
			$('#organizer').addClass("showgrid");
			$(this).text('Hide Grid');
		},
		function() {
			$('#organizer').removeClass("showgrid");
			$(this).text('Show Grid');
		}
	);

	// Reset Selection
	$('#reset').click(function() {
		$('#labs').nextAll('input:checkbox').attr('checked', false);
	});

});