var theimages = [];
var nextpic = 0; 
var seconds = 5; 
var isPlaying = true;
var bLoaded = false;
var timer; 
var thumbsdoc;

function loadme()
{
	var iFrame = document.getElementById("BottomFrame");
	var framedoc = (iFrame.contentWindow) ? iFrame.contentWindow : iFrame.contentDocument;
	thumbsdoc = framedoc.document;
	
	theimages = thumbsdoc.getElementsByTagName("IMG");
	timer = window.setTimeout(doNext, seconds * 1000);
	bLoaded = true;
}
function setNextPic(iframe)
{
	if(!bLoaded) return; //top page not yet loaded
	if(!iframe) iframe = document.getElementById("TopFrame");
	var framedoc = (iframe.contentWindow) ? iframe.contentWindow : iframe.contentDocument;
	var loadedimage = framedoc.document.images[0]; //should be only the one image on the display page
	//the way the galleries are built the two have the same src
	var selectedsrc = String(theimages[nextpic].src);
	selectedsrc = selectedsrc.substring(selectedsrc.lastIndexOf("/")+1);//file name
	if(loadedimage.src.indexOf(selectedsrc) == -1) //if not the same filename, user must have selected, set the nextpic var (to the current pic)
	{
		var file;
		for(var i=0;i<theimages.length;i++)
		{
			srce = theimages[i].src;
			srce = srce.substring(srce.lastIndexOf("/")+1);
			if(loadedimage.src.indexOf(srce) != -1)
			{
				nextpic = i; 
				doPausePlay(false);//pause if playing
				return;
			}
		}
	}
}

function clickit(ind)
{
	try
	{
		theimages[ind].click();
	}
	catch(e)
	{
		var src = theimages[ind].parentNode.href;
		document.getElementById("TopFrame").src = src; 
	}
}
function doPrev(bClicked)
{
	nextpic = nextpic == 0 ? theimages.length-1 : nextpic-1; 
	
	clickit(nextpic);

	window.clearTimeout(timer);
	doPausePlay(false);//pause the slideshow
}
function doPausePlay(bPlay)
{
	if(bPlay == undefined)
	{
		bPlay = !isPlaying;
	}
	isPlaying = bPlay;
	if(bPlay)
	{
		doNext();
		document.getElementById("btnPlayPause").src = "common/pause.gif"; 
	}
	else
	{
		//show play button
		window.clearTimeout(timer);
		document.getElementById("btnPlayPause").src = "common/play.gif"; 
	}
}

function doNext(bClicked)
{
	nextpic = nextpic+1 == theimages.length ? 0 : nextpic+1; 
	
	clickit(nextpic);
	
	if((bClicked != true) && isPlaying == true) //bug in firefox where bClicked is showing up as a number, don't know why
	{
		timer = window.setTimeout(doNext, seconds * 1000);//continue the slideshow
	}
	else
	{
		doPausePlay(false);//show the play button
	}
		
}
function playPauseOver(tag)
{
	//common/pause.gif
	tag.src = isPlaying ? "common/pause_over.gif" : "common/play_over.gif";
}
function playPauseOut(tag)
{
	tag.src = isPlaying ? "common/pause.gif" : "common/play.gif";
}
