-- MyDisplaySplash function is copied from ScreenInit.lua and
-- have autoscale tag in Bitmap object set to false
function MyDisplaySplash( splashMovie, splashGraphic, time )
--	DebugOut("DisplaySplash "..tostring(splashMovie) );

	local id = PushModal("DisplaySplash");

	local translate = GetTag( {}, "translate" );
	if not translate then
		translate = false;
	end;

	local allowInput = GetTag( {}, "allowInput" );
	if not allowInput then
		allowInput = false;
	end;
--	DebugOut("DisplaySplash:StartMovie "..tostring(splashMovie) );
	inMovie = StartMovie(splashMovie, translate, allowInput);

	local endTime = GetTimer()+time;
	local disableAbort = GetTag( {}, "disableAbort" );
	if not disableAbort then
		disableAbort = false;
	end;

	if (inMovie == false) then
--		DebugOut("Displaying Graphic "..splashGraphic.." for "..time.." ms");
		local overlay = GetTag( {}, "splashoverlay" );
        if not overlay then overlay = Group{} ; end;
		if not splashGraphic then return ; end;

		MakeDialog
		{
			Bitmap
			{
				name="splashimage",
				image=splashGraphic,
				autoscale=false,
				overlay
			}
		}

		UpdateWindow();
		Pause(50);
		UpdateWindow();
		Pause(50);
	end

	if (inMovie) then
		time = 50;
	end

	if (inMovie == true or ImageLoaded("splashimage") == true) then
		while (true) do
			local message = WaitForMessage(1);	-- Get message pauses the script and waits for a message of some kind, or a delay

			if ( not inMovie and GetTimer()>endTime ) then
				DebugOut("timeout!");
				break;
			elseif message ~= nil then
				if ( message.type == kCloseWindow ) then
					DebugOut("message == kCloseWindow");
					break ;
				end

				if ( message.type == kModalClosed ) then
					if (message.name == id ) then
						DebugOut("message == kModalClosed");
						break ;
					else
						gClosedWindows[ message.name ] = true ;
					end
				end

				if ( disableAbort == false and message.type == kPressAnyKey ) then
					break ;
				end
			end

			if (inMovie == true) then
				if (IsMoviePlaying() == false) then
--					DebugOut("IsMoviePlaying() == false");
					break;
				end
			end
		end
	end

	if (inMovie == true) then
--		DebugOut("Stopping Movie");
		StopMovie();
	end

--	DebugOut("DisplaySplash Popmodal");
	PopModal(id);
--	DebugOut("DisplaySplash done");

	-- Will clip to actual screen coordinates
	--FillRect( {0,0,4096,4096}, FColor(0,0,0,0) );
end


-- Main game loop
function Main()

	LoaderReset();

	-- Queue up some assets to load
	Load("scripts/preload.loader.lua","coreassets");

	MyDisplaySplash("", "splash/logo_portal",4000);
	MyDisplaySplash("", "splash/logo_gamersdigital",4000);
	MyDisplaySplash("splash/gblogointro.swf","splash/gblogointro",4000);
	--DisplaySplash("","splash/gblogointro",4000);
	

	DoModal("scripts/firstload.lua");
	
	-- Push the game selection screen
	while true do
		DoMainWindow("scripts/mainmenu.lua");
		-- DoMainWindow will exit only if there are NO windows pushed on the stack, so
		-- a PopModal()/PushModal() combination will not cause this to loop.
	end
end

-- Return a function to be executed in a thread
return Main