// ======================================================================================================
// Set the following variables...
// ======================================================================================================

//NOTE: true and false should be in all lowercase and have NO quotations

var tagId = "slideshow"; //Id of <img> tag in which slideshow will be contained
var imgLocation = "galleryHome/"; //Location of all of the images *make sure to include the forward slash after dir name*
var imgRandomOrder = false; //Set to true to sort images randomly, otherwise set to false to sort alphabetically by filename
var autoChange = true; //Set to true for slideshow effect, otherwise set to false
var rotationSpeed = 3500; //If autoChange is set to true, this will set the interval between image changes
var loop = false; //Set to true to loop slideshow, otherwise set to false
var fade = true; //Set to true to add a fade effect in between photos, otherwise set to false
var showCaptions = false; //Set to true to show captions, otherwise set to false
var showImgNumber = false; //Set to true to show image display number, otherwise set to false
var showButtons = false; //Set to true to show buttons, otherwise set to false
var thumbs = false; //Set to true to show thumbnail images, otherwise set to false
var showScrollBar = false; //If thumbs is set to true, set this to true to show thumbnail scrollbar, otherwise set to false
var thumbLocation = "thumbs/"; //If thumbs is set to true, this is the location of all of the thumbnails *make sure to include the forward slash after dir name* NOTE: This directory should be inside the imgLocation directory

// ======================================================================================================
// End of editable section     		   *** Script Version 2.1 ***   		  DO NOT EDIT BELOW THIS LINE
// ======================================================================================================

var count = 0;
var imgs = new Array();
var opacity = 10;

function init() {
	if (imgRandomOrder) {
		galleryarray.sort(sortRandom);
	}
	preLoadImages();
	if (thumbs) {
		enableThumbs();
		if (showScrollBar) {
			enableScrollBar();
		}
	}
	if (showButtons) {
		enableButtons();
	}
	changeImg(tagId);
	if (autoChange) {
		var fx = function() {rotateImages(true);}
		setTimeout(fx, rotationSpeed);
	}
}

function preLoadImages() {
	for (var i = 0; i < galleryarray.length; i++) {
		imgs[i] = new Image();
		imgs[i].src = imgLocation + galleryarray[i];
	}	
}

function sortRandom() {
	return (Math.round(Math.random() * galleryarray.length));
}

function enableThumbs(){
	var html = "<table border=\"0\" cellpadding=\"2\" cellspacing=\"5\"><tr>";
	for (var i = 0; i < imgs.length; i++){
	html += "<td class=\"inactive\" id=\"thumb" + i + "\" width=\"100\"><a href=\"javascript:;\" id=\"" + i + "\" name=\"" + i + "\"><img src=\"" + imgLocation + "" + thumbLocation + "" + imgs[i] + "\" width=\"100\" height=\"75\" border=\"0\" alt=\"" + i + "\" /></td>";
	}
	html += "</tr></table>";
	document.getElementById('navigation').innerHTML = html;}

function enableButtons() {
	document.getElementById("buttons").style.display = "inline";
}

function enableScrollBar() {
	document.getElementById("navigation").style.overflow = "auto";
}

function showCaption() {
	var rawcaption = imgs[count].src.substr((imgs[count].src.indexOf("$") + 1));
	document.getElementById("CaptionBox").innerHTML = rawcaption.substring(0, rawcaption.lastIndexOf("."));
}

function showNumber() {
	document.getElementById("picNumber").innerHTML = "Image " + (count + 1) + " of " + imgs.length;
}

function changeImg(id) {
	if (showImgNumber) {
		showNumber();
	}
	if (showCaptions) {
		showCaption();
	}
	document.getElementById(id).src = imgs[count].src;
}

function rotateImages(cont) {
	if (cont) {
		var fx = function() {rotateImages(cont);}
		var timeout = setTimeout(fx, rotationSpeed);
	}
	if (count < (imgs.length - 1)) {
		count++;
	} else {
		if (loop) {
			count = 0;
		} else {
			cont = false;
			clearTimeout(timeout);
		}
	}
	if (cont) {
		if (fade) {
			fadeImage();
		} else {
			changeImg(tagId);
		}
	}
}

function fadeImage() {
	var fx = function() {fadeImage();}
	var fadetimeout = setTimeout(fx, 50);
	if (opacity > 0) {
		opacity--;
		document.getElementById(tagId).style.opacity = opacity / 10;
		document.getElementById(tagId).style.MozOpacity = opacity / 10;
		document.getElementById(tagId).style.filter = "alpha(opacity=" + (opacity * 10) + ")";
	} else if (opacity == 0) {
		opacity = 10;
		if (count < (imgs.length - 1)) {
			document.getElementById(tagId).src = imgs[count].src;
			document.getElementById(tagId).style.opacity = opacity / 10;
			document.getElementById(tagId).style.MozOpacity = opacity / 10;
			document.getElementById(tagId).style.filter = "alpha(opacity=" + (opacity * 10) + ")";
			document.getElementById("fade").style.backgroundImage = "url('" + imgs[count + 1].src + "')";
		}
		clearTimeout(fadetimeout);
	}
}
