
function Lock(screen)
	SendMessage(screen, "template ZGlassTemplate")
	SendMessage(screen, "create top instance")
end

function Unlock(screen)
	assert(SendMessageReply(screen, "name of top") == "ZGlassTemplate")
	SendMessage(screen, "erase top child")
end

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

-- функция создания GameLogo (анимированного/неанимированного)
function InstantinateGameLogo(mutableReceiver, firstEnterInGame)
	local animLogo = PermanentStorage.GetGlobalBool("isGameLogoAnimated")
    if animLogo then
        SendMessage(mutableReceiver, "template GameLogoTemplate")
	else
	    SendMessage(mutableReceiver, "template NotAnimatedGameLogoTemplate")
	end
	
	local hat_texture = "loading_screen_zaglushka"
	
	local version = PermanentStorage.GetGlobalStringOrDef("temp:Ver", "Full")
	if version ~= "Full" then firstEnterInGame = false end
	
	local effTop, effBottom, tX, tY, bX, bY   =   "null", "null", 400, 400, 400, 400
	
    if not firstEnterInGame then
		local tankId   = "1"
		if PermanentStorage.GetGlobalBool("temp:ScreensaverMode") then
			local defaultTank = version
			if version == "Full" then
				defaultTank = "1"
			end
		    tankId = PermanentStorage.GetLocalStringOrDef("Screensaver Tank", defaultTank)
		else
			tankId = PermanentStorage.GetLocalString("Current Tank")
		end
       	local tankType = PermanentStorage.GetLocalString(tankId.." Tank Type")
		if     tankType == "1" then
			hat_texture = "loading_screen_riuska_helloween"
			effTop      = "Halloween"
			tX, tY      = 405, 484
		elseif tankType == "2" then
			hat_texture = "loading_screen_riuska_thanksgiving"
			effTop      = "Thanksgiving"
			tX, tY      = 793, 485
			effBottom   = "Thanksgiving_background"
			bX, bY      = 800, 500
		elseif tankType == "3" then
			hat_texture = "loading_screen_riuska_christmas"
			effTop      = "Christmas"
			tX, tY      = 15, 582
			effBottom   = "Christmas_background"
			bX, bY      = 400, 600
		else assert(false)
		end
    end
    if animLogo then
	    SendMessage(mutableReceiver, "defineText [effectNameTop] "..effTop)
	    SendMessage(mutableReceiver, "defineText [tX] "..tX)
	    SendMessage(mutableReceiver, "defineText [tY] "..tY)
	    SendMessage(mutableReceiver, "defineText [effectNameBottom] "..effBottom)
	    SendMessage(mutableReceiver, "defineText [bX] "..bX)
	    SendMessage(mutableReceiver, "defineText [bY] "..bY)
	end
    SendMessage(mutableReceiver, "defineText [hat_texture] "..hat_texture)
    
	if (version == "1") then
		SendMessage(mutableReceiver, "defineText [loading_screen_title] loading_screen_title_halloween")
	elseif (version == "2") then
		SendMessage(mutableReceiver, "defineText [loading_screen_title] loading_screen_title_thanksgiving")
	elseif (version == "3") then
		SendMessage(mutableReceiver, "defineText [loading_screen_title] loading_screen_title_christmas")
	else
		SendMessage(mutableReceiver, "defineText [loading_screen_title] loading_screen_title")
	end
	local state_logo = ""
    if mutableReceiver == "GameLogoLoadScreen" then state_logo = "load screen"
    elseif mutableReceiver == "Current tank content" then state_logo = ""
    else assert(false)
	end
	SendMessage(mutableReceiver, "defineText [state_logo] "..state_logo) --определили где мы, в экарне загрузки или в меню

	SendMessage(mutableReceiver, "create top instance")
end

function getCurrentTankType()
	local ver = PermanentStorage.GetGlobalStringOrDef("temp:Ver", "Full")
	local defaultTank = ver
	if ver == "Full" then
		defaultTank = "1"
	end
	return PermanentStorage.GetLocalString(PermanentStorage.GetLocalStringOrDef("Current Tank", defaultTank).." Tank Type")
end

function SwitchTrack(mode)
    if PermanentStorage.GetGlobalBool("temp:ScreensaverMode") then
		local screensaverTankType = PermanentStorage.GetLocalString(PermanentStorage.GetLocalStringOrDef("Screensaver Tank", "1").." Tank Type")
		Sound:PlayTrack("Menu"..screensaverTankType, true)
    else
		if mode == "Match3" then
		    Sound:FadeOutTrack(PermanentStorage.GetGlobalString("temp:current track"), 1.0)

			local res = PermanentStorage.GetLocalString("currentgamebackground")
			local track = res..PermanentStorage.GetLocalIntOrDef("Last track in game", 1)

			Sound:FadeInTrack(track, 2.0, true)
			PermanentStorage.SetGlobalString("temp:current track", track)

			if PermanentStorage.GetLocalInt("Last track in game") == 1 then
				PermanentStorage.SetLocalInt("Last track in game", 2)
			else
				PermanentStorage.SetLocalInt("Last track in game", 1)
			end
		elseif mode == "Intro" then
		    Sound:FadeOutTrack(PermanentStorage.GetGlobalString("temp:current track"), 1.0)
		
			local track = "zastavka"

			Sound:FadeInTrack(track, 2.0, true)
			PermanentStorage.SetGlobalString("temp:current track", track)

		else
			if not PermanentStorage.IsGlobalNameDefined("temp:current track") then
			    local track = "Menu"..getCurrentTankType()
			    Sound:PlayTrack(track, true)
			    PermanentStorage.SetGlobalString("temp:current track", track)
			else
			    local cur_track = PermanentStorage.GetGlobalString("temp:current track")
			    local new_track = "Menu"..getCurrentTankType()
			    if cur_track ~= new_track then
				    Sound:FadeOutTrack(cur_track, 1.0)
				    Sound:FadeInTrack(new_track, 2.0, true)
				    PermanentStorage.SetGlobalString("temp:current track", new_track)
				end
			end
		end
	end
end