//City of Saint Paul
//Date: 3/10/2008
//Author: Glen Fingerholz
//Javascript to load and rotate images with captions
//OnMouseOver pauses the rotation
//OnMouseOut resumes the rotation
//Modified 3/17/08 to add option to include/declude picture captions
//modified 3/19/08 to allow pre-loading of images

/*
Web page setup example
<script type="text/javascript">

//set the options

SetDelay(3000); //in milliseconds
SetRandomOn(false); //random display or linier
SetPictureTagId("pictureframe"); //image tag with id of pictureframe
SetCommentTagId("comment"); //div tag with id of comment

//add the images
AddImages("one.jpg,two.jpg,three.jpg,four.jpg,five.jpg,six.jpg", ","); first parm is the list of images, sencond parm is the character to use to split the groups
AddComments("Picture one, Picture Two, Picture 3, Picture 4, Picture 5, Picture 6", ","); same as AddImages but for comments, first item will relate to first picture and so on

//start the rotation
TimerStart(); 
</script>
*/

var img_list = new Array(); //list to hold all image that will be displayed.
var img_comments = new Array(); //list to hold the image comments.
var currentImage = 0; //index id for the current image displayed.
var useRandom = true; //boolean value to indicate if to use a random image
var timerID = 0; //the timer id
var delayValue = 5000; //the timer delay value in milliseconds - 5 second default
var pictureTagID = null;
var captionTagID = null;
var useCaptions = true;

//function to add images to images list
function AddImages(imgList, splitcharacter)
{
	//split image list into each img name
	var parts = imgList.split(splitcharacter);
	
	//add each item to the image list
	for(i=0;i<parts.length;i++)
	{
		//make sure its not blank
		if(parts[i] != "")
		{
			img_list[img_list.length] = new Image();
			img_list[img_list.length - 1].src = parts[i];
		}
	}
}

//function to add the picture comments
function AddComments(commentList, splitcharacter)
{
	var parts = commentList.split(splitcharacter);
	
	for(i=0;i<parts.length;i++)
	{
		if(parts[i] != "")
			img_comments[img_comments.length] = parts[i];
	}
}

//function to display the current image.
function DisplayImage(index)
{
	if(pictureTagID != null)
		document.getElementById(pictureTagID).src = img_list[index].src;
	
	if(useCaptions)
	{	
		if(captionTagID != null)
			document.getElementById(captionTagID).innerHTML = "<p>" + img_comments[index] + "</p>";
	}
}

//function to clear the image list
function ClearImages()
{
	img_list = new Array();
}

//function to set current image
function SetCurrentImage()
{
	value = -1; //variable to hold what picture comes next
	
	//are we using random order?
	if(useRandom)
	{
		max = img_list.length - 1;
		min = 0;
		//generate a random number between 0 and the length of the image list -1
		value = Math.floor(Math.random() * (max - min +1) + min);
	}
	else
	{
		value = currentImage + 1; //increment the current image
		
		//check to make sure we are within bounds. if over - reset to 0
		if(value >= img_list.length)
			value = 0;
	}
	
	//prevent showing of same image
	if(currentImage !== value)
	{
		currentImage = value;
		DisplayImage(value);
	}
}

//function to start Timer, return false on error
function TimerStart()
{
	//set the initial picture
	if(pictureTagID != null)
		DisplayImage(currentImage);
	else
	{
		//attempt to alter the first image tag - dangerous and unsafe as it could break a website design
		document.getElementsByTagName("img")[0].src = img_list[0].src;
	}
	
	//make sure images are present before starting the timer
	if(img_list.length > 0)
	{
		timerID = setInterval("SetCurrentImage()", delayValue);
	}
	else
		return false;
}

//function to stop timer
function TimerStop()
{
	if(timerID)
	{
		clearInterval(timerID);
		timerID = 0;
	}
}

//function to set the delay value
function SetDelay(value)
{
	delayValue = value;
}

//function to set random
function SetRandomOn(value)
{
	useRandom = value;
}

//function to pause rotation
function Pause()
{
	clearInterval(timerID);
}

//function to resume rotation
function Resume()
{
	timerID = setInterval("SetCurrentImage()", delayValue);
}

//function to set the img tag id
function SetPictureTagId(value)
{
	pictureTagID = value;
}

//function to set the comment tag id
function SetCommentTagId(value)
{
	captionTagID = value;
}

//function to set captions on/off
function SetCaptions(value)
{
	useCaptions = value;
}
