var currentIndex = 0;
var autoplayRunning = false;
var animationRunning = false;
var changingPhoto = false;

function loadGal() {
	var paddHeight = ($("#gallery #imgWrapper").height() - $("#gallery #img").height()) / 2;
	var paddWidth = ($("#gallery #imgWrapper").width() - $("#gallery #img").width()) / 2;

	setTimeout(function() {$("#gallery #img").css("padding",  paddHeight+ "px " + paddWidth + "px");}, 0);
	$("#gallery").append("<div id='bar'><a id='autoplay' href='javascript:void(0)'> </a><span id='controls'><a id='prevButton' href='javascript:void(0)'>&laquo Prev</a> <span id='currentPhoto'></span>&nbsp;of&nbsp;<span id='totalPhotos'></span> <a id='nextButton' href='javascript:void(0)'>Next &raquo;</a></span><div style='clear: right;'></div></div><div id='filmstrip'><div id='thumbHandle'>&nbsp;</div><div id='thumbs'></div><a id='scrollPrev'><img height='53' width='44' alt='Previous' src='img/prev.gif'/></a><a id='scrollNext'><img height='53' width='44' alt='Next' src='img/next.gif'/></a></div>");
	$("#gallery #nextButton, #gallery #imgWrapper").click(function() {
		if ((currentIndex + 1) < gallPhotos.length)
			ChangePhoto(currentIndex + 1);
	});
	$("#gallery #prevButton").click(function() {
		var newIndex = currentIndex - 1;
		if ( newIndex >= 0)
			ChangePhoto(newIndex);
	});
	$("#gallery #autoplay").click(function(){
		if (autoplayRunning)
		{
			autoplayRunning = false;
			$(this).css("color", "black").removeClass("selected");
		}
		else
		{
			autoplayRunning = true;
			$(this).css("color", "red").addClass("selected");
			doAutoplay();
		}
	});
	$("#gallery #currentPhoto").html(currentIndex + 1);
	$("#gallery #totalPhotos").html(gallPhotos.length);
	$("#imgWrapper #img").load(function() {
		var paddHeight = ($("#gallery #imgWrapper").height() - $("#imgWrapper #img").height()) / 2;
		var paddWidth = ($("#gallery #imgWrapper").width() - $("#imgWrapper #img").width()) / 2;
		$("#imgWrapper #img").css("padding",  paddHeight+ "px " + paddWidth + "px");
	});
	for (thumb in gallThumbs) {
		if (thumb != "indexOf")
			$("#filmstrip #thumbs").append("<img id='thumb"+thumb+"' class='thumbnail' src='"+gallThumbs[thumb]+"' onclick='ChangePhoto("+thumb+")' onload='resizeFilmstrip($(this));'/>");
	}
	$("#filmstrip").hover(function() {showFilmstrip();}, function() {hideFilmstrip();});
	$("#filmstrip #scrollPrev").click(function() {prevFilmstrip()});
	$("#filmstrip #scrollNext").click(function() {nextFilmstrip()});
	$("#filmstrip").hide();
	hideFilmstrip();
}

function resizeFilmstrip(el) {
	var totalWidth = 0;
	$("#thumbs img").each(function() {
		totalWidth += $(this).width() + 7;
	});
	if (totalWidth < 660) {totalWidth = 660;}
	$("#filmstrip #thumbs").width(totalWidth);
}

function ChangePhoto (index) {
	if (!changingPhoto) {
		changingPhoto = true;
		$("#gallery #imgTemp").remove();
		$("#gallery #img").after("<img id='imgTemp' onload='doImageLoad("+index+");'/>");
		$("#gallery .thumbnail").css("border", "none");
		$("#gallery #thumb"+index).css("border", "1px solid white");
		// Thread race, setTimeout required.
		setTimeout(function() {$("#gallery #imgTemp").attr("src", gallPhotos[index]).attr("title", gallDesc[index]).attr("alt", gallDesc[index]);}, 0);
	}
}
function doImageLoad(index) {
	var paddHeight = ($("#gallery #imgWrapper").height() - $("#gallery #imgTemp").height()) / 2;
	var paddWidth = ($("#gallery #imgWrapper").width() - $("#gallery #imgTemp").width()) / 2;
	if (imageSource == "DOMAIN" && $("#gallery #imgTemp").width() > 660 && $("#gallery #imgTemp").height() > 485) {
		$("#gallery #imgTemp").addClass("domainImage");
	}
	setTimeout(function() {$("#gallery #imgTemp").css("padding",  paddHeight+ "px " + paddWidth + "px");}, 0);
	$("#gallery #img").fadeOut(500, function() {
		$("#gallery #img").remove();
		$("#gallery #imgTemp").attr("id", "img");
		currentIndex = index;
		$("#gallery #currentPhoto").html(index + 1);
		changingPhoto = false;
	});
}

function doAutoplay() {
	if (autoplayRunning) {
		if ((currentIndex + 1) < gallPhotos.length) {
			ChangePhoto((currentIndex + 1));
			setTimeout("doAutoplay();", 3000);
		} else {
			autoplayRunning = false;
			$("#gallery #autoplay").css("color", "black").removeClass("selected");
		}
	}
}

function showFilmstrip() {
	if (!animationRunning) {
		animationRunning = true;
		$("#filmstrip").animate({
			top: "355px",
			height: "130px"
		}, 300, function() {animationRunning = false;});
	}
}

function hideFilmstrip() {
	if (!animationRunning) {
		animationRunning = true;
		$("#filmstrip").animate({
			top: "435px",
			height: "50px"
		}, 300, function() {animationRunning = false;});
	}
}

function nextFilmstrip() {
	var currentPos = $("#filmstrip #thumbs").position().left;
	var amountToMove;
	if (currentPos - 652 < -$("#filmstrip #thumbs").width() + $("#filmstrip").width())
	{
		amountToMove = -$("#filmstrip #thumbs").width() + $("#filmstrip").width();
	}
	else
	{
		amountToMove = currentPos - $("#filmstrip").width();
	}
	$("#filmstrip #thumbs").animate({
		left: (amountToMove) + "px"
	}, 300);
}

function prevFilmstrip() {
	var currentPos = $("#filmstrip #thumbs").position().left;
	var amountToMove;
	if (currentPos + $("#filmstrip").width() > 0)
	{
		amountToMove = 0;
	}
	else
	{
		amountToMove = currentPos + $("#filmstrip").width();
	}
	$("#filmstrip #thumbs").animate({
		left: (amountToMove) + "px"
	}, 300);
}