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 PostUnlockScreen() 
	assert(SendMessageReply("GameScreen", "name of top") == "ZGlassTemplate")
	SendMessage("post:GameScreen", "erase top child")
end

function EnterMainMenu(container)
	if container == nil then
		SendMessage("Screen", "template MainMenuTemplate")
		SendMessage("Screen", "create top instance")
		SendMessage("Current tank content", "template MainMenuTankTemplate")
		SendMessage("Current tank content", "create top instance")
		SendMessage("Current tank content", "template GameLogoTemplate")
		SendMessage("Current tank content", "create top instance")
	else 
		SendMessage("Screen", "template MainMenuTemplate")
		SendMessage("Screen", "create top instance")
		SendMessage(container, "template IntroEnterEffectTemplate")
		SendMessage(container, "create top instance")
	end
end

function MainMenuFunc(message)
	if message == "Enter main menu" then
		EnterMainMenu()
		MoveMainMenuPanels("in", "null", "null")
		SendMessage("Swinging 2", "run")
		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
				ShowHelpSmall(502, 190, "NewScreensaverAquariumText", 502, 70, -90)
			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", "move to in")
		end
	elseif message == "Exit confirmation dialog yes pressed" then
		local isScreensaverEnabled = PermanentStorage.GetGlobalBool("isScreensaverEnabled")
		if not oldScreensaverEnabled and isScreensaverEnabled then
			InstallScreensaver()
			PermanentStorage.SetGlobalString("screensaver tank file",
			"textures\\choose_tank\\tanks\\"..
			PermanentStorage.GetLocalInt("_Id").."_tank_"..
			PermanentStorage.GetLocalStringOrDef("Screensaver Tank",
				PermanentStorage.GetGlobalString("Screen saver tank"))..".jpg");
		end
		oldScreensaverEnabled = nil
		CloseApplication()
	elseif message == "Exit confirmation dialog no pressed" then
		oldScreensaverEnabled = nil
		if IsReceiver("Exit panel mover") then
			SendMessage("Exit panel mover", "move to out")
		end
		SendMessage("Main menu modal dialog", "hide")		
	elseif message == "Change player link pressed" then
		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
		local isScreensaverEnabled = IsScreensaverEnabled()
		oldScreensaverEnabled = isScreensaverEnabled
		PermanentStorage.SetGlobalBool("isScreensaverEnabled", isScreensaverEnabled)
		oldScreensaverMute = PermanentStorage.GetGlobalBoolOrDef("isScreensaverMute", false)
		if PermanentStorage.GetGlobalBoolOrDef("temp:ExtrasScreen enabled", false) then
			SendMessage("Extras modal dialog", "instantiate ScreensaverOptionsDialogTemplate2")
		else
			SendMessage("Main menu modal dialog", "instantiate ScreensaverOptionsDialogTemplate2")
		end
		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")
	elseif message == "Main menu darkened for screensaver preview" then
		local currentTank = PermanentStorage.GetLocalString("Current Tank")	
		local mute = PermanentStorage.GetGlobalBool("isScreensaverMute")
		if tonumber(currentTank) > 3 then
			Sound:FadeOutTrack("Menu"..PermanentStorage.GetLocalString(currentTank.." Tank Type"), 1.0)
		else
			Sound:FadeOutTrack("Menu"..currentTank, 1.0)
		end
		if not mute then
			local screensaverTank = PermanentStorage.GetLocalString("Screensaver Tank")	
			if tonumber(screensaverTank) > 3 then
				Sound:FadeInTrack("Menu"..PermanentStorage.GetLocalString(screensaverTank.." Tank Type"), 1.0, true)
			else
				Sound:FadeInTrack("Menu"..screensaverTank, 1.0, true)
			end
		end
		SendMessage("Screen", "erase top child") -- DarkeningTemplate
		SendMessage("Aquarium bottom", "Load Screensaver Content")
		SendMessage("Aquarium bottom", "Save 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")
	elseif message == "Screensaver preview darkened" then
		local currentTank = PermanentStorage.GetLocalString("Current Tank")	
		local mute = PermanentStorage.GetGlobalBool("isScreensaverMute")
		if not mute then
			local screensaverTank = PermanentStorage.GetLocalString("Screensaver Tank")	
			if tonumber(screensaverTank) > 3 then
				Sound:FadeOutTrack("Menu"..PermanentStorage.GetLocalString(screensaverTank.." Tank Type"), 1.0)
			else
				Sound:FadeOutTrack("Menu"..screensaverTank, 1.0)
			end
		end
		if tonumber(currentTank) > 3 then
			Sound:FadeInTrack("Menu"..PermanentStorage.GetLocalString(currentTank.." Tank Type"), 1.0, true)
		else
			Sound:FadeInTrack("Menu"..currentTank, 1.0, true)
		end
		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")
		if PermanentStorage.GetGlobalBoolOrDef("temp:ExtrasScreen enabled", false) then
			SendMessage("Extras modal dialog", "hide")
		else
			SendMessage("Main menu modal dialog", "hide")
		end
		SendMessage("Screensaver Options mover", "report arrival to null")
		SendMessage("Screensaver Options mover", "set arrival message null")
		SendMessage("Screensaver Options mover", "move to out")
		PermanentStorage.SetGlobalString("screensaver tank file",
			"textures\\choose_tank\\tanks\\"..
			PermanentStorage.GetLocalInt("_Id").."_tank_"..
			PermanentStorage.GetLocalStringOrDef("Screensaver Tank",
				PermanentStorage.GetGlobalString("Screen saver tank"))..".jpg");
	elseif message == "Screensaver options cancel button pressed" then
		PermanentStorage.UndefGlobalName("isScreensaverEnabled")
		oldScreensaverEnabled = nil
		PermanentStorage.SetGlobalBool("isScreensaverMute", oldScreensaverMute)
		oldScreensaverMute = nil
		if PermanentStorage.GetGlobalBoolOrDef("temp:ExtrasScreen enabled", false) then
			SendMessage("Extras modal dialog", "hide")
		else
			SendMessage("Main menu modal dialog", "hide")
		end
		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
		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
		SendMessage("Screen", "template MainMenuCreditsZGlassTemplate")
		SendMessage("Screen", "create top instance")
		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("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")
	
	--
	-- More Games Screen
	--
	
	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 back panel mover", "move to in")
		SendMessage("More games title panel mover", "move to in")
	
	elseif message == "More games exit" then
	
		SendMessage("More games back panel mover", "move to out")
		SendMessage("More games title 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")
	
	--
	-- Extras Screen
	--
	
	elseif message == "Extras button pressed" then
	
		MoveMainMenuPanels("out", "lua:MainMenuFunc", "Extras button pressed continue")
		SendMessage("Game logo alpha changer", "change alpha to 0.0")
		LockScreen()
	
	elseif message == "Extras button pressed continue" then
	
		UnlockScreen()
		SendMessage("Main menu modal dialog", "template ExtrasTemplate")
		SendMessage("Main menu modal dialog", "create instance")
		SendMessage("Extras back panel mover", "move to in")
		SendMessage("Extras title panel mover", "move to in")
	
	elseif message == "Extras exit" then
	
		SendMessage("Extras back panel mover", "move to out")
		SendMessage("Extras title panel mover", "move to out")
		SendMessage("Main menu modal dialog", "hide")
		SendMessage("TimedSMS", "lua:MainMenuFunc, Extras exit continue, 0.5")
		LockScreen()
	
	elseif message == "Extras exit continue" then
		
		UnlockScreen()
		SendMessage("Game logo alpha changer", "change alpha to 1.0")
		MoveMainMenuPanels("in")
	
	elseif message == "reload templates" then
		--ReloadTemplate("ChooseTank.xml")
	else
		assert(false, "Unknown message "..message)
	end
end