var midBackgroundImageHeight = 262;
var marginTop = 50;
var marginBottom = 100;

function adjustBackground() {
	var contentArea = document.getElementById("pageArea");
	var topBackgroundCell = document.getElementById("topBackgroundCell");
	var midBackgroundCell = document.getElementById("midBackgroundCell");
	var bottomBackgroundCell = document.getElementById("bottomBackgroundCell");
	var verticalMargins = marginTop + marginBottom;
	var topHeight = parseInt(topBackgroundCell.offsetHeight);
	var bottomHeight = parseInt(bottomBackgroundCell.offsetHeight);
	
	if (contentArea.offsetHeight < topHeight - verticalMargins) {
		midBackgroundCell.style.display = "none";
		bottomBackgroundCell.style.display = "none";
		contentArea.style.height = topHeight - verticalMargins;
	} else if (contentArea.offsetHeight < topHeight + midBackgroundImageHeight) {
		bottomBackgroundCell.style.display = "none";
		midBackgroundCell.style.height = midBackgroundImageHeight
		contentArea.style.height = midBackgroundCell.offsetHeight + topHeight - verticalMargins;
	} else {
		var desiredHeight = contentArea.offsetHeight - (topHeight-marginTop) - (bottomHeight-marginBottom);
		midBackgroundCell.style.height = Math.ceil(desiredHeight/midBackgroundImageHeight) * midBackgroundImageHeight;
		contentArea.style.height = midBackgroundCell.clientHeight + topHeight + bottomHeight - verticalMargins;
	}
	
	document.getElementById('finalPageArea').innerHTML = contentArea.innerHTML;
	contentArea.innerHTML = "";
}

function showImage(img) {
	var galleryContainer = document.getElementById("galleryContainer");
	var galleryTable = document.getElementById("galleryTable");
	var loadingMessage = document.getElementById("loadingMessage");
	loadingMessage.style.left = "350px";
	loadingMessage.style.top =  "450px";
	loadingMessage.style.display = "block";

	galleryContainer.innerHTML = '<img id="galleryImage" onClick="hideImage();">';
	var galleryImage = document.getElementById("galleryImage");
	galleryImage.onload = function() {
		var maxSize = parseInt(galleryContainer.style.width);
		galleryContainer.style.visibility = "hidden";
		galleryContainer.style.display = "";
		if (this.width > this.height) {
			this.height = maxSize * this.height / this.width;
			this.width = maxSize;
			this.style.marginTop = ((maxSize - this.height) / 2 )+ "px";
		} else {
			this.width = maxSize * this.width / this.height;
			this.height = maxSize;
			this.style.marginLeft = ((maxSize - this.width) / 2 )+ "px";
		}
		loadingMessage.style.display = "none";
		document.getElementById("galleryTable").style.display = "none";
		galleryContainer.style.visibility = "visible";
	}
	galleryImage.src = img.src.replace("thumbs", "photos");
	
	return false;
}

function hideImage() {
	document.getElementById("galleryContainer").style.display = "none";
	document.getElementById("galleryTable").style.display = "";
}
