
function initializePage () {
	setResizeDimensions ();
	setInitialFocus ();
	setAnimationTimer();
}

var pageWidth = 0;
var horizontalOverhang = 0;
function setResizeDimensions () {

	// left exetensions
	var contentWidth = 1024;
	pageWidth = document.documentElement.clientWidth;
	horizontalOverhang = (pageWidth - contentWidth) / 2;
	document.getElementById("content_left_extension").style.left = -horizontalOverhang + "px";
	document.getElementById("content_left_extension").style.width = horizontalOverhang + "px";

	// overlay bars
	var barsLeft = 368;
	var barsWidth = contentWidth - barsLeft + horizontalOverhang;
	document.getElementById("overlay_bars").style.width = barsWidth + "px";

	// gallery pages
	if (document.getElementById("gallery_thumbnails_area")) {
		document.getElementById("gallery_thumbnails_area").style.left = -horizontalOverhang + "px";
		document.getElementById("gallery_thumbnails_area").style.width = pageWidth + "px";
		buildGalleryThumbnails();
		preloadGalleryImages();
	}

}

function setInitialFocus () {
	if (document.getElementById("contact_email_address")) {
		document.getElementById("contact_email_address").focus();
	}
}

var animationTimer;
function setAnimationTimer () {
	if (document.getElementById("gallery_thumbnails")) {
		anamationTimer = window.setInterval("scrollThumbnailImages()", 10);
	}
}

function submitContact () {
	if ( ! document.getElementById("contact_email_address").value ) {
		document.getElementById("contact_email_address").focus();
		alert ( "Please enter your email address." );
	}
	else if ( ! document.getElementById("contact_message").value ) {
		document.getElementById("contact_message").focus();
		alert ( "Please enter a message." );
	}
	else {
		document.getElementById("contact_input_form").submit();
	}

}

function submitManage () {
	if ( document.getElementById("manage_input_form") ) {
		document.getElementById("manage_input_form").submit();
	}
	else if ( document.getElementById("manage_gallery_input_form") ) {
		document.getElementById("manage_gallery_input_form").submit();
	}
}

function manageChangeStatus (selectedField) {
	// alert(selectedField.name);
	if (selectedField.value == "featured") {
		var index = 0;
		while (document.getElementById("gallery_" + index + "_status")) {
			if (document.getElementById("gallery_" + index + "_status").name != selectedField.name
				&& document.getElementById("gallery_" + index + "_status").value == "featured") {
				document.getElementById("gallery_" + index + "_status").value = "active";
			}
			index++;
		}
	}
}

var galleryImages = Array (); // description, thumbfile, thumbwidth, thumbheight
var galleryImageAreaWidth = 533;
var galleryImageAreaHeight = 400;
var galleryThumbnailAreaWidth = 0;
function buildGalleryThumbnails () {

	var totalWidth = 2;
	var left;

	for (var i = 0; i < galleryImages.length; i++) {
		left = totalWidth;
		document.getElementById("gallery_thumbnails").innerHTML += "<a href=\"JavaScript: setGalleryImage (" + i + ");\" style=\"left: " + left + "px\"><img src=\"" + galleryImages[i].smallfile + "\"\></a>";
		totalWidth += galleryImages[i].smallwidth + 2;
	}

	galleryThumbnailAreaWidth = totalWidth;
	setGalleryImage (galleryCurrentIndex);
}

function preloadGalleryImages () {

	for (var i = 0; i < galleryImages.length; i++) {
		galleryImages[i].mediumfileimage = new Image ();
		galleryImages[i].mediumfileimage.src = galleryImages[i].mediumfile;
	}

}


var galleryLastImageId = 1;
var galleryCurrentImageId = 1;
var galleryLastLayer = 9998;
var galleryCurrentIndex = 0;
function setGalleryImage (index) {
	galleryCurrentIndex = index;

	if (galleryImages[index].mediumfileimage) {
		//if (galleryImages[index].mediumfileimage.complete) alert("loaded");
		//else alert("not loaded");
	}

	var left = 0;
	var width = galleryImages[index].mediumwidth;
	if (width < galleryImageAreaWidth) left = (galleryImageAreaWidth - width) / 2;

	var top = 0;
	var height = galleryImages[index].mediumheight;
	if (height < galleryImageAreaHeight) top = 0 + (galleryImageAreaHeight - height) / 2;

	galleryLastImageId = galleryCurrentImageId;
	galleryCurrentImageId = (galleryLastImageId == 2) ? 1 : 2;

	document.getElementById("gallery_image_viewer_image_" + galleryLastImageId).style.opacity = 1;
	document.getElementById("gallery_image_viewer_image_" + galleryLastImageId).style.filter = "alpha(opacity=" + 100 + ")";

	document.getElementById("gallery_image_viewer_image_" + galleryCurrentImageId).style.opacity = 0;
	document.getElementById("gallery_image_viewer_image_" + galleryCurrentImageId).style.filter = "alpha(opacity=" + 0 + ")";

	document.getElementById("gallery_image_viewer_image_" + galleryCurrentImageId).style.left = left + "px";
	document.getElementById("gallery_image_viewer_image_" + galleryCurrentImageId).style.top = top + "px";
	document.getElementById("gallery_image_viewer_image_" + galleryCurrentImageId).style.zIndex = galleryLastLayer--;
	document.getElementById("gallery_image_viewer_image_" + galleryCurrentImageId).onclick = displayFullSizeImage;

	var fadeInId = 0;
	if (parseInt(document.getElementById("gallery_image_viewer_image_" + galleryLastImageId).style.left.replace("px", "")) > 0) fadeInId = galleryCurrentImageId;
	else {
		document.getElementById("gallery_image_viewer_image_" + galleryCurrentImageId).style.display = "block";
		document.getElementById("gallery_image_viewer_image_" + galleryCurrentImageId).style.opacity = 1;
		document.getElementById("gallery_image_viewer_image_" + galleryCurrentImageId).style.filter = "alpha(opacity=" + 100 + ")";
	}

	document.getElementById("gallery_image_viewer_image_" + galleryCurrentImageId).onLoad = fadeGalleryImages (galleryLastImageId, fadeInId);
	document.getElementById("gallery_image_viewer_image_" + galleryCurrentImageId).src = galleryImages[index].mediumfile;
	document.getElementById("gallery_info_image_description").innerHTML = galleryImages[index].description;

	document.getElementById("gallery_next_link").style.display = (galleryCurrentIndex == galleryImages.length - 1) ? "none" : "block";
}

function displayFullSizeImage () {
	var newWindow = window.open(galleryImages[galleryCurrentIndex].largefile, '_blank');
}

var fadePosition = 100;
function fadeGalleryImages (outImageId, inImageId) {

	if (outImageId) {
		document.getElementById("gallery_image_viewer_image_" + outImageId).style.opacity = fadePosition / 100;
		document.getElementById("gallery_image_viewer_image_" + outImageId).style.filter = "alpha(opacity=" + fadePosition + ")";
	}

	if (inImageId) {
		document.getElementById("gallery_image_viewer_image_" + inImageId).style.opacity = 1 - fadePosition / 100;
		document.getElementById("gallery_image_viewer_image_" + inImageId).style.filter = "alpha(opacity=" + (100 - fadePosition) + ")";
	}

	fadePosition -= 5;
	if (fadePosition >= 0) var timeOut = window.setTimeout("fadeGalleryImages(" + outImageId + ", " + inImageId + ")", 10);
	else {
		//document.getElementById("gallery_image_viewer_image_" + outImageId).style.display = "none";
		fadePosition = 100;
	}
}

var refresh = 0;
var targetPosition = 0;
function scrollGalleryImages (event) {
	var ev = event || window.event;
	var cursorPosition = ev.clientX;

	var cursorRatio = cursorPosition / pageWidth;
	var imageOffset = Math.ceil((galleryThumbnailAreaWidth - pageWidth) * cursorRatio) - (-2);
	targetPosition = -imageOffset;
}


function scrollThumbnailImages () {
	var currentPosition = parseInt(document.getElementById("gallery_thumbnails").style.left.replace("px",""));
	if (!currentPosition) currentPosition = 0;

	if (currentPosition != targetPosition) {
		document.getElementById("gallery_thumbnails").style.left = Math.floor(targetPosition - (targetPosition - currentPosition ) / 1.1) + "px";
	}
}

function scrollNextImage () {
	setGalleryImage (galleryCurrentIndex + 1);

	var offset = 0;
	for (var i = 0; i < galleryCurrentIndex; i++) {
		offset += galleryImages[i].smallwidth + 2
	}
	var percent = offset / galleryThumbnailAreaWidth;
	targetPosition = -(galleryThumbnailAreaWidth - (pageWidth)) * percent;
}