// alert("hi: " + games_total);
var current_image = 0;
var timerid = "";
var timer_length = 8000;

// populate the image listing
for (a = 0; a < gamesObject.info.length; a++) {
	$j('#image_list').append("\n<a href=\"/games/" + gamesObject.info[a].url + "\"><span id=\"gamespan" + gamesObject.info[a].ID + "\"><img src='" + gamesObject.info[a].icon + "' id='icon-" + gamesObject.info[a].ID + "' value='" +gamesObject.info[a].main + "' class='opacity25'></span></a>");
}

$j('img[id^=icon]').mouseover(function(e) { $j("#"+ e.target.id).addClass("opacity100"); }).mouseout(function(e) { id = icon_index(e.target.id); if (id != current_image) { $j('#'+e.target.id).removeClass("opacity100").addClass("opacity25"); } }).click(function(e) { ID = icon_index(e.target.id); icon_select(ID); return false; });

function icon_select(index) {
	if (index > (gamesObject.info.length - 1)) {
		index = 0;
	}
	clearTimeout(timerid);
	current_image = index;
	current_left = 0 - Math.floor(index / 16) * image_list_width;
	$j('#image_list').animate({ left: current_left + "px"}, "slow" ); 

	$j('img[id^=icon]').removeClass("opacity100").addClass("opacity25");
	$j('#icon-'+gamesObject.info[index].ID).removeClass("opacity25").addClass("opacity100");
	$j('span[id^=gamespan]').removeClass("highlight");
	$j('#gamespan'+gamesObject.info[index].ID).addClass("highlight");
	if (GAME_ID > 0 && index != icon_index("icon-" + GAME_ID)) {
		location.href("/games/" . gamesObject.info[index].url);
		return false;
	}

	$j('#main_image').css("background", "url("+gamesObject.info[index].main+")");
	$j('#goto-website').attr("href", "/games/" + gamesObject.info[index].url);

	// set the cookie for the currently selected icon
	document.cookie = "current_header="+escape(index) + ";expires=";

	if (GAME_ID == "") {
		timerid=setTimeout("icon_shift()", timer_length);
	}
	icon_arrows();
	return false;
}

function icon_arrows() {
	if (current_left < 0) {
		$j('#thumb-prev').removeClass("disabled");
	} else {
		$j('#thumb-prev').addClass("disabled");
	}

	sets = Math.ceil(gamesObject.info.length / 16);
	curset = Math.abs(current_left / image_list_width) + 1;
	if (curset < sets) {
		$j('#thumb-next').removeClass('disabled');
	} else {
		$j('#thumb-next').addClass('disabled');
	}
}

function icon_shift() {
	current_image++;
	if (current_image > (gamesObject.info.length - 1)) {
		current_image = 0;
	}
	icon_select(current_image);
}

function icon_index(imageid) {
	for (a = 0; a < gamesObject.info.length; a++) {
		if (("icon-" + gamesObject.info[a].ID) == imageid) {
			return a;
		}
	}
}

var current_left = 0;
var image_list_width = 912;
if (GAME_ID > 0) {
	icon_select(icon_index("icon-"+GAME_ID));
} else {
	var old_icon = pr_getCookie("current_header");
	if (old_icon != "") {
		icon_select(old_icon);
	} else {
		icon_select(0);
	}
}

function pr_getCookie(name) {
	var theCookies = document.cookie.split(/[; ]+/);
	for (var i = 0 ; i < theCookies.length; i++) {
		var aName = theCookies[i].substring(0,theCookies[i].indexOf('='));
		if (aName == name) {
			return theCookies[i].replace(/.*=/, "");
		}
	}
	return "";
}

$j('#thumb-next').mousedown(function() {
	current_left = current_left - image_list_width;
	sets = Math.ceil(gamesObject.info.length / 16);
	curset = (current_left / image_list_width);
	if (Math.abs(curset) >= sets) {
		current_left = image_list_width - (sets * image_list_width);
	}
	icon_arrows();
	$j('#image_list').animate({ left: current_left + "px"}, "slow" );
	return false;
});

$j('#thumb-prev').mousedown(function() {
	current_left = current_left + image_list_width;
	if (current_left > 0) {
		current_left = 0;
	}
	icon_arrows();
	$j('#image_list').animate({ left: current_left + "px"}, "slow" );
	return false;
});
