function WriteToLogIfBetaLog(message)
	if PermanentStorage.GetGlobalBoolOrDef("betaLog", false) then
		WriteToLog(message)
	end
end

function GameFunc(message)
	if message == "XML loaded" then
		if PermanentStorage.GetGlobalBool("temp:ScreensaverMode") then
			SendMessage("Screen", "clear children list")
			SendMessage("Screen", "template TankOnlyTemplate")
			--SendMessage("Screen", "defineText [variable] Screensaver Tank")
			SendMessage("Screen", "defineText [exitMessageReceiver] post:lua:GameFunc")
			SendMessage("Screen", "defineText [exitMessage] Screen saver done")
			SendMessage("Screen", "create top instance")
			Sound:FadeInTrack("Menu"..PermanentStorage.GetLocalStringOrDef("Screensaver Tank", "1"), 3, true)
		else
			SendMessage("Screen", "clear children list")
			SendMessage("Screen", "template ClickToStartTemplate")
			SendMessage("Screen", "defineText [onClickMessage] Start game")
			SendMessage("Screen", "create top instance")
			SendMessage("Startup progress mover", "report arrival to lua:GameFunc")
			SendMessage("Startup progress mover", "set arrival message Startup progress bar out")
			SendMessage("Startup progress mover", "move to out")
			--Sound:PlaySample("window_moving")
			Sound:PlaySample("fishdom_load_sound")
		end
	elseif message == "Screen saver done" then
		CloseApplication()
	elseif message == "Startup progress bar out" then
		--Sound:FadeInTrack("Menu"..PermanentStorage.GetLocalStringOrDef("Current Tank", "1"), 3, true)
		if not ignoreStartupProgressBarOut then
			SendMessage("Startup click to start mover", "move to in")
		end
	elseif message == "Start intro key pressed" then
		if PermanentStorage.GetGlobalBoolOrDef("dev", true) then
			SendMessage("lua:GameFunc", "Start intro")
		end
	elseif message == "Start intro" then
		SendMessage("Screen", "clear children list")
		SendMessage("Screen", "template IntroTemplate")
		SendMessage("Screen", "create top instance")
		SendMessage("last intro action", "set delay 2.0")
		SendMessage("last intro action", "set message on post:lua:GameFunc/Exit intro")
		SendMessage("last intro action", "report to Click to start sensor")
		SendMessage("Screen", "template BrighteningTemplate")
		SendMessage("Screen", "create top instance")
		Sound:FadeOutTrack("Menu"..PermanentStorage.GetLocalString("Current Tank"), 1)
		Sound:FadeInTrack("Intro", 2, true)
	elseif message == "Exit intro" then
		SendMessage("view port moving", "move to in")
		SendMessage("Zooming little viewport", "change zoom to original")
		SendMessage("last text ramka", "change alpha to 0.0")
		SendMessage("Build up your Fishdom", "change alpha to 0.0")
		SendMessage("last intro action", "set delay 0.5")
		SendMessage("last intro action", "set message Click to start main menu")
		SendMessage("last intro action", "report to post:lua:GameFunc")
		PermanentStorage.SetLocalBool("black screen", false)
		Sound:PlaySample("intro_last_screen_zoom")
		Sound:FadeOutTrack("Intro", 1)
		Sound:FadeInTrack("Menu"..PermanentStorage.GetLocalString("Current Tank"), 3, true)
	elseif message == "Start game" then
		SendMessage("Screen", "template DarkeningTemplate")
		SendMessage("Screen", "defineText [receiver] post:lua:GameFunc")
		SendMessage("Screen", "defineText [message] Click to start main menu")
		SendMessage("Screen", "create top instance")
		PermanentStorage.SetLocalBool("black screen", true)
	elseif message == "Click to start main menu" then
		ignoreStartupProgressBarOut = true
		SendMessage("Screen", "clear children list")
		ignoreStartupProgressBarOut = nil
		if PermanentStorage.GetGlobalBoolOrDef("Ask player name", true) then
			SendMessage("lua:MainMenuFunc", "Create main menu")
			SendMessage("lua:GameFunc", "Ask first player name")
		else
			SendMessage("lua:MainMenuFunc", "Enter main menu")
		end
		if PermanentStorage.GetLocalBool("black screen") then
			SendMessage("Screen", "template BrighteningTemplate")
			SendMessage("Screen", "create top instance")
		end
	elseif message == "Ask first player name" then
		SendMessage("Main menu modal dialog", "instantiate FirstPlayerNameEditTemplate")
		SendMessage("Player name mover", "move to in")
	elseif message == "Enter your name ok pressed" then
		local newName = SendMessageReply("Player name edit", "get value")
		while string.len(newName) > 0 and string.find(newName, "%s") == 1 do 
			newName = string.sub(newName, 2);
		end
		if string.len(newName) > 0 then
			local oldName = PermanentStorage.GetActivePlayerName()
			if oldName == newName then
				PermanentStorage.RenamePlayer(oldName, "["..oldName.."]")
			end
			PermanentStorage.SetGlobalBool("Ask player name", false)
			PermanentStorage.CreatePlayer(newName)
			PermanentStorage.DeleteInactivePlayers()
			PermanentStorage.SetLocalString("Current Tank", "1")
			-- установка дефолтовых названий аквариумов
			local tank1name = GetResourceString("Tank1DefaultName")
			local tank2name = GetResourceString("Tank2DefaultName")
			local tank3name = GetResourceString("Tank3DefaultName")
			PermanentStorage.SetLocalString("Tank 1 name", tank1name)
			PermanentStorage.SetLocalString("Tank 2 name", tank2name)
			PermanentStorage.SetLocalString("Tank 3 name", tank3name)
			PermanentStorage.SetLocalString("Tank name", tank1name)
			Sound:FadeInTrack("Menu"..PermanentStorage.GetLocalString("Current Tank"), 1.0, true)
			-- if PermanentStorage.GetGlobalBoolOrDef("Ask relax mode", false) then
			SendMessage("Player name mover", "report arrival to lua:ChoiseRelaxOrTimedFunc")
			SendMessage("Player name mover", "set arrival message player name moved out")
			SendMessage("Player name mover", "move to out")
			SendMessage("Edit name glass", "turn on")
		end
	elseif message == "new player created" then
		SendMessage("Player name mover", "report arrival to lua:ChoiseRelaxOrTimedFunc")
		SendMessage("Player name mover", "set arrival message player name moved out")
		SendMessage("Player name mover", "move to out")
		Sound:PlaySample("window_moving")
	else
		assert(false, "Unknown message "..message)
	end
end

function ChoiseRelaxOrTimedFunc(message)
	if message == "player name moved out" then
		SendMessage("ChangeRelaxOrTimedHolder", "template ChoiseRelaxOrTimedTemplate")
		SendMessage("ChangeRelaxOrTimedHolder", "create top instance")
		SendMessage("choise relax or timed mover", "move to in")
		Sound:PlaySample("window_moving")
	elseif message == "Relaxed pressed" then
		PermanentStorage.SetLocalBool("IsRelaxedMode", true);
		SendMessage("choise relax or timed mover", "report arrival to lua:ChoiseRelaxOrTimedFunc")
		SendMessage("choise relax or timed mover", "set arrival message choose relax or timed moved out")
		SendMessage("choise relax or timed mover", "move to out")
		Sound:PlaySample("window_moving")
		if PermanentStorage.GetGlobalBoolOrDef("Enter main menu at once", true) then
			PermanentStorage.SetGlobalBool("Enter main menu at once", false)
		--	SendMessage("post:lua:MainMenuFunc", "Enter main menu")
		end
		WriteToLogIfBetaLog("Default Mode - Relax");
	elseif message == "Timed pressed" then
		PermanentStorage.SetLocalBool("IsRelaxedMode", false);
		SendMessage("choise relax or timed mover", "report arrival to lua:ChoiseRelaxOrTimedFunc")
		SendMessage("choise relax or timed mover", "set arrival message choose relax or timed moved out")
		SendMessage("choise relax or timed mover", "move to out")
		Sound:PlaySample("window_moving")
		if PermanentStorage.GetGlobalBoolOrDef("Enter main menu at once", true) then
			PermanentStorage.SetGlobalBool("Enter main menu at once", false)
		--	SendMessage("post:lua:MainMenuFunc", "Enter main menu")
		end
		WriteToLogIfBetaLog("Default Mode - Timed");
	elseif message == "choose relax or timed moved out" then
		SendMessage("post:ChangeRelaxOrTimedHolder", "clear children list")
		SendMessage("lua:MainMenuFunc", "Player change finished")
	else
		assert(false)
	end
end

function BetaFunc(message)
	if message == "Game field to main screen" then
		--SendMessage("post:lua:BetaFunc", "Duplicate player if hour")
	elseif message == "Main menu to main screen" then
		--SendMessage("post:lua:BetaFunc", "Duplicate player if hour")
	elseif message == "Main screen to shop" then
		--SendMessage("post:lua:BetaFunc", "Duplicate player if hour")
	elseif message == "Main screen to game field" then
		--SendMessage("post:lua:BetaFunc", "Duplicate player if hour")
	elseif message == "Shop to main menu" then
		--SendMessage("post:lua:BetaFunc", "Duplicate player if hour")
	elseif message == "Duplicate player if hour" then
		if PermanentStorage.GetGlobalBoolOrDef("betaLog", false) then
			local totalTime = PermanentStorage.GetLocalFloatOrDef("Total time", 0)
			local backuped = PermanentStorage.GetLocalBoolOrDef("Was one hour backup", false)
			if totalTime > 3600 and not backuped then
				PermanentStorage.SetLocalBool("Was one hour backup", true)
				PermanentStorage.BackupActivePlayer("  (1h)")
				WriteToLogColor("One hour checkpoint!", "Red")
			end
		end
	elseif message == "Duplicate player won cup" then
		if PermanentStorage.GetGlobalBoolOrDef("betaLog", false) then
			local tankId = PermanentStorage.GetLocalString("Current Tank")
			local cup = PermanentStorage.GetLocalString(tankId.." Cup")
			if cup == "Bronze" then
				PermanentStorage.BackupActivePlayer(tankId.." B")
			elseif cup == "Silver" then
				PermanentStorage.BackupActivePlayer(tankId..".S")
			elseif cup == "Gold" then
				PermanentStorage.BackupActivePlayer(tankId..":G")
			else
				assert(false)
			end
		end
	elseif message == "Clear cup time" then
		if PermanentStorage.GetGlobalBoolOrDef("betaLog", false) then
			PermanentStorage.SetLocalFloat("Cup time", 0)
			WriteToLog("Cup time reset")
		end
	elseif message == "Commit" then
		if PermanentStorage.GetGlobalBoolOrDef("betaLog", false) then
			WriteToLog("autosave...")
		end
		PermanentStorage.Commit()
	else
		assert(false, "Unknown message "..message)
	end
end