function fadein() {
	($f = $('<div style="position:fixed;left:0;top:0;z-index:999;width:100%;height:' + $(window).height() + 'px;background:#fff;" />')).appendTo("body");
	$(window).load(function() {
		$f.fadeOut(1000, function() {
			$f.remove();
		});
	});
}
function prepare() {
	$("thead").find("th").width(function() {
		return $(this).width();
	});
	$("tr.hidden").children("td").children().wrap("<div></div>").parent().height(function() {
		return $(this).parent().height();
	}).hide();
	$("tbody").find("a").wrapInner('<span class="foreground"></span>').append('<span class="fadeout"></span>').hover(function() {
		$(this).children("span.fadeout").stop(true, true).show();
	}, function () {
		$(this).children("span.fadeout").fadeOut();
	});
}
function size() {
	windowwidth = $(window).width();
	windowheight = $(window).height();
	if (windowwidth / windowheight < 1.5)
		windowwidth = windowheight * 1.5;
	imagewidth = Math.ceil((windowwidth - 900) / 300) * 300 + 900;
	if (imagewidth < 900)
		imagewidth = 900;
	else if (imagewidth > 1800)
		imagewidth = 1800;
	$("body").css("backgroundImage", $("body").css("backgroundImage").replace(/_\d+\./, "_" + imagewidth + "."));
}
$(document).ready(function() {
	fadein();
	size();
	prepare();
	$("a.toggle").live("click", function(event) {
		$row = $(this).closest("tr");
		$targetrow = $row.next();
		$cells = $targetrow.children();
		if (!$targetrow.data("open")) {
			$cells.children().stop(true, true).slideDown($($cells.children()[0]).height() * 2 + 250);
			$row.find("span.pulldown").html("&#9650;");
			$targetrow.data("open", true);
		} else {
			$cells.children().slideUp($($cells.children()[0]).height() * 2 + 250, function() {
				$row.find("span.pulldown").html("&#9660;");
			});
			$targetrow.data("open", false);
		}
		event.preventDefault();
	});
	$("tbody").find("tr").live("hover", function(event) {
		$pairedrow = ($(this).hasClass("hidden")) ? $(this).prev() : $(this).next();
		if (event.type == "mouseover") {
			$(this).addClass("hover");
			$pairedrow.addClass("hover");
		} else {
			$(this).removeClass("hover");
			$pairedrow.removeClass("hover");
		}
	});
	$("input[type=checkbox]").live("click", function() {
		request = $.post($(this).closest("form").attr("action"), {action: "modify", checkbox: this.name, checked: this.checked, person: document.forms[0].elements["currentperson"].value, canadian: document.forms[0].elements["canadian"].checked}, function(data) {
			$("table").html(data);
			prepare();
		});
	});
	$("form").live("submit", function(event) {
		$.post($(this).attr("action"), {action: "choose", person: this.elements["person"].value}, function(data) {
			$("#everything").html(data);
			prepare();
		});
		event.preventDefault();
	});
	$("a.change").live("click", function(event) {
		$clicked = $(this);
		paramstring = this.href.split("?")[1].split("&");
		params = [];
		for (i in paramstring) {
			param = paramstring[i].split("=");
			params[param[0]] = param[1];
		}
		if ($clicked.hasClass("up"))
			action = "-";
		else
			action = "+";
		$cells = $clicked.closest("tr").children("td");
		$.post("changeroom2.php", {id: params["person"], conid: params["conid"], day: params["day"], action: action}, function(data) {
			$.each(data.split("|"), function (key, list) {
				$($cells[key + 1]).html(list);
			});
		});
		event.preventDefault();
	});
});
$(window).resize(function() {
	size();
	$divs = $("tr.hidden").find("div");
	$divs.show();
	$("thead").find("th").removeAttr("style").width(function() {
		return $(this).width();
	});
	$divs.removeAttr("style").height(function() {
		return $(this).parent().height();
	}).each(function() {
		if (!$(this).closest("tr").data("open"))
			$(this).hide();
	});
});