var backgrounds = new Array();
var preloads = new Array();
$(function() {
	$.post("/etusivu/getbackgrounds", {}, function(data) {
		for (var i in data) {
			backgrounds[i] = data[i];
			// comment the two following to stop preloading
			preloads[i] = new Image();
			preloads[i].src = data[i];
		}
		$(".eskoboxes li a:not(.eskoboxes li:first-child a):not(.eskoboxes li:last-child a)").mouseover(function() {
			var pageId = findPageId($(this).attr("class"));
			$(this).attr("title", $(this).html());
			$(this).html("");
			$(this).parent().parent("li").css("background-image", "url('"+backgrounds[pageId]+"')");		
		});
		$(".eskoboxes li a:not(.eskoboxes li:first-child a):not(.eskoboxes li:last-child a)").mouseout(function() {
			var pageId = findPageId($(this).attr("class"));
			$(this).html($(this).attr("title"));
			$(this).parent().parent("li").css("background-image", "none");
		});
	}, "json");
});

function findPageId(str) {
	var classes = str.split(" ");
	for (var i=0 ; i<classes.length ; i++) {
		if (classes[i].indexOf("page-") != -1) {
			return classes[i].replace(/page-/, "");
		}
	}
	return 0;
}

