function MoveMainMenuPanels(where, reportArrivalTo, arrivalMessage)
	if reportArrivalTo == nil then
	    reportArrivalTo = "null"
	    arrivalMessage  = "null"
	end
	SendMessage("More games panel mover", "move to "..where)
	SendMessage("Main menu blue panel mover", "move to "..where)
	SendMessage("Main menu bottom panel mover", "report arrival to "..reportArrivalTo)
	SendMessage("Main menu bottom panel mover", "set arrival message "..arrivalMessage)
	SendMessage("Main menu bottom panel mover", "move to "..where)
end

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

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

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

function MainMenuFunc(message)
	if message == "Create main menu" then
	    SendMessage("Screen", "template MainMenuTemplate")
	 	SendMessage("Screen", "create top instance")
	 	
 	elseif message == "Create Tank" then
		SendMessage("Current tank content", "template MainMenuTankTemplate")
		SendMessage("Current tank content", "create top instance")

	elseif message == "Enter main menu" then
	
	    MainMenuFunc("Create main menu")
	    MainMenuFunc("Create Tank")
	    MainMenuFunc("First enter main menu")

	elseif message == "First enter main menu" then

		InstantinateGameLogo("Current tank content")

		MoveMainMenuPanels("in")
		do
			local change = false
			for tankId = 1, 2 do
				local lastOpen = PermanentStorage.GetLocalStringOrDef(tankId.." Last main menu cup", "None") ~= "None"
				local newOpen = PermanentStorage.GetLocalStringOrDef(tankId.." Cup", "None") ~= "None"
				if newOpen and not lastOpen then
					change = true
				else
					--assert(newOpen == lastOpen)
				end
				PermanentStorage.SetLocalString(tankId.." Last main menu cup", PermanentStorage.GetLocalString(tankId.." Cup"))
			end
			if change then
				--SendMessage("New background holder", "change alpha to 1.0")
			end
		end
		
	elseif message == "Main menu exit button pressed" then
		local isScreensaverEnabled = IsScreensaverEnabled()
		oldScreensaverEnabled = isScreensaverEnabled
		if not isScreensaverEnabled then
			SendMessage("Main menu modal dialog", "instantiate CheckboxExitConfirmationTemplate")
		else
			PermanentStorage.SetGlobalBool("isScreensaverEnabled", isScreensaverEnabled)
			YesNoDialog("Main menu modal dialog", "AreYouSureYouWantToExit", "post:lua:MainMenuFunc",
				"Exit confirmation dialog yes pressed",
				"Exit confirmation dialog no pressed")
		end
		if IsReceiver("Exit panel mover") then
			SendMessage("Exit panel mover", "report arrival to null")
			SendMessage("Exit panel mover", "set arrival message null")
			SendMessage("Exit panel mover", "move to in")
		end
		SendMessage("CSVSTATISTICS","exit button pressed")
	elseif message == "Exit confirmation dialog yes pressed" then
		local isScreensaverEnabled = PermanentStorage.GetGlobalBool("isScreensaverEnabled")
		if not oldScreensaverEnabled and isScreensaverEnabled then
			InstallScreensaver()
		end
		oldScreensaverEnabled = nil
		Application.Close()
	elseif message == "Exit confirmation dialog no pressed" then
		SendMessage("CSVSTATISTICS","Exit confirmation dialog no pressed")
		oldScreensaverEnabled = nil
		SendMessage("Main menu modal dialog", "hide")
		if IsReceiver("Exit panel mover") then
			SendMessage("Exit panel mover", "report arrival to null")
			SendMessage("Exit panel mover", "set arrival message null")
			SendMessage("Exit panel mover", "move to out")
		end
	elseif message == "Change player link pressed" then
		--SendMessage("New background holder", "change alpha to 0.0")
		SendMessage("Main menu modal dialog", "instantiate PlayersDialogTemplate")
		SendMessage("Players dispatcher", "report finish to lua:MainMenuFunc")
		SendMessage("Players dispatcher", "set finish message Player change finished")
		SendMessage("Players dispatcher", "Show dialog")
	elseif message == "Player change finished" then
		SendMessage("Main menu modal dialog", "hide")
	elseif message == "Main menu screensaver button pressed" then
		--SendMessage("New background holder", "change alpha to 0.0")
		local isScreensaverEnabled = IsScreensaverEnabled()
		oldScreensaverEnabled = isScreensaverEnabled
		PermanentStorage.SetGlobalBool("isScreensaverEnabled", isScreensaverEnabled)
		oldScreensaverMute = PermanentStorage.GetGlobalBoolOrDef("isScreensaverMute", false)
		SendMessage("Main menu modal dialog", "instantiate ScreensaverOptionsDialogTemplate2")
		SendMessage("Screensaver Options mover", "report arrival to null")
		SendMessage("Screensaver Options mover", "set arrival message null")
		SendMessage("Screensaver Options mover", "move to in")
	elseif message == "Screensaver options preview button pressed" then
		-- После предыдущего просмотра сверху остаётся контрол осветления, который
		-- по-хорошему надо бы удалять
		local topControl = SendMessageReply("Screen", "name of top")
		if topControl == "BrighteningControl" then
			SendMessage("post:Screen", "erase top child")
		end
		SendMessage("Game logo alpha changer", "change alpha to 0.0")
		SendMessage("Screen", "template DarkeningTemplate")
		SendMessage("Screen", "defineText [receiver] post:lua:MainMenuFunc")
		SendMessage("Screen", "defineText [message] Main menu darkened for screensaver preview")
		SendMessage("Screen", "create top instance")
		if PermanentStorage.GetGlobalBool("isScreensaverMute") then
			Sound:FadeOutTrack(PermanentStorage.GetGlobalString("temp:current track"), 1.0)
		end
	elseif message == "Main menu darkened for screensaver preview" then
		SendMessage("Screen", "erase top child") -- DarkeningTemplate
		SendMessage("Aquarium bottom", "Save Content")
		SendMessage("Aquarium bottom", "Load Screensaver Content")
		SendMessage("Main menu alpha changer", "change alpha to 0.0")
		SendMessage("Screen", "template MainMenuScreensaverPreviewZGlassTemplate")
		SendMessage("Screen", "defineText [exitMessageReceiver] post:lua:MainMenuFunc")
		SendMessage("Screen", "defineText [exitMessage] Screensaver preview done")
		SendMessage("Screen", "create top instance")
		SendMessage("Screen", "template BrighteningTemplate")
		SendMessage("Screen", "create top instance")
	elseif message == "Screensaver preview done" then
		SendMessage("Screen", "erase top child") -- BrighteningTemplate
		SendMessage("Screen", "erase top child") -- MainMenuScreensaverPreviewZGlassTemplate
		SendMessage("Screen", "template DarkeningTemplate")
		SendMessage("Screen", "defineText [receiver] post:lua:MainMenuFunc")
		SendMessage("Screen", "defineText [message] Screensaver preview darkened")
		SendMessage("Screen", "create top instance")
		if PermanentStorage.GetGlobalBool("isScreensaverMute") then
			Sound:FadeInTrack(PermanentStorage.GetGlobalString("temp:current track"), 1.0, true)
		end
	elseif message == "Screensaver preview darkened" then
		SendMessage("Screen", "erase top child") -- DarkeningTemplate
		SendMessage("Aquarium bottom", "Change Aquarium")
		SendMessage("Main menu alpha changer", "change alpha to 1.0")
		SendMessage("Game logo alpha changer", "change alpha to 1.0")
		SendMessage("Screen", "template BrighteningTemplate")
		SendMessage("Screen", "create top instance")
	elseif message == "Screensaver options ok button pressed" then
		local isScreensaverEnabled = PermanentStorage.GetGlobalBool("isScreensaverEnabled")
		PermanentStorage.UndefGlobalName("isScreensaverEnabled")
		if not oldScreensaverEnabled and isScreensaverEnabled then
			InstallScreensaver()
		elseif oldScreensaverEnabled and not isScreensaverEnabled then
			UninstallScreensaver()
		else
			assert(oldScreensaverEnabled == isScreensaverEnabled)
		end
		oldScreensaverEnabled = nil
		oldScreensaverMute = PermanentStorage.GetGlobalBool("isScreensaverMute")
		SendMessage("Main menu modal dialog", "hide")
		SendMessage("Screensaver Options mover", "report arrival to null")
		SendMessage("Screensaver Options mover", "set arrival message null")
		SendMessage("Screensaver Options mover", "move to out")
	elseif message == "Screensaver options cancel button pressed" then
		PermanentStorage.UndefGlobalName("isScreensaverEnabled")
		oldScreensaverEnabled = nil
		PermanentStorage.SetGlobalBool("isScreensaverMute", oldScreensaverMute)
		oldScreensaverMute = nil
		SendMessage("Main menu modal dialog", "hide")
		SendMessage("Screensaver Options mover", "report arrival to null")
		SendMessage("Screensaver Options mover", "set arrival message null")
		SendMessage("Screensaver Options mover", "move to out")
	elseif message == "Main menu options button pressed" then
		--SendMessage("New background holder", "change alpha to 0.0")
		OptionsDialog("Main menu modal dialog", 100
				, "Main menu options credits button pressed"
				, "lua:MainMenuFunc" )
		SendMessage("Options dispatcher", "report finish to lua:MainMenuFunc")
		SendMessage("Options dispatcher", "set finish message Main menu options dialog finished")
		SendMessage("Options dispatcher", "Show dialog")
	elseif message == "Main menu options credits button pressed" then
		SendMessage("Screen", "template ZGlassTemplate")
		SendMessage("Screen", "create top instance")
		SendMessage("Main menu modal dialog alpha changer", "change alpha to 0.0")
		SendMessage("Game logo alpha changer", "change alpha to 0.0")
		MoveMainMenuPanels("out", "post:lua:MainMenuFunc", "Main menu panel out for credits")
	elseif message == "Main menu panel out for credits" then
		SendMessage("Screen", "erase top child")
			-- ZGlass
		SetClickMessage("Screen", "post:lua:MainMenuFunc", "Main menu credits click")
		--SendMessage("Screen", "template MainMenuCreditsZGlassTemplate")
		--SendMessage("Screen", "create top instance")

		ShowCredits("Screen")
		--SendMessage("Screen", "template CreditsTemplate")
		--SendMessage("Screen", "create top instance")
	elseif message == "Main menu credits click" then
		SendMessage("Screen", "erase top child")
			-- Credits
		SendMessage("Screen", "erase top child")
			-- ZGlass
		SendMessage("Main menu modal dialog alpha changer", "change alpha to 1.0")
		SendMessage("Game logo alpha changer", "change alpha to 1.0")
		MoveMainMenuPanels("in", "null", "null")
	elseif message == "Main menu options dialog finished" then
		SendMessage("Main menu modal dialog", "hide")
	elseif message == "Main menu start button pressed" then
		--SendMessage("New background holder", "change alpha to 0.0")
		SendMessage("Screen", "template ZGlassTemplate")
		SendMessage("Screen", "create top instance")
			-- Блокировка экрана
		MoveMainMenuPanels("out", "post:lua:MainScreenFunc", "Enter main screen")
		SendMessage("lua:BetaFunc", "Main menu to main screen")
		SendMessage("Game logo alpha changer", "change alpha to 0.0")
	
	elseif message == "Main menu more games button pressed" then
	
		MoveMainMenuPanels("out", "lua:MainMenuFunc", "Main menu more games button pressed continue")
		SendMessage("Game logo alpha changer", "change alpha to 0.0")
		LockScreen()
	
	elseif message == "Main menu more games button pressed continue" then
	
		UnlockScreen()
		SendMessage("Main menu modal dialog", "template MoreGamesTemplate")
		SendMessage("Main menu modal dialog", "create instance")
		SendMessage("More games done panel mover", "move to in")
	
	elseif message == "More games exit" then
	
		SendMessage("More games done panel mover", "move to out")
		SendMessage("Main menu modal dialog", "hide")
		SendMessage("TimedSMS", "lua:MainMenuFunc, More games exit continue, 0.5")
		LockScreen()
	
	elseif message == "More games exit continue" then
		
		UnlockScreen()
		SendMessage("Game logo alpha changer", "change alpha to 1.0")
		MoveMainMenuPanels("in")
	
	else
		assert(false, "Unknown message "..message)
	end
end
