
var animImg = {};
animImg.opacity = 1;
animImg.increment = 0.01;
animImg.threshold = 0.01;
animImg.running = false;
animImg.leftAnim = true;
animImg.isIE = false;

var animLeft = {};
animLeft.foreImg = {};
animLeft.foreImgIndex = 0;
animLeft.backImg = {};
animLeft.backImgIndex = 1;

var animRight = {};
animRight.foreImg = {};
animRight.foreImgIndex = 2;
animRight.backImg = {};
animRight.backImgIndex = 3;

// must be at least 5 images
var imgArray = new Array();
imgArray[0] = new Image();
imgArray[0].src = "../img/fp_01.jpg";
imgArray[1] = new Image();
imgArray[1].src = "../img/fp_02.jpg";
imgArray[2] = new Image();
imgArray[2].src = "../img/fp_03.jpg";
imgArray[3] = new Image();
imgArray[3].src = "../img/fp_04.jpg";
imgArray[4] = new Image();
imgArray[4].src = "../img/fp_05.jpg";
imgArray[5] = new Image();
imgArray[5].src = "../img/fp_06.jpg";
imgArray[6] = new Image();
imgArray[6].src = "../img/fp_07.jpg";
imgArray[7] = new Image();
imgArray[7].src = "../img/fp_08.jpg";

function $(e) {
	return document.getElementById(e);
}

function init() {
	animLeft.foreImg = $("img1");
	animLeft.backImg = $("img2");
	animRight.foreImg = $("img3");
	animRight.backImg = $("img4");
	animImg.isIE = ((navigator.appVersion.indexOf("MSIE")!= -1)&&!window.opera)
	fadeControl()
}

function fadeControl() {
	startFader();
	timerHandle2 = setInterval(startFader, 10000);
}

function startFader() {
	if (animImg.running == true) return;
	timerHandle = setInterval(fade, 50);
}

function selectImgIndex() {
	var freeImgIndex = false;
	var imgIndex = Math.floor(Math.random()*imgArray.length)
	if (imgIndex == imgArray.length) imgIndex = 0;
	
	freeImgIndex = isImgIndexFree(imgIndex);
	while (!freeImgIndex) {
		imgIndex++;
		if (imgIndex >= imgArray.length) imgIndex = 0;
		freeImgIndex = isImgIndexFree(imgIndex);
	}
	
	return imgIndex;
}

function isImgIndexFree(i) {
	if (i == animLeft.foreImgIndex || i == animLeft.backImgIndex || i == animRight.foreImgIndex || i == animRight.backImgIndex) return false;
	else return true;
}

function fade() {
	if (animImg.leftAnim) animBlock = animLeft;
	else animBlock = animRight;
	animImg.running = true;
	
	animImg.opacity = animImg.opacity - animImg.increment;
	if (animImg.isIE) {
		// IE only code so the poor thing can cope
		animBlock.foreImg.style.filter = "alpha(opacity=" + animImg.opacity*100 + ")";
	}
	else {
		// code for normal correctly programmed browsers
		animBlock.foreImg.style.opacity = animImg.opacity;
	}
	if (animImg.opacity <= animImg.threshold) {
		// swap image order on page
		animBlock.foreImg.style.zIndex = 2;
		animBlock.backImg.style.zIndex = 3;
		//swap references
		var imgTemp = animBlock.foreImg;
		var imgTempIndex = animBlock.foreImgIndex
		animBlock.foreImg = animBlock.backImg;
		animBlock.foreImgIndex = animBlock.backImgIndex;
		animBlock.backImg = imgTemp;
		animBlock.backImgIndex = imgTempIndex;
		// reset opacity to 1 (actual image and our value in memory)
		if (animImg.isIE) {
			// IE only code so the poor thing can cope
			animBlock.backImg.style.filter = "alpha(opacity=100)";
		}
		else {
			// code for normal correctly programmed browsers
			animBlock.backImg.style.opacity = 1;
		}
		animImg.opacity = 1;
		// replace backImg with new image from pool
		var imgIndex = selectImgIndex();
		animBlock.backImg.src = imgArray[imgIndex].src;
		animBlock.backImgIndex = imgIndex;
		// reset animator
		animImg.running = false;
		animImg.leftAnim = !animImg.leftAnim;
	  clearInterval(timerHandle);
	}
}
