$(function(){$("a.sortlink").click(function(){
	// here we store what the sort is by, stored in the clicked link
	var sortBy = $(this).attr("sortBy");

	// the link also tells which div gets sorted. figure out if its ASC or DESC
	var sortDiv = $("#"+$(this).attr("sortDiv"));
	var isDESC = sortDiv.attr("LastSort") == sortBy && sortDiv.attr("LastSortOrder") == "ASC";
	sortDiv.attr("LastSortOrder", isDESC ? "DESC" : "ASC");
	sortDiv.attr("LastSort", sortBy);
	
	// This does the sorting of the list based on what is being sorted by
	var els = $($(sortDiv).find("div.sortable").get().sort(function(a, b){
		if (isDESC) { var c = a; a = b; b = c; };
		var x = $(a).attr("sort-" + sortBy), y = $(b).attr("sort-" + sortBy);
		if (!isNaN(x) && !isNaN(y)) return x - y;
		if (x == y) return 0;
		return x < y ? -1 : 1;
	}))

	$(sortDiv).fadeOut("fast", function() {
		// now wrap each existing sortable div in a placeholder, remove them, then add the sorted ones
		// back in and then remove the placeholders
		$($(sortDiv).find("div.sortable")).wrap("<div class='sortplaceholder'></div>"); // insert the placeholders
		$(sortDiv).find(".sortplaceholder").html(""); // remove the unsorted elements but keep the placeholder
		$.each(els, function(i, el) {$($(sortDiv).find("div.sortplaceholder")[i]).after(el);}); // insert the sorted elements
		$(sortDiv).find(".sortplaceholder").remove(); // remove the placeholders
		$(sortDiv).fadeIn("fast");
	});
	return false;
})});