// Global variables
// a variable to hold the timer
var running;
// the array of file paths
var imagePaths = new Array();
// the array of corresponding notes
var notes = new Array();
// the array of authors
var authors = new Array();
// The current index of the arrays
var index = 0;
// The default speed between pictures on the slideshow
var speed = 3000;
// whether the slideshow should loop back at the end
loop = false;
// the default name of the image in the JSP
var imageHtmlID = "pic";
// the default name of the caption in the JSP
var captionHtmlID = "caption";
// the default name of the image counter caption
var imageCounter = "imageCounter";
// the default name of the image credit caption
var creditHtmlID = "credit";
// the image server
var imageUrl = "http://www2.worldpub.net/images";

function setImagePaths(imagePaths) {
  this.imagePaths = imagePaths;
}

function setNotes(notes) {
  this.notes = notes;
}

function setAuthors(authors) {
  this.authors = authors;
}

function setImageUrl(imageUrl) {
  this.imageUrl = imageUrl;
}

function setImageHtmlID(imageHtmlID) {
  this.imageHtmlID = imageHtmlID;
}

function setCaptionHtmlID(captionHtmlID) {
  this.captionHtmlID = captionHtmlID;
}

function setCreditHtmlID(creditHtmlID) {
  this.creditHtmlID = creditHtmlID;
}

function setImageCounter(imageCounter) {
  this.imageCounter = imageCounter;
}

function setSpeed(speed) {
	this.speed = speed;
}

function setLoop(loop) {
	this.loop = loop;
}

function previous() {
	if (index >= 1) {
		index--;
		showImage();
		showText();
		showCredit();
		updateCount();		
	}
}

function next() {
	if (index < (imagePaths.length - 1)) {
		index++;
		showImage();
		showText();
		showCredit();
		updateCount();		
	}
}

function first() {
	index = 0;
	showImage();
	showText();
	showCredit();
	updateCount();	
}

function last() {
	index = imagePaths.length - 1;
	showImage(index);
	showText(index);
	showCredit();
	updateCount();
}

function play() {
	if (index < imagePaths.length) {		
		showImage();
		showText();
		showCredit();
		updateCount();				
	} else {
		if (loop) {
			index = -1;
		} else {
			return;
		}
	}
	index++;
	running = setTimeout("play()",speed);	
}

function pause() {
	clearTimeout(running);
}

function showImage() {
	var imageElement = document.getElementById(imageHtmlID);	
	if (imageElement != null) 
		imageElement.src = imageUrl + "/" + imagePaths[index];
}

function showText() {
	var captionElement = document.getElementById(captionHtmlID);
	if (captionElement != null && notes[index] != null) {
		var newText = notes[index];
		var newTextNode = document.createTextNode(newText);
		if (captionElement.lastChild != null)
			captionElement.removeChild(captionElement.lastChild);
		captionElement.appendChild(newTextNode);
	}
}

function showCredit() {
  var creditElement = document.getElementById(creditHtmlID);
  if (creditElement != null) {
    var newText = (authors[index].length > 0) ? "Photo by: " + authors[index] : "";
	var newTextNode = document.createTextNode(newText);
	if (creditElement.lastChild != null) 
		creditElement.removeChild(creditElement.lastChild);
	creditElement.appendChild(newTextNode);
	if (newText == "")
	  creditElement.style.borderWidth = "0px";
	else 
	  creditElement.style.borderWidth = "1px";	
  }
}

function showAll(index) {
  this.index = index;
  showImage();
  showText();
  showCredit();
  updateCount();
}

function updateCount() {
  var counterElement = document.getElementById(imageCounter);
  if (counterElement != null) {
    var newText = (index + 1) + " of " + imagePaths.length + " images";
	var newTextNode = document.createTextNode(newText);
		if (counterElement.lastChild != null)
			counterElement.removeChild(counterElement.lastChild);
		counterElement.appendChild(newTextNode);
  }
}