//Copyright (C) Ben Parbury - All Rights Reserved. Unauthorized copying of this file, via any medium is strictly prohibited. Written by Ben Parbury <ben@gridlockinteractive.com> 2014

function Init()
{
	gConsole.Log(0, "[MAIN] Init()");

	gDraw.InitializeCanvases();

	var urlVars = getUrlVars();
	if(urlVars["scale"])
	{
		gDraw.Scale(urlVars["scale"]);
	}

	gDraw.DrawCallback = DrawGame;
	
	InitGame();
	
	gLevelSystem.SetLoadedCallback(GetPostInitCallback());
	gFonts.SetLoadedCallback(GetPostInitCallback());

	gLevelSystem.Init();
}

function PostInit()
{
	gConsole.Log(0, "[MAIN] PostInit()");

	PostInitGame();

	setInterval("Update()", 33);
	
	gLevelSystem.LoadedCallback = null;
}

function Update()
{
	UpdateGame();
	
	gDraw.Draw();	//DrawGame
}

var gPostInitExpectedCalls = 0;
var gPostInitCalls = 0;

function GetPostInitCallback()
{
	gPostInitExpectedCalls++
	return function()
		{
			gPostInitCalls++;
			if(gPostInitCalls == gPostInitExpectedCalls)
			{
					PostInit();
			}
		}
}