$(function() {
	fixLastPost();
	owlInsurance();
	pointerBlockers();
});

$(window).resize(function() {
	owlInsurance();
});

//	We don't always have enough vertical space available for the owl to fit,
//	so this function makes adjustments in that case.
//	Thanks to Samuel Cochran of http://sj26.com for some code optimization here.
function owlInsurance() {
	var $nav = $("#nav"), $owl = $("#ahhh-not-another"), $pointer = $("#are-you-freaking-kidding-me");
	//	This one is a little fancy because jQuery will provide a value with "px", which I need to remove.
	var navMarginTop = parseInt($nav.css("top").substring(0, $nav.css("top").indexOf("px")));
	
	//	If the window height is greater than those three, we fix the owl to the bottom of the viewport.
	if ($(window).height() > ($owl.height() + $nav.height() + navMarginTop)) {
		$owl.css({top: "", bottom: -20});
		$pointer.css({top: "", bottom: 220});
	} else {
		//	If there's not enough space, we put it directly below the navigation.
		//	The bottom of the owl is hidden in this case so that the owl doesn't overlap the navigation.
		$owl.css({top: $nav.height() + navMarginTop, bottom: ""});
		$pointer.css({top: $nav.height() + navMarginTop + 280, bottom: ""});
	}
}

//	Until IE supports :last-child I have to do this crap.
function fixLastPost() {
	$("div.post:last").addClass("last-post");
}

//	This adds a "pointer blocker" for each post.
//	These are the images that sit between posts and hide the "pointer" in front of the owl,
//	so that it fades in and out between posts.
function pointerBlockers() {
	$(".post").each(function() {
		//	First, we create a new pointer-blocker div for each post.
		$("<div>")
			.addClass("pointer-blocker")
			//	Then we position it at the top of the current post.
			//	The CSS for the pointer-blocker class will give it a negative margin
			//	so that it slides up between the posts.
			.css("top", $(this).offset().top + "px")
			//	Then this is ready to go, so we append it to the body of the document.
			.appendTo(document.body);
	});
	
	//	Then we also need to handle the last post, so that there is a blocker at the bottom.
	$(".last-post").each(function() {
		$("<div>")
			.addClass("pointer-blocker")
			.css("top", $(this).offset().top + $(this).height() + 28 + "px")
			.appendTo(document.body);
	});
}