function HideMainMenu(part)	--Прячем главное меню 
 if part == "Hide Main Menu Part 1" then
--	SendMessage("Main menu all panels mover", "report arrival to post:lua:HideMainMenu")
--	SendMessage("Main menu all panels mover", "set arrival message Hide Main Menu Part 2")	
	SendMessage("Upper layer content", "template ZGlassTemplate")
	SendMessage("Upper layer content", "create top instance")
	SendMessage("Main menu all panels mover", "move to out")			
	SendMessage("Main menu all panels alpha changer", "change alpha to 0.0") 	
	SendMessage("Game logo alpha changer", "change alpha to 0.0") 
-- elseif part == "Hide Main Menu Part 2" then	
	--SendMessage("Desc Caustic", "set state to off")				 
	--SendMessage("Current tank content", "clear children list")
	--SendMessage("Fon tank mover", "report arrival to post:lua:MainMenuFunc")
	--SendMessage("Fon tank mover", "set arrival message Main menu panel out for shop")	
	SendMessage("Fon tank mover", "report arrival to post:lua:HideMainMenu")
	SendMessage("Fon tank mover", "set arrival message Hide Main Menu Part 3")	
	SendMessage("Fon tank mover", "move to out")				
	--SendMessage("Monstera mover", "move to out")				
	--SendMessage("fake Mover1", "report arrival to post:lua:HideMainMenu")
	--SendMessage("fake Mover1", "set arrival message Hide Main Menu Inter")	
	--SendMessage("fake Mover1", "move to in")				
	
	SendMessage("Main Fon mover", "move to out")		
	--SendMessage("Janifer alpha changer", "change alpha to 0.5") 	
	SendMessage("Janifer mover", "move to out")				
 elseif part == "Hide Main Menu Part 3" then	
	--SendMessage("Fon Caustic", "set state to off")				 
	SendMessage("Janifer alpha changer", "change alpha to 0.0") 
	SendMessage("Fon alpha changer", "change alpha to 0.0") 		
	SendMessage("All down remover", "report arrival to post:lua:MainMenuFunc")
	SendMessage("All down remover", "set arrival message Main menu panel out for shop")	
	SendMessage("All down remover", "move to out")				
	SendMessage("Fon down remover", "move to out")					
	--SendMessage("lua:MainMenuFunc", "Main menu panel out for shop")				
 elseif part == "Hide Main Menu Inter" then		
	--SendMessage("Fon alpha changer", "change alpha to 0.0") 	 
 else
	assert(false, "No part "..part) 
 end
end
function ShowMainMenu(part)	--Показываем главное меню обратно
 if part == "Show Main Menu Part 1" then
	SendMessage("Upper layer content", "template ZGlassTemplate")
	SendMessage("Upper layer content", "create top instance")
 
	SendMessage("Janifer mover", "move to out strictly")			
	SendMessage("Fon tank mover", "move to out strictly")			
	SendMessage("Main Fon mover", "move to out strictly")	
	SendMessage("All down remover", "move to out strictly")				
	SendMessage("Fon down remover", "move to out strictly")					
			 
	SendMessage("Main menu all panels mover", "move to out strictly")			 
	--SendMessage("Fon Caustic", "set state to off")				 
	--SendMessage("Desc Caustic", "set state to off")				 
	--SendMessage("Janifer alpha changer", "change alpha to 0.07") 	
	SendMessage("Fon alpha changer", "change alpha to 1.0") 	
	SendMessage("All down remover", "report arrival to post:lua:ShowMainMenu")
	SendMessage("All down remover", "set arrival message Show Main Menu Part 2")	
	SendMessage("All down remover", "move to in")				
	SendMessage("Fon down remover", "move to in")								 	
	--SendMessage("lua:ShowMainMenu", "Show Main Menu Part 2")	
 
 elseif part == "Show Main Menu Part 2" then	
	SendMessage("Fon tank mover", "report arrival to post:lua:ShowMainMenu")
	SendMessage("Fon tank mover", "set arrival message Show Main Menu Part 3")	
	SendMessage("Fon tank mover", "move to in")				
	SendMessage("Main Fon mover", "move to in")		
	SendMessage("Janifer alpha changer", "change alpha to 1.0") 	
	SendMessage("Janifer mover", "move to in")			
	SendMessage("Main menu all panels mover", "move to in")			
	SendMessage("Main menu all panels alpha changer", "change alpha to 1.0") 	

	
 elseif part == "Show Main Menu Part 3" then	
	--SendMessage("Main menu all panels mover", "report arrival to post:lua:ShowMainMenu")
	--SendMessage("Main menu all panels mover", "set arrival message Hide Main Menu Part 2")	
	--SendMessage("Fon Caustic", "set state to on")				 		
	--SendMessage("Desc Caustic", "set state to on")				  
	SendMessage("Game logo alpha changer", "change alpha to 1.0")  	
	SendMessage("Upper layer content", "clear children list")	
 else
	assert(false, "No part "..part) 
 end
end


function MoveMainMenuPanels(where, reportArrivalTo, arrivalMessage)
	SendMessage("Main menu all panels mover", "report arrival to "..reportArrivalTo)
	SendMessage("Main menu all panels mover", "set arrival message "..arrivalMessage)	
	SendMessage("Main menu all panels mover", "move to "..where)	
end

function MainMenuFunc(message)
	if message == "Create main menu" then
		local time = TimeCounter("Create main menu time = ")
		SendMessage("Screen", "template MainMenuTemplate")
		SendMessage("Screen", "create top instance")
		SendMessage("Current tank content", "template MainMenuCurrentTankSwitchTemplate")
		SendMessage("Current tank content", "create top instance")
		SendMessage("Left bubbles shower", "start")
		time:WriteToLog()	
	elseif message == "Enter main menu" then
		Sound:PlaySample("from_shop")
		local showIntro = true --PermanentStorage.GetLocalBoolOrDef("is show scene11",false)
		if not showIntro then
			SendMessage("post:lua:MovieShow", "first show scene11")
		else	
			SendMessage("lua:MainMenuFunc", "Create main menu")
			
			ShowMainMenu("Show Main Menu Part 1")
			--MoveMainMenuPanels("in", "null", "null")
			SendMessage("New background holder", "change alpha to 1.0")
		end
	elseif message == "Main menu play button pressed" then
		Sound:PlaySample("to_shop")
		SendMessage("post:Janifer head", "clear")
		local showIntro = PermanentStorage.GetLocalBoolOrDef("is show scene11",false)
		if not showIntro then
			SendMessage("post:lua:MovieShow", "first show scene11")
			if PermanentStorage.GetGlobalBoolOrDef("betaLog", false) then
				WriteToLog("Show Movie: Intro")
			end
		else
			SendMessage("New background holder", "change alpha to 0.0")
			SendMessage("Screen", "template ZGlassTemplate")
			SendMessage("Screen", "create top instance")
				-- Блокировка экрана
			HideMainMenu("Hide Main Menu Part 1")
		end
		--MoveMainMenuPanels("out", "post:lua:MainMenuFunc", "Main menu panel out for shop")
	elseif message == "Main menu panel out for shop" then			
		local currentTank = PermanentStorage.GetLocalString("Current Tank")
		SendMessage("Screen", "clear children list")
		--SendMessage("Screen", "template Aquarium"..currentTank.."Template")
		SendMessage("Screen", "template AnyShopTemplate")
		SendMessage("Screen", "create top instance")

		MoveShopPanels("in")
		--SendMessage("Shop cup alpha changer", "change alpha to 1.0")
		--SendMessage(currentTank.." Aquarium bottom", "entered shop")

		--SendMessage("lua:TutorialFunc", "Main screen panel out for shop")

		SendMessage("lua:BetaFunc", "Main screen to shop")
	elseif message == "Main menu exit button pressed" then
		-- PermanentStorage.SetLocalBool("New User", false);
		-- Этот ацкий хак сделан специально для Spintop,
		-- которому НЕ нужно устанавливать скринсейвер по умолчанию
		
		SendMessage("post:Janifer head", "bye user ))")
		--SendMessage("New background holder", "change alpha to 0.0")
		local isScreensaverEnabled = IsScreensaverEnabled()
		oldScreensaverEnabled = isScreensaverEnabled
		PermanentStorage.SetGlobalBool("isScreensaverEnabled", isScreensaverEnabled)
		--if isScreensaverEnabled then
		--	SendMessage("Main menu modal dialog", "instantiate ExitConfirm1Template")
		--else
		--	SendMessage("Main menu modal dialog", "instantiate ExitConfirm2Template")
		--end
		--SendMessage("ExitConfirm1", "show")
		--SendMessage("Exit panel mover", "move to in")
	elseif message == "Exit confirmation dialog yes pressed" then
		local isScreensaverEnabled = PermanentStorage.GetGlobalBool("isScreensaverEnabled")
		if not oldScreensaverEnabled and isScreensaverEnabled then
			InstallScreensaver()
		end
		oldScreensaverEnabled = nil
		if PermanentStorage.GetLocalBoolOrDef("doNotShowExitDemo", true) == false then
			SendMessage("Screen", "template DarkeningTemplate")
			SendMessage("Screen", "defineText [receiver] post:lua:MainMenuFunc")
			SendMessage("Screen", "defineText [message] Main menu darkened for demo screen")
			SendMessage("Screen", "create top instance")
		else
			CloseApplication()
		end
	elseif message == "Main menu darkened for demo screen" then
		SendMessage("Screen", "clear children list")
		SendMessage("Screen", "template ExitDemoTemplate")
		SendMessage("Screen", "create top instance")
		SendMessage("Screen", "template BrighteningTemplate")
		SendMessage("Screen", "create top instance")
	elseif message == "Demo screen exit pressed" then
		CloseApplication()
	elseif message == "Exit confirmation dialog no pressed" then
		SendMessage("post:Janifer head", "clear")
		local isScreensaverEnabled = PermanentStorage.GetGlobalBool("isScreensaverEnabled")
		if not oldScreensaverEnabled and isScreensaverEnabled then
		--	InstallScreensaver()
		end
		--SendMessage("Main menu modal dialog", "hide")
		--SendMessage("Exit panel mover", "move to out")
	elseif message == "Change player link pressed" then
		SendMessage("post:Janifer head", "clear")
		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
		-- установка дефолтовых названий аквариумов
		local tank1name = GetResourceString("Tank1DefaultName")
		local tank2name = GetResourceString("Tank2DefaultName")
		local tank3name = GetResourceString("Tank3DefaultName")
		PermanentStorage.GetLocalStringOrDef("Tank 1 name", tank1name)
		PermanentStorage.GetLocalStringOrDef("Tank 2 name", tank2name)
		PermanentStorage.GetLocalStringOrDef("Tank 3 name", tank3name)
		PermanentStorage.GetLocalStringOrDef("Tank name", tank1name)

		SendMessage("Main menu modal dialog", "hide")
		local first = PermanentStorage.GetLocalBoolOrDef("first main menu enter",true)
		if first then		
			SendMessage("post:Janifer head", "hello user")
			--PermanentStorage.SetLocalBool("first main menu enter",false)					
		end
		--local showIntro = PermanentStorage.GetLocalBoolOrDef("is show scene11",false)
		--if not showIntro then
		--	SendMessage("post:lua:MovieShow", "first show scene11")
		--end
	elseif message == "Main menu screensaver button pressed" then
		if PermanentStorage.GetGlobalBoolOrDef("betaLog", false) then
			WriteToLog("Press Screensave")
		end
		SendMessage("New background holder", "change alpha to 0.0")
		local isScreensaverEnabled = IsScreensaverEnabled()
		oldScreensaverEnabled = isScreensaverEnabled
		PermanentStorage.SetGlobalBool("isScreensaverEnabled", isScreensaverEnabled)
		SendMessage("Main menu modal dialog", "instantiate ScreensaverOptionsDialogTemplate")
		SendMessage("Screensaver Options mover", "move to in")
		Sound:PlaySample("window_moving")
	elseif message == "Screensaver options tank 1 pressed" then
		PermanentStorage.SetLocalString("Screensaver Tank", "1")
	elseif message == "Screensaver options tank 2 pressed" then
		PermanentStorage.SetLocalString("Screensaver Tank", "2")
	elseif message == "Screensaver options tank 3 pressed" then
		PermanentStorage.SetLocalString("Screensaver Tank", "3")
	elseif message == "Screensaver options preview button pressed" then
		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
		SendMessage("Current tank content", "clear children list")
		SendMessage("Screen", "template TankOnlyTemplate")
		local tankId = PermanentStorage.GetLocalString("Current Tank")
		PermanentStorage.SetGlobalString("Temp tank Id", tankId)
		local saverId = PermanentStorage.GetLocalString("Screensaver Tank")
		PermanentStorage.SetLocalString("Current Tank", saverId)
		SendMessage("Aquarium bottom", "Change Aquarium")
		--SendMessage("Screen", "defineText [variable] Screensaver Tank")
		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", "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
		SendMessage("Screen", "erase top child") -- DarkeningTemplate
		SendMessage("Screen", "erase top child") -- BrighteningTemplate
		SendMessage("Screen", "erase top child") -- TankOnlyTemplate
		SendMessage("Screen", "erase top child") -- DarkeningTemplate
		SendMessage("Screen", "template BrighteningTemplate")
		SendMessage("Screen", "create top instance")
		local tankId = PermanentStorage.GetGlobalString("Temp tank Id")
		PermanentStorage.SetLocalString("Current Tank", tankId)
		PermanentStorage.UndefGlobalName("Temp tank Id")
		SendMessage("Aquarium bottom", "Change Aquarium")
		
		SendMessage("Current tank content", "template MainMenuCurrentTankSwitchTemplate")
		SendMessage("Current tank content", "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
		SendMessage("Main menu modal dialog", "hide")
		SendMessage("Screensaver Options mover", "move to out")
		Sound:PlaySample("window_moving")
	elseif message == "Screensaver options cancel button pressed" then
		PermanentStorage.UndefGlobalName("isScreensaverEnabled")
		oldScreensaverEnabled = nil
		SendMessage("Main menu modal dialog", "hide")
		SendMessage("Screensaver Options mover", "move to out")
		Sound:PlaySample("window_moving")
	elseif message == "Main menu options button pressed" then
		if PermanentStorage.GetGlobalBoolOrDef("betaLog", false) then
			WriteToLog("Press Options")
		end
		--SendMessage("New background holder", "change alpha to 0.0")
		SendMessage("Main menu modal dialog", "instantiate MainMenuOptionsDialogTemplate")
		SendMessage("Options dispatcher", "report finish to lua:MainMenuFunc")
		SendMessage("Options dispatcher", "set finish message Main menu options dialog finished")
		SendMessage("Options dispatcher", "Show dialog")
		Sound:PlaySample("window_moving")
	elseif message == "Main menu options credits button pressed" then
		if PermanentStorage.GetGlobalBoolOrDef("betaLog", false) then
			WriteToLog("Press Credits")
		end
		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")
		Sound:PlaySample("window_moving")
	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:MainMenuFunc", "Main menu panel out for main screen")
		SendMessage("Game logo alpha changer", "change alpha to 0.0")
	elseif message == "Main menu panel out for main screen" then
		SendMessage("Screen", "clear children list")
		SendMessage("lua:MainScreenFunc", "Enter main screen")

		--SendMessage("lua:TutorialFunc", "Main menu panel out for main screen")

		SendMessage("lua:BetaFunc", "Main menu to main screen")
	else
		assert(false, "Unknown message "..message)
	end
end