$(document).ready(function() {
	// Start updating pl credits
	if (global.bIsPlLogged) {
		$("#spanShowCredits").html("Loading...");
		GetPlFreePoints(OnGetCredits);
	}
	// Start getting jps
	if (global.bJpInfo) {
		self.setInterval(function() {
			GetJpInfos(global.idCurrency, OnGetJpInfos);
		}, global.tsJp);
	}
});

var plCreditsTimer = -1;

function OnGetCredits(credits) {
	if (credits != "") {
		// Successfull call, continue getting credits
		if (plCreditsTimer == -1) {
			// First call, start timer
			plCreditsTimer = self.setInterval(function() {
			    GetPlFreePoints(OnGetCredits); 
			}, global.tsMoney);
		}
		// Update money
		$("#spanShowCredits").html(credits);
	} else {
		// Stop timer
		if (plCreditsTimer != -1) {
			window.clearInterval(plCreditsTimer);
		}
		$("#spanShowCredits").html("N/A");
	}
}

function OnGetJpInfos(idJp, idPackage, idCurrency, amount) {
	var movie = GetMovie(GetJackpotMovieName(idJp, idPackage));
	if (movie) {
		movie.setValue(idCurrency, amount);
	}
}

function LoadJackpotMovie(idJp, idPackage, color, value, currency) {
	id = GetJackpotMovieName(idJp, idPackage);
	
	AC_FL_RunContent(
		'codebase', GetHttpOrHttps() + '://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,0,0',
		'width', '100',
		'height', '20',
		'src', global.siteLocalURL + 'WebPageJP?color=' + color + '&value=' + value + '&currency=' + currency,
		'quality', 'high',
		'pluginspage', 'http://www.macromedia.com/go/getflashplayer',
		'align', 'middle',
		'play', 'true',
		'loop', 'true',
		'scale', 'showall',
		'wmode', 'Transparent',
		'devicefont', 'false',
		'id', id,
		'bgcolor', '#ffffff',
		'name', id,
		'menu', 'true',
		'allowFullScreen', 'false',
		'allowScriptAccess', 'allowAll',
		'movie', global.siteLocalURL + 'WebPageJp?color=' + color + '&value=' + value + '&currency=' + currency,
		'salign', '');

	// Save object
	window[id] = document.getElementById(id);
}

function GetJackpotMovieName(idJp, idPackage) {
	return "jackpot_" + idJp + "_" + idPackage;
}

function over_menu(image) {
	image.src = image.src.substr(0, image.src.length - 6) + "Ov.gif";
	return true;
}

function out_menu(image) {
	image.src = image.src.substr(0, image.src.length - 6) + "Ot.gif";
	return true;
}

function addToFavorites() {
	AddToFav();
}

function UpdateLast5Played() {
	if (global.bIsPlLogged) {
		var cookies = document.cookie;

		if (cookies.indexOf(global.Latest5Cookie) != -1) {

			var startpos = cookies.indexOf(global.Latest5Cookie) + global.Latest5Cookie.length + 1;
			var endpos = cookies.indexOf(";", startpos) - 1;

			if (endpos == -2) endpos = cookies.length;

			var games = unescape(cookies.substring(startpos, endpos)).split(",");

			$("#recentGames").html("");
			
			$.each(eval(games), function(i, el) {
				$("#recentGames").append("<tr><td>" + $("#" + el).parent().html() + "</td></tr>");
			});
		}
	}
}