
// Array starts at 0.  maxPhoto is number of photos - 1, i.e. last array index
var photo = 0;
var maxPhoto = vImage.length - 1;
var interval;
var state = "Run";
var period = 2000;
var ThisImage = new Image();
var NextImage = new Image();
var PrevImage = new Image();
var LoadWait = 100;

function Start(elmnt)
{
	document.getElementById(elmnt).style.display = 'block'; // This line must duplicate the thisgroup() function
	
	photo = 0;							// Start at the first photo
	ThisImage.src = Path+vImage[0];			// Load current image
	NextImage.src = Path+vImage[1];			// Load next image
	PrevImage.src = Path+vImage[maxPhoto];	// Load image for back step
	WaitLoad();                     // Wait for first photo to be fully loaded
	SizePhoto();					// Set the size for display
   	document.getElementById("slidemain").src = ThisImage.src;	// Display current photo	
	interval = setTimeout("Run()", period);  // Start the timer
	document.getElementById("play_stop").src = "images/Stop.gif";
}

function Toggle()
{
	if (state == "Pause")
	{
		state = "Run";
		NextPhoto();
		interval = setTimeout("Run()", period);
		document.getElementById("play_stop").src = "images/Stop.gif";
	}
	else
	{
		state = "Pause";
		clearTimeout(interval);
		document.getElementById("play_stop").src = "images/Play.gif";
	}
}

function Pause()
{
	if (state != "Pause")
	{
		state = "Pause";
		clearTimeout(interval);
		document.getElementById("play_stop").src = "images/Play.gif"
	}
}

function Run()
{
	if (NextPhoto() == true)        // loaded next
	{
	    interval = setTimeout("Run()", period);
	}
	else
	{                               // wait for it to load
	    interval = setTimeout("Run()", LoadWait);
	}
}

function SizePhoto()
{                                                                   // Get size of display area on screen
    var screenHeight = document.documentElement.clientHeight -150;  // minus banner etc. at top
    var screenWidth = document.documentElement.clientWidth - 220;   // width minus menu and padding
    var photoWidth = ThisImage.width;	// Get size of current photo
    var photoHeight = ThisImage.height;
	var width_str, height_str
	if (screenWidth < photoWidth)  // Too wide for the screen
    {
		photoHeight = (photoHeight * screenWidth) / photoWidth;
		photoWidth = screenWidth;
	}
	if (screenHeight < photoHeight)	// Too tall for the screen
	{
		photoWidth =  (photoWidth * screenHeight) / photoHeight;
		photoHeight = screenHeight;
	}
    width_str = String(parseInt(photoWidth))+"px"
    height_str = String(parseInt(photoHeight))+"px"
	document.getElementById("slidemain").style.height = height_str;	// Set display size
	document.getElementById("slidemain").style.width = width_str;
	sizeMenu();
}

function Prev()  // Prev button pressed
{
	Pause();
	PrevPhoto();
}

function PrevPhoto()
{
	var LoadPrev;
	
	if (PrevImage.width == 0) // If previous image not ready
	{                                                              // do nothing
	}
	else
	{														  // then show it
		photo = photo -1;	// move back one image
		if (photo < 0)		// need to wrap
		{
			photo = maxPhoto;
		}
		LoadPrev = photo - 1;	// Find photo before the new current
		if (LoadPrev < 0)		// need to wrap
		{
			LoadPrev = maxPhoto;
		}
	
		NextImage.src = ThisImage.src;	// Move images into correct position
		ThisImage.src = PrevImage.src;
		
		document.getElementById("slidemain").style.display = "none"; // hide while changing
		SizePhoto();				// Set the display area
   		document.getElementById("slidemain").src = ThisImage.src;	// Display current photo	
		document.getElementById("slidemain").style.display = "inline";
		
		PrevImage.src = Path+vImage[LoadPrev];	// Load the previous image
	}
}


function Next()  // Next button pressed
{
var load;
	Pause();
	load = NextPhoto();
}

function NextPhoto()
{
	var NextLoad;
	if (NextImage.width == 0) // If next image not ready
	{
	    return(false);
	}
	else
	{														  // then show it
		photo = photo + 1;	// move forward one image
		if (photo > maxPhoto) // need to wrap
		{
			photo = 0;
		}
		LoadNext = photo + 1;	//Find image after this
		if (LoadNext > maxPhoto) // need to wrap
		{
			LoadNext = 0;
		}
	
		PrevImage.src = ThisImage.src;	// Move images into correct place
		ThisImage.src = NextImage.src;

		document.getElementById("slidemain").style.display = "none"; // hide while changing
		SizePhoto();				// Set the display area	
   		document.getElementById("slidemain").src = ThisImage.src;	// Display current photo	
		document.getElementById("slidemain").style.display = "inline";

		NextImage.src = Path+vImage[LoadNext];	// Load the next image
		return(true);
	}
}

function WaitLoad()
{
    if (ThisImage.width == 0) // Not loaded yet
    {
        interval = setTimeout("WaitLoad()", LoadWait);  // Start the timer
    }
    else
    {                                                   // Show image
        document.getElementById("WaitMsg").style.display = "none";
        document.getElementById("slidemain").style.display = "block";
    }
}
