var totalPhotos = 17;

var photo = new Array(totalPhotos);
var photosLeft = new Array(totalPhotos) ;
var numPhotosLeft = totalPhotos;

for (i=0; i<totalPhotos; i++) {
	photo[i] = "photos/" + i + ".jpg";
	photosLeft[i] = true;
}

var photoCaption= new Array(totalPhotos) 
photoCaption[0] = "Welcome to Calvary Chapel of the Foothills";
photoCaption[1] = "Children's Ministries";
photoCaption[2] = "Senior Pastor John Lukas";
photoCaption[3] = "Sunday Service";
photoCaption[4] = "Children's Ministries";
photoCaption[5] = "Baptism Picnic";
photoCaption[6] = "Children's Ministries";
photoCaption[7] = "Worship Ministry";
photoCaption[8] = "Women's Ministries";
photoCaption[9] = "Book Store";
photoCaption[10] = "Children's Ministries";
photoCaption[11] = "Men's Ministries";
photoCaption[12] = "Children's Ministries";
photoCaption[13] = "Children's Playground";
photoCaption[14] = "Men's Minstries";
photoCaption[15] = "Worship Ministry";
photoCaption[16] = "Children's Ministries";

var currentPhoto = 0;
var captionIndex = new Array(7);
captionIndex[0] = "";
captionIndex[1] = "";
captionIndex[2] = "";
captionIndex[3] = "";
captionIndex[4] = "";
captionIndex[5] = "";
captionIndex[6] = "";

var initialWait = 2000;
var timeOnPhoto = 3500;
var CaptionHeight = 55;
var imageWidth = 700;
var logoWidth = 370;
var imageHeight = 400;
var logoHeight = 170;
var fadeSpeed = 20;
var leftPixel = 0;
var topPixel = 0;
var pic1 = "1";
var pic2 = "2";
var useRandom = true;

function calculatePositions() {
	leftPixel = (document.documentElement.clientWidth/2)-(imageWidth + logoWidth)/2;
	topPixel = (document.documentElement.clientHeight/2)-(imageHeight/2);

	document.getElementById("photoBackground").style.left = leftPixel + "px";
	document.getElementById("mainPicture"+pic1).style.left = leftPixel + "px";
	document.getElementById("mainPicture"+pic2).style.left = leftPixel + "px";
	document.getElementById("ccotfLogo").style.left = (leftPixel + imageWidth) + "px";
	document.getElementById("mainText").style.left = (leftPixel + imageWidth) + "px";
	document.getElementById("mainText").style.width = (logoWidth-20) + "px";
	document.getElementById("mainText").style.height = (imageHeight - logoHeight-20) + "px";
	document.getElementById("caption").style.left = leftPixel + "px";
	document.getElementById("captionText").style.left = leftPixel + "px";

	document.getElementById("photoBackground").style.top = topPixel + "px";
	document.getElementById("photoBackground").style.width = imageWidth + "px";
	document.getElementById("photoBackground").style.height = imageHeight + "px";
	document.getElementById("mainPicture"+pic1).style.top = topPixel + "px";
	document.getElementById("mainPicture"+pic2).style.top = topPixel + "px";
	document.getElementById("ccotfLogo").style.top = topPixel + "px";
	document.getElementById("mainText").style.top = (topPixel + logoHeight) + "px";
	document.getElementById("captionText").style.top = ((parseInt(topPixel)+parseInt(imageHeight)) - CaptionHeight) + "px";
	document.getElementById("caption").style.top = ((parseInt(topPixel)+parseInt(imageHeight)) - CaptionHeight + 1) + "px";
	document.getElementById("caption").style.height = (CaptionHeight + 1) + "px";
	document.getElementById("caption").style.width = imageWidth + "px";

	document.getElementById("menuBackground").style.top = "0px";
	document.getElementById("menuBackground").style.left = "0px";
	document.getElementById("menuBackground").style.width = document.body.clientWidth + "px";
	document.getElementById("menuBackground").style.height = "25px";
}

function initialize() {
	calculatePositions();
	getNextPicture(pic1);
	window.setTimeout("fadeIn()", initialWait);
}

function fadeIn() {
	if (navigator.product == 'Gecko') { 
		document.getElementById("mainPicture"+pic1).style.opacity = parseFloat(document.getElementById("mainPicture"+pic1).style.opacity) + .01;

		if (parseFloat(document.getElementById("mainPicture"+pic1).style.opacity) < 1) {
			window.setTimeout("fadeIn()", fadeSpeed);
			if (parseFloat(document.getElementById("mainPicture"+pic1).style.opacity) == .5) {
				document.getElementById("captionText").innerHTML = captionIndex[pic1];
			}
		}
		else {
			getNextPicture(pic2);
			window.setTimeout("fade(" +pic2 + ")", timeOnPhoto);
		}
	}
	else {
		document.getElementById("mainPicture"+pic1).filters.alpha.opacity = document.getElementById("mainPicture"+pic1).filters.alpha.opacity + 1;

		if (document.getElementById("mainPicture"+pic1).filters.alpha.opacity <100) {
			window.setTimeout("fadeIn()", fadeSpeed);
			if (parseInt(document.getElementById("mainPicture"+pic1).filters.alpha.opacity) == 50) {
				document.getElementById("captionText").innerHTML = captionIndex[pic1];
			}
		}
		else {
			getNextPicture(pic2);
			window.setTimeout("fade(" + pic2 + ")", timeOnPhoto);
		}
	}
}

function fade(picA) {
	picB = (picA == pic1) ? pic2 : pic1;

	if (navigator.product == 'Gecko') { 
		document.getElementById("mainPicture"+picA).style.opacity = parseFloat(document.getElementById("mainPicture"+picA).style.opacity) + .01;
		document.getElementById("mainPicture"+picB).style.opacity = parseFloat(document.getElementById("mainPicture"+picB).style.opacity) - .01;

		if (parseFloat(document.getElementById("mainPicture"+picB).style.opacity) > 0) {
			window.setTimeout("fade(" + picA + ")", fadeSpeed);
			if (parseFloat(document.getElementById("mainPicture"+picA).style.opacity) == .5) {
				document.getElementById("captionText").innerHTML = captionIndex[picA];
			}
		}
		else {
			getNextPicture(picB);
			window.setTimeout("fade(" + picB + ")", timeOnPhoto);
		}
	}
	else {
		document.getElementById("mainPicture"+picA).filters.alpha.opacity = document.getElementById("mainPicture"+picA).filters.alpha.opacity + 1;
		document.getElementById("mainPicture"+picB).filters.alpha.opacity = document.getElementById("mainPicture"+picB).filters.alpha.opacity - 1;

		if (document.getElementById("mainPicture"+picB).filters.alpha.opacity > 0) {
			window.setTimeout("fade(" + picA + ")", fadeSpeed);
			if (parseInt(document.getElementById("mainPicture"+picA).filters.alpha.opacity) == 50) {
				document.getElementById("captionText").innerHTML = captionIndex[picA];
			}
		}
		else {
			getNextPicture(picB);
			window.setTimeout("fade(" + picB + ")", timeOnPhoto);
		}
	}
}

function incrPhoto() {
	if (numPhotosLeft == 0) {
		numPhotosLeft = totalPhotos - 1;
		for (i=0; i<totalPhotos; i++) {
			photosLeft[i] = true;
		}
		photosLeft[currentPhoto] = false;
	}

	now = new Date();
	seed = now.getSeconds();
	randomNumber = Math.floor(Math.random(seed)*(numPhotosLeft-1));
	currentPhoto = -1;
	for (i=0; i<=randomNumber; i++) {
		currentPhoto = currentPhoto + 1;
		while (!photosLeft[currentPhoto]) {
			currentPhoto = currentPhoto + 1;
		}
	}
	photosLeft[currentPhoto] = false;
	numPhotosLeft = numPhotosLeft - 1;
}

function getNextPicture(pickNum) {
	incrPhoto();
	document.getElementById("mainPicture"+pickNum).src = photo[currentPhoto];
	captionIndex[pickNum] = photoCaption[currentPhoto];
}
