var home_height = 0;
$(window).load(function (e) {
	
	
	// Collapse the Dean's message
	$("div.home").each(function () {
		home_height = $(this).height();
		$(this).children("h1").next("p").after('<div class="readon"><a href="#">Read More</a></div>');
		var readon = $("div.readon", this);
		var readon_height = readon.height();
		$(this).animate({height: readon.offset().top + readon.height() - $(this).offset().top - 21}, 1000, "swing", function () {
			// Add the aerial image and make the height correct
			var primarybottom = $("div.inner").offset().top + $("div.inner").height() + 12;
			var sidebottom = $("div.sidebar").offset().top + $("div.sidebar").height();
			
			if (primarybottom - sidebottom > 80) {
				$("div.sidebar").append('<div class="aerial"></div>');
				$("div.aerial").height(primarybottom - sidebottom - 23).hide().fadeIn();
				if ($("div.aerial").height() > 200) {
					$("div.aerial").css({backgroundPosition: "center -20px"});
				}
			} else if (primarybottom - sidebottom < -100) {
				$("div.inner").append('<div class="aerial"></div>');
				$("div.aerial").height(Math.min((sidebottom - primarybottom - 12), 520)).css("top", primarybottom-10 ).hide().fadeIn();
				if ($("div.aerial").height() > 200) {
					$("div.aerial").css({backgroundPosition: "center 50%"});
				}
			}
		});
		
	});
	
	$("div.home div.readon a").click(function () {
		$(this).fadeOut(800, function () {
			var home_collapsed = $("div.home").height();
			$(this).parents("div.home").animate({ height: home_height }, 2000, "swing").find("div.readon").hide();
			$("div.aerial").animate({ height: $("div.aerial").height() + home_height - home_collapsed }, 2000, "swing");
		});
		return false;
	});
	
	
	// Set the heights of the adjacent boxes correctly
	$("div.first").each(function () {
		if ($(this).height() > $(this).next("div.second").height()) {
			$(this).next("div.second").height($(this).height());
		} else {
			$(this).height($(this).next("div.second").height())
		}
	});
	
	// Get rid of JS style changer as it causes havoc on the home page
	$("a.smalltext, a.mediumtext, a.largetext").unbind("click");
	
	
});