/*
	JUUCE Preloading Gallery With Fading
	
	Significant testing in Internet Explorer 6.0.2900, Firefox 2.0.0.12, Opera 9.24 and Safari 3.0.4
	
	IE6 seems to have problems clearing timeouts sometimes. I've tried to write it so that timeouts that fire when they're not supposed to
	should not cause any permanent problems to the runtime.
	
	Firefox does not fade very smoothly. Their image renderer is easily the slowest out of the major browsers. Opera is smoother even though it
	uses bilinear filtering. Firefox 3 should be screamingly fast though, when it finally turns up.
	
	Preloader changed appears to work fine in all browsers.
*/

var idArray = new Array();
var imageArray = new Array();
var loadedArray = new Array();
var currentID;
var currentIndex;
var opacity = 0;
var switching = 0;
var fadeInAuto = 0;
var fadeOutAuto = 0;
var switchAuto = 0;
var autoSwitch = 0;
var currentLoaded = 0;
var bigImageNr = 0;

function addImage(id, src) {
	idArray.push(id);
	imageArray.push(src);
	loadedArray.push(0);
}

function switchImage(index) {
	if ((opacity <= 0) && (currentLoaded == 1)) {
		document.getElementById('bigImage'+bigImageNr).src = imageArray[index];
		clearTimeout(fadeOutAuto);
		fadeOutAuto = 0;
		switchFadeUp();
	} else if (opacity <= 0) {
		document.getElementById('bigImage'+bigImageNr).src = imageArray[index];
		document.getElementById('bigImage'+bigImageNr).onload = function () {
			currentLoaded = 1;
		}
		switchAuto = setTimeout('switchImage('+index+')', 100);
	} else {
		switchAuto = setTimeout('switchImage('+index+')', 100);
	}
}

function setFade(id, amount) {
	if (amount > 100) {
		amount = 100;
	}
	if (amount < 0) {
		amount = 0;
	}
	document.getElementById(id).style.opacity = amount/100;
	document.getElementById(id).style.MozOpacity = amount/100;
	document.getElementById(id).style.KhtmlOpacity = amount/100;
	document.getElementById(id).style.filter = 'alpha(opacity='+amount+')';
}

function switchFadeOut() {
	if (fadeInAuto == 0) {
		fadeOutAuto = 0;
		clearTimeout(fadeInAuto);
		fadeInAuto = 0;
		if (opacity > 0) {
			opacity -= 2;
			if (opacity < 0) {
				opacity = 0;
			}
			setFade('bigImage'+bigImageNr, opacity);
			fadeOutAuto = setTimeout('switchFadeOut()', 10);
		} else {
			opacity = 0;
		}
	}
}

function switchFadeUp() {
	fadeInAuto = 0;
	clearTimeout(fadeOutAuto);
	fadeOutAuto = 0;
	if (opacity < 100) {
		opacity += 2;
		if (opacity > 100) {
			opacity = 100;
			switching = 0;
		}
		setFade('bigImage'+bigImageNr, opacity);
		fadeInAuto = setTimeout('switchFadeUp()', 10);
	} else {
		opacity = 100;
		switching = 0;
		bigImageNr++;
		if (bigImageNr >= 3) {
			bigImageNr = 0;
		}
	}
}

function selectImage(id) {
	clearTimeout(autoSwitch);
	autoSwitch = 0;
	selectImaged(id);
}

function selectImageAuto() {
	if ((switching == 0) || (opacity == 0)) {
		currentIndex++;
		if (currentIndex >= idArray.length) {
			currentIndex = 0;
		}
		selectImaged(idArray[currentIndex]);
	}
	autoSwitch = setTimeout('selectImageAuto()', 2500);
}

function selectImaged(id) {
	if ((id != currentID)) {
		clearTimeout(fadeInAuto);
		clearTimeout(fadeOutAuto);
		clearTimeout(switchAuto);
		fadeInAuto = 0;
		fadeOutAuto = 0;
		switchAuto = 0;
		switching = 1;
		currentID = id;
		var index;
		var src = '';
		var loaded = 0;
		for (var i=0; i < idArray.length;i++) {
			if (idArray[i] == id) {
				index = i;
				src = imageArray[i];
				loaded = loadedArray[i];
				if (loadedArray[i] == 0) {
					loadedArray[i] = 1;
				}
			}
		}
		currentLoaded = 0;
		currentIndex = index;
		if (loaded == 0) {
			if (src != '') {
				var image = new Image();
				image.src=src;
			}
		}
		switchImage(index);
		if (opacity > 0) {
			switchFadeOut();
		} else {
			switchFadeUp();
		}
	}
}

function pickRandom() {
	currentIndex = Math.floor(Math.random()*idArray.length);
	selectImageAuto();
}