var currentPhoto = 0; 
var initialWait = 2000;
var timeOnPhoto = 1000;
var imageWidth = 245;
var imageHeight = 100;
var fadeSpeed = 100;
var moveSpeed = 100;
var totalHeaderWidth = 1000;
var logoWidth = 218;
var headerWidth = totalHeaderWidth - logoWidth;
var logoWidth = totalHeaderWidth - headerWidth;
var pos = new Array(7)
pos[0] = 0;
pos[1] = 0;
pos[2] = 0;
pos[3] = 0;
pos[4] = 0;
pos[5] = 0;
pos[6] = 0;
var speed = new Array(7)
speed[0] = 0;
speed[1] = 0;
speed[2] = 0;
speed[3] = 0;
speed[4] = 0;
speed[5] = 0;
speed[6] = 0;
var leftPixel_old = 0;
var leftPixel = 0;
var topPixel = 45;
var contentTop = topPixel + imageHeight + 2;
var startShow = true;
var donePreLoad = false; 
var pic1 = "1";
var pic2 = "2";
var ministryid = -1;
var contentHeight = 0;
var sameHeightCounter = 0;
var defaultMenuLeft = 0;
var sameLeftCounter = 0;

var photosLeft = new Array(totalPhotos);
var numPhotosLeft = totalPhotos;

for (i=0; i<totalPhotos; i++) {
	photosLeft[i] = true;
}

function calculatePositions() {
	totalHeaderWidth = parseInt(0.8 * document.body.clientWidth);
	headerWidth = totalHeaderWidth - logoWidth;
	logoWidth = totalHeaderWidth - headerWidth;
	leftPixel = (document.body.clientWidth/2)-(totalHeaderWidth/2);

	document.getElementById("headerBackground").style.width = (headerWidth + 2) + "px";
	document.getElementById("headerBackground").style.height = imageHeight + "px";
	document.getElementById("headerBackground").style.left = (leftPixel - 2) + "px";
	document.getElementById("headerBackground").style.top = topPixel + "px";
	document.getElementById("headerLogo").style.left = leftPixel + headerWidth + "px";
	document.getElementById("headerLogo").style.top = topPixel + "px";
	document.getElementById("headerLogo").style.height = imageHeight + "px";
	document.getElementById("headerLogo").style.width = logoWidth + "px";
	document.getElementById("headerLogo").src = "images/CalvaryLogoSmall.gif";

	document.getElementById("mainPicture1").style.top = topPixel + "px";
	document.getElementById("mainPicture2").style.top = topPixel + "px";
	document.getElementById("mainPicture3").style.top = topPixel + "px";
	document.getElementById("mainPicture4").style.top = topPixel + "px";
	document.getElementById("mainPicture5").style.top = topPixel + "px";
	document.getElementById("mainPicture6").style.top = topPixel + "px";

	document.getElementById("mainContent").width = (totalHeaderWidth + 2) + "px";
	document.getElementById("mainContent").style.top = contentTop + "px";
	document.getElementById("mainContent").style.left = (leftPixel - 1) + "px";
	document.getElementById("mainContentBackground").style.width = (totalHeaderWidth + 2) + "px";
	document.getElementById("mainContentBackground").style.top = contentTop + "px";
	document.getElementById("mainContentBackground").style.left = (leftPixel - 1) + "px";
	setContentHeight();
	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";

	// If we are showing pictures we need to handle the resize here
	if (document.getElementById("mainPicture1").style.display == "block") {
		// Store our new values
		for (var i=1; i<7; i++) {
			pos[i] = pos[i] + (leftPixel - leftPixel_old);
			if (document.getElementById("mainPicture" + i)) {
				document.getElementById("mainPicture" + i).style.left = pos[i] + "px";
			}
		}
	}
	leftPixel_old = leftPixel;
}

function initialize() {
	calculatePositions();

	document.getElementById("mainPicture1").style.display = "block";
	document.getElementById("mainPicture2").style.display = "block";
	document.getElementById("mainPicture3").style.display = "block";
	document.getElementById("mainPicture4").style.display = "block";
	document.getElementById("mainPicture5").style.display = "block";
	document.getElementById("mainPicture6").style.display = "block";

	if (totalPhotos > 0) {

		getNextPicture(1);
		getNextPicture(2);
		getNextPicture(3);
		getNextPicture(4);
		getNextPicture(5);
		getNextPicture(6);

		getNewPosition(pic1);

		window.setTimeout("fadeIn(" + pic1 + ")", initialWait);

		if (startShow) {
			speed[pic1] = getSpeed();
			window.setTimeout("movePictures()", moveSpeed);
		}
	}
	else {
		document.getElementById("mainPicture1").style.display = "none";
		document.getElementById("mainPicture2").style.display = "none";
		document.getElementById("mainPicture3").style.display = "none";
		document.getElementById("mainPicture4").style.display = "none";
		document.getElementById("mainPicture5").style.display = "none";
		document.getElementById("mainPicture6").style.display = "none";
	}

	// Select the correct menu item
	if (document.getElementById(menuid)) {
		document.getElementById(menuid).className = "menuLinkhover";

		var pos = getAbsolutePos(document.getElementById(menuid));
		document.getElementById("menuItemBackground_current").style.top = "0px";
		document.getElementById("menuItemBackground_current").style.left = pos.x + "px";
		document.getElementById("menuItemBackground_current").style.width = parseInt(document.getElementById(menuid).offsetWidth) + "px";
		document.getElementById("menuItemBackground_current").style.height = "25px";
		document.getElementById("menuItemBackground_current").style.display = "block";
		setDefaultMenuLeft();
	}

	// Select the correct ministry
	if (ministryid != "-1") {
		if (document.getElementById("Ministries-Link-" + ministryid)) {
			document.getElementById("Ministries-Link-" + ministryid).setAttribute("onmouseover", "");
			document.getElementById("Ministries-Link-" + ministryid).setAttribute("onmouseout", "");
			document.getElementById("Ministries-Link-" + ministryid).style.fontWeight = "Bold";
		}
	}

	setupDove();
}

function setupDove() {
	var logo_width = 2640;
	var logo_height = 982;
	var logoImage = document.createElement('img');
	
	logoImage.setAttribute('id', "doveImage");
	logoImage.setAttribute('src', "images/large_logo_no_words_color.gif");
	logoImage.setAttribute('style', 'z-index:-1; border:0px;  opacity:.20; filter: alpha(opacity=20);');
	logoImage.setAttribute('width', logo_width);
	logoImage.setAttribute('height', logo_height);
	logoImage.style.position = "absolute";
	logoImage.style.display = "block";
	logoImage.style.padding = "0";
	logoImage.style.margin = "0";

	logo_img_top = (Math.random() * logo_height/2) - 200;
	logo_img_left = (Math.random() * document.body.clientWidth) - (logo_width / 2);

	logoImage.style.top = logo_img_top + "px";
	logoImage.style.left = logo_img_left + "px";
	document.getElementById("doveContainer").appendChild(logoImage);
}

function getNewPosition(picNum) {
	var otherImageLeft = 0;
	var randomNumber = 0;
	var path = "";

	otherImageLeft = (picNum == pic1) ? pos[pic2] : pos[pic1];

	// Where can we place the picture? Lets see where the current one is, and place this one accordingly.
	if (otherImageLeft == 0) {
		now = new Date();
		seed = now.getSeconds();
		randomNumber = Math.floor(Math.random(seed) * (headerWidth - imageWidth) + leftPixel);

	} else {
		if (otherImageLeft <= (imageWidth + leftPixel)) {
			now = new Date();
			seed = now.getSeconds();
			randomNumber = Math.floor(Math.random(seed) * ((headerWidth + leftPixel - imageWidth) - (otherImageLeft + imageWidth))) + (otherImageLeft + imageWidth);
		}
		else {
			if (otherImageLeft >= (leftPixel + headerWidth - (2 * imageWidth))) {
				now = new Date();
				seed = now.getSeconds();
				randomNumber = Math.floor(Math.random(seed) * (otherImageLeft - imageWidth - leftPixel)) + leftPixel;
			}
			else {
				now = new Date();
				seed = now.getSeconds();
				randomNumber = Math.floor(Math.random(seed) * (headerWidth - (3 * imageWidth))) + leftPixel;
				if (randomNumber >= (otherImageLeft - imageWidth)) {
					randomNumber += (2 * imageWidth);
				}
			}
		}
	}

	// Store our new values
	pos[picNum] = randomNumber;
	document.getElementById("mainPicture"+picNum).style.left = pos[picNum] + "px";
}

function setContentHeight() {
	var continueSizing = true;
	if (contentHeight == document.getElementById("mainContent").offsetHeight) {
		if (sameHeightCounter++ > 20) {
			continueSizing = false;
		}
	} else {
		contentHeight = document.getElementById("mainContent").offsetHeight;
		sameHeightCounter = 0;
	}
	document.getElementById("mainContentBackground").style.height = contentHeight + "px";
	if (continueSizing) {
		window.setTimeout("setContentHeight()", 1000);
	}
}

function setDefaultMenuLeft() {
	if (menuid != null) {
		var pos = getAbsolutePos(document.getElementById(menuid));
		var continueAdjusting = true;
		if (defaultMenuLeft == pos.x) {
			if (sameLeftCounter++ > 10) {
				continueAdjusting = false;
			}
		} else {
			defaultMenuLeft = pos.x;
			sameLeftCounter = 0;
			document.getElementById("menuItemBackground_current").style.left = defaultMenuLeft + "px";
		}
		if (continueAdjusting) {
			window.setTimeout("setDefaultMenuLeft()", 500);
		}
	}
}

function getSpeed() {
	// Calculate a new random speed. Basically we only have 1 speed, so this is really 
	// about getting a random direction.
	now = new Date();
	seed = now.getSeconds();
	var randomNumber = Math.floor(Math.random(seed)*2);
	if (randomNumber == 0) {
		randomNumber = -1;
	}
	return randomNumber;
}

function movePictures() {
	// Move picture A
	if (pos[pic1] <= leftPixel) { 
		// If we hit the left wall, turn around.
		speed[pic1] = speed[pic1] * -1;
		document.getElementById("mainPicture"+pic1).style.left = leftPixel + "px";
	}
	else {
		if (pos[pic1] >= (leftPixel + headerWidth - imageWidth)) { 
			// If we hit the right wall, turn around.
			speed[pic1] = speed[pic1] * -1;
			document.getElementById("mainPicture"+pic1).style.left = (leftPixel + headerWidth - imageWidth) + "px";
		}
		else {
			document.getElementById("mainPicture"+pic1).style.left = pos[pic1] + "px";
		}
	}
	pos[pic1] = pos[pic1] + speed[pic1];

	// Move picture B
	if (pos[pic2] <= leftPixel) { 
		// If we hit the left wall, turn around.
		speed[pic2] = speed[pic2] * -1;
		document.getElementById("mainPicture"+pic2).style.left = leftPixel + "px";
	}
	else {
		if (pos[pic2] >= (leftPixel + headerWidth - imageWidth)) {  
			// If we hit the right wall, turn around.
			speed[pic2] = speed[pic2] * -1;
			document.getElementById("mainPicture"+pic2).style.left = (leftPixel + headerWidth - imageWidth) + "px";
		}
		else {
			document.getElementById("mainPicture"+pic2).style.left = pos[pic2] + "px";
		}
	}
	pos[pic2] = pos[pic2] + speed[pic2];

	// If the images hit each other and both a visible, then change both directions.
	var collision = false;
	var bothVisible = true;

	// are they both visible
	if (navigator.product == 'Gecko') { 
		if (parseFloat(document.getElementById("mainPicture"+pic1).style.opacity) == 0 || parseFloat(document.getElementById("mainPicture"+pic2).style.opacity) == 0) {
			bothVisible = false;
		}
	}
	else {
		if (document.getElementById("mainPicture"+pic1).filters.alpha.opacity == 0 || document.getElementById("mainPicture"+pic2).filters.alpha.opacity == 0) {
			bothVisible = false;
		}
	}

	if (bothVisible) {
		if (pos[pic1] <= pos[pic2]) {
			if (pos[pic1] + imageWidth >= pos[pic2]) {
				collissionOverage = pos[pic1] + imageWidth - pos[pic2];
				speed[pic1] = speed[pic1] * -1;
				speed[pic2] = speed[pic2] * -1;

				// Check if we can move B, if so, move B, otherwise move A
				if (pos[pic2] + collissionOverage + imageWidth <= leftPixel + headerWidth) {
					pos[pic2] = pos[pic2] + collissionOverage;
				}
				else {
					pos[pic1] = pos[pic1] - collissionOverage;
				}
			}
		}
		else {
			if (pos[pic2] + imageWidth >= pos[pic1]) {
				collissionOverage = pos[pic2] + imageWidth - pos[pic1];
				speed[pic1] = speed[pic1] * -1;
				speed[pic2] = speed[pic2] * -1;

				// Check if we can move A, if so, move A, otherwise move B
				if (pos[pic1] + collissionOverage + imageWidth <= leftPixel + headerWidth) {
					pos[pic1] = pos[pic1] + collissionOverage;
				}
				else {
					pos[pic2] = pos[pic2] - collissionOverage;
				}
			}
		}
	}

	window.setTimeout("movePictures()", moveSpeed);
}

function fadeIn(picNum) {
	if (navigator.product == 'Gecko') { 
		document.getElementById("mainPicture"+picNum).style.opacity = parseFloat(document.getElementById("mainPicture"+picNum).style.opacity) + .05;

		if (parseFloat(document.getElementById("mainPicture"+picNum).style.opacity) < 1) {
			window.setTimeout("fadeIn(" + picNum + ")", fadeSpeed);
		}
		else {
			if (startShow) {
				startNextPicture((picNum == pic1) ? pic2 : pic1);
			}
		}
	}
	else {
		document.getElementById("mainPicture"+picNum).filters.alpha.opacity = document.getElementById("mainPicture"+picNum).filters.alpha.opacity + 5;

		if (document.getElementById("mainPicture"+picNum).filters.alpha.opacity < 100) {
			window.setTimeout("fadeIn(" + picNum + ")", fadeSpeed);
		}
		else {
			if (startShow) {
				startNextPicture((picNum == pic1) ? pic2 : pic1);
			}
		}
	}
}

function fadePicture(picA) {
	picB = (pic1 == picA ? pic2 : pic1);

	if (navigator.product == 'Gecko') { 
		document.getElementById("mainPicture"+picA).style.opacity = parseFloat(document.getElementById("mainPicture"+picA).style.opacity) + .05;
		document.getElementById("mainPicture"+picB).style.opacity = parseFloat(document.getElementById("mainPicture"+picB).style.opacity) - .05;

		if (parseFloat(document.getElementById("mainPicture"+picB).style.opacity) > 0) {
			window.setTimeout("fadePicture(" + picA + ")", fadeSpeed);
		}
		else {
			getNextPicture(picB);
			picB = incrPointer(picB);
			startNextPicture(picB);
		}
	}
	else {
		document.getElementById("mainPicture"+picA).filters.alpha.opacity = document.getElementById("mainPicture"+picA).filters.alpha.opacity + 5;
		document.getElementById("mainPicture"+picB).filters.alpha.opacity = document.getElementById("mainPicture"+picB).filters.alpha.opacity - 5;

		if (document.getElementById("mainPicture"+picB).filters.alpha.opacity > 0) {
			window.setTimeout("fadePicture(" + picA + ")", fadeSpeed);
		}
		else {
			getNextPicture(picB);
			picB = incrPointer(picB);
			startNextPicture(picB);
		}
	}
}

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 incrPointer(picNum) {
	if (picNum == pic1) {
		pic1 = ( pic2 % 6 ) + 1;
		return pic1;
	} else {
		pic2 = ( pic1 % 6 ) + 1;
		return pic2;
	}
}

function getNextPicture(picNum) {
	incrPhoto();
	document.getElementById("mainPicture"+picNum).src = photo[currentPhoto];
}

function startNextPicture(picNum) {
	getNewPosition(picNum);
	speed[picNum] = getSpeed();
	window.setTimeout("fadePicture(" + picNum + ")", timeOnPhoto);
}

