/************************************************************************
This library is used to change a picture and its caption on the fly

NOTE: to add pics from an external file, it should be an external .js file that
simply has multiple calls to addPic() in it.  However, this file needs to be
loaded first so that the addPic() function will already be loaded

The caption of an image should be placed inside a <div> tag that has an id
attribute (the capToChange variable will hold this id)
*************************************************************************/

/************************************************************************
 Initialize global variables
*************************************************************************/
var picArray = new Array();
var capArray = new Array();

var totalNumOfPics = 0;
var currentpic = 0;

var picToChange = "image";
var capToChange = "caption"

/************************************************************************
 Sets which <img> tag (and caption) will be modified.  The default is "image" and "caption", respectively
*************************************************************************/
function setPicToChange(imgName, capName)
{
	picToChange = imgName;
	capToChange = capName;
} // end setPicToChange()


/************************************************************************
 Adds an image and a corresponding caption to their respective arrays 
*************************************************************************/
function addPic(picpath, caption)
{
	picArray[totalNumOfPics] = new Image();
	picArray[totalNumOfPics].src = picpath;

	capArray[totalNumOfPics] = caption;

	totalNumOfPics++;
} // end addPic()

/************************************************************************
 Displays the next image in the array with its corresponding caption
*************************************************************************/
function next()
{
	if (currentpic == (totalNumOfPics - 1) )
	{
		currentpic = 0;
	}
	else
	{
		currentpic++;
	}

	if (document.images)
	{
		document.images[picToChange].src = picArray[currentpic].src;
		document.getElementById(capToChange).innerHTML = capArray[currentpic];
	}
	else
	{
		// Set the currentpic variable back to its previous state
		if (currentpic == 0)
		{
			currentpic = totalNumOfPics - 1;
		}
		else
		{
			currentpic = currentpic--;
		} // end if-else
	} // end if-else
} // end next()

/************************************************************************
 Displays the previous image in the array with its corresponding caption
*************************************************************************/
function previous()
{
	if (currentpic == 0)
	{
		currentpic = totalNumOfPics - 1;
	}
	else
	{
		currentpic--;
	}

	if (document.images)
	{
		document.images[picToChange].src = picArray[currentpic].src;
		document.getElementById(capToChange).innerHTML = capArray[currentpic];
	}
	else
	{
		// Set the currentpic variable back to its previous state
		if (currentpic == (totalNumOfPics - 1) )
		{
			currentpic = 0;
		}
		else
		{
			currentpic++;
		} // end if-else
	} // end if-else
} // end previous()