var maxshow = 14;
var stopshow = 0;

// this function swaps out the old image for the new (usually while it is hidden)
function swapem(currimage)
{
		$("#slides div").attr('style', 'display: none'); // hide the currently showing image
		$("#slidetext div").attr('style', 'display: none');
		$("#slidenav a").removeClass('showing');
		$("#slide"+currimage).attr('style', 'display: block'); // show the new image div
		$("#slidetext"+currimage).attr('style', 'display: block'); // show the new image div
		$("#slidenav"+currimage).addClass('showing');
}

// looping image to fade out the old image and fade in the new one.
function fadeit(currimage){
	if (stopshow >= maxshow)  // because of recursion, we don't want this loop to run forever or it will use too much cpu
		return;
	else {
		stopshow++;
		var origimage = currimage;
		currimage++;
		if (currimage > maximage)  // loop when we get to the end
			currimage = 0;
		$("#slides").animate({"opacity" : 1}, 3000); // 
		$("#slides").animate({"opacity" : 1}, 6000); // show the image for six seconds
		$("#slides").animate({"opacity" : 0}, 3000, "", function () {
			swapem(currimage);
		});
		$("#slides").animate({"opacity" : 1}, 3000, fadeit(currimage));  // fade in the new image, recall this function
	}
}

function jumpto(jumpimage)
{
	$("#slides").clearQueue();  // stop any fading that is going on.
	$("#slides").stop();
	if (jumpimage >= 0)
		swapem(jumpimage);
	$("#slides").animate({"opacity" : 1}, 2000);  //fade in the indicated slide
	stopimage = jumpimage;
//	$("#slides").animate({"opacity" : 1}, 2000, fadeit(jumpimage));
}

function play()
{
	var origimage = stopimage;
	currimage = stopimage+1;
	if (currimage > maximage)  // loop if we were on the last one
		currimage = 0;
	$("#slides").animate({"opacity" : 0}, 1000, "", function () {
		swapem(currimage);
	});
	$("#slides").animate({"opacity" : 1}, 3000, fadeit(currimage));  // fade in the new image, start the loop.
}

// this funciton will preload the slideshow images.
function loadslides()
{
	for (counter = 0; counter <= maximage; counter++)
	{
		if (counter != currimage)
		{
			$("#slides").append("<div id=\"slide"+counter+"\" style=\"display: none\"><a href=\""+imgURL[counter]+"\"><img src=\""+imgArray[counter]+"\" width=\"" + width + "\" height=\"" + height + "\" /></a></div>");
			$("#slidetext").append("<div id=\"slidetext"+counter+"\" style=\"display: none\"><a href=\""+imgURL[counter]+"\">"+imgText[counter]+"</a></div>");
		}
	} 
}
