var slideShow  = null;

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

function prepareSlideShow(baseURL, slides)
{
	prepareSlideShowWithFormat(baseURL, slides, 1);
}

var currentSlide;
var currentImage;

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

	slideshowFormatVersion = slideShow.formatVersion;

	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 = "";
	}
	if (slideshowFormatVersion == 1)
		slideHref = currentSlide;
	else
		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.
		currentImage = new Image();
		currentImage.src = slideHref;

		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);
		}
	}

	if (slideshowFormatVersion > 1) {
		updateCaption();
	}

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

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

function updateCaption()
{
	var caption;
	var element;

	caption = currentSlide.caption;
	element = document.getElementById("caption");
	// innerText is IE extension; works in OmniWeb and Safari, but not in FireFox
	// textContent works in FireFox, but doesn't work in OmniWeb
	if (element != null) {
		if ((currentImage != null) && !currentImage.complete) {
			element.innerHTML = "...loading...";
			setTimeout('updateCaption()', 100);

		} else {
			element.innerHTML = caption;
		}
	}
}

function goToNextSlide()
{
	goToSlideAtIndex(slideShow.currentIndex + 1);
}

function goToPreviousSlide()
{
	goToSlideAtIndex(slideShow.currentIndex - 1);
}

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

