// JavaScript Document

// Turn on image caching for IE6 - stops annoying flicker when we fade in / out
try { 
	document.execCommand('BackgroundImageCache', false, true); 
} catch(e) {} 


var i = 0;
var rotateSpeed = 5; // Seconds
var fadeSpeed = 0.5; // Dont go less than 0.5 because IE will choke

var homeFlashArray = new Array("one", "two", "three", "four", "eight");

function initRotator() {
	for(i = 0; i < homeFlashArray.length; i++) {
		document.getElementById(homeFlashArray[i]).style.display = "none";
		setOpacity(document.getElementById(homeFlashArray[i]), 0);
	}

	setOpacity(document.getElementById(homeFlashArray[0]), 10);
	document.getElementById(homeFlashArray[0]).style.display = "block";
	i = 0;
	showTimeout = setTimeout(function() { showFlash(); }, rotateSpeed * 1000);
}

function fadeOutListen() {
	currentBox = document.getElementById(homeFlashArray[i]);
	if(eval(currentBox.style.opacity) == 0) {
		currentBox.style.display = "none";

		i = i + 1;
		if(i >= homeFlashArray.length) i = 0;
		currentBox = document.getElementById(homeFlashArray[i]);
	
		// Fade in the next box
		fadeInInterval = setInterval(function() { fadeIn(currentBox); }, fadeSpeed * 100);
		currentBox.style.display = "block";
		fadeInListener = setInterval(function() { fadeInListen(); }, 10); // Wait until the box has completely faded in

		clearInterval(fadeOutInterval);
		clearInterval(fadeOutListener);
	}
}

function fadeInListen() {
	currentBox = document.getElementById(homeFlashArray[i]);
	if(eval(currentBox.style.opacity) == 1) {
		clearInterval(fadeInInterval);
		clearInterval(fadeInListener);
		showTimeout = setTimeout(function() { showFlash(); }, rotateSpeed * 1000);
	}
}

function showFlash() {
	currentBox = document.getElementById(homeFlashArray[i]);
	fadeOutInterval = setInterval(function() { fadeOut(currentBox); }, fadeSpeed * 100); // Fade out the currently displayed box
	fadeOutListener = setInterval(function() { fadeOutListen(); }, 10); // Wait until the box has completely faded out
	clearTimeout(showTimeout);
}

function setOpacity(element, value) {
	element.style.opacity = value / 10;
	element.style.filter = 'alpha(opacity=' + value * 10 + ')';
}

function fadeOut(element) {
	opacity = eval(element.style.opacity);
	opacity = opacity - 0.1;
	opacity = opacity.toFixed(1);
	setOpacity(element, opacity * 10);
}

function fadeIn(element) {
	opacity = eval(element.style.opacity);
	opacity = opacity + 0.1;
	opacity = opacity.toFixed(1);
	setOpacity(element, opacity * 10);
}

function toggleBox(elementID) {
	boxElement = document.getElementById('box' + elementID);
	linkElement = document.getElementById('moreInfo' + elementID);

	if(boxElement.style.display == "none") {
		boxElement.style.display = "block";
		linkElement.innerHTML = "Less Info....";
	} else {
		boxElement.style.display = "none";
		linkElement.innerHTML = "More Info....";
	}
}
