// Global constants
var replacementChar = /%s/
var timeoutID
var playing

// Global variables
var themeURL = "<A HREF=\"javascript:self.close()\" ><IMG src=\"home-button.gif\" width=\"33\" height=\"31\" NAME=\"IGNORE\" alt=\"%s\"></A>"
var themeImageLinkPrevious = "<A HREF=\"page.htm?%s,%s\" ><IMG src=\"previous-button.gif\" width=\"33\" height=\"31\" NAME=\"IGNORE\" ></A>"
var themeImageLinkNext = "<A HREF=\"page.htm?%s,%s\" ><IMG src=\"next-button.gif\" width=\"33\" height=\"31\" NAME=\"IGNORE\" ></A>"
var themeImageLinkPlay = "<A HREF=\"javascript:void(0)\" name=slideShowControl onclick=\"stopStartSlideShow()\"><IMG src=\"play-button.gif\" width=\"33\" height=\"31\" NAME=\"IGNORE\" ></a>"
var themeImageLinkStop = "<A HREF=\"javascript:void(0)\" name=slideShowControl onclick=\"stopStartSlideShow()\"><IMG src=\"stop-button.gif\" width=\"33\" height=\"31\" NAME=\"IGNORE\" ></a>"
var themeImageLink = "<IMG>"
var outputURL
var index

function parseCommandLine() {   //xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
	if (location.href.indexOf("?") == -1) {
		return 1
	}
	// Parse any command line arguments
	urlQuery = location.href.split("?")
	urlTerms = urlQuery[1].split(",")
	if (urlTerms[0] != null) {
		index = parseInt(urlTerms[0])
	}
	if (urlTerms[1] != null) {
		playing = parseInt(urlTerms[1])
	}
}

function stopStartSlideShow(firstTime) {  //xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
	if (playing == 1) {
		playing = 0
		clearTimeout(timeoutID)		
		if (firstTime != 1)
		{
			slideShow()
		}
	}
	else {
		playing = 1
		timeoutID = setInterval("slideShow()", 3500)
	}
}

function slideShow() {  //xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
	index = index + 1
	if (index >= imageDB.length) {
		index = 0
               stopStartSlideShow(1)
	}

	// Change the image
	window.location.href = "page.htm?" + index + "," + playing
	
	// Change the play text to stop
	slideShowControl.innerText="Stop";
}

function init() {  //xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

	// Make output strings from theme.js and data.js
 	outputURL = themeURL.replace(replacementChar, URL);
    outputURL = outputURL.replace(replacementChar, URL);

	// Perform some initial calculations
	playing = 0
	index = 0
	parseCommandLine()

	// Start or stop the slideshow
	if (playing == 0) {
		playing = 1
	}
	else {
		playing = 0
	}
	stopStartSlideShow(1)
}

function printImage() {    //xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

	outputImageLink = themeImageLink.replace(replacementChar, index);
	outputImageLink = outputImageLink.replace(replacementChar, playing);
	
	document.write("<table align='center' border='1'>");
	document.write("<TD>")
	document.write(outputImageLink)
	document.write("</TD>")
	document.write("</table>");
	
}

function writeLinks()            {   //xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
  	var themePreviousHolder = themeImageLinkPrevious
	var themeNextHolder = themeImageLinkNext
	var themePlayHolder = themeImageLinkPlay
	var themeStopHolder = themeImageLinkStop

	// Write previous link ***************

		previousIndex = index-1;
		if (previousIndex < 0) {
			previousIndex = 0	  		
		}
		outputPreviousLink = themePreviousHolder.replace(replacementChar, previousIndex);
		outputPreviousLink = outputPreviousLink.replace(replacementChar, playing);
	document.write("<table align='center' border='1'>");
	document.write("<tr><TD>")
	document.write(outputPreviousLink)
	document.write("</TD>")
		
 	 // Write URL***********************
	 
	document.write("<TD>")
	document.write(outputURL)
	document.write("</TD>")

	// Write next link********************

	var nextIndex
			nextIndex = index + 1;
		if (nextIndex < imageDB.length) {
			outputNextLink = themeNextHolder.replace(replacementChar, nextIndex);
			outputNextLink = outputNextLink.replace(replacementChar, playing);
			
	document.write("<TD>")
	document.write(outputNextLink)
	document.write("</TD>")
		}
	

	// Write play/stop******************
	document.write("<TD>")
	if (playing == 1) {
	document.write(themeStopHolder)
	}
	else {
	document.write(themePlayHolder)
	}
	document.write("</TD></tr>")
	document.write("</table>");


}

function loadImage() {   //xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
   var i = 0
   while (document.images[i].name == "IGNORE") {   i++     }
	document.images[i].src = imageDB[index]
}
