$(document).ready(function() {

	// improvements for IE CSS support
	$(".post-body > p").eq(0).addClass("first-of-type");
	
	$("#other-posts ol#post-skip li:first-child").addClass("first-child");
	
	$("#post .post-images ul.thumbs li span").click(
		function() {
			var clicked = $("#post .post-images ul.thumbs li").index($(this).parent());
			$("#post .post-images ul li").removeClass("here");
			$("#post .post-images ul.images li").eq(clicked).hide().addClass("here").fadeIn(1000);
			$(this).parent().addClass("here");
		}
	);

	$("a.terms").click(
		function() {
			window.open($(this).attr("href"), "terms_popup", "width=631, scrollbars=yes, resizable=yes");
			return false;
		}
	);
	
	if(navigator.userAgent.indexOf("iPad") == -1) {
		$("#posts img, #more-like img").lazyload({
			effect : "fadeIn",
			effectspeed : 1000,
			placeholder : themeFolderBaseURL + "/images/trans.png"
		});
	}
	
	// apply text-overflow replacement for Firefox
	$("#other-posts #more-like h4 a span, #other-posts #post-skip h4 a span").ellipsis();

	$("#share a.share").hover(
		function() {
			$("#share").addClass("share-hover");
		}
	);

	$("#share").mouseleave(
		function() {
			$("#share").removeClass("share-hover");
		}
	);
	
	$("#post ul.post-links li:first-child").addClass("first-child");
	
	
	
	// replace "newer/older" post links with AJAX-driven button
	if ($("#post-nav a").length && ieJSVersionNumber !== 6 && window.location.href.search(/page/) == -1) {

		var offset = 10;

		function placeLoadedContent() {
			$("#loading-bay > li").addClass("loading");
			$("#posts").append($("#loading-bay").html());
			$("#posts li.loading").animate({opacity : 1.0}, 2500, function() { $(this).removeClass("loading"); });
			$("#post-nav li.loading").hide();
			$("#post-nav li.more").show();

			offset += 6;
			
			// if 40 or more posts are displayed, switch back to regular post nav
			if (offset >= 40) {
				$("#post-nav li").hide();
				$("#post-nav li.older").html('<a href="' + WPSiteBaseURL + '/page/5/">&lsaquo; Older Posts</a>').show();
			};
			
			// if there's no more posts to come, hide "more" button
			if ($("#no-more-posts").length) { $("li.more").hide(); };

			postListings();
		}

		$("#post-nav li").hide();
		$("#post-nav").append('<li class="more"><a href="#" title="Load older posts.">More Posts</a></li><li class="loading">loading posts&hellip;</li>');
		$("body").append('<div id="loading-bay"></div>');
				
		$("#post-nav .more a").click(
			function() {
				
				$("#post-nav li.more").hide();
				$("#post-nav li.loading").show();
				
				// load content and append onto posts
				var images, imagesLoaded = 0;
				// TODO: change below URL to be dynamic when site goes live
				$("#loading-bay").load("/index.php/more-posts/?" + WPQueryString + "&offset=" + offset, function() { 
					if($("#loading-bay img").length) {
						$("#loading-bay img").eq(0).load(
							function() { 
								images = $("#loading-bay img").length;
								imagesLoaded++;
								if (imagesLoaded == 1) {
									placeLoadedContent();
								}
							
							}
						);						
					} else {
						placeLoadedContent();
					};
				});				
				
				return false;
			}
		);
	};

});


