//
// Simple JavaScript SlideShow
//
// Adapted from http://www.webmonkey.com/tutorial/Make_a_JavaScript_Slideshow
// Adapted by Andrew Rees (a rees at eng dev net dot com)
//
// Use:
//
// Include this script file by adding the following inside the <head>...</head> area of the html file:
//		<script type="text/javascript" language="javascript" src="scripts/slideshowcode.js"></script>
//
// Add the following (or similar where the image is to be located:
//		<img class=style1 name="slideImg" src="images/P1000337.JPG" width=300 height=95>
//		<script type="text/javascript">
//			SSInitialize(3000, 0);
//			SSAddImage("images/P1000337.JPG");
//			SSAddImage("images/P1000338.JPG");
//			SSAddImage("images/P1000339.JPG");
//			SSAddImage("images/P1000340.JPG");
//			SSAddImage("images/P1000341.JPG");
//			SSStartRotation('slideImg');
//		</script>
//
// The exampe above automatically starts the image rotation.  Slideshow controls can be added as follows:
//		<a href="#" onClick="SSRotateImage('slideImg')">> play slideshow</a>
//		<a href="#" onClick="SSStopRotation()">|| pause slideshow</a>
//		<a href="#" onClick="SSPrevImage('slideImg'); SSStopRotation()"><< previous</a>
//		<a href="#" onClick="SSRotateImage('slideImg'); SSStopRotation()">next >></a>
//

// Initialize the global variables for the slideshow
// 	Delay : the image rotation delay in milliseconds while the slideshow is playing
// 	RandomPlan : 0 to play the show in a fixed order or 1 to play the show in a random order
function SSInitialize(Delay, RandomPlay)
{
//alert("SSInitialize");
	SS_Interval = Delay; 
	SS_RandomDisplay = RandomPlay;
	SS_ImageNum = 0;
	SS_ImageArray = new Array();
	SS_ImageCount = 0;
}

// Add an image to the slideshow
//	image_source : the path to the image file to add.
// It is recommended that all images be the same size to keep the rest of the page from jumping each time an image loads.
function SSAddImage(image_source)
{
//alert("SSAddImage");
	SS_ImageArray[SS_ImageNum++] = new SSImageItem(image_source);
	SS_ImageCount = SS_ImageArray.length;
}

// Create a new image_item object
//	image_source : the path to the image file to add.
function SSImageItem(image_source)
{
//alert("SSImageItem");
	this.image_item = new Image();
	this.image_item.src = image_source;
}

// Get the path to an image object
// imageObj : the image object being queried.
function SSGetImageItemLocation(imageObj)
{
//alert("SSGetImageItemLocation");
	return(imageObj.image_item.src)
}

// Get a random number in the range of x to y, inclusive
//	x : the minimum value to return
//	y : the maximum value to return
function SSRandNum(x, y)
{
//alert("SSRandNum");
	var range = y - x + 1;
	return Math.floor(Math.random() * range) + x;
}

// Get the next image to display in the rotation or pick a random image
function SSGetNextImage()
{
//alert("SSGetNextImage");
	if (SS_RandomDisplay)
	{
		SS_ImageNum = SSRandNum(0, SS_ImageCount-1);
	}
	else
	{
		SS_ImageNum = (SS_ImageNum+1) % SS_ImageCount;
	}
	var new_image = SSGetImageItemLocation(SS_ImageArray[SS_ImageNum]);
	return(new_image);
}

// Get the previous image to display in the rotation.
function SSGetPrevImage()
{
//alert("SSGetPrevImage");
	SS_ImageNum = (SS_ImageNum-1) % SS_ImageCount;
	var new_image = SSGetImageItemLocation(SS_ImageArray[SS_ImageNum]);
	return(new_image);
}

// Move the slideshow to the previous image
function SSPrevImage(place)
{
//alert("SSPrevImage");
	var new_image = SSGetPrevImage();
	document[place].src = new_image;
}

// Move the slideshow to the next image or pick a random image
function SSRotateImage(place)
{
//alert("SSRotateImage");
	var new_image = SSGetNextImage();
	document[place].src = new_image;
	var recur_call = "SSRotateImage('"+place+"')";
	SS_TimerID = setTimeout(recur_call, SS_Interval);
}

// Start the slideshow automatically rotating
//	place : the name of the image object in the html page
function SSStartRotation(place)
{
//alert("SSStartRotation");
	var recur_call = "SSRotateImage('"+place+"')";
	SS_TimerID = setTimeout(recur_call, SS_Interval);
}

// Stop the slideshow from automatically rotating
function SSStopRotation()
{
//alert("SSStopRotation");
	clearTimeout(SS_TimerID);
}
