function adjustHeight() {
	var adjust = 60;

	var pageHeight = $(window).height() - $('div.header').height() - $('div.footer').height() - adjust;
	$('div.page').height(pageHeight);
	
	if ($('#gallery')) {
		var galleryadjust = 40;
		var minGalleryHeight = 200;
		var galleryHeight = pageHeight - $('#thumbs').height() - $('#caption').height() - galleryadjust;
		if (galleryHeight < minGalleryHeight) {
			galleryHeight = minGalleryHeight;
		}

		var image = new Image();
		image.src = $('span.image-wrapper a.advance-link img').attr('src');

		if (galleryHeight < image.height) {
			var galleryWidth = (galleryHeight / image.height) * image.width;
			$('span.image-wrapper a.advance-link img').height(galleryHeight + 'px');
			$('span.image-wrapper a.advance-link img').width(galleryWidth + 'px');
		}

		var imageheightadjust = 0;
		var top = (galleryHeight - ($('span.image-wrapper a.advance-link img').height() + imageheightadjust)) / 2;
		if (top < 0) {
			top = 0;
		}
		$('span.image-wrapper a.advance-link img').css('top', top + 'px');
	}
}

$(document).ready(function() {
	adjustHeight();
	$(window).resize(function() {
		adjustHeight();
	});
});

