//alert ("Loading AIgamez.js");
//===========================================================================
var openTime = 0;	// Deprecated. See $AIG.uiScriptStartTime
// Deprecated.
function openFor() {
	return new Date().getTime() - openTime;
	}
var $Libs = {
	sDebug : ""
	};
//===========================================================================
var $AIG = {
	uiScriptStartTime	: 0,						// Initialised when body.onload is called.
	sImagesUrl	: "http://i.aigamez.net/"	// May be overriden in the page script.
	};

//===========================================================================
var $Memory = {
	sTopic		: "",			// Client-side definition. The game name or the 
								// page name, etc. Typically set once for the 
								// page and the parameter used for exceptions. 
	sLoadIn		: "*",			// Server-side definition. The pages in which to 
								// preload the saved value.
	fnOnSaved	: function(){},	// No action required. Could implement automatic retry.
	tDebug		: false			// True if to report actions in the debug log.
	};

//===========================================================================
// Shorthand methods for accessing the page-loaded memory 
// values without having to use the topic or do conversions.
//
$Memory.sVal = function (sVar) {return sVal ($Memory [this.sTopic + sVar]);}
$Memory.iVal = function (sVar) {return iVal ($Memory [this.sTopic + sVar]);}
$Memory.fVal = function (sVar) {return fVal ($Memory [this.sTopic + sVar]);}
$Memory.tVal = function (sVar) {return tVal ($Memory [this.sTopic + sVar]);}

//===========================================================================
$Memory.Save = function (sVar, sValue, sTopic /*Optional*/)
	{
	sTopic = sVal (sTopic, this.sTopic);
	x_ajaxRequest (".save", sTopic + sVar, sValue, this.sLoadIn, this.fnOnSaved);
	if (this.tDebug)
		$DA ("$Memory [" + sTopic + sVar + "] := [" + sValue + "]");
	}

//===========================================================================
$Memory.Load = function (sVar, fnOnLoaded, sTopic /*Optional*/)
	{
	sTopic = sVal (sTopic, this.sTopic);
	x_ajaxRequest (".load", sTopic + sVar, fnOnLoaded);
	if (this.tDebug)
		$DA ("Load $Memory [" + sTopic + sVar + "]");
	}

//===========================================================================
$Memory.Delete = function (sVar, fnOnDeleted, sTopic /*Optional*/)
	{
	sTopic = sVal (sTopic, this.sTopic);
	x_ajaxRequest (".load", sTopic + sVar, fnOnDeleted);
	if (this.tDebug)
		$DA ("$Memory [" + sTopic + sVar + "] deleted");
	}

//===========================================================================
function startClock()
	{
	$AIG.uiScriptStartTime = new Date().getTime();

	setInterval ("requestMenuCounts()", 60000);

	dynamicStart(); // Server-defined initialisation.

	if (window.Init)
		Init();		// Client-side (page or game) initialisation.
	}

//===========================================================================
function requestMenuCounts()
	{
	x_ajaxRequest (".menunumbers", updateMenuCounts);
	}

//===========================================================================
function updateMenuCounts (oCounters)
	{
	var asCountIds = ["onlineUsers", "board1", "board2", "board3", "picapproval"];
	for (var i = 0; i < asCountIds.length; i++)
		{
		var uiNumItems = oCounters [asCountIds [i]];
		if (uiNumItems > 0)
			{
			var spnCounter = document.getElementById (asCountIds [i]);
			if (spnCounter)
				spnCounter.innerHTML = '(' + uiNumItems + ')';
			}
		}
	}

//===========================================================================
function reloadPage()
	{
	uiTimeSinceStart = new Date().getTime() - $AIG.uiScriptStartTime;
	if (uiTimeSinceStart < 1500)
		return setTimeout (reloadPage, 1505 - uiTimeSinceStart);

	window.location.href = $AIG.sCurrentPage;
	}

//===========================================================================
function ShowCountdown (uiServerTimeNowSecs, uiServerEndTimeSecs, sClockSpanId)
	{
	if (uiServerTimeNowSecs)
		{
		var iTimeDiff = new Date().getTime() - uiServerTimeNowSecs * 1000;
		ShowCountdown.uiEndTime = uiServerEndTimeSecs * 1000 + iTimeDiff;
		ShowCountdown.spnClock  = document.getElementById (sClockSpanId);
		}

	if (!ShowCountdown.spnClock)
		return; // The clock isn't in use.

	var uiTicks = ShowCountdown.uiEndTime - new Date().getTime();
	var sTime   = new Date (uiTicks + 500).format ("MM:SS");

	ShowCountdown.spnClock.innerHTML = ''
		+ '<span id="spnHHMM">'
		+ (uiTicks / 1000 / 3600).int() // Hours
		+ '<b>:</b>' + sTime.substr (0, 2) + '</span>' 
		+ '<span id="spnSecs"><b>:</b>' + sTime.substr (3) + '</span>';

	// Set the timer so that the seconds are always in units of 5.
	setTimeout ("ShowCountdown()", uiTicks % 5000);
	// Warning: Has to be a function call in quotes because with just 
	// the function name there Fartfox calls it with a random value
	// as the first argument!
	}

//=====================================================================================
//
function LoadScript (sId, sUrl)
	{
	var elScript  = document.createElement ("SCRIPT");
	elScript.id   = sId;
	elScript.type = "text/javascript";
	elScript.src  = sUrl;

	var oHead = document.getElementsByTagName ("HEAD") [0];
	oHead.appendChild (elScript);
	}

LoadScript ("jsLibBase", "scripts/Lib/LibBase.js");
LoadScript ("jsADNS", "scripts/Lib/ADNS.js");

//===========================================================================
//alert ("AIgamez.js loaded");