lastMusicVolume = 0
lastSoundVolume = 0
lastFullscreen = false
lastCustomCursor = false
lastShowHints = false
optionsLayerName = "Options"

function AnimationInit()
end

function SkeletonFunc(message)

	if message:is("Add", "press") then
		GUI:getLayer("TestLayer"):getWidget("Test"):AcceptMessage(Message("Add"))
	elseif message:is("Set", "press") then
		GUI:getLayer("TestLayer"):getWidget("Test"):AcceptMessage(Message("Set"))
	elseif message:is("Play", "press") then
		GUI:getLayer("TestLayer"):getWidget("Test"):AcceptMessage(Message("Play"))
	elseif message:is("Stop", "press") then
		GUI:getLayer("TestLayer"):getWidget("Test"):AcceptMessage(Message("Stop"))
	elseif message:is("Frames", "select") then
		GUI:getLayer("TestLayer"):getWidget("Test"):AcceptMessage(Message("SetFrame"))
	elseif message:is("SaveAnim", "press") then
		GUI:getLayer("TestLayer"):getWidget("Test"):AcceptMessage(Message("SaveAnimation"))
	elseif message:is("LoadAnim", "press") then
		GUI:getLayer("TestLayer"):getWidget("Test"):AcceptMessage(Message("LoadAnimation"))
	elseif message:is("AddAnim", "press") then
		GUI:getLayer("AnimName"):getWidget("Name"):AcceptMessage(Message("Clear"))
		Screen:pushLayer( GUI:getLayer("AnimName") )
	elseif message:is("Exit", "press") then
		CloseApplication()
	end
end

function AddAnimFunc(message)

	if message:is("Ok", "press") then
		GUI:getLayer("TestLayer"):getWidget("Test"):AcceptMessage(Message("AddAnimation"))
		Screen:popLayer()
	elseif message:is("Cancel", "press") then
		Screen:popLayer()
	end
end

function RunISpyLevel(level)
	UploadResourceGroup("ISpy")
	GUI:getLayer("ISpyLevel"):setUpdate(true)
	Screen:Clear()
	Screen:pushLayer(GUI:getLayer("ISpyLevel"))
	GUI:getLayer("ISpyLevel"):getWidget("ISpyLevel"):AcceptMessage(Message("StartLevel", level))
end

function SaveOptions(message)
	
	-- Запоминаем опции
	lastMusicVolume = gameInfo:getMusicVolume()
	lastSoundVolume = gameInfo:getSoundVolume()
	lastFullscreen = gameInfo:isFullScreen()
	lastCustomCursor = gameInfo:isCustomCursor()
	lastShowHints = gameInfo:isHintsEnabled()

end

function RestoreOptions(message)

	-- Восстанавливаем опции
	Sound:SetSoundVolume(lastSoundVolume)
	Sound:SetMusicVolume(lastMusicVolume)

	gameInfo:setMusicVolume(lastMusicVolume)
	gameInfo:setSoundVolume(lastSoundVolume)
	gameInfo:setCustomCursor(lastCustomCursor)
	gameInfo:setHintsEnabled(lastShowHints)
	gameInfo:setFullScreen(lastFullscreen)

	-- Восстанавливаем значения на слоях
	local optionsName = "Options"

	local iBoolean = "0"
	if lastFullscreen then
		iBoolean = "1"
	end
	GUI:getLayer(optionsName):getWidget("Fullscreen"):AcceptMessage(Message("SetState", iBoolean))

	iBoolean = "0"
	if lastShowHints then
		iBoolean = "1"
	end
	GUI:getLayer(optionsName):getWidget("ATips"):AcceptMessage(Message("SetState", iBoolean))
	
	iBoolean = "0"
	if lastCustomCursor then
		iBoolean = "1"
	end
	GUI:getLayer(optionsName):getWidget("CustomCursor"):AcceptMessage(Message("SetState", iBoolean))

	GUI:getLayer(optionsName):getWidget("Music"):AcceptMessage(Message("SetPos", tostring(lastMusicVolume)))
	GUI:getLayer(optionsName):getWidget("Sound"):AcceptMessage(Message("SetPos", tostring(lastSoundVolume)))
end

function SetOptions(message)

	-- Устанавливаем опции (для чтения используется слой optionsLayerName)
	local state = GUI:getLayer(optionsLayerName):getWidget("Music"):QueryState(Message("GetPos"))
	local musVolume = 0+state:getData()

	state = GUI:getLayer(optionsLayerName):getWidget("Sound"):QueryState(Message("GetPos"))
	local sndVolume = 0+state:getData()

	state = GUI:getLayer(optionsLayerName):getWidget("Fullscreen"):QueryState(Message("GetState"))

	Sound:SetMusicVolume(musVolume)
	Sound:SetSoundVolume(sndVolume)
	gameInfo:setMusicVolume(musVolume)
	gameInfo:setSoundVolume(sndVolume)
	
	if state:getData() == "1" and gameInfo:isFullScreen() == false then
		gameInfo:setFullScreen(true)
		SetFullscreenMode()
	end

	if state:getData() == "0" and gameInfo:isFullScreen() == true then
		gameInfo:setFullScreen(false)
		SetWindowedMode()
	end
	
	state = GUI:getLayer(optionsLayerName):getWidget("CustomCursor"):QueryState(Message("GetState"))
	if state:getData() == "1" then
		SetCustomCursor(true)
		gameInfo:setCustomCursor(true)
	else
		SetCustomCursor(false)
		gameInfo:setCustomCursor(false)
	end
	
	state = GUI:getLayer(optionsLayerName):getWidget("ATips"):QueryState(Message("GetState"))
	if state:getData() == "1" then
		gameInfo:setHintsEnabled(true)
	else
		gameInfo:setHintsEnabled(false)
	end

	-- Устанавливаем значения на слоях
	local optionsName = "Options"

	GUI:getLayer(optionsName):getWidget("Music"):AcceptMessage(Message("SetPos", tostring(musVolume)))
	GUI:getLayer(optionsName):getWidget("Sound"):AcceptMessage(Message("SetPos", tostring(sndVolume)))

	local iBoolean = "0"
	if gameInfo:isFullScreen() then
		iBoolean = "1"
	end
	GUI:getLayer(optionsName):getWidget("Fullscreen"):AcceptMessage(Message("SetState", iBoolean))

	iBoolean = "0"
	if gameInfo:isHintsEnabled() then
		iBoolean = "1"
	end
	GUI:getLayer(optionsName):getWidget("ATips"):AcceptMessage(Message("SetState", iBoolean))
	
	iBoolean = "0"
	if gameInfo:isCustomCursor() then
		iBoolean = "1"
	end
	GUI:getLayer(optionsName):getWidget("CustomCursor"):AcceptMessage(Message("SetState", iBoolean))
end

function OptionsFunc(message)
	if message:is("Ok", "press") then
		SetOptions()

		FadePopLayer("Options", "Options")

	elseif message:is("Cancel", "press") then

		FadePopLayer("Options", "Options")
		RestoreOptions()

	elseif message:is("Music") then
		local musicVolume = message:getIntegerParam()
		gameInfo:setMusicVolume(musicVolume)
		Sound:SetMusicVolume(musicVolume)

	elseif message:is("Sound") then

		local soundVolume = message:getIntegerParam()
		gameInfo:setSoundVolume(soundVolume)
		Sound:SetSoundVolume(soundVolume)

		if message:is("Sound", "up") then
			Sound:PlaySample("ClickBonus")
		end
	end
end

function SimpleFadePushLayer(layerName, widgetName)

	fadeScreens = fadeScreens+1
	
	local fadeLayer = ""
	if (fadeScreens == 1) then
		fadeLayer = "Cross1"
	elseif (fadeScreens == 2) then
		fadeLayer = "Cross2"
	else
		fadeLayer = "Cross3"
	end
	
	local panelWidget = GUI:getLayer(layerName):getWidget(widgetName)
	panelWidget:setColor(Color(255, 255, 255, 0))

	local fadeWidget = GUI:getLayer(fadeLayer):getWidget("BlackScreen")
	fadeWidget:setColor(Color(0, 0, 0, 0))

	local param = VariableSet()
	param:setWidget("Widget", panelWidget)
	param:setColor("To", Color(255, 255, 255, 255))
	kernel:addController(controlFactory:Create("Fader", param))

	param = VariableSet()
	param:setWidget("Widget", fadeWidget)
	param:setColor("To", Color(0, 0, 0, 150))
	kernel:addController(controlFactory:Create("Fader", param))

	Screen:pushLayer(GUI:getLayer(fadeLayer))
	Screen:pushLayer(GUI:getLayer(layerName) )
end

function SimpleFadePopLayer(layerName, widgetName)

	local fadeLayer = ""
	if (fadeScreens == 1) then
		fadeLayer = "Cross1"
	elseif (fadeScreens == 2) then
		fadeLayer = "Cross2"
	else
		fadeLayer = "Cross3"
	end

	fadeScreens = fadeScreens-1

	local panelWidget = GUI:getLayer(layerName):getWidget(widgetName)
	local fadeWidget = GUI:getLayer(fadeLayer):getWidget("BlackScreen")

	param = VariableSet()
	param:setWidget("Widget", panelWidget)
	param:setColor("To", Color(255, 255, 255, 0))
	param:setBool("PopLayer", true)
	param:setString("Layer", layerName)
	kernel:addController(controlFactory:Create("Fader", param))

	param = VariableSet()
	param:setWidget("Widget", fadeWidget)
	param:setColor("To", Color(0, 0, 0, 0))
	param:setBool("PopLayer", true)
	param:setString("Layer", fadeLayer)
	kernel:addController(controlFactory:Create("Fader", param))
end

function NewProfileFunc(message)
	if message:is("Cancel", "press") then

		FadePopLayer("NewProfile", "NewProfile")

	elseif message:is("Ok", "press") then

		state = GUI:getLayer("NewProfile"):getWidget("Name"):QueryState(Message("Text"))
		local profileName = state:getData()
		if not (profileName == "") then
			if gameInfo:isPlayerExist(profileName) then
              		GUI:getLayer("NewProfile"):getWidget("InfoText"):AcceptMessage(Message("SetString", "player already exist!"))
			else
				local namesWidget = GUI:getLayer("Profiles"):getWidget("Names")
				namesWidget:AcceptMessage(Message("Add", profileName))
				namesWidget:AcceptMessage(Message("Set", profileName))
				gameInfo:AddNewPlayer(profileName)
				
				FadePopLayer("NewProfile", "NewProfile")

				FadePopLayer("Profiles", "Profiles")
			end
		end
	end
end

function ProfilesFunc(message)
	if message:is("New", "press") then
		GUI:getLayer("NewProfile"):getWidget("Name"):AcceptMessage(Message("Clear"))

		FadePushLayer("NewProfile", "NewProfile")

	elseif message:is("Select", "press") or message:is("Names", "DoubleClick") then
		local state = GUI:getLayer("Profiles"):getWidget("Names"):QueryState(Message("CurrentItem"))
		local playerName = state:getData()
		gameInfo:setActivePlayer(playerName)

		GUI:getLayer("BooksLayer"):getWidget("Books"):AcceptMessage(Message("UpdateView"))

		FadePopLayer("Profiles", "Profiles")

	elseif message:is("B_Up", "press") then
		local namesWidget = GUI:getLayer("Profiles"):getWidget("Names")
		namesWidget:AcceptMessage(Message("ScrollUp"))

	elseif message:is("B_Down", "press") then
		local namesWidget = GUI:getLayer("Profiles"):getWidget("Names")
		namesWidget:AcceptMessage(Message("ScrollDown"))

	elseif message:is("Cancel", "press") then
		FadePopLayer("Profiles", "Profiles")

	elseif message:is("Delete", "press") then
		local state = GUI:getLayer("Profiles"):getWidget("Names"):QueryState(Message("CurrentItem"))
		local playerName = state:getData()

		if gameInfo:getNumberOfPlayers() == 1 then
			local panelWidget = GUI:getLayer("LastNoDelete"):getWidget("LastNoDelete")
			panelWidget:setColor(Color(255, 255, 255, 0))

			local fadeWidget = GUI:getLayer("LastNoDeleteFade"):getWidget("BlackScreen")
			fadeWidget:setColor(Color(0, 0, 0, 0))

			local param = VariableSet()
			param:setWidget("Widget", panelWidget)
			param:setColor("To", Color(255, 255, 255, 255))
			kernel:addController(controlFactory:Create("Fader", param))

			param = VariableSet()
			param:setWidget("Widget", fadeWidget)
			param:setColor("To", Color(0, 0, 0, 150))
			kernel:addController(controlFactory:Create("Fader", param))

			Screen:pushLayer(GUI:getLayer("LastNoDeleteFade"))
			Screen:pushLayer(GUI:getLayer("LastNoDelete"))
		elseif gameInfo:getName() == playerName then
			local panelWidget = GUI:getLayer("ActiveNoDelete"):getWidget("ActiveNoDelete")
			panelWidget:setColor(Color(255, 255, 255, 0))

			local fadeWidget = GUI:getLayer("ActiveNoDeleteFade"):getWidget("BlackScreen")
			fadeWidget:setColor(Color(0, 0, 0, 0))

			local param = VariableSet()
			param:setWidget("Widget", panelWidget)
			param:setColor("To", Color(255, 255, 255, 255))
			kernel:addController(controlFactory:Create("Fader", param))

			param = VariableSet()
			param:setWidget("Widget", fadeWidget)
			param:setColor("To", Color(0, 0, 0, 150))
			kernel:addController(controlFactory:Create("Fader", param))

			Screen:pushLayer(GUI:getLayer("ActiveNoDeleteFade"))
			Screen:pushLayer(GUI:getLayer("ActiveNoDelete"))
		else
			GUI:getLayer("DeletePlayerConfirm"):getWidget("PlayerName"):AcceptMessage(Message("SetString", playerName))

			local panelWidget = GUI:getLayer("DeletePlayerConfirm"):getWidget("DeletePlayerConfirm")
			panelWidget:setColor(Color(255, 255, 255, 0))
	
			local fadeWidget = GUI:getLayer("DeletePlayerConfirmFade"):getWidget("BlackScreen")
			fadeWidget:setColor(Color(0, 0, 0, 0))
	
			local param = VariableSet()
			param:setWidget("Widget", panelWidget)
			param:setColor("To", Color(255, 255, 255, 255))
			kernel:addController(controlFactory:Create("Fader", param))
	
			param = VariableSet()
			param:setWidget("Widget", fadeWidget)
			param:setColor("To", Color(0, 0, 0, 150))
			kernel:addController(controlFactory:Create("Fader", param))
	
			Screen:pushLayer(GUI:getLayer("DeletePlayerConfirmFade"))
			Screen:pushLayer(GUI:getLayer("DeletePlayerConfirm"))
		end
	end
end

function LastNoDeleteFunc(message)
	if message:is("Ok", "press") then
		local panelWidget = GUI:getLayer("LastNoDelete"):getWidget("LastNoDelete")
		local fadeWidget = GUI:getLayer("LastNoDeleteFade"):getWidget("BlackScreen")

		param = VariableSet()
		param:setWidget("Widget", panelWidget)
		param:setColor("To", Color(255, 255, 255, 0))
		param:setBool("PopLayer", true)
		kernel:addController(controlFactory:Create("Fader", param))

		param = VariableSet()
		param:setWidget("Widget", fadeWidget)
		param:setColor("To", Color(0, 0, 0, 0))
		param:setBool("PopLayer", true)
		kernel:addController(controlFactory:Create("Fader", param))

	end
end

function ActiveNoDeleteFunc(message)
	if message:is("Ok", "press") then
		local panelWidget = GUI:getLayer("ActiveNoDelete"):getWidget("ActiveNoDelete")
		local fadeWidget = GUI:getLayer("ActiveNoDeleteFade"):getWidget("BlackScreen")

		param = VariableSet()
		param:setWidget("Widget", panelWidget)
		param:setColor("To", Color(255, 255, 255, 0))
		param:setBool("PopLayer", true)
		kernel:addController(controlFactory:Create("Fader", param))

		param = VariableSet()
		param:setWidget("Widget", fadeWidget)
		param:setColor("To", Color(0, 0, 0, 0))
		param:setBool("PopLayer", true)
		kernel:addController(controlFactory:Create("Fader", param))

	end
end

function DeletePlayerConfirmFunc(message)

	if message:is("No", "press") then

		local panelWidget = GUI:getLayer("DeletePlayerConfirm"):getWidget("DeletePlayerConfirm")
		local fadeWidget = GUI:getLayer("DeletePlayerConfirmFade"):getWidget("BlackScreen")

		param = VariableSet()
		param:setWidget("Widget", panelWidget)
		param:setColor("To", Color(255, 255, 255, 0))
		param:setBool("PopLayer", true)
		kernel:addController(controlFactory:Create("Fader", param))

		param = VariableSet()
		param:setWidget("Widget", fadeWidget)
		param:setColor("To", Color(0, 0, 0, 0))
		param:setBool("PopLayer", true)
		kernel:addController(controlFactory:Create("Fader", param))

	elseif message:is("Yes", "press") then

		local panelWidget = GUI:getLayer("DeletePlayerConfirm"):getWidget("DeletePlayerConfirm")
		local fadeWidget = GUI:getLayer("DeletePlayerConfirmFade"):getWidget("BlackScreen")
		
		local state = GUI:getLayer("Profiles"):getWidget("Names"):QueryState(Message("CurrentItem"))
		local playerName = state:getData()

		if ( not(gameInfo:getName() == playerName) and not(gameInfo:getNumberOfPlayers() == 1)  ) then
			gameInfo:DeletePlayer(playerName)
			GUI:getLayer("Profiles"):getWidget("Names"):AcceptMessage(Message("Delete", playerName))
		end

		param = VariableSet()
		param:setWidget("Widget", panelWidget)
		param:setColor("To", Color(255, 255, 255, 0))
		param:setBool("PopLayer", true)
		kernel:addController(controlFactory:Create("Fader", param))

		param = VariableSet()
		param:setWidget("Widget", fadeWidget)
		param:setColor("To", Color(0, 0, 0, 0))
		param:setBool("PopLayer", true)
		kernel:addController(controlFactory:Create("Fader", param))
	end
end

function ExitConfirmFunc(message)

	if message:is("No", "press") then

		FadePopLayer("ExitConfirm", "ExitConfirm")

	elseif message:is("Yes", "press") then
		CloseApplication()
	end
end

function ResetProgressAndStartGameFromMenu()
	Sound:FadeOutAllTracks(1.0)

	gameInfo:ResetAllProgress()
	GUI:getLayer("MainMenu"):getWidget("zMenu"):AcceptMessage(Message("Start"))
	GUI:getLayer("MainMenu"):getWidget("zMenu"):AcceptMessage(Message("HideFaerie"))
	GUI:getLayer("BooksLayer"):getWidget("Books"):AcceptMessage(Message("InitViewFromMainMenu"))

	Sound:FadeInTrack("Books", 2.0, true)

	BlockMenu();
end

function StartGameFromMenu()
	Sound:FadeOutAllTracks(1.0)

	GUI:getLayer("MainMenu"):getWidget("zMenu"):AcceptMessage(Message("Start"))
	GUI:getLayer("MainMenu"):getWidget("zMenu"):AcceptMessage(Message("HideFaerie"))
	GUI:getLayer("BooksLayer"):getWidget("Books"):AcceptMessage(Message("InitViewFromMainMenu"))

	Sound:FadeInTrackContinue("Books", 3.0, true)

	BlockMenu();
end

function StartNewGameConfirmFunc(message)
	if message:is("No", "press") then
		FadePopLayer("StartNewGameConfirm", "StartNewGameConfirm")
	elseif message:is("Yes", "press") then
		FadePopLayer("StartNewGameConfirm", "StartNewGameConfirm")
		FadePopLayerWithEndHandler("ContinueSavedGame", "ContinueSavedGamePanel", "ResetProgressAndStartGameFromMenu")
	end
end

function ContinueSavedGameFunc(message)
	if message:is("Cancel", "press") then
		FadePopLayer("ContinueSavedGame", "ContinueSavedGamePanel")
	elseif message:is("StartNewGame", "press") then
		FadePushLayer("StartNewGameConfirm", "StartNewGameConfirm")
--		FadePopLayerWithEndHandler("ContinueSavedGame", "ContinueSavedGamePanel", "ResetProgressAndStartGameFromMenu")
	elseif message:is("Continue", "press") then
		gameInfo:ContinueSavedGame()

	elseif message:is("StartInBonusLevel") then
		param = VariableSet()
		param:setString("Layer1", "BonusLevel")
		param:setString("CrossHandler", "StartInBonusLevelCross")
		kernel:addController(controlFactory:Create("CrossFade", param))

		Sound:FadeOutAllTracks(1.0)
	elseif message:is("StartInISpyLevel") then
		param = VariableSet()
		param:setString("Layer1", "ISpyLevel")
		param:setString("CrossHandler", "StartInISpyCross")
		kernel:addController(controlFactory:Create("CrossFade", param))

		Sound:FadeOutAllTracks(1.0)
	elseif message:is("StartInLevelSavedGame") then
		param = VariableSet()
		param:setString("Layer1", "GameLayer")
		param:setString("CrossHandler", "StartInLevelSavedGame")
		kernel:addController(controlFactory:Create("CrossFade", param))

		Sound:FadeOutAllTracks(1.0)
	elseif message:is("StartInBooksSavedGame") then
		FadePopLayerWithEndHandler("ContinueSavedGame", "ContinueSavedGamePanel", "StartGameFromMenu")		
	end
end

function StartInISpyCross()
	UploadResourceGroup("ISpy")
	GUI:getLayer("ISpyLevel"):setUpdate(true)
	GUI:getLayer("ISpyLevel"):getWidget("ISpyLevel"):AcceptMessage(Message("LoadState"))

	Sound:FadeInTrack("ISpy", 2.0, true)
end

function StartInBonusLevelCross()
	GUI:getLayer("BooksLayer"):getWidget("Books"):AcceptMessage(Message("InitViewFromMainMenu"))
--	UploadResourceGroup("BooksAndBonus")
	gameInfo:UploadBooksAndBonus()

	GUI:getLayer("BonusLevel"):getWidget("BonusLevel"):AcceptMessage(Message("LoadState"))

	Sound:FadeInTrack("Differences", 2.0, true)
end

fadeScreens = 0

function ResetFadeScreens()
	fadeScreens = 0
end

function FadePushLayer(layerName, widgetName)

	fadeScreens = fadeScreens+1
	
	local fadeLayer = ""
	if (fadeScreens == 1) then
		fadeLayer = "Cross1"
	elseif (fadeScreens == 2) then
		fadeLayer = "Cross2"
	else
		fadeLayer = "Cross3"
	end
	
	local panelWidget = GUI:getLayer(layerName):getWidget(widgetName)
	panelWidget:setColor(Color(255, 255, 255, 0))

	local fadeWidget = GUI:getLayer(fadeLayer):getWidget("BlackScreen")
	fadeWidget:setColor(Color(0, 0, 0, 0))

	local param = VariableSet()
	param:setWidget("Widget", panelWidget)
	param:setColor("To", Color(255, 255, 255, 255))
	kernel:addController(controlFactory:Create("Fader", param))

	param = VariableSet()
	param:setWidget("Widget", fadeWidget)
	param:setColor("To", Color(0, 0, 0, 150))
	param:setString("FaderType", "Scaled")
	kernel:addController(controlFactory:Create("Fader", param))

	Screen:pushLayer(GUI:getLayer(fadeLayer))
	Screen:pushLayer(GUI:getLayer(layerName) )
end

function FadePushLayerWithEndHandler(layerName, widgetName, endHandler)

	fadeScreens = fadeScreens+1
	
	local fadeLayer = ""
	if (fadeScreens == 1) then
		fadeLayer = "Cross1"
	elseif (fadeScreens == 2) then
		fadeLayer = "Cross2"
	else
		fadeLayer = "Cross3"
	end
	
	local panelWidget = GUI:getLayer(layerName):getWidget(widgetName)
	panelWidget:setColor(Color(255, 255, 255, 0))

	local fadeWidget = GUI:getLayer(fadeLayer):getWidget("BlackScreen")
	fadeWidget:setColor(Color(0, 0, 0, 0))

	local param = VariableSet()
	param:setWidget("Widget", panelWidget)
	param:setColor("To", Color(255, 255, 255, 255))
	kernel:addController(controlFactory:Create("Fader", param))

	param = VariableSet()
	param:setWidget("Widget", fadeWidget)
	param:setColor("To", Color(0, 0, 0, 150))
	param:setString("FaderType", "Scaled")
	param:setString("EndHandler", endHandler)
	kernel:addController(controlFactory:Create("Fader", param))

	Screen:pushLayer(GUI:getLayer(fadeLayer))
	Screen:pushLayer(GUI:getLayer(layerName) )
end

function FadePopLayerWithEndHandler(layerName, widgetName, endHandler)
	
	local fadeLayer = ""
	if (fadeScreens == 1) then
		fadeLayer = "Cross1"
	elseif (fadeScreens == 2) then
		fadeLayer = "Cross2"
	else
		fadeLayer = "Cross3"
	end

	fadeScreens = fadeScreens-1

	local panelWidget = GUI:getLayer(layerName):getWidget(widgetName)
	local fadeWidget = GUI:getLayer(fadeLayer):getWidget("BlackScreen")

	param = VariableSet()
	param:setWidget("Widget", panelWidget)
	param:setColor("To", Color(255, 255, 255, 0))
	param:setBool("PopLayer", true)
	param:setString("Layer", layerName)
	kernel:addController(controlFactory:Create("Fader", param))

	param = VariableSet()
	param:setWidget("Widget", fadeWidget)
	param:setColor("To", Color(0, 0, 0, 0))
	param:setBool("PopLayer", true)
	param:setString("Layer", fadeLayer)
	param:setString("EndHandler", endHandler)
	param:setString("FaderType", "Scaled")
	kernel:addController(controlFactory:Create("Fader", param))
end

function FadePopLayer(layerName, widgetName)

	local fadeLayer = ""
	if (fadeScreens == 1) then
		fadeLayer = "Cross1"
	elseif (fadeScreens == 2) then
		fadeLayer = "Cross2"
	else
		fadeLayer = "Cross3"
	end

	fadeScreens = fadeScreens-1

	local panelWidget = GUI:getLayer(layerName):getWidget(widgetName)
	local fadeWidget = GUI:getLayer(fadeLayer):getWidget("BlackScreen")

	param = VariableSet()
	param:setWidget("Widget", panelWidget)
	param:setColor("To", Color(255, 255, 255, 0))
	param:setBool("PopLayer", true)
	param:setString("Layer", layerName)
	kernel:addController(controlFactory:Create("Fader", param))

	param = VariableSet()
	param:setWidget("Widget", fadeWidget)
	param:setColor("To", Color(0, 0, 0, 0))
	param:setBool("PopLayer", true)
	param:setString("Layer", fadeLayer)
	param:setString("FaderType", "Scaled")
	kernel:addController(controlFactory:Create("Fader", param))
end

function MainMenuToCredits(message)
	Screen:Clear()
	Screen:pushLayer(GUI:getLayer("Credits"))
end

function BlockMenu()
	GUI:getLayer("MainMenu"):getWidget("StartGame"):AcceptMessage(Message("Active", 0))
	GUI:getLayer("MainMenu"):getWidget("Options"):AcceptMessage(Message("Active", 0))
	GUI:getLayer("MainMenu"):getWidget("Help"):AcceptMessage(Message("Active", 0))
	GUI:getLayer("MainMenu"):getWidget("Hiscores"):AcceptMessage(Message("Active", 0))
	GUI:getLayer("MainMenu"):getWidget("Intro"):AcceptMessage(Message("Active", 0))
	GUI:getLayer("MainMenu"):getWidget("Exit"):AcceptMessage(Message("Active", 0))
	GUI:getLayer("MainMenu"):getWidget("ProfileButton"):AcceptMessage(Message("Active", 0))
end

function UnBlockMenu()
	GUI:getLayer("MainMenu"):getWidget("StartGame"):AcceptMessage(Message("Active", 1))
	GUI:getLayer("MainMenu"):getWidget("Options"):AcceptMessage(Message("Active", 1))
	GUI:getLayer("MainMenu"):getWidget("Help"):AcceptMessage(Message("Active", 1))
	GUI:getLayer("MainMenu"):getWidget("Hiscores"):AcceptMessage(Message("Active", 1))
	GUI:getLayer("MainMenu"):getWidget("Intro"):AcceptMessage(Message("Active", 1))
	GUI:getLayer("MainMenu"):getWidget("Exit"):AcceptMessage(Message("Active", 1))
	GUI:getLayer("MainMenu"):getWidget("ProfileButton"):AcceptMessage(Message("Active", 1))
end

function StartGameFirstTime()
	Sound:FadeOutAllTracks(1.0)
	GUI:getLayer("MainMenu"):getWidget("zMenu"):AcceptMessage(Message("Start"))
	GUI:getLayer("MainMenu"):getWidget("zMenu"):AcceptMessage(Message("HideFaerie"))
	GUI:getLayer("BooksLayer"):getWidget("Books"):AcceptMessage(Message("InitViewFromMainMenu"))
	Sound:FadeInTrack("Books", 1.0, true)

	BlockMenu()

end

multOnStart = false
function MainMenuFunc(message)
	if message:is("StartGame", "press") then

		if gameInfo:NoSavedStates() then
			multOnStart = true
			Sound:FadeOutAllTracks(1.0)
			local param = VariableSet()
			param:setString("Layer1", "Mult")
			param:setString("CrossHandler", "StartMultCross")
			param:setString("EndHandler", "StartMultEnd")
			kernel:addController(controlFactory:Create("CrossFade", param))
		else
			GUI:getLayer("ContinueSavedGame"):getWidget("ContinueSavedGamePanel"):AcceptMessage(Message("UpdateView"))
			GUI:getLayer("BooksLayer"):getWidget("Books"):AcceptMessage(Message("UpdateView"))
			FadePushLayer("ContinueSavedGame", "ContinueSavedGamePanel")
		end


	-- Опции
	elseif message:is("Options", "press") then
		SaveOptions()
		RestoreOptions()
		FadePushLayer("Options", "Options")

	-- Профили пользователей
	elseif message:is("ProfileButton", "press") then
		FadePushLayer("Profiles", "Profiles")
	-- Выход
	elseif message:is("Exit", "press") then
		FadePushLayer("ExitConfirm", "ExitConfirm")

	elseif message:is("Start") then

		UnBlockMenu();
		Screen:Clear()
		Screen:pushLayer(GUI:getLayer("BooksLayer"))
		GUI:getLayer("BooksLayer"):getWidget("Books"):setUpdate(true)
		GUI:getLayer("BooksLayer"):getWidget("Books"):AcceptMessage(Message("UpdateHints"))
		GUI:getLayer("MainMenu"):getWidget("zMenu"):AcceptMessage(Message("StopStarting"))
		GUI:getLayer("BooksLayer"):getWidget("Books"):AcceptMessage(Message("CheckFinalScene"))

	elseif message:is("Help", "press") then
		--UploadResourceGroup("ISpy")
		--GUI:getLayer("ISpyLevel"):setUpdate(true)
		--Screen:Clear()
		--Screen:pushLayer(GUI:getLayer("ISpyLevel"))
		--GUI:getLayer("ISpyLevel"):getWidget("ISpyLevel"):AcceptMessage(Message("LoadState"))
		--GUI:getLayer("ISpyLevel"):getWidget("ISpyLevel"):AcceptMessage(Message("StartLevel", 3))
		GUI:getLayer("Help"):getWidget("zHelp"):AcceptMessage(Message("SetPage", 0))
--		SimpleFadePushLayer("Help", "Help")
		GUI:getLayer("Help"):getWidget("zHelp"):AcceptMessage(Message("Upload"))
		UploadResourceGroup("Help")
		FadePushLayer("Help", "Help")
		--Screen:pushLayer(GUI:getLayer("Help"))
	elseif message:is("Intro", "press") then
--	        local param = VariableSet()
--	        param:setString("Layer1", "Credits")
--	        param:setString("CrossHandler", "ResetCredits2")
--		ResetCredits2()
--		Screen:pushLayer(GUI:getLayer("Credtis"))
--	        kernel:addController(controlFactory:Create("CrossFade", param))
--		GUI:getLayer("MainMenu"):getWidget("zMenu"):AcceptMessage(Message("HideInterface"))


--	        local param = VariableSet()
--	        param:setString("Layer1", "Presentation")
--	        param:setString("CrossHandler", "PresentationStartCrossHandler")
--	        param:setString("EndHandler", "PresentationStartHandler")
--	        kernel:addController(controlFactory:Create("CrossFade", param))


--	        local param = VariableSet()
--	        param:setString("Layer1", "FinalScreen")
--	        param:setString("CrossHandler", "ResetCredits")
--	        kernel:addController(controlFactory:Create("CrossFade", param))


		multOnStart = false

		Sound:FadeOutAllTracks(1.0)

		local param = VariableSet()
		param:setString("Layer1", "Mult")
		param:setString("CrossHandler", "StartMultCross")
		kernel:addController(controlFactory:Create("CrossFade", param))
		

	elseif message:is("Hiscores", "press") then
		GUI:getLayer("Highscores"):getWidget("zHighscores"):AcceptMessage(Message("Start"))
		FadePushLayer("Highscores", "Highscores")
	end
end

function PresentationStartCrossHandler()
	UploadResourceGroup("BooksLayer");
	UploadResourceGroup("BonusLevelRes");
	UploadResourceGroup("Book1");
	UploadResourceGroup("Book2");
	UploadResourceGroup("Book3");
	UploadResourceGroup("Book4");
end

function ResetCredits2()
	GUI:getLayer("Credits"):getWidget("Credits"):AcceptMessage(Message("Reset"))
	UploadResourceGroup("MainMenu")
end

function ResetCredits()
	GUI:getLayer("FinalScreen"):getWidget("FinalScreen"):AcceptMessage(Message("Reset"))
	Sound:FadeInTrackContinue("MainMenu", 2.0, true)
end

function PresentationStartHandler()
    GUI:getLayer("Presentation"):getWidget("Presentation"):AcceptMessage(Message("Activate"))
end

function PresentationFunc(message)
    if message:is("ClosePresentation") then
        local param = VariableSet()
        param:setString("Layer1", "MainMenu")
        kernel:addController(controlFactory:Create("CrossFade", param))
    else
        GUI:getLayer("Presentation"):getWidget("Presentation"):AcceptMessage(message)
    end
end

function FromFinalToMainMenu()
	gameInfo:UploadBooksAndBonus()
	gameInfo:DoCommand("ResetProgressAfterWin")
	GUI:getLayer("MainMenu"):getWidget("zMenu"):AcceptMessage(Message("ShowingFaerie"))
end

function FinalScreenFunc(message)
	if (message:is("Continue")) then
		param = VariableSet()
		param:setString("Layer1", "MainMenu")
	        param:setString("CrossHandler", "FromFinalToMainMenu")
		kernel:addController(controlFactory:Create("CrossFade", param))
	end
end

function ReleaseHelp()
	ReleaseResourceGroup("Help")
	GUI:getLayer("Help"):getWidget("zHelp"):AcceptMessage(Message("Release"))
end

function HelpFunc(message)
	if message:is("Back", "press") then
		FadePopLayerWithEndHandler("Help", "Help", "ReleaseHelp")
	elseif message:is("Left", "press") then
		GUI:getLayer("Help"):getWidget("zHelp"):AcceptMessage(Message("PrevPage"))
	elseif message:is("Right", "press") then
		GUI:getLayer("Help"):getWidget("zHelp"):AcceptMessage(Message("NextPage"))
	end
end

function HintFunc(message)
	if message:is("z_Continue", "press") then
		GUI:getLayer("BooksLayer"):getWidget("Books"):AcceptMessage(Message("CloseBook"))	
		GUI:getLayer("Hint"):getWidget("Hint"):AcceptMessage( Message("Hide") )
	elseif message:is("CrossToFinalScreen") then
	        local param = VariableSet()
	        param:setString("Layer1", "FinalScreen")
	        param:setString("CrossHandler", "ResetCredits")
	        kernel:addController(controlFactory:Create("CrossFade", param))
		Sound:FadeOutAllTracks(1.0)
	elseif message:is("1_BackToMenu", "press") then
		FadePushLayerWithEndHandler("MenuInGame", "MenuInGame", "PushMenuInGame")
--		FadePushLayer("MenuInGame", "MenuInGame")
--		PushMenuInGame()
	elseif message:is("Menu", "press") then
		GUI:getLayer("GameLayer"):getWidget("GameField"):AcceptMessage( Message("Pause") );
		if gameInfo:isSavedStateInLevel() then
			FadePushLayerWithEndHandler("MenuInGameMatch3", "MenuInGameMatch3", "PushMenuInGame")
		else
			FadePushLayerWithEndHandler("MenuInGame", "MenuInGame", "PushMenuInGame")
		end
--		FadePushLayerWithEndHandler("MenuInGameMatch3", "MenuInGameMatch3", "PushMenuInGame")
--		FadePushLayer("MenuInGameMatch3", "MenuInGameMatch3")
--		PushMenuInGame()
	elseif (message:is("1_BonusExit", "press")) then
		FadePushLayerWithEndHandler("MenuInGame", "MenuInGame", "PushMenuInGame")
--		FadePushLayer("MenuInGame", "MenuInGame")
--		PushMenuInGame()
	elseif message:is("1_ISpyMenu", "press") then
		FadePushLayerWithEndHandler("MenuInGame", "MenuInGame", "PushMenuInGame")
--		FadePushLayer("MenuInGame", "MenuInGame")
--		PushMenuInGame()
	elseif message:is("StartBonusLevel") then
		GUI:getLayer("BonusLevel"):setUpdate(true)
		Screen:Clear()
		Screen:pushLayer(GUI:getLayer("BonusLevel"))
		Screen:pushLayer(GUI:getLayer("Hint"))
		GUI:getLayer("BonusLevel"):getWidget("BonusLevel"):AcceptMessage(Message("StartLevel", message:getIntegerParam()))
		
		Sound:FadeOutAllTracks(1.0)
		Sound:FadeInTrack("Differences", 2.0, true)
	elseif message:is("1_LevelStart", "press") then
		BooksFunc(message)
	elseif message:is("Continue", "press") then
		GUI:getLayer("Hint"):getWidget("Hint"):AcceptMessage( Message("Hide") )
	elseif message:is("Ok", "press") then
		GUI:getLayer("Hint"):getWidget("Hint"):AcceptMessage( Message("Hide") )
	elseif message:is("DontShowAgain", "press") then
--		GUI:getLayer("Hint"):getWidget("Hint"):AcceptMessage( Message("HideAndDisableHints") )
		GUI:getLayer("Hint"):getWidget("Hint"):AcceptMessage( Message("Show", "DontShowHintConfirm") )
	elseif message:is("Yes", "press") then
		GUI:getLayer("Hint"):getWidget("Hint"):AcceptMessage( Message("Hide") )		
	elseif message:is("No", "press") then
		GUI:getLayer("Hint"):getWidget("Hint"):AcceptMessage( Message("Show", "DontShowHintConfirm2") )
		gameInfo:setHintsEnabled(false)
		GUI:getLayer("Hint"):getWidget("Hint"):AcceptMessage( Message("Hide") )		
	elseif message:is("Yes1", "press") then
		GUI:getLayer("Presentation"):getWidget("Presentation"):AcceptMessage(Message("FaerieYes"))
		GUI:getLayer("Hint"):getWidget("Hint"):AcceptMessage( Message("Hide") )		
	elseif message:is("No1", "press") then
		GUI:getLayer("Presentation"):getWidget("Presentation"):AcceptMessage(Message("FaerieNo"))
		GUI:getLayer("Hint"):getWidget("Hint"):AcceptMessage( Message("Hide") )
	elseif message:is("Map", "press") then
		GUI:getLayer("GameLayer"):getWidget("GameField"):AcceptMessage( Message("ShowMap") );
	elseif message:is("PopLayer") then
--		Screen:popLayer()
		Screen:RemoveLayer("Hint")
	elseif message:is("CompleteLevelCrossToBooks") then
		param = VariableSet()
		param:setString("Layer1", "BooksLayer")
		param:setString("CrossHandler", "CompleteLevelGameToBooksCrossHandler")
		kernel:addController(controlFactory:Create("CrossFade", param))

		Sound:FadeOutAllTracks(1.0)
	elseif message:is("FinishISpyLevelComplete") then
		GUI:getLayer("ISpyLevelComplete"):setUpdate(false)
		param = VariableSet()
		param:setString("Layer1", "BooksLayer")
		param:setString("CrossHandler", "CompleteISpyToBooksCross")
		param:setString("EndHandler", "CompleteISpyToBooksEnd")
		kernel:addController(controlFactory:Create("CrossFade", param))
		
		Sound:FadeOutAllTracks(1.0)
	end
end

function CompleteISpyToBooksCross()
	Sound:FadeInTrackContinue("Books", 3.0, true)
	ISpyClearLevel()
	ReleaseResourceGroup("ISpy")
end

function MenuInGameFunc(message)
	if message:is("AResume", "press") or message:is("Escape") then
		if (gameInfo:MenuInGameOnScreen()) then

			if gameInfo:getCurrentScreen() == "Game" and gameInfo:isSavedStateInLevel() then
				FadePopLayerWithEndHandler("MenuInGameMatch3", "MenuInGameMatch3", "ResumeGame_EndHandler")
			else
				FadePopLayerWithEndHandler("MenuInGame", "MenuInGame", "ResumeGame_EndHandler")
			end
		
			PopMenuInGame()
		end
	elseif message:is("ARestart", "press") then
		FadePushLayer("RestartLevelConfirm", "RestartLevelConfirm")
	elseif message:is("AExit", "press") then
		FadePushLayer("ExitToMainMenuConfirm", "ExitToMainMenuConfirm")
	elseif message:is("AHelp", "press") then
		GUI:getLayer("Help"):getWidget("zHelp"):AcceptMessage(Message("Upload"))
		UploadResourceGroup("Help")
		if gameInfo:getCurrentScreen() == "Game" then
			GUI:getLayer("Help"):getWidget("zHelp"):AcceptMessage(Message("SetPage", 2))
		elseif gameInfo:getCurrentScreen() == "Books" then
			GUI:getLayer("Help"):getWidget("zHelp"):AcceptMessage(Message("SetPage", 0))
		elseif gameInfo:getCurrentScreen() == "BonusLevel" then
			GUI:getLayer("Help"):getWidget("zHelp"):AcceptMessage(Message("SetPage", 3))
		elseif gameInfo:getCurrentScreen() == "MainMenu" then
			GUI:getLayer("Help"):getWidget("zHelp"):AcceptMessage(Message("SetPage", 0))
		elseif gameInfo:getCurrentScreen() == "ISpy" then
			GUI:getLayer("Help"):getWidget("zHelp"):AcceptMessage(Message("SetPage", 1))
		end
		FadePushLayer("Help", "Help")
		
	elseif message:is("AOptions", "press") then
		SaveOptions()
		RestoreOptions()
		FadePushLayer("Options", "Options")
	end	
end

function ResumeGame_EndHandler()

	if gameInfo:getCurrentScreen() == "Game" then
		GUI:getLayer("GameLayer"):getWidget("GameField"):AcceptMessage( Message("Continue") );
	elseif gameInfo:getCurrentScreen() == "Books" then
	elseif gameInfo:getCurrentScreen() == "BonusLevel" then
	elseif gameInfo:getCurrentScreen() == "MainMenu" then
	elseif gameInfo:getCurrentScreen() == "ISpy" then
	end
end

function ExitToMainMenuConfirmFunc(message)
	if message:is("Yes", "press") then
		Sound:FadeOutAllTracks(1.0)
		
		param = VariableSet()
		param:setString("Layer1", "MainMenu")

		if gameInfo:getCurrentScreen() == "Game" then
			param:setString("CrossHandler", "FromGameToMainMenu")
		elseif gameInfo:getCurrentScreen() == "Books" then
			param:setString("CrossHandler", "FromBooksToMainMenuCross")
		elseif gameInfo:getCurrentScreen() == "BonusLevel" then
			param:setString("CrossHandler", "FromBonusLevelToMainMenuCross")

		elseif gameInfo:getCurrentScreen() == "MainMenu" then

		elseif gameInfo:getCurrentScreen() == "ISpy" then
			param:setString("CrossHandler", "FromISpyToMainMenuCross")

		end

		kernel:addController(controlFactory:Create("CrossFade", param))
	elseif message:is("No", "press") then
		FadePopLayer("ExitToMainMenuConfirm", "ExitToMainMenuConfirm")
	end	
end

function RestartLevelConfirmFunc(message)
	if message:is("Yes", "press") then
		Sound:FadeOutAllTracks(1.0)
		
		local param = VariableSet()
		param:setString("Layer1", "GameLayer")
		param:setString("CrossHandler", "RestartGame")
		kernel:addController(controlFactory:Create("CrossFade", param))
	elseif message:is("No", "press") then
		FadePopLayer("RestartLevelConfirm", "RestartLevelConfirm")
	end	
end

function FromGameToMainMenu()
	ReleaseResourceGroup("GameLayer")
	ReleaseResourceGroup("Fish")
--	UploadResourceGroup("BooksAndBonus")
	gameInfo:UploadBooksAndBonus()

	GUI:getLayer("GameLayer"):getWidget("GameField"):AcceptMessage(Message("ReleaseLevelResource"))

	gameInfo:SaveState()
	
	gameInfo:MenuInGameExit();

	Sound:FadeInTrackContinue("MainMenu", 3.0, true)

	GUI:getLayer("MainMenu"):getWidget("zMenu"):AcceptMessage(Message("ShowingFaerie"))
end

function FromBonusLevelToMainMenuCross()
	Sound:FadeInTrackContinue("MainMenu", 3.0, true)

	gameInfo:MenuInGameExit()
	GUI:getLayer("MainMenu"):getWidget("zMenu"):AcceptMessage(Message("ShowingFaerie"))
end

function FromISpyToMainMenuCross()
	Sound:FadeInTrackContinue("MainMenu", 3.0, true)
	
	gameInfo:MenuInGameExit()

	GUI:getLayer("MainMenu"):getWidget("zMenu"):AcceptMessage(Message("ShowingFaerie"))
end

function GameFunc(message)

	if message:is("RunTextureRectGetter") then
		Screen:pushLayer(GUI:getLayer("FadeScreen"))
		Screen:pushLayer(GUI:getLayer("TextureRectGetter"))
	elseif message:is("ShowEditorList") then
		Screen:pushLayer(GUI:getLayer("EditorList"))
	elseif message:is("ShowEditorFileList") then
		Screen:pushLayer(GUI:getLayer("EditorFileList"))
	elseif message:is("ShowChipSelecter") then
		Screen:pushLayer(GUI:getLayer("ChipSelecter"))
	elseif message:is("RecSelecter") then
		Screen:pushLayer(GUI:getLayer("RecSelecter"))
	elseif message:is("ExitGame") then
		GUI:getLayer("GameLayer"):getWidget("GameField"):setUpdate(true)
		param = VariableSet()
		param:setString("Layer1", "BooksLayer")
		kernel:addController(controlFactory:Create("CrossFade", param))
	elseif message:is("ShowHint") then
		Screen:pushLayer( GUI:getLayer("Hint") )
		GUI:getLayer("GameLayer"):getWidget("GameField"):AcceptMessage( Message("ShowHint", message:getData() ) );
	elseif message:is("Map", "press") then
		GUI:getLayer("GameLayer"):getWidget("GameField"):AcceptMessage( Message("ShowMap") );
	elseif message:is("CloseMap", "press") then
		GUI:getLayer("GameLayer"):getWidget("GameField"):AcceptMessage( Message("HideMap") );
	elseif message:is("Menu", "press") then
		GUI:getLayer("GameLayer"):getWidget("GameField"):AcceptMessage( Message("Pause") );
		if gameInfo:isSavedStateInLevel() then
			FadePushLayerWithEndHandler("MenuInGameMatch3", "MenuInGameMatch3", "PushMenuInGame")
		else
			FadePushLayerWithEndHandler("MenuInGame", "MenuInGame", "PushMenuInGame")
		end
--		PushMenuInGame()
	elseif message:is("RestartLevel") then
		param = VariableSet()
		param:setString("Layer1", "GameLayer")
		param:setString("CrossHandler", "StartGameAfterLoseLife")
		kernel:addController(controlFactory:Create("CrossFade", param))
	end
end

function PushMenuInGame()
	gameInfo:PushMenuInGame()
end

function PopMenuInGame()
	gameInfo:PopMenuInGame()
end

function CompleteLevelGameToBooksCrossHandler()
	

	GUI:getLayer("GameLayer"):getWidget("GameField"):AcceptMessage(Message("ReleaseLevelResource"))
	ReleaseResourceGroup("Fish")

--	UploadResourceGroup("BooksAndBonus")
	gameInfo:UploadBooksAndBonus()

	GUI:getLayer("BooksLayer"):getWidget("Books"):AcceptMessage(Message("RunCompleteLevelProcess"))
	
	Sound:FadeInTrackContinue("Books", 3.0, true)
end

function FaerieBlockFadeFunc(message)
	if message:is("PopLayer") then
		Screen:popLayer()
	elseif message:is("CrossToFinalScreen") then
	        local param = VariableSet()
	        param:setString("Layer1", "FinalScreen")
	        param:setString("CrossHandler", "ResetCredits")
	        kernel:addController(controlFactory:Create("CrossFade", param))
		Sound:FadeOutAllTracks(1.0)
	end
end

function BooksFunc(message)
	if message:is("PushFaerieBlockFade") then
		Screen:pushLayer(GUI:getLayer("FaerieBlockFade"))
	elseif message:is("CrossToFinalScreen") then
	        local param = VariableSet()
	        param:setString("Layer1", "FinalScreen")
	        param:setString("CrossHandler", "ResetCredits")
	        kernel:addController(controlFactory:Create("CrossFade", param))
		Sound:FadeOutAllTracks(1.0)

	elseif message:is("StartBonusLevel") then
		GUI:getLayer("BonusLevel"):setUpdate(true)
		Screen:Clear()
		Screen:pushLayer(GUI:getLayer("BonusLevel"))
		GUI:getLayer("BonusLevel"):getWidget("BonusLevel"):AcceptMessage(Message("StartLevel", message:getIntegerParam()))
		
		Sound:FadeOutAllTracks(1.0)
		Sound:FadeInTrack("Differences", 2.0, true)
	elseif message:is("z_Continue", "press") then
		GUI:getLayer("BooksLayer"):getWidget("Books"):AcceptMessage(Message("CloseBook"))	
	elseif message:is("1_BackToMenu", "press") then
		FadePushLayerWithEndHandler("MenuInGame", "MenuInGame", "PushMenuInGame")
--		FadePushLayer("MenuInGame", "MenuInGame")
--		PushMenuInGame()
	elseif message:is("1_LevelStart", "press") then


		GUI:getLayer("BooksLayer"):getWidget("Books"):AcceptMessage( Message("UnblockAndHideFaerie") )

		if gameInfo:NeedStartISpyLevel() then
			Sound:FadeOutAllTracks(1.0)
--			RunISpyLevel(0)
			local param = VariableSet()
			param:setString("Layer1", "ISpyLevel")
			param:setString("CrossHandler", "FromBooksToISpyCross")
			param:setString("EndHandler", "FromBooksToISpyEnd")
			kernel:addController(controlFactory:Create("CrossFade", param))
		else
--			Sound:FadeOutAllTracks(1.0)
			local param = VariableSet()
			param:setString("Layer1", "GameLayer")
			param:setString("CrossHandler", "StartGame")
			param:setString("EndHandler", "FadeOutAllAndStartEnergyTrack")
			kernel:addController(controlFactory:Create("CrossFade", param))
		end
	else
		GUI:getLayer("BooksLayer"):getWidget("Books"):AcceptMessage(message)
	end
end

function FadeOutAll()
	Sound:FadeOutAllTracks(2.0)
end

function FadeOutAllAndStartEnergyTrack()
	Sound:FadeOutAllTracks(2.0)
	GUI:getLayer("GameLayer"):getWidget("GameField"):AcceptMessage( Message("PlayEnergyTrack") )
end

function FromBooksToISpyCross()
	UploadResourceGroup("ISpy")
	GUI:getLayer("ISpyLevel"):setUpdate(true)
	GUI:getLayer("ISpyLevel"):getWidget("ISpyLevel"):AcceptMessage(Message("StartLevel", gameInfo:getCompletedISpyLevels()))

	Sound:FadeInTrack("ISpy", 2.0, true)
end

function FromBooksToISpyEnd()
end

function FromBooksToMainMenuCross()
	Sound:FadeInTrackContinue("MainMenu", 3.0, true)

	gameInfo:MenuInGameExit()

--	GUI:getLayer("MainMenu"):getWidget("zMenu"):AcceptMessage(Message("ShowingFaerie"))

	GUI:getLayer("MainMenu"):getWidget("zMenu"):AcceptMessage(Message("ShowingFaerie"))
end

function StartGame()
	GUI:getLayer("GameLayer"):setUpdate(true)
	GUI:getLayer("GameLayer"):getWidget("GameField"):AcceptMessage(Message("StartLevel"))

--	ReleaseResourceGroup("BooksAndBonus")
	gameInfo:ReleaseBooksAndBonus()

	ReleaseResourceGroup("MainMenu")
	
--	gameInfo:FadeInGameTrack()

	gameInfo:PopMenuInGame()
end

function RestartGame()
	GUI:getLayer("GameLayer"):getWidget("GameField"):AcceptMessage(Message("RestartLevel"))

--	gameInfo:FadeInGameTrack()

	gameInfo:PopMenuInGame()
end

function StartGameAfterLoseLife()
	GUI:getLayer("GameLayer"):setUpdate(true)
	GUI:getLayer("GameLayer"):getWidget("GameField"):AcceptMessage(Message("StartLevelAfterLoseLife"))
	
	gameInfo:FadeInGameTrack()
end

function StartInLevelSavedGame()

--	ReleaseResourceGroup("BooksAndBonus")
	gameInfo:ReleaseBooksAndBonus()

	GUI:getLayer("GameLayer"):setUpdate(true)
	GUI:getLayer("GameLayer"):getWidget("GameField"):AcceptMessage(Message("StartSavedGame"))

	gameInfo:FadeInGameTrack()

	gameInfo:PopMenuInGame()
end

function TextureRectGetterFunc(message)
	if (message:is("Ok", "press")) then
		GUI:getLayer("TextureRectGetter"):getWidget("TextureRectGetter"):AcceptMessage(Message("RectGetterEnd"))
		Screen:popLayer()
		Screen:popLayer()
	end
end;

function EditorListFunc(message)
	if (message:is("cUp", "press")) then
		GUI:getLayer("EditorList"):getWidget("List"):AcceptMessage(Message("ScrollUp"))
	elseif (message:is("cDown", "press")) then
		GUI:getLayer("EditorList"):getWidget("List"):AcceptMessage(Message("ScrollDown"))
	elseif (message:is("cOk", "press")) then
		GUI:getLayer("GameLayer"):getWidget("GameField"):AcceptMessage(Message("RyushkaSelected"));
		Screen:popLayer()
	elseif (message:is("cCancel", "press")) then
		Screen:popLayer()
	elseif (message:is("c1", "press")) then
		GUI:getLayer("EditorList"):getWidget("zRyushkaSelect"):AcceptMessage(Message("SelectTab", 1))
	elseif (message:is("c2", "press")) then
		GUI:getLayer("EditorList"):getWidget("zRyushkaSelect"):AcceptMessage(Message("SelectTab", 2))
	elseif (message:is("c3", "press")) then
		GUI:getLayer("EditorList"):getWidget("zRyushkaSelect"):AcceptMessage(Message("SelectTab", 3))
	elseif (message:is("c4", "press")) then
		GUI:getLayer("EditorList"):getWidget("zRyushkaSelect"):AcceptMessage(Message("SelectTab", 4))
	elseif (message:is("c5", "press")) then
		GUI:getLayer("EditorList"):getWidget("zRyushkaSelect"):AcceptMessage(Message("SelectTab", 5))
	elseif (message:is("c6", "press")) then
		GUI:getLayer("EditorList"):getWidget("zRyushkaSelect"):AcceptMessage(Message("SelectTab", 6))
	elseif (message:is("c7", "press")) then
		GUI:getLayer("EditorList"):getWidget("zRyushkaSelect"):AcceptMessage(Message("SelectTab", 7))
	elseif (message:is("c8", "press")) then
		GUI:getLayer("EditorList"):getWidget("zRyushkaSelect"):AcceptMessage(Message("SelectTab", 8))
	elseif (message:is("c9", "press")) then
		GUI:getLayer("EditorList"):getWidget("zRyushkaSelect"):AcceptMessage(Message("SelectTab", 9))
	elseif (message:is("c10", "press")) then
		GUI:getLayer("EditorList"):getWidget("zRyushkaSelect"):AcceptMessage(Message("SelectTab", 10))
	elseif (message:is("c11", "press")) then
		GUI:getLayer("EditorList"):getWidget("zRyushkaSelect"):AcceptMessage(Message("SelectTab", 11))
	elseif (message:is("c12", "press")) then
		GUI:getLayer("EditorList"):getWidget("zRyushkaSelect"):AcceptMessage(Message("SelectTab", 12))
	elseif (message:is("c13", "press")) then
		GUI:getLayer("EditorList"):getWidget("zRyushkaSelect"):AcceptMessage(Message("SelectTab", 13))
	elseif (message:is("c14", "press")) then
		GUI:getLayer("EditorList"):getWidget("zRyushkaSelect"):AcceptMessage(Message("SelectTab", 14))
	elseif (message:is("c15", "press")) then
		GUI:getLayer("EditorList"):getWidget("zRyushkaSelect"):AcceptMessage(Message("SelectTab", 15))
	elseif (message:is("c16", "press")) then
		GUI:getLayer("EditorList"):getWidget("zRyushkaSelect"):AcceptMessage(Message("SelectTab", 16))
	end
end

function EditorFileListFunc(message)
	if (message:is("Return", "press")) then
		Screen:popLayer()
	elseif (message:is("Load", "press")) then
		GUI:getLayer("EditorFileList"):getWidget("EditorFileList"):AcceptMessage(Message("Load"))
	elseif (message:is("Save", "press")) then
		GUI:getLayer("EditorFileList"):getWidget("EditorFileList"):AcceptMessage(Message("Save"))
	elseif (message:is("Delete", "press")) then
		GUI:getLayer("EditorFileList"):getWidget("EditorFileList"):AcceptMessage(Message("Delete"))
	elseif (message:is("ShowConfirmRewrite")) then
		Screen:pushLayer(GUI:getLayer("ConfirmRewriteLevel"))
	elseif (message:is("Clear")) then
		GUI:getLayer("GameLayer"):getWidget("GameField"):AcceptMessage(Message("ClearLevel"));
	end
end

function ConfirmRewriteLevelFunc(message)
	if (message:is("Yes", "press")) then
		GUI:getLayer("EditorFileList"):getWidget("EditorFileList"):AcceptMessage(Message("SaveAlways"))
		Screen:popLayer()
		Screen:popLayer()
	elseif (message:is("Cancel", "press")) then
		Screen:popLayer()
	end
end

function ChipSelecterFunc(message)
	if (message:is("ok", "press")) then
		GUI:getLayer("GameLayer"):getWidget("GameField"):AcceptMessage(Message("GetColors"));
		Screen:popLayer()	
	elseif (message:is("cancel", "press")) then
		Screen:popLayer()	
	end
end

function RecSelecterFunc(message)
	if (message:is("Ok")) then
		GUI:getLayer("GameLayer"):getWidget("GameField"):AcceptMessage(Message("GetRec"));
		Screen:popLayer()	
	end
end

function BonusLevelFunc(message)
	if (message:is("NewDifference", "press")) then
		GUI:getLayer("BonusLevel"):getWidget("BonusLevel"):AcceptMessage(Message("NewDifference"));	
	elseif (message:is("DeleteDifference", "press")) then
		GUI:getLayer("BonusLevel"):getWidget("BonusLevel"):AcceptMessage(Message("DeleteDifference"));	
	elseif (message:is("Load", "press")) then
		GUI:getLayer("EditorFileList"):getWidget("EditorFileList"):AcceptMessage(Message("Load"))
	elseif message:is("ShowEditorFileList") then
		Screen:pushLayer(GUI:getLayer("EditorFileList"))
	elseif message:is("PopLayer") then
		Screen:popLayer()
	elseif (message:is("1_BonusExit", "press")) then
		FadePushLayerWithEndHandler("MenuInGame", "MenuInGame", "PushMenuInGame")
--		FadePushLayer("MenuInGame", "MenuInGame")
--		PushMenuInGame()
	elseif message:is("CompleteLevelReturnToBooks") then
		Screen:Clear()
		Screen:pushLayer(GUI:getLayer("BooksLayer"))
		
		Sound:FadeOutAllTracks(1.0)
		Sound:FadeInTrackContinue("Books", 3.0, true)
		
	elseif message:is("1_BonusHint", "press") then
		GUI:getLayer("BonusLevel"):getWidget("BonusLevel"):AcceptMessage(Message("FaerieBonusLevelShowHint"));
	end
end

function ISpyClearLevel()
	GUI:getLayer("ISpyLevel"):setUpdate(false)
	GUI:getLayer("ISpyLevel"):getWidget("ISpyLevel"):AcceptMessage(Message("Clear"))
end

function CompleteISpyToBooksEnd()
	GUI:getLayer("BooksLayer"):getWidget("Books"):AcceptMessage(Message("DestroyLockAfterISpy"))	
end

function ISpyLevelFunc(message)
	if (message:is("1_ISpyMenu", "press")) then
		FadePushLayerWithEndHandler("MenuInGame", "MenuInGame", "PushMenuInGame")
--		FadePushLayer("MenuInGame", "MenuInGame")
--		PushMenuInGame()
		--param = VariableSet()
		--param:setString("Layer1", "MainMenu")
		--param:setString("EndHandler", "ISpyClearLevel")
		--kernel:addController(controlFactory:Create("CrossFade", param))
		--GUI:getLayer("MainMenu"):getWidget("zMenu"):AcceptMessage(Message("ShowingFaerie"))
	elseif message:is("StartISpyLevelComplete") then
		Screen:pushLayer(GUI:getLayer("ISpyLevelComplete"))
		GUI:getLayer("ISpyLevelComplete"):setUpdate(true)
		GUI:getLayer("ISpyLevelComplete"):getWidget("ISpyLevelComplete"):AcceptMessage(message)
	elseif message:is("1_ISpyHint", "press") then
		GUI:getLayer("ISpyLevel"):getWidget("ISpyLevel"):AcceptMessage(Message("FaerieISpyHint"))
		GUI:getLayer("ISpyLevel"):getWidget("ISpyLevel"):AcceptMessage(message)
	else
		GUI:getLayer("ISpyLevel"):getWidget("ISpyLevel"):AcceptMessage(message)
	end
end

function ISpyLevelCompleteFunc(message)
	GUI:getLayer("ISpyLevelComplete"):getWidget("ISpyLevelComplete"):AcceptMessage(message)
end

function CreditsToMainMenu(message)
	Screen:Clear()
	Screen:pushLayer(GUI:getLayer("MainMenu"))

	GUI:getLayer("MainMenu"):getWidget("zMenu"):AcceptMessage(Message("ShowingFaerie"))
end

function CreditsFunc(message)
	if (message:is("Exit")) then
		Screen:Clear()
		Screen:pushLayer(GUI:getLayer("MainMenu"))
		GUI:getLayer("MainMenu"):getWidget("zMenu"):AcceptMessage(Message("ShowInterface"))
	end
end

function LoadScreenFunc(message)
	if message:is("Start") then
		local param = VariableSet()
--		param:setString("Layer1", "GameLayer")
--		param:setString("CrossHandler", "DebugStartGame")

--		param:setString("Layer1", "BooksLayer")
--		param:setString("CrossHandler", "DebugStartFromBooks")
--		param:setString("EndHandler", "DebugStartFromBooks1")

--		param:setString("Layer1", "Mult")
--		param:setString("CrossHandler", "StartMultCross")

		param:setString("Layer1", "MainMenu")
		param:setString("CrossHandler", "StartFromMainMenu")
		param:setString("EndHandler", "StartFromMainMenuEnd")

--		param:setString("Layer1", "ISpyLevel")
--		param:setString("CrossHandler", "DebugStartISpy")

		kernel:addController(controlFactory:Create("CrossFade", param))

--			local param = VariableSet()
--			param:setString("Layer1", "MainMenu")
--			param:setString("CrossHandler", "ToMainMenuCross")
--			param:setString("EndHandler", "CreateFirstPlayer")
--			kernel:addController(controlFactory:Create("CrossFade", param))
--		else
--			local param = VariableSet()
--			param:setString("Layer1", "JuleBook")
--			param:setString("CrossHandler", "FromLoadScreenStart")
--			kernel:addController(controlFactory:Create("CrossFade", param))
--		end
	end
end

function DebugStartGame()
	gameInfo:setLevel(58)
	gameInfo:FadeInGameTrack()
	GUI:getLayer("GameLayer"):setUpdate(true)
	GUI:getLayer("GameLayer"):getWidget("GameField"):AcceptMessage(Message("StartLevel"))
end

function DebugStartISpy()
	gameInfo:ResetAllProgress()

	UploadResourceGroup("ISpy")
	GUI:getLayer("ISpyLevel"):setUpdate(true)
	GUI:getLayer("ISpyLevel"):getWidget("ISpyLevel"):AcceptMessage(Message("StartLevel", 0))
end

function DebugStartFromBooks()
--	gameInfo:myDebug1()
--	gameInfo:ResetAllProgress()
	gameInfo:setLevel(9)
	GUI:getLayer("BooksLayer"):getWidget("Books"):AcceptMessage(Message("InitViewFromMainMenu"))
	GUI:getLayer("BooksLayer"):setUpdate(true)
--	UploadResourceGroup("BooksAndBonus")
	gameInfo:UploadBooksAndBonus()
end

function StartMultCross()
--	ReleaseResourceGroup("BooksAndBonus")

	UploadResourceGroup("Mult")
	UploadResource("ReceiversSymbols")

	gameInfo:DoCommand("SetMultSound")

	GUI:getLayer("Mult"):getWidget("Mult"):AcceptMessage(Message("Upload"))
	GUI:getLayer("Mult"):getWidget("Mult"):AcceptMessage(Message("Reset"))
	GUI:getLayer("Mult"):getWidget("Mult"):AcceptMessage(Message("Play"))

	Sound:FadeInTrack("Theme", 2.0, true)
end

function StartMultEnd()
end

function StartFromMainMenu()
--	gameInfo:ResetAllProgress()
--	gameInfo:setLevel(2)
--	GUI:getLayer("BooksLayer"):setUpdate(true)
--	UploadResourceGroup("BooksAndBonus")
	gameInfo:UploadBooksAndBonus()
--	UploadResourceGroup("MainMenu")
end

function StartFromMainMenuEnd()
--	gameInfo:setLevel(0)
	if (gameInfo:NoPlayers()) then
		FadePushLayer("Profiles", "Profiles")
		GUI:getLayer("NewProfile"):getWidget("Name"):AcceptMessage(Message("Clear"))
		FadePushLayer("NewProfile", "NewProfile")
	end
end

function DebugStartFromBooks1()
	Sound:FadeInTrack("Books", 1.0, true)
end

function HighscoresFunc(message)
	if message:is("Ok", "press") then
		GUI:getLayer("Highscores"):getWidget("zHighscores"):AcceptMessage(Message("Finish"))
		FadePopLayer("Highscores", "Highscores")
	end
end

function ExitMultEnd()
	if gameInfo:NoSavedStates() and multOnStart then
		StartGameFirstTime()
		gameInfo:DoCommand("SaveMultLog");
	else
		Sound:FadeInTrackContinue("MainMenu", 3.0, true)
	end
end

function ExitMultCross()
--	UploadResourceGroup("BooksAndBonus")
	
	gameInfo:DoCommand("ResetMultSound")

	Sound:FadeOutAllTracks(1.0)
	Sound:StopAllSounds()

	GUI:getLayer("Mult"):getWidget("Mult"):AcceptMessage(Message("Stop"))
	GUI:getLayer("Mult"):getWidget("Mult"):AcceptMessage(Message("Release"))
	GUI:getLayer("MainMenu"):getWidget("zMenu"):AcceptMessage(Message("Init"))

	ReleaseResource("ReceiversSymbols")
end

function MultFunc(message)
	if (message:is("continue", "press")) then
		Sound:FadeOutAllTracks(1.0)

		local param = VariableSet()
		param:setString("Layer1", "MainMenu")
		param:setString("CrossHandler", "ExitMultCross")
		param:setString("EndHandler", "ExitMultEnd")

		kernel:addController(controlFactory:Create("CrossFade", param))
	end
end