$(document).ready(function() {
	var minus = "/images/minus.gif";
	var plus = "/images/plus.gif";
	$("#classroom").tablesorter({
		widgets: ['zebra'],
		sortList: [[2,1],[1,0]],
		headers: {
			2: {
				sorter: "text"
			}
		},
		textExtraction: function (node) {
			return $(node).children("a:first, span:first").html();
		}
	});
	
	// Add to the click binding on the tablesorter to remove any detailed views before sorting
	$("#classroom th").click(function() {
		$(".details").remove();
		$(".activeRow").toggleClass("activeRow");
	});
	
	function ToggleDetails(obj) {
		var myclass = $(obj).parent().attr("class");
		if ($(obj).parent().hasClass("activeRow")) {
			$(obj).parent().toggleClass("activeRow").next().remove();
			$(obj).children("img").attr({src: "images/plus.gif"});
		} else {
			$(obj).children("img").attr({src: "images/minus.gif"});
			$(obj).parent().toggleClass("activeRow").after('<tr class="details hidden ' + myclass + '"><td colspan="3"></td></tr>');
			
			$(obj).parent().next().children().load($(obj).children("a").attr("href"), function(){
				$(this).parent().toggleClass("hidden");
			});
		}
	}

	$("#classroom").click(function(e) {
		if ($(e.target).is('.expander')) {
			ToggleDetails(e.target);
			return false;
		} else if ($(e.target).parent().is('.expander')) {
			ToggleDetails($(e.target).parent());
			return false;
		} else if ($(e.target).is('.close')){
			$(e.target).parent().parent().parent().prev().toggleClass("activeRow");
			$(e.target).parent().parent().parent().remove();
		}
	});
});