var slideShow  = null;
var hasBuiltGalleryIndex = false;

function buildGalleryIndex()
{
	var galleryIndex = document.getElementById("gallery_index");
	var galleryTable = document.createElement('table');
	galleryTable.className = "galleryTable";

	var galleryTableBody = document.createElement('tbody');
	galleryTable.appendChild(galleryTableBody);

	var galleryRow;
	var slideCount = slides.length;
	var slideIndex;
	for (slideIndex = 0; slideIndex < slideCount; slideIndex++) {
		if (slideIndex % 4 == 0) {
			galleryRow = document.createElement('tr');
			galleryRow.className = "galleryRow";
			galleryTableBody.appendChild(galleryRow);
		}

		var galleryCell = document.createElement('td');
		galleryCell.className = "galleryColumn";
		galleryRow.appendChild(galleryCell);

		var slide = slides[slideIndex];

		var galleryThumbnailLink = document.createElement('a');
		galleryThumbnailLink.href = "index.html?slideIndex=" + slideIndex;
		galleryCell.appendChild(galleryThumbnailLink);

		var galleryThumbnail = document.createElement('img');
		var path = slideShow.baseURL + slide.href;
		path = path.replace("images","thumbnails");
		galleryThumbnail.src = path;
		galleryThumbnail.alt = slide.caption;
		galleryThumbnailLink.appendChild(galleryThumbnail);
	}

	galleryIndex.appendChild(galleryTable);

	hasBuiltGalleryIndex = true;
}

function prepareSlideShowWithBaseURL(slides, baseURL)
{
	slideShow = new Object;
	slideShow.baseURL = baseURL;
	slideShow.slides = slides;
	slideShow.currentIndex = -1;
}

function prepareSlideShow(slides)
{
	prepareSlideShowWithBaseURL(slides, "");
}

function addSlide(array, href, caption)
{
	var slide = new Object();
	slide.href = href;
	slide.caption = caption;
	array.push(slide);
}

var currentSlide;
var currentImage;

function goToSlideAtIndex(newIndex)
{
	var baseURL;
	var newImage;
	var slide;
	var slideHref;
	var fullPath;
	var element;

	if (newIndex >= slideShow.slides.length) {
		if (slideShow.currentIndex == -1) {
			newIndex = 0;
		} else {
			return;
		}
	}
	if (newIndex < 0) {
		if (slideShow.currentIndex == -1) {
			newIndex = 0;
		} else {
			return;
		}
	}

	slideShow.currentIndex = newIndex;
	currentSlide = slideShow.slides[slideShow.currentIndex];

	element = document.getElementById("status_text");
	if (element != null) {
		element.innerHTML = (slideShow.currentIndex + 1) + " of " + slideShow.slides.length;
	}


	baseURL = slideShow.baseURL;
	if (baseURL == null) {
		baseURL = "";
	}
	slideHref = currentSlide.href;
	fullPath = baseURL + slideHref;

	element = document.getElementById("screen");
	if (element != null) {
		// I would just change the src of the old img element, but for some reason at least some browsers won't always resize the img element correctly based on the new image src. (The main time I would see this was in OmniWeb and Safari when switching from a square image to a portrait-oriented image. <shrug>) Replacing the img node resolves the issue. I make an attempt to support browsers that don't support the DOM-modification stuff, but I don't know how likely that is or if my code actually works - I don't have anything to test against.
imageIsFinishedLoading = false;
		currentImage = new Image();
		currentImage.onload = finishedLoadingImage;
		currentImage.onerror = finishedLoadingImage;
		currentImage.onabort = finishedLoadingImage;
		currentImage.src = slideHref;

		setTimeout('updateCaption()', 50);

		if (document.createElement == null) {
			element.src = fullPath;
			element.title = slideHref;
		} else {
			var oldElement = element;
			element = document.createElement('img');
			element.id = oldElement.id;
			element.src = fullPath;
			element.title = slideHref;
			var parentNode = oldElement.parentNode;
			parentNode.replaceChild(element, oldElement);
		}
	}

	newIndex = slideShow.currentIndex + 1;
	if (newIndex < slideShow.slides.length) {
		var slide = slideShow.slides[newIndex];
		slideHref = slide.href;
		fullPath = baseURL + slideHref;

		var preloadImage = new Image();
		preloadImage.src = fullPath;
	}
}

function finishedLoadingImage()
{
	imageIsFinishedLoading = true;
	updateCaption();
}

function setCaption(caption)
{
	// innerText is IE extension; works in OmniWeb and Safari, but not in FireFox
	// textContent works in FireFox, but doesn't work in OmniWeb
	var element = document.getElementById("caption");
	if (element != null) {
		element.innerHTML = caption;
	}
}

function updateCaption()
{
	var caption = currentSlide.caption;
	if ((currentImage != null) && !currentImage.complete && !imageIsFinishedLoading) {
		setCaption("...loading...");
		setTimeout('updateCaption()', 100);

	} else {
		imageIsFinishedLoading = true;
		setCaption(caption);
	}
}

function nextSlide()
{
    var currentSlideIndex = parseInt(getURLParameter(window.document.location, "slideIndex", "-1"));
    currentSlideIndex = currentSlideIndex + 1;
    if (currentSlideIndex < slides.length) {
        window.document.location = "index.html?slideIndex=" + currentSlideIndex;
    } else {
        window.document.location = "index.html";
    }
}

function previousSlide()
{
    var currentSlideIndex = parseInt(getURLParameter(window.document.location, "slideIndex", slides.length));
    currentSlideIndex = currentSlideIndex - 1;
    if (currentSlideIndex >= 0) {
        window.document.location = "index.html?slideIndex=" + currentSlideIndex;
    } else {
        window.document.location = "index.html";
    }
}

function showGalleryIndexArea()
{
	var element;
	element = document.getElementById("gallery_index");
	element.style.display = "";
	element = document.getElementById("gallery_photo");
	element.style.display = "none";
}

function showGalleryPhotoArea()
{
	var element;
	element = document.getElementById("gallery_photo");
	element.style.display = "";
	element = document.getElementById("gallery_index");
	element.style.display = "none";
}

function goToRequestedGalleryPage()
{
	var slideIndex = parseInt(getURLParameter(window.document.location, "slideIndex", "-1"));
	if (slideIndex == -1) {
		showGalleryIndexArea();
		element = document.getElementById("status_text");
		if (element != null) {
			element.innerHTML = slides.length + " photos";
		}

		if (!hasBuiltGalleryIndex) {
			buildGalleryIndex();
		}

	} else {
		showGalleryPhotoArea();
		goToSlideAtIndex(slideIndex);
	}
}
