function LockScreen()
	SendMessage("GameScreen", "template ZGlassTemplate")
	SendMessage("GameScreen", "create top instance")
end

function UnlockScreen()
	assert(SendMessageReply("GameScreen", "name of top") == "ZGlassTemplate")
	SendMessage("post:GameScreen", "erase top child")
end

function Instantiate(screen, template, order)
	SendMessage(screen, "template "..template)
	if top == nil then
		SendMessage(screen, "create top instance")
	else
		SendMessage(screen, "create instance before "..order)
	end
end

function CreateLoadScreen() -- создание "окна загрузки"
	if PermanentStorage.GetGlobalBool("Ask player name") then
		Instantiate("GameScreen", "IntroEnterEffectMainTemplate")
		SendMessage("End Intro", "run")
		Instantiate("EffectGameScreen", "LoadingTemplate")
		Instantiate("EffectGameScreen", "LoadingLogoTemplate")
	else
		Instantiate("GameScreen", "LoadingTemplate")
		Instantiate("GameScreen", "LoadingLogoTemplate")
	end 
	if PermanentStorage.GetGlobalBool("temp:ScreensaverMode") then
		SendMessage("Startup logo alpha changer", "change alpha to 1.0")
	end
	LockScreen()--кладем сверху слой, блокирующий ввод пользователя
	PermanentStorage.SetLocalBool("temp:Startup screen loaded", true)
end

local loadingTime = TimeCounter("Loading time = ")
LoadResource("fonts.xml")
LoadText("Language.xml")
if PermanentStorage.GetGlobalBool("temp:screensaverLogo") then
	LoadResource("../screenbranding/advertisement/logo.xml")
end
LoadTemplate("StartupTemplate.xml")
LoadResource("StartupResources.xml")
LoadControlTree("Startup.xml")

CreateLoadScreen()

do
	local loadingTime = TimeCounter("Lua loading time = ")
	require "Common.lua"
	require "Game.lua"
	require "MainMenu.lua"
	require "MainScreen.lua"
	require "MainScreenAndShop.lua"
	require "SpawnScreen.lua"
	require "ExtrasScreen.lua"
	require "Shop.lua"
	require "ChooseTank.lua"
	require "GameField.lua"
	require "Tutorial.lua"
	require "Help.lua"
	require "GameFunctions.lua"
	require "SendMail.lua"
	loadingTime:WriteToLog()
end

do
	local loadingTime = TimeCounter("Music and Sound loading time = ")
	LoadResource("Music!.xml")
	if (PermanentStorage.IsLocalNameDefined("Music Volume")) then
		Sound:SetMusicVolume(PermanentStorage.GetLocalFloat("Music Volume") * 100)
	else
		Sound:SetMusicVolume(PermanentStorage.GetGlobalFloat("Music Volume") * 100)
	end
	
	if PermanentStorage.GetGlobalBool("temp:ScreensaverMode") then
		if PermanentStorage.GetGlobalBoolOrDef("isScreensaverMute", false) then
			PermanentStorage.SetLocalFloat("Music Volume", 0)
			PermanentStorage.SetLocalFloat("Sound Volume", 0)
			Sound:SetMusicVolume(0)
		end
	end
	
	LoadResource("Sound.xml")
	loadingTime:WriteToLog()
end

local currentTank = PermanentStorage.GetLocalStringOrDef("Current Tank", "1");
if tonumber(currentTank) > 3 then
	currentTank = PermanentStorage.GetLocalString(currentTank.." Tank Type");
end
Sound:FadeInTrack("Menu"..currentTank, 2.0, true)

do
	local loadingTime = TimeCounter("Resources loading time = ")
	if PermanentStorage.GetGlobalBool("temp:advertisement") then
		LoadTemplate("../screenbranding/advertisement/templates.xml")
		LoadResource("../screenbranding/advertisement/ad.xml")
	end
	LoadResource("LocalResources.xml")
	LoadResource("GUI_Resources.xml")
	LoadResource("FishResources.xml")
	LoadResource("SrResources.xml")
	LoadResource("BackgroundResources.xml")
	LoadResource("FishipediaResources.xml")
	LoadTemplate("CommonDialogs.xml")
	LoadTemplate("FishInstances.xml")
	LoadTemplate("BeautyInstances.xml")
	LoadTemplate("ComfortsTemplate.xml" )
	LoadTemplate("TankBackground.xml" )
	LoadTemplate("AnyShop.xml")
	LoadTemplate("GameBackgroundsGui.xml")
	LoadTemplate("MainScreen.xml")
	LoadTemplate("TankOnly.xml")
	LoadTemplate("OptionsDialog.xml")
	LoadTemplate("Credits.xml")
	LoadTemplate("PlayersDialog.xml")
	LoadTemplate("Intro.xml")
	LoadTemplate("ScreensaverOptionsDialog.xml")
	LoadTemplate("ChooseTank.xml")
	LoadTemplate("HintTextTemplates.xml")
	LoadTemplate("GameField.xml")
	LoadTemplate("Help.xml")
	LoadTemplate("PathTemplates.xml")
	LoadTemplate("TooltipTemplates.xml")
	LoadTemplate("MainMenu.xml")
	LoadTemplate("TemplatesLib.xml")
	LoadTemplate("GUI.xml")
	LoadTemplate("SpawnScreen.xml")
	LoadTemplate("ExtrasScreen.xml")
	LoadTemplate("Fishipedia.xml")
	loadingTime:WriteToLog()
end

do
	local loadingTime = TimeCounter("Effect loading time = ")
	LoadEffects("effects/GameBackgrounds.xml")
	LoadEffects("effects/ComfortEffect3.xml")
	require "plstart.lua"
	LoadEffects("effects\\LockEffect.xml")
	LoadEffects("effects\\MetersEffects.xml")
	loadingTime:WriteToLog()
end


GameFunc("XML loaded")

loadingTime:WriteToLog()

