/* Now with 50% less rubbish code */

var games; // Nice global array for storing our games
var current = 1;
var index = 0; // For checking what picture we're up to

var pic1 = new Image(585,220); // Preload funsies
var pic2 = new Image(585,220);

var background = "background2";
var foreground = "background"; // This continues to be fun

function preloadImages(){
	pic1.src = pic2.src;
	if(current+1==games.length) current = -1;
	pic2.src = "/data/games/"+games[current+1]+"/splash/splash.jpg";
}

function homeInit(gamesarg) {
	games = gamesarg;
	pic2.src = "/data/games/"+games[current]+"/splash/splash.jpg";
	preloadImages();
	setTimeout('nextImage();', 7000);
	return;
}

function nextImage(){
	$(background).src = pic1.src;
	// I'm fed up of following the numbering logic; this is easier
	// (provided you can read regex ;)
	$(background+"l").href = "?game="+($(background).src.replace(/.*data\/games\/(.+)\/splash\/splash\.jpg/, "$1"));
	$(foreground).fade({ afterFinish: onFadeFinish });
	swapLayers();
	current++;
	preloadImages();
	// wrapping
	if(++index == 4)
		index = 0;
}

function swapLayers(){
	var holder = background;
	background = foreground;
	foreground = holder;
}
function onFadeFinish (){
	$(foreground).setStyle({ zIndex: 1 });
	$(background).setStyle({ zIndex: 0, display: 'inline'});
	setTimeout('nextImage();', 6000);
}