
-- Главное Меню

gameLayer = "StageLayer_1"
helpFromGame = 0
lastStage = -1
currentHistoryPage = 0
currentCompletePage = 0
shieldFromLastLevel = false

needShieldTip = false

needPlayAnimation = false
MultName = {"Phoenicia", "Babylon", "Egypt", "Carth", "Rome", "Greece", "Troy"}
fieldNames = {"Phoenicia", "Babylon", "Egypt", "Carthage", "Rome", "Greece", "Troy"}

showMults = true

cycleMult = true

lastMusicVolume = 0
lastSoundVolume = 0
lastFullscreen = false
lastCustomCursor = false
lastShowHints = false

creditsFromGame = false

optionsLayerName = "Options"
exitGameLayer = "ExitGame"

function SaveOptions()
	
	-- Запоминаем опции
	lastMusicVolume = gameInfo:getMusicVolume()
	lastSoundVolume = gameInfo:getSoundVolume()
	lastFullscreen = gameInfo:getFullscreen()
	lastCustomCursor = gameInfo:getCustomCursor()
	lastShowHints = gameInfo:getHintsEnabled()

end

function RestoreOptions()

	-- Восстанавливаем опции
	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("Tips"):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()

	-- Устанавливаем опции (для чтения используется слой 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:SetSoundVolume(sndVolume)
	Sound:SetMusicVolume(musVolume)
	gameInfo:setMusicVolume(musVolume)
	gameInfo:setSoundVolume(sndVolume)

	if state:getData() == "1" and gameInfo:getFullscreen() == false then
		gameInfo:setFullscreen(true)
		SetFullscreenMode()
	end

	if state:getData() == "0" and gameInfo:getFullscreen() == 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("Tips"):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:getFullscreen() then
		iBoolean = "1"
	end
	GUI:getLayer(optionsName):getWidget("Fullscreen"):AcceptMessage(Message("SetState", iBoolean))

	iBoolean = "0"
	if gameInfo:getHintsEnabled() then
		iBoolean = "1"
	end
	GUI:getLayer(optionsName):getWidget("Tips"):AcceptMessage(Message("SetState", iBoolean))
	
	iBoolean = "0"
	if gameInfo:getCustomCursor() then
		iBoolean = "1"
	end
	GUI:getLayer(optionsName):getWidget("CustomCursor"):AcceptMessage(Message("SetState", iBoolean))

end

function SendReshuffle()
	GUI:getLayer(gameLayer):getWidget("GameField"):AcceptMessage(Message("Reshuffle"))
end

function GameFieldWriteDebugInfo()
	GUI:getLayer(gameLayer):getWidget("GameField"):AcceptMessage(Message("WriteDebugInfo"))
end

function StartGame()

	--gameInfo:Load()
	--gameInfo:setLives(3)
	--gameInfo:Save()
	      
	GUI:getLayer("Map"):getWidget("Map"):AcceptMessage( Message("InitFlagPos") )
	GUI:getLayer("Map"):getWidget("Map"):AcceptMessage( Message("ShowArtefactTip") )
	GUI:getLayer(gameLayer):getWidget("GameField"):AcceptMessage(Message("Load"))
	GUI:getLayer(gameLayer):getWidget("GameField"):AcceptMessage(Message("Reset"))

	param = VariableSet()
	param:setString("Layer1", "Map")
	creditsFromGame = true

	if (gameInfo:getMaxLevel() == 1  and showMults) then
		
		gameInfo:FirstMultIsShow()

        	local widget = GUI:getLayer("Mult"):getWidget("Panel")
		local fadeWidget = GUI:getLayer("Fade"):getWidget("Fade")
		fadeWidget:setColor(Color(0, 0, 0, 150))

		local animationWidget = GUI:getLayer("Mult"):getWidget("Mult")
		animationWidget:setColor(Color(255, 255, 255, 255))
		widget:setColor(Color(255, 255, 255, 255))
		animationWidget:AcceptMessage(Message("BlackScreen", 1))
		SetArtefactScene(gameInfo:getStage())

		param:setString("CrossHandler", "UploadAnimation")
		param:setString("EndHandler", "ShowAnimation")
		param:setString("Layer2", "Fade")
		param:setString("Layer3", "Mult")
	end

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

	Sound:FadeOutAllTracks(3.0)
	Sound:FadeInTrack("Map", 3.0, true)
end

function SyncMenuGems()
	GUI:getLayer("Menu"):getWidget("1_MenuEffect"):AcceptMessage(Message("SyncGems"))
end

function HistoryReset()
	historyWidget = GUI:getLayer("History"):getWidget("History")
	historyWidget:AcceptMessage(Message("SetPage", 0))
	historyWidget:AcceptMessage(Message("Show", 1))
	
	gameInfo:setMenuParam1(7)
end

function GalleryUpload()
	UploadResource("ANIMATION_PANEL")
	UploadResource("ANIMATION_FADE")
end

function PlayGalleryAnimation()
	animName = MultName[gameInfo:getMaxStage()]
	local animationWidget = GUI:getLayer("Gallery"):getWidget("Mult")
	animationWidget:AcceptMessage(Message("Active", 0))
	animationWidget:AcceptMessage(Message("BlackScreen", 1))
end

function ShowAnimation()

	cycleMult = false
	animationWidget = GUI:getLayer("Mult"):getWidget("Mult")
	animationWidget:AcceptMessage(Message("BlackScreen", 0))
	animationWidget:AcceptMessage(Message("Reset"))
	animationWidget:AcceptMessage(Message("Active", 1))
	animationWidget:AcceptMessage(Message("Play", MultName[gameInfo:getStage()]))
end

function UploadAnimation()

	animationWidget = GUI:getLayer("Mult"):getWidget("Mult")
	
	animationWidget:AcceptMessage(Message("Play", MultName[gameInfo:getStage()]))
	animationWidget:AcceptMessage(Message("UploadResources"))
	animationWidget:AcceptMessage(Message("Reset"))
end

function HistoryUpload()
	UploadResource("fish0")
	UploadResource("fish1")
	UploadResource("fish2")
	UploadResource("fish3")
	UploadResource("fish4")
end

function IncrementTip()
	local tipNum = gameInfo:getWiseTipNumber()
	tipNum = tipNum+1
	if tipNum > 26 then
		tipNum = 0
	end
	gameInfo:setWiseTipNumber(tipNum)
end

function SetHiscoreActive(actived)
	GUI:getLayer("Menu"):getWidget("1_MenuEffect"):AcceptMessage(Message("HiscoreTick", actived))
end

function MainMenuFunc(message)

	if message:is("Options", "press") then
		--Sound:PlaySample("Test")
		
		--GUI:getLayer("Menu"):getWidget("1_MenuEffect"):AcceptMessage(Message("SyncGems"))
		
		SaveOptions()

		optionsWidget = GUI:getLayer("Options"):getWidget("Options")
		optionsWidget:setColor(Color(255, 255, 255, 0))

		fadeWidget = GUI:getLayer("Fade"):getWidget("Fade")
		fadeWidget:setColor(Color(0, 0, 0, 0))

		param = VariableSet()
		param:setWidget("Widget", optionsWidget)
		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("Fade"))
		Screen:pushLayer(GUI:getLayer("Options"))
		
		SetHiscoreActive(0)
		
		--optionsWidget:AddEffect("WindowShow")
		
	elseif message:is("GetSaver", "press") then
		--Sound:PlaySample("Test")
		
		--GUI:getLayer("Menu"):getWidget("1_MenuEffect"):AcceptMessage(Message("SyncGems"))

                GUI:getLayer("Menu"):getWidget("1_MenuEffect"):AcceptMessage(Message("HideSaverTip"))
                gameInfo:setShowSSTip(true)
		local saverLayer = "ScreensaverNotReady"

		if gameInfo:getCycle() > 1 then
			saverLayer = "ScreensaverReady"
		end

		local rootWidget = GUI:getLayer(saverLayer):getWidget("Panel")
		rootWidget:setColor(Color(255, 255, 255, 0))

		fadeWidget = GUI:getLayer("Fade"):getWidget("Fade")
		fadeWidget:setColor(Color(0, 0, 0, 0))

		param = VariableSet()
		param:setWidget("Widget", rootWidget)
		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("Fade"))
		Screen:pushLayer(GUI:getLayer(saverLayer))

		GUI:getLayer(saverLayer):getWidget("SaverView"):AcceptMessage(Message("Reset"))
		
		SetHiscoreActive(0)

		--optionsWidget:AddEffect("WindowShow")

	elseif message:is("ProfileButton", "press") then
	
		local panelWidget = GUI:getLayer("Profiles"):getWidget("Panel")
		panelWidget:setColor(Color(255, 255, 255, 0))

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

		GUI:getLayer("Profiles"):getWidget("Names"):AcceptMessage(Message("Set", gameInfo:getName()))

		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("Fade"))
		Screen:pushLayer(GUI:getLayer("Profiles"))
		
		SetHiscoreActive(0)

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

        	GUI:getLayer("Map"):getWidget("Map"):AcceptMessage(Message("InitAboutText"))
		GUI:getLayer("Shield"):getWidget("Shield"):AcceptMessage(Message("Reset"))
		SetHiscoreActive(0)
		StartGame()

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

		helpFromGame = 0
		GUI:getLayer("HelpLayer"):getWidget("Help"):AcceptMessage( Message("Init") )
		param = VariableSet()
		param:setString("Layer1", "HelpLayer")
		param:setString("CrossHandler", "UploadHelp")
		param:setString("EndHandler", "HelpEndHandler")
		SetHiscoreActive(0)
		kernel:addController(controlFactory:Create("CrossFade", param))


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

		local panelWidget = GUI:getLayer("EndGame"):getWidget("Panel")
		panelWidget:setColor(Color(255, 255, 255, 0))

		local fadeWidget = GUI:getLayer("Fade"):getWidget("Fade")
		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))

                SetHiscoreActive(0)
		Screen:pushLayer(GUI:getLayer("Fade"))
		Screen:pushLayer(GUI:getLayer("EndGame"))


        elseif message:is("Scores", "press") then
		--helpFromGame = 0
		--GUI:getLayer("HelpLayer"):getWidget("Help"):AcceptMessage( Message("Init") )
		--param = VariableSet()
		--param:setString("Layer1", "HelpLayer")
		--param:setString("EndHandler", "HelpEndHandler")
		--kernel:addController(controlFactory:Create("CrossFade", param))
		
		--GUI:getLayer("Gallery"):getWidget("0_MultList"):AcceptMessage(Message("Reset"))
		GUI:getLayer("Gallery"):getWidget("a_MultList"):AcceptMessage(Message("Reset"))
		--GUI:getLayer("Gallery"):getWidget("zCaption"):AcceptMessage(Message("SetString", "Travels"))

		cycleMult = true
		local animationWidget = GUI:getLayer("Gallery"):getWidget("Mult")
		animationWidget:AcceptMessage(Message("BlackScreen", 1))
		animationWidget:AcceptMessage(Message("Visible", 0))

             	SetHiscoreActive(0)

		param = VariableSet()
		param:setString("Layer1", "Gallery")
		param:setString("EndHandler", "PlayGalleryAnimation")
		param:setString("CrossHandler", "GalleryUpload")
		kernel:addController(controlFactory:Create("CrossFade", param))

	end

end

function ScreensaverReadyFunc(message)

	if message:is("Ok", "press") then
		local panelWidget = GUI:getLayer("ScreensaverReady"):getWidget("Panel")
		local fadeWidget = GUI:getLayer("Fade"):getWidget("Fade")

		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))

		SetHiscoreActive(1)

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

		local rootWidget = GUI:getLayer("GetScreensaver"):getWidget("Panel")
		rootWidget:setColor(Color(255, 255, 255, 0))

		local fadeWidget = GUI:getLayer("Fade2"):getWidget("Fade")
		fadeWidget:setColor(Color(0, 0, 0, 0))

		local param = VariableSet()
		param:setWidget("Widget", rootWidget)
		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("Fade2"))
		Screen:pushLayer(GUI:getLayer("GetScreensaver"))

	end
end

function GetScreensaverFunc(message)

	if message:is("No", "press") then
		local panelWidget = GUI:getLayer("GetScreensaver"):getWidget("Panel")
		local fadeWidget = GUI:getLayer("Fade2"):getWidget("Fade")

		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
		NeedGetScreensaver()
		CloseApplication()
	end

end

function ScreensaverNotReadyFunc(message)
	if message:is("Ok", "press") then
		local panelWidget = GUI:getLayer("ScreensaverNotReady"):getWidget("Panel")
		local fadeWidget = GUI:getLayer("Fade"):getWidget("Fade")

		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))

		SetHiscoreActive(1)
	end
end


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

		local panelWidget = GUI:getLayer("EndGame"):getWidget("Panel")
		local fadeWidget = GUI:getLayer("Fade"):getWidget("Fade")

		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))
		
		SetHiscoreActive(1)

		--optionsWidget:AddEffect("WindowShow")

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

		CloseApplication()

	end


end

function ProfileFunc(message)

	if message:is("Names", "DoubleClick") then
		Sound:PlaySample("ButtonClick")
	end

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

		local panelWidget = GUI:getLayer("Profiles"):getWidget("Panel")
		local fadeWidget = GUI:getLayer("Fade"):getWidget("Fade")

		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))
		
		SetHiscoreActive(1)

		--optionsWidget:AddEffect("WindowShow")

	elseif message:is("Ok", "press") or message:is("Names", "DoubleClick") then
	
		GUI:getLayer("Menu"):getWidget("1_MenuEffect"):AcceptMessage(Message("HideSaverTip"))
		GUI:getLayer("Menu"):getWidget("1_MenuEffect"):AcceptMessage(Message("EndGemEffects"))

		local panelWidget = GUI:getLayer("Profiles"):getWidget("Panel")
		local fadeWidget = GUI:getLayer("Fade"):getWidget("Fade")

		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))

		local state = GUI:getLayer("Profiles"):getWidget("Names"):QueryState(Message("CurrentItem"))
		local playerName = state:getData()

		gameInfo:setActivePlayer(playerName)
		SetShowHints()
		GUI:getLayer("Menu"):getWidget("ProfileButton"):AcceptMessage(Message("SetProfile", playerName))

		SetHiscoreActive(1)

		--optionsWidget:AddEffect("WindowShow")
	elseif message:is("0_Up", "press") then
		local namesWidget = GUI:getLayer("Profiles"):getWidget("Names")
		namesWidget:AcceptMessage(Message("ScrollDown"))
	elseif message:is("0_Down", "press") then
		local namesWidget = GUI:getLayer("Profiles"):getWidget("Names")
		namesWidget:AcceptMessage(Message("ScrollUp"))
	elseif message:is("New", "press") then

    		GUI:getLayer("NewProfile"):getWidget("InfoText"):AcceptMessage(Message("SetString", "maximum 12 characters"))
		GUI:getLayer("NewProfile"):getWidget("Name"):AcceptMessage(Message("Clear"))

		local panelWidget = GUI:getLayer("NewProfile"):getWidget("Panel")
		panelWidget:setColor(Color(255, 255, 255, 0))

		local fadeWidget = GUI:getLayer("Fade2"):getWidget("Fade")
		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("Fade2"))
		Screen:pushLayer(GUI:getLayer("NewProfile"))

	elseif message:is("Delete", "press") then
		
		local needDeletePlayer = true
		local needCheckActive = true
		
		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("Panel")
			panelWidget:setColor(Color(255, 255, 255, 0))
	
			local fadeWidget = GUI:getLayer("Fade2"):getWidget("Fade")
			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("Fade2"))
			Screen:pushLayer(GUI:getLayer("LastNoDelete"))
			
			needDeletePlayer = false
			needCheckActive = false
		end

		if needCheckActive and (gameInfo:getName() == playerName) then

			local panelWidget = GUI:getLayer("ActiveNoDelete"):getWidget("Panel")
			panelWidget:setColor(Color(255, 255, 255, 0))
	
			local fadeWidget = GUI:getLayer("Fade2"):getWidget("Fade")
			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("Fade2"))
			Screen:pushLayer(GUI:getLayer("ActiveNoDelete"))
			
			needDeletePlayer = false
		end

		if needDeletePlayer then

			GUI:getLayer("DeletePlayerConfirm"):getWidget("PlayerName"):AcceptMessage(Message("SetString", playerName))

			local panelWidget = GUI:getLayer("DeletePlayerConfirm"):getWidget("Panel")
			panelWidget:setColor(Color(255, 255, 255, 0))
	
			local fadeWidget = GUI:getLayer("Fade2"):getWidget("Fade")
			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("Fade2"))
			Screen:pushLayer(GUI:getLayer("DeletePlayerConfirm"))
		
		end

	end


end

function ActiveDeleteMessageFunc(message)

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

		local panelWidget = GUI:getLayer("ActiveNoDelete"):getWidget("Panel")
		local fadeWidget = GUI:getLayer("Fade2"):getWidget("Fade")

		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 LastDeleteMessageFunc(message)

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

		local panelWidget = GUI:getLayer("LastNoDelete"):getWidget("Panel")
		local fadeWidget = GUI:getLayer("Fade2"):getWidget("Fade")

		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 DeletePlayerFunc(message)

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

		local panelWidget = GUI:getLayer("DeletePlayerConfirm"):getWidget("Panel")
		local fadeWidget = GUI:getLayer("Fade2"):getWidget("Fade")

		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("Panel")
		local fadeWidget = GUI:getLayer("Fade2"):getWidget("Fade")
		
		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 NewProfileFunc(message)

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

		local panelWidget = GUI:getLayer("NewProfile"):getWidget("Panel")
		local fadeWidget = GUI:getLayer("Fade2"):getWidget("Fade")

		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))

		--optionsWidget:AddEffect("WindowShow")

	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
                		GUI:getLayer("Menu"):getWidget("1_MenuEffect"):AcceptMessage(Message("HideSaverTip"))
				GUI:getLayer("Menu"):getWidget("ProfileButton"):AcceptMessage(Message("SetProfile", profileName))
				local prologueCaption = profileName.."!"
				GUI:getLayer("History"):getWidget("Prologue"):AcceptMessage(Message("SetString", prologueCaption))
				GUI:getLayer("Menu"):getWidget("ProfileButton"):AcceptMessage(Message("SetProfile", profileName))
				GUI:getLayer("Profiles"):getWidget("Names"):AcceptMessage(Message("Add", profileName))
				gameInfo:AddNewPlayer(profileName)
				SetShowHints()

	               		--historyButton = GUI:getLayer("History"):getWidget("Next")
	        	       	--historyButton:AcceptMessage(Message("SetText", "Next"))

				param = VariableSet()
				param:setString("CrossHandler", "HistoryUpload")
				param:setString("Layer1", "History")
				param:setString("EndHandler", "HistoryReset")
				kernel:addController(controlFactory:Create("CrossFade", param))
			end

		end
	end

end

function FirstProfileFunc(message)

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

		state = GUI:getLayer("FirstProfile"):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
				GUI:getLayer("Menu"):getWidget("ProfileButton"):AcceptMessage(Message("SetProfile", profileName))
				local prologueCaption = profileName.."!"
				GUI:getLayer("History"):getWidget("Prologue"):AcceptMessage(Message("SetString", prologueCaption))
				GUI:getLayer("Menu"):getWidget("ProfileButton"):AcceptMessage(Message("SetProfile", profileName))
				GUI:getLayer("Profiles"):getWidget("Names"):AcceptMessage(Message("Add", profileName))
				gameInfo:AddNewPlayer(profileName)
				SetShowHints()

	               		--historyButton = GUI:getLayer("History"):getWidget("Next")
	        	       	--historyButton:AcceptMessage(Message("SetText", "Next"))

				param = VariableSet()
				param:setString("CrossHandler", "HistoryUpload")
				param:setString("Layer1", "History")
				param:setString("EndHandler", "HistoryReset")
				kernel:addController(controlFactory:Create("CrossFade", param))
			end

		end
	end

end


function UploadHelp()
	UploadResourceGroup("Egypt")
	UploadResourceGroup("StageLayer_3")
	UploadArtefactElements(2)
end

function ReleaseHelp()
	GUI:getLayer("HelpLayer"):getWidget("Help"):AcceptMessage( Message("Stop") )
	ReleaseResourceGroup("Egypt")
	ReleaseResourceGroup("StageLayer_3")
	ReleaseArtefactElements(2)
	
	if helpFromGame == 1 then
		UploadGamefield()
	end
end


function MenuInGameFunc(message)

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

		widget = GUI:getLayer("MenuInGame"):getWidget("MenuInGame")
		fadeWidget = GUI:getLayer("Fade_2"):getWidget("Fade_2")

		param = VariableSet()
		param:setWidget("Widget", widget)
		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))
		GUI:getLayer(gameLayer):getWidget("GameField"):AcceptMessage(Message("Continue"))

	elseif message:is("Help", "press") then
		helpFromGame = 1
		GUI:getLayer("HelpLayer"):getWidget("Help"):AcceptMessage( Message("Init") )
		param = VariableSet()
		param:setString("Layer1", "HelpLayer")
		param:setString("CrossHandler", "UploadHelp")
		param:setString("EndHandler", "HelpEndHandler")
		kernel:addController(controlFactory:Create("CrossFade", param))
	elseif message:is("Options", "press") then
		
		SaveOptions()

		optionsWidget = GUI:getLayer("Options"):getWidget("Options")
		optionsWidget:setColor(Color(255, 255, 255, 0))

		fadeWidget = GUI:getLayer("Fade"):getWidget("Fade")
		fadeWidget:setColor(Color(0, 0, 0, 0))

		param = VariableSet()
		param:setWidget("Widget", optionsWidget)
		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("Fade"))
		Screen:pushLayer(GUI:getLayer("Options"))
	elseif message:is("MainMenu", "press") then

		exitGameLayer = "ExitGameNoLostLife"
		if (gameInfo:NeedDecLivesOnExit() == true) then
			exitGameLayer = "ExitGame"
		end
		
		optionsWidget = GUI:getLayer(exitGameLayer):getWidget("ExitGame")
		optionsWidget:setColor(Color(255, 255, 255, 0))

		fadeWidget = GUI:getLayer("Fade"):getWidget("Fade")
		fadeWidget:setColor(Color(0, 0, 0, 0))

		param = VariableSet()
		param:setWidget("Widget", optionsWidget)
		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("Fade"))
		Screen:pushLayer(GUI:getLayer(exitGameLayer))
	end
end

function ExitGameFunc(message)

	if message:is("Yes", "press") then
	
		creditsFromGame = false
	
		Sound:FadeOutAllTracks(3.0)
		Sound:FadeInTrack("Menu", 3.0, true)

		param = VariableSet()
		param:setString("Layer1", "Menu");
		param:setString("CrossHandler", "ExitGameCrossHandler")
		param:setString("EndHandler", "SyncMenuGems")
		kernel:addController(controlFactory:Create("CrossFade", param))
		
		SetHiscoreActive(1)

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

		optionsWidget = GUI:getLayer(exitGameLayer):getWidget("ExitGame")
		fadeWidget = GUI:getLayer("Fade"):getWidget("Fade")

		param = VariableSet()
		param:setWidget("Widget", optionsWidget)
		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 ExitGameCrossHandler()
	GUI:getLayer(gameLayer):getWidget("GameField"):AcceptMessage(Message("Reset"))

	gameInfo:WriteResultToHiScores()
	gameInfo:ResetInfoToBeginLevel()
	
	if (gameInfo:NeedDecLivesOnExit() == true) then
		local lives = gameInfo:getLives()
		lives = lives - 1
		gameInfo:setLives(lives)
		if (lives < 1) then
			gameInfo:IncreaseDifficultyLevel()
			GameOverEndHandler()
		end
	end
	
	if (gameInfo:IsGameOver() == true) then
		gameInfo:setScore(0)
		gameInfo:setUID()
	end
	gameInfo:Save()
end

function GameOverFunc(message)
	if message:is("GameOver", "press") then
		Sound:FadeOutAllTracks(2.0);
		Sound:FadeInTrack("Map", 3.0, true);
		param = VariableSet()
		param:setString("Layer1", "Map");
		param:setString("EndHandler", "GameOverEndHandler")
		kernel:addController(controlFactory:Create("CrossFade", param))
	end
end

function GameOverEndHandler()
	GUI:getLayer("Map"):getWidget("Map"):AcceptMessage( Message("DestroyCountryProgress") )
	gameInfo:setLives(3)
	gameInfo:setScore(0)
	gameInfo:setUID()
end

function CreditsUpload()
	UploadResource("fish0")
	UploadResource("fish1")
	UploadResource("fish2")
	UploadResource("fish3")
	UploadResource("fish4")
	
	GUI:getLayer("Credits"):getWidget("Credits"):AcceptMessage(Message("Reset"))

	UploadResourceGroup("History")
end

function CreditsRelease()
	ReleaseResourceGroup("History")
end

-- Опции
function OptionsFunc(message)

	if message:is("Cancel", "press") then
	
		RestoreOptions()
	
		optionsWidget = GUI:getLayer("Options"):getWidget("Options")
		fadeWidget = GUI:getLayer("Fade"):getWidget("Fade")

		param = VariableSet()
		param:setWidget("Widget", optionsWidget)
		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))
		
		if not creditsFromGame then
			SetHiscoreActive(1)
		end

		--optionsWidget:AddEffect("WindowShow")

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

		optionsWidget = GUI:getLayer("Options"):getWidget("Options")
		fadeWidget = GUI:getLayer("Fade"):getWidget("Fade")

		param = VariableSet()
		param:setWidget("Widget", optionsWidget)
		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))

		if not creditsFromGame then
			SetHiscoreActive(1)
		end

		--optionsWidget:AddEffect("WindowShow")
	elseif message:is("Credits", "press") then
	
		param = VariableSet()
		param:setString("CrossHandler", "CreditsUpload")
		param:setString("Layer1", "Credits")
		--param:setString("EndHandler", "CreditsReset")
		kernel:addController(controlFactory:Create("CrossFade", param))

	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)

	end
end

function CreditsFunc(message)

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

		--param = VariableSet()
		--param:setString("CrossHandler", "CreditsRelease")
		--param:setString("Layer1", "Menu")
		--kernel:addController(controlFactory:Create("CrossFade", param))
		if creditsFromGame then
			param = VariableSet()
			param:setString("Layer1", gameLayer);
			param:setString("Layer2", "Fade_2");
			param:setString("Layer3", "MenuInGame");
			param:setString("CrossHandler", "CreditsRelease")
			kernel:addController(controlFactory:Create("CrossFade", param))
		else
			SetHiscoreActive(1)
			param = VariableSet()
			param:setString("Layer1", "Menu");
			param:setString("CrossHandler", "CreditsRelease")
			kernel:addController(controlFactory:Create("CrossFade", param))
		end

	end
end

-- Карта
function MapFunc(message)

	if message:is("cancel", "press") then
	
		creditsFromGame = false
	
		GUI:getLayer("Map"):getWidget("Map"):AcceptMessage(Message("HideArtefactTip"))

		Sound:FadeOutAllTracks(3.0)
		Sound:FadeInTrack("Menu", 3.0, true)

		param = VariableSet()
		param:setString("Layer1", "Menu");
		param:setString("EndHandler", "SyncMenuGems")
		kernel:addController(controlFactory:Create("CrossFade", param))

		SetHiscoreActive(1)

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

		if (gameInfo:getStage() == 1) then
			gameLayer = "StageLayer_1"
		elseif (gameInfo:getStage() == 2) then
			gameLayer = "StageLayer_2"
		elseif (gameInfo:getStage() == 3) then
			gameLayer = "StageLayer_3"
		elseif (gameInfo:getStage() == 4) then
			gameLayer = "StageLayer_4"
		elseif (gameInfo:getStage() == 5) then
			gameLayer = "StageLayer_5"
		elseif (gameInfo:getStage() == 6) then
			gameLayer = "StageLayer_6"
		elseif (gameInfo:getStage() == 7) then
			gameLayer = "StageLayer_7"
		end
		
		GUI:getLayer("Map"):getWidget("Map"):AcceptMessage(Message("HideArtefactTip"))

		--GUI:getLayer(gameLayer):getWidget("GameField"):AcceptMessage( Message("InitLevelView") )
		GUI:getLayer(gameLayer):getWidget("GameField"):AcceptMessage( Message("ClearSun") )

		param = VariableSet()
		param:setString("Layer1", gameLayer);
		param:setString("EndHandler", "MapEndHandler")
		param:setString("CrossHandler", "MapCrossHandler")
		kernel:addController(controlFactory:Create("CrossFade", param))
		
		shieldFromLastLevel = true

	elseif message:is("GoToArtefactScreen") then
	
		gameInfo:setNeedShowArtefactTip(false)
		GUI:getLayer("Map"):getWidget("Map"):AcceptMessage(Message("HideArtefactTip"))

		param = VariableSet()
		param:setString("Layer1", "Shield");
		kernel:addController(controlFactory:Create("CrossFade", param))
		
		GUI:getLayer("Shield"):getWidget("Shield"):AcceptMessage(Message("Reset"))
		
		shieldFromLastLevel = false

	end
end

function ReleaseGamefield()
	if (not (lastStage == -1)) then
		ReleaseResourceGroup(fieldNames[lastStage])
		ReleaseArtefactElements(lastStage-1)
	end
end

function UploadGamefield()
	if (not (lastStage == -1)) then
		UploadResourceGroup(fieldNames[lastStage])
		UploadArtefactElements(lastStage-1)
	end
end

function MapCrossHandler()
	if (not (gameInfo:getStage() == lastStage)) then
		ReleaseGamefield()
		lastStage = gameInfo:getStage()
		--UploadGamefield()
	end
	UploadGamefield()
	GUI:getLayer(gameLayer):getWidget("GameField"):AcceptMessage( Message("InitLevelView") )
end

function GameFunc(message)

	GUI:getLayer(gameLayer):getWidget("GameField"):AcceptMessage( message )

	--if message:is("Escape") then

       		--Sound:FadeOutAllTracks(3.0)
		--Sound:FadeInTrack("Map", 3.0, true)

		--param = VariableSet()
		--param:setString("Layer1", "Map")
		--kernel:addController(controlFactory:Create("CrossFade", param))


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

		GUI:getLayer(gameLayer):getWidget("GameField"):AcceptMessage(Message("Pause"))

		optionsWidget = GUI:getLayer("MenuInGame"):getWidget("MenuInGame")
		optionsWidget:setColor(Color(255, 255, 255, 0))

		fadeWidget = GUI:getLayer("Fade_2"):getWidget("Fade_2")
		fadeWidget:setColor(Color(0, 0, 0, 0))

		param = VariableSet()
		param:setWidget("Widget", optionsWidget)
		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("Fade_2"))
		Screen:pushLayer(GUI:getLayer("MenuInGame"))

	elseif message:is("NextLevel") then

		if (gameInfo:getStage() == 1) then
			gameLayer = "StageLayer_1"
		elseif (gameInfo:getStage() == 2) then
			gameLayer = "StageLayer_2"
		elseif (gameInfo:getStage() == 3) then
			gameLayer = "StageLayer_3"
		elseif (gameInfo:getStage() == 4) then
			gameLayer = "StageLayer_4"
		elseif (gameInfo:getStage() == 5) then
			gameLayer = "StageLayer_5"
		elseif (gameInfo:getStage() == 6) then
			gameLayer = "StageLayer_6"
		elseif (gameInfo:getStage() == 7) then
			gameLayer = "StageLayer_7"
		end
		GUI:getLayer(gameLayer):getWidget("GameField"):AcceptMessage(Message("InitLevelView"))
		GUI:getLayer(gameLayer):getWidget("GameField"):AcceptMessage(Message("Start"))

		Screen:popLayer()
		Screen:pushLayer( GUI:getLayer(gameLayer) )

	end


end

function LoseLifeFunc(message)
	if message:is("LoseLife", "press") then	
		widget = GUI:getLayer("LoseLife"):getWidget("LoseLife")
		fadeWidget = GUI:getLayer("Fade_2"):getWidget("Fade_2")

		param = VariableSet()
		param:setWidget("Widget", widget)
		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))

--		GUI:getLayer(gameLayer):getWidget("GameField"):AcceptMessage( Message("InitLevelView") )
--		GUI:getLayer(gameLayer):getWidget("GameField"):AcceptMessage( Message("Start") )
--		GUI:getLayer(gameLayer):getWidget("GameField"):AcceptMessage( Message("SetActive", 1) )
		GUI:getLayer(gameLayer):getWidget("GameField"):AcceptMessage( Message("RunStart") )

	end
end


function HelpEndHandler()
	GUI:getLayer("HelpLayer"):getWidget("Help"):AcceptMessage( Message("Start") )
end

function HelpFunc(message)
	if message:is("Start", "press") then
		if helpFromGame==1 then
			param = VariableSet()
			param:setString("Layer1", gameLayer);
			param:setString("Layer2", "Fade_2");
			param:setString("Layer3", "MenuInGame");
			param:setString("EndHandler", "ExitHelpHandler")
			param:setString("CrossHandler", "ReleaseHelp")
			kernel:addController(controlFactory:Create("CrossFade", param))
		else
			SetHiscoreActive(1)
			param = VariableSet()
			param:setString("Layer1", "Menu");
			param:setString("EndHandler", "ExitHelpHandler")
			param:setString("CrossHandler", "ReleaseHelp")
			kernel:addController(controlFactory:Create("CrossFade", param))
		end
	end
end

function StatCrossHandler()
	gameInfo:WriteResultToHiScores()
	if (gameInfo:IsGameOver() == true) then
		gameInfo:setScore(0)
		gameInfo:setUID()
	end
	gameInfo:Save()
end

function StartArtCollecting()
			GUI:getLayer(gameLayer):getWidget("GameField"):AcceptMessage(Message("StartCollectArtefact"))
end

function StatFunc(message)
	if message:is("Ok", "press") then
		if gameInfo:getNeedCollectArtefact() == true then
			widget = GUI:getLayer("Statistics"):getWidget("Statistics")
			fadeWidget = GUI:getLayer("Fade"):getWidget("Fade")
	
			param = VariableSet()
			param:setWidget("Widget", widget)
			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)
			param:setString("EndHandler", "StartArtCollecting")
			kernel:addController(controlFactory:Create("Fader", param))
		elseif (gameInfo:IsGameOver() == true) then

			Sound:FadeOutAllTracks(3.0)
			Sound:FadeInTrack("Menu", 3.0, true)

			param = VariableSet()
			param:setString("Layer1", "Menu")
			kernel:addController(controlFactory:Create("CrossFade", param))
		else
			Sound:FadeOutAllTracks(3.0)
			Sound:FadeInTrack("Map", 3.0, true)
			
			IncrementTip()

			GUI:getLayer("Map"):getWidget("Map"):AcceptMessage( Message("PauseLightPath") )
			GUI:getLayer("Map"):getWidget("Map"):AcceptMessage( Message("EnableFlagMover") )
			GUI:getLayer("Map"):getWidget("Map"):AcceptMessage( Message("ShowArtefactTip") )
			param = VariableSet()
			param:setString("Layer1", "Map")
			param:setString("EndHandler", "RunFlagMover")
			param:setString("CrossHandler", "StatCrossHandler")
			kernel:addController(controlFactory:Create("CrossFade", param))
		end
	elseif message:is("Statistics", "press") then
		GUI:getLayer("Statistics"):getWidget("GameStatistics"):AcceptMessage(Message("Finish"))
	else
		GUI:getLayer(gameLayer):getWidget("GameField"):AcceptMessage( message )
	end
end

function MapEndHandler()

	stageNum = gameInfo:getStage()-1
	startName = gameInfo:getStageStartMusicName(stageNum)
	mainName =  gameInfo:getStageMainMusicName(stageNum)
	mainDelay = gameInfo:getStageMainMusicDelay(stageNum)
	startFade = gameInfo:getStageStartFadeSpeed(stageNum)
	mainFade = gameInfo:getStageMainFadeSpeed(stageNum)

	Sound:FadeOutAllTracks(1.0)
	Sound:FadeInTrack(mainName, mainFade, true)
	--Sound:DelayPlayTrack(mainName, mainDelay, mainFade, true)
	
	if stageNum == 0 then
		Sound:FadeInTrack("Sea", mainFade, true)
	end

	GUI:getLayer(gameLayer):getWidget("GameField"):AcceptMessage( Message("Start") )
	GUI:getLayer(gameLayer):getWidget("GameField"):AcceptMessage( Message("SetActive", 1) )
end

function RunFlagMover()
	GUI:getLayer("Map"):getWidget("Map"):AcceptMessage( Message("MoveFlag") )
end

function PlayMultAfterShield()
	if needPlayAnimation then
		ShowAnimation()
		needPlayAnimation = false
	end
end

function ExitHelpHandler()
	GUI:getLayer("HelpLayer"):getWidget("Help"):AcceptMessage( Message("Stop") )
end

function ShieldFunc(message)
	if message:is("Ok", "press") then
		if (gameInfo:getWin() == true) then
		
			GUI:getLayer("Map"):getWidget("Map"):AcceptMessage( Message("InitFlagPos") )

			Sound:FadeOutAllTracks(3.0)
			Sound:FadeInTrack("Map", 3.0, true)

			param = VariableSet()
			param:setString("Layer1", "CompleteScreen")
			param:setString("CrossHandler", "UploadCompleteScreen")
			param:setString("EndHandler", "CompleteScreenReset")
			kernel:addController(controlFactory:Create("CrossFade", param))
		else
			Sound:FadeOutAllTracks(3.0)
			Sound:FadeInTrack("Map", 3.0, true)

			param = VariableSet()
			param:setString("Layer1", "Map")

			if (gameInfo:getMaxLevel() == 1 and showMults and shieldFromLastLevel) then

				needPlayAnimation = true

		        	local widget = GUI:getLayer("Mult"):getWidget("Panel")
				local fadeWidget = GUI:getLayer("Fade"):getWidget("Fade")
				fadeWidget:setColor(Color(0, 0, 0, 150))

				local animationWidget = GUI:getLayer("Mult"):getWidget("Mult")
				animationWidget:setColor(Color(255, 255, 255, 255))
				widget:setColor(Color(255, 255, 255, 255))
				animationWidget:AcceptMessage(Message("BlackScreen", 1))
				SetArtefactScene(gameInfo:getStage())
				
				if gameInfo:getMaxStage() == 2 then
					needShieldTip = true
				end

				param:setString("CrossHandler", "UploadAnimation")
				param:setString("Layer2", "Fade")
				param:setString("Layer3", "Mult")
				param:setString("EndHandler", "PlayMultAfterShield")
			else
				param:setString("EndHandler", "RunFlagMover")
			end

			kernel:addController(controlFactory:Create("CrossFade", param))
		end
	else
		GUI:getLayer(gameLayer):getWidget("GameField"):AcceptMessage( message )
	end
end

function AnimationFunc(message)
	if message:is("Ok", "press") then
		local widget = GUI:getLayer("Mult"):getWidget("Panel")
		fadeWidget = GUI:getLayer("Fade"):getWidget("Fade")

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

		local animationWidget = GUI:getLayer("Mult"):getWidget("Mult")
		animationWidget:AcceptMessage(Message("Active", 0))

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

		Screen:popLayer()

	elseif message:is("Panel", "press") then
		SkipScene()
	end

end


