$(document).ready(function() {

	cleanupsidebar();
	$("#files").hide();
	$("#links").hide();

	$("#resourceForm").submit(function() {

		q = $("#resourceForm input[type=text]").val();
		if (q) {

			// Hide both sets
			$("#files").fadeOut(300);
			$("#links").fadeOut(300);

			// Fetch our query
			$.getJSON("resources.php", {"q": q}, function(resources) {

				// Loop through the results
				for (set in resources) {

					// Nuke whatever's in there
					$("#" + set + " ul").html("");

					// Loop through results
					for (resource in resources[set]) {

						// Populate the list, revealing the list if there are any results
						r = resources[set][resource];
						r["description"] = (r["description"]) ? "<div class=\"description\">" + r["description"] + "</div>" : "";
						r["author"]      = (r["author"])      ? "<div class=\"author\">"      + r["author"]      + "</div>" : "";
						if (set == "files") {
							$("#files").fadeIn(300);
							$("#" + set + " ul").append("<li class=\"file" + r["type"] + "\"><a href=\"resources/" + r["path"] + "\">" + r["name"] + "</a>" + r["description"] + r["author"] + "</li>");
						} else {
							$("#links").fadeIn(300);
							$("#" + set + " ul").append("<li><a href=\"" + r["url"] + "\">" + r["name"] + "</a>" + r["description"] + r["author"] + "</li>");
						}

					}

				}

				cleanupsidebar();

			});

		}

		return false;

	});

});


function cleanupsidebar() {
	cw = $(".content").width();
	ch = $(".content .sectionOne:not(.footer)").height();
	ch = (ch >= 400) ? ch : 400;
	$(".content .sectionOne:not(.footer)").width(cw - 420);
	$(".content .sectionTwo:not(.footer)").height(ch);
}
