var slideshowPages = new Array();
var slideshowCurrent = 0;
var slideshowTimer;

function SlideshowPrev()
{
	if(slideshowCurrent == 0) SlideshowGoto(slideshowPages.length - 1);
	else SlideshowGoto(slideshowCurrent - 1);
}

function SlideshowNext()
{
	if(slideshowCurrent == slideshowPages.length - 1) SlideshowGoto(0);
	else SlideshowGoto(slideshowCurrent + 1);
}

function SlideshowGoto(page)
{
	slideshowPages[slideshowCurrent].style.display = "none";
	document.getElementById("__slideshow__"+slideshowCurrent).className = "";
	slideshowPages[page].style.display = "";
	document.getElementById("__slideshow__"+page).className = "SlideshowSelected";
	slideshowCurrent = page;
}

function SlideshowPause()
{
	document.getElementById("SlideshowPauseBtn").style.display="none";
	document.getElementById("SlideshowPlayBtn").style.display="block";
	clearTimeout(sildeshowTimer);
}

function SlideshowPlay()
{
	document.getElementById("SlideshowPauseBtn").style.display="block";
	document.getElementById("SlideshowPlayBtn").style.display="none";
	SlideshowContinue();
}

function SlideshowContinue()
{
	sildeshowTimer = setTimeout(function(){
		SlideshowNext(); SlideshowContinue();
	},6000);
}

function initSlideshow()
{
	
	if (!document.getElementsByTagName){ return; }
	var anchors = document.getElementsByTagName("tr");

    var found = false;
    var pageIndex = 0;

	// loop through all anchor tags
	for (var i=0; i<anchors.length; i++){
		var anchor = anchors[i];
		if(anchor.parentNode.parentNode.parentNode.className == "ScreenShots")
		{
			slideshowPages[pageIndex] = anchor;
			if(found)
			{
				anchor.style.display = "none";
			}
			else	
			{
				found=true;
			}
			pageIndex++;
		}
	}
	
	var buttonHost = document.getElementById("SlideshowButtons");
	for(var j=0;j<slideshowPages.length;j++)
	{
		var button = document.createElement("a");
		button.setAttribute("id", "__slideshow__"+j);
		button.setAttribute("__slideshow_id", j);
		button.setAttribute("href", "#");
		button.innerHTML = j + 1;
		button.onclick = function(){
			var p = this.getAttribute("__slideshow_id");
			SlideshowGoto(p);
			return false;
		};
		if(j==0) button.className = "SlideshowSelected";
		buttonHost.insertBefore(button,null);	
	}
	document.getElementById("SlideshowControl").style.display = "block";
	SlideshowPlay();
}

initSlideshow();
