(function($) {
	var slideshowWidth;
	var totalPages;
	var curPage = 0; // keep track of the current page

	// adjust the height of the wrapper
	function resize() {
		// adjust heights to ensure no vertical scrollbar appears
		var adjust = parseInt($("#wrap").css("paddingTop")) * 2 + parseInt($("#wrap").css("marginTop"));
		$("#wrap").css("height", (window.innerHeight - adjust) + "px");
		var contentHeight = window.innerHeight - adjust - $(".header").outerHeight();
		$(".content").css("height", contentHeight + "px");
		$(".homepage_divisions li").css("height", (contentHeight / 2) + "px");

		// maintain aspect ratio
		if (window.bodyAspectRatio) {
			if ($(".content").width() > contentHeight * window.bodyAspectRatio) {
				// adjust the height for a vertical scroll
				$(".content").css("height", ($(".content").width() / window.bodyAspectRatio) + "px")
				$(".homepage_divisions li").css("height", ($(".homepage_divisions li").width() / window.bodyAspectRatio) + "px");
			}
			else {
				// adjust the width of the body for a horizontal scroll
				$("body").css("width", (contentHeight * window.bodyAspectRatio + (parseInt($("#wrap").css("marginLeft")) * 2)) + "px");
			}
		}
		contentHeight = parseInt($(".content").css("height"));

		// slideshow resizes
		slideshowWidth = $(".slideshow").width(); // width of the viewable slideshow area
		var maxImageWidth = slideshowWidth;
		totalPages = $(".slideshow ul li").size();
		// total scrollable width based on the number of items
		var totalWidth = totalPages * slideshowWidth;
		$(".slideshow li").css("width", slideshowWidth + "px");
		$(".slideshow ul").css("width", totalWidth);
		$(".slideshow").scrollLeft(curPage * slideshowWidth);
		// adjust the controls
		$(".slideshow_control").css("paddingTop", (contentHeight / 2 - 42) + "px");
		// adjust the image sizes
		$(".slideshow li img").bind("load", function() {
			$(this).css("height", (contentHeight - 60) + "px");
			if ($(this).width() > maxImageWidth) {
				$(this).css("width", maxImageWidth + "px").css("height", "auto");
			}
		});
	}

	$(document).ready(function() {
		$(window).bind("resize orientationchange", resize).trigger("resize");
		initSlideshow();
	});

	function initSlideshow() {
		$(".slideshow").animate({scrollLeft: 0}, 1000);

		// generate large image popup
		$('<div style="display:none" id="image_modal"><div style="padding-top:155px;position:absolute;text-align:center;width:500px" class="loading"><img src="/images/loading.gif" /></div><div style="height:375px" class="image"><img src="" /></div><div><a href="#" class="jqmClose">Close Image</a></div></div>')
			.appendTo("body")
			.jqm({overlay: 50, overlayClass: "modal_overlay"});

		$(".slideshow a").click(function() {
			$("#image_modal .loading").show();
			$("#image_modal .image img").attr("src", $(this).attr("href")).load(function() {
				$("#image_modal .loading").hide();
			});
			$("#image_modal").jqmShow();
			return false;
		});

		// slideshow navigation
		$(".slideshow_control.left a").click(function() {
			if (curPage > 0) {
				curPage--;
				$(".pagination span.active").removeClass("active").prev().addClass("active");
				$(".slideshow").animate({scrollLeft: curPage * slideshowWidth}, 750);
			}
			return false;
		});
		$(".slideshow_control.right a").click(function() {
			if (curPage < totalPages - 1) {
				curPage++;
				$(".pagination span.active").removeClass("active").next().addClass("active");
				$(".slideshow").animate({scrollLeft: curPage * slideshowWidth}, 750);
			}
			return false;
		});		
	}

})(jQuery);

