$(function() {
	/* Tabbed navigation */
	$("#main").tabs({
		ajaxOptions: {
			success: tabCallback,
			error: function(xhr, status, index, anchor) {
				$(anchor.hash).html(
					"Sorry, an error has occured. Please try again later."
				);
			}
		}
	});
	
	/* Header and navigation overlap */
	$("#header").css("margin-bottom", -1 * ($("#navigation").height() + 1));
	
	/* Apply jquery-ui styling classes */
	$(".media-item").addClass("ui-corner-all");
	
	/* TODO: implement cookie persistence and remove this hack */
	$("#navigation a").last().click();
});

var tabCallback = function() {
	/* Media item dialogs */
	$(".media-item").each(function() {
		var $dialog = $(this).find(".media-item-dialog");
		$dialog.dialog({
			autoOpen: false,
			modal: true,
			resizable: false,
			draggable: false,
			width: 700,
			title: $dialog.parent(".media-item").find("p").first().text()
		});
		$(this).data("dialog", $dialog);
	});
	$(".media-item").click(function() {
		$(this).data("dialog").dialog("open");
	});
	
	/* Download buttons */
	$(".download").button();
};
