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

// Global variables
var outputTitle
var outputSubTitle
var outputURL
var imagesOnAPage
var index


function parseCommandLine() {
	if (location.href.indexOf("?") == -1) {
		return 0
	}

	// 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) {
	if (playing == 1) {
		playing = 0
		clearTimeout(timeoutID)

		if (firstTime != 1)
			{
			index = index - 1
			slideShow()
			}
	}
	else {
	playing = 1
	clearTimeout(timeoutID)
	timeoutID = setInterval("slideShow()", 7500)
			
	if (firstTime != 1)
		{
		timeoutID = setInterval("slideShow()", 500)
		}
	}
}

function init() {
	// Make output strings from buttons.js and data.js
	outputTitle = themeTitle.replace(replacementChar, title);
	outputSubTitle = themeSubTitle.replace(replacementChar, subTitle);
	outputURL = themeURL.replace(replacementChar, URL);
	outputURL = outputURL.replace(replacementChar, URL);

	// Perform some initial calculations
	imagesOnAPage = rows*columns
	
	playing = 0
	index = 0
	parseCommandLine()

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

function printTitle(writePageTitle) {
   if ((rows == 1 || columns == 1) && writePageTitle == 0) {
      return
   }
	document.write(outputTitle)
}

function printSubTitle(writePageSubTitle) {
   if ((rows == 1 || columns == 1) && writePageSubTitle == 0) {
      return
   }
	document.write(outputSubTitle)
}

function printImage() {
	outputImageLink = themeImageLink.replace(replacementChar, index);
	outputImageLink = outputImageLink.replace(replacementChar, playing);
	document.write("<div id=img_frame>  " + outputImageLink + "  </div>")

   for (var i = index*1; i < index*1+1; i++) {
	document.write("<div id=img_info>" + imageMetadataDB[i] + "</div>")
   }
}

function writeLinks(writePageLinks) {
   if ((rows == 1 || columns == 1) && writeLinks == 1) {
      return
   	}
   
	var themePreviousHolder = themeImageLinkPrevious
	var themeNextHolder = themeImageLinkNext
	var themePlayHolder = themeImageLinkPlay
	var themeStopHolder = themeImageLinkStop
	if (writePageLinks == 1)
		{
		themePreviousHolder = themeThumbLinkPrevious
		themeNextHolder = themeThumbLinkNext
		themePlayHolder = themeThumbLinkPlay
		themeStopHolder = themeThumbLinkStop
		}



	document.write('<table id=buttons> <tr> <td colspan=2 align=center>')
	// Write URL
		document.write(outputURL)
	document.write('</td> </tr>')
	document.write('<tr> <td class=arrows align=left>')
	// Write previous link
		if (index != 0) {
			var previousIndex
			if (writePageLinks == 0) {
				previousIndex = index - 1;
				outputPreviousLink = themePreviousHolder.replace(replacementChar, previousIndex);
				outputPreviousLink = outputPreviousLink.replace(replacementChar, playing);
				document.write(outputPreviousLink)
				}
			else {
				if (index != 0) {
					previousIndex = index-imagesOnAPage;
					if (previousIndex < 0) {
						previousIndex = 0	  		
						}
					outputPreviousLink = themePreviousHolder.replace(replacementChar, previousIndex);
					outputPreviousLink = outputPreviousLink.replace(replacementChar, playing);
					document.write(outputPreviousLink)
					}
				}
			}
	document.write('</td>')
	document.write('<td class=arrows align=right>')
	// Write next link
		var nextIndex
		if (writePageLinks == 0)
			{
			nextIndex = index + 1;
			if (nextIndex < imageDB.length) {
				outputNextLink = themeNextHolder.replace(replacementChar, nextIndex);
				outputNextLink = outputNextLink.replace(replacementChar, playing);
				document.write(outputNextLink)
				}
			}
		else {
			nextIndex = index+imagesOnAPage;
			if (nextIndex < thumbDB.length) {
				outputNextLink = themeNextHolder.replace(replacementChar, nextIndex);
				outputNextLink = outputNextLink.replace(replacementChar, playing);
				document.write(outputNextLink)
				}
			}
	document.write('</td> </tr>')
	document.write('<tr> <td colspan=2 align=center>')
	// Write play/stop
		if (playing == 1) {
			document.write(themeStopHolder)
			}
		else {
			document.write(themePlayHolder)
			}
	document.write('</td> </tr>	</table>')
}

// thumbnail tables
function printTable() {
	document.write("<TABLE align=center border=0 width=80%>")
	counter = index
	for (var i = 0; i < rows; i++) {
		document.write("<TR>")
		for (var j = 0; j < columns; j++) {
			if (counter < thumbDB.length) {
				document.write("<TD valign=bottom align=center>")
					document.write("<TABLE><TR>")			
							// Write out the metadata
							for (var k = counter*1; k < counter*1+1; k++) {
					   			document.write("<TD align=center valign=top style=padding-bottom:2px;>"
									+"<span style=font-size:65%;font-weight:200;color:#666;vertical-align:top;>"
									+ thumbMetadataDB[k] + "</span></TD>")
								}
							outputThumbLink = themeThumbLink.replace(replacementChar, counter)
							outputThumbLink = outputThumbLink.replace(replacementChar, playing)
							document.write(outputThumbLink)
					document.write("</TR></TABLE>");
				document.write("</TD>")
			}
			counter++
		}
		document.write("</TR>")
	}
	document.write("</TABLE>")
}

function loadImages() {
	for (var i = 0; i < imagesOnAPage; i++) {
		if ((i+index) < thumbDB.length) {
			var j = 0
			while (document.images[j] != null && document.images[j].name == "IGNORE") {
				j++
			}
			if (document.images[i+j] != null) {
				document.images[i+j].src = thumbDB[i+index]
			}
		}
	}
}

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




// Popup window

function popup_1(url,name){
	popupWin = window.open(url,name, 'titlebar=1,scrollbars=1,location=1,status=1,toolbar=1,menubar=1,resizable=1, width=840,height=400,left=100,top=125');
	popupWin.focus()
	}