
function FirstMenuLoad()

	Screen:pushLayer( GUI:getLayer("Menu") )
	Sound:FadeInTrack("Menu", 2.0, true)

	if gameInfo:NoPlayers() then

		SetHiscoreActive(0)

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

		local panelWidget = GUI:getLayer("FirstProfile"):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("FirstProfile"))

	end

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

function AnimationInit()
end

function SetSplashInvisible()
	ResetSplash()
	GUI:getLayer("PubSplash"):getWidget("Splash"):AcceptMessage(Message("View"))
end

function CrossToSplash2()
	GUI:getLayer("PubSplash2"):getWidget("Splash"):AcceptMessage(Message("View"))
end

function StartMenu()

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

	param = VariableSet()
	param:setString("Layer1", "Menu");
	param:setString("CrossHandler", "SetSplashInvisible")
	param:setString("EndHandler", "FirstMenuLoad")
	kernel:addController(controlFactory:Create("CrossFade", param))
end

function SplashFunc(message)
	if message:is("Panel", "press") then
		if gameInfo:getNumOfSplashes() > 2 then
			param = VariableSet()
			param:setString("Layer1", "PubSplash2");
			param:setString("CrossHandler", "CrossToSplash2")
			kernel:addController(controlFactory:Create("CrossFade", param))
		else
			StartMenu()
		end
	end
end

function SplashFunc2(message)
	if message:is("Panel", "press") then
		StartMenu()
	end
end

function DoMenu()

	if gameInfo:getNumOfSplashes() > 1 then

		param = VariableSet()
		param:setString("Layer1", "PubSplash");
		param:setString("CrossHandler", "SetSplashInvisible")
		kernel:addController(controlFactory:Create("CrossFade", param))
	else
		StartMenu()
	end
end

function DoGallery(multId)
	UploadResource("ANIMATION_FADE")
	GUI:getLayer("Gallery"):getWidget("MultList"):AcceptMessage(Message("Reset"))
	Screen:pushLayer( GUI:getLayer("Gallery") )
	PlayGalleryMult(multId)
end

function DoTest()
	GUI:LoadLayers("PartTest.xml")
	Screen:pushLayer( GUI:getLayer("Test") )
	--local effect = GUI:getLayer("Test"):getWidget("ptest"):AddEffect("MouseDown")
end

function DoCompleteGame()
	UploadResource("fish0")
	UploadResource("fish1")
	UploadResource("fish2")
	UploadResource("fish3")
	UploadResource("fish4")
	UploadResourceGroup("CompleteScreen")
	UploadResourceGroup("History")
	GUI:getLayer("History"):getWidget("History"):AcceptMessage(Message("Show", 1))
	Screen:pushLayer(GUI:getLayer("CompleteScreen"))
end

function SetShowHints()
	if gameInfo:getHintsEnabled() then
		GUI:getLayer("Options"):getWidget("Tips"):AcceptMessage(Message("SetState", "1"))
	else
		GUI:getLayer("Options"):getWidget("Tips"):AcceptMessage(Message("SetState", "0"))
	end
end

LoadResource("fonts.xml")

-- Shared resources
LoadResource("GUI_Resource.xml")
LoadResource("Sound.xml")
LoadResource("MultSound.xml")
LoadResource("Music.xml")

LoadResource("GameResource.xml")
LoadResource("ChipsAnim.xml")
LoadResource("Gallery_resource.xml")

LoadParticles("effects.xml")
LoadEffects("effects.xml")

LoadParticles("fakel_eff.xml")
LoadEffects("fakel_eff.xml")

LoadParticles("StringEffects.xml")
LoadEffects("StringEffects.xml")

LoadParticles("ShieldEffects.xml")
LoadEffects("ShieldEffects.xml")

LoadParticles("MenuParticles.xml")
LoadEffects("MenuParticles.xml")

LoadParticles("MapEffects.xml")
LoadEffects("MapEffects.xml")

GUI:LoadText("text.xml")

LoadResource("Map_Resource.xml")
LoadResource("Menu_Resource.xml")

-- GUI Layers
GUI:LoadLayers("Map_GUI.xml")
GUI:LoadLayers("Menu_GUI.xml")
GUI:LoadLayers("Stat_GUI.xml")
GUI:LoadLayers("MenuInGame_GUI.xml")
GUI:LoadLayers("GameField_GUI.xml")
GUI:LoadLayers("Help_GUI.xml")
GUI:LoadLayers("PartTest.xml")
GUI:LoadLayers("Shield_GUI.xml")
GUI:LoadLayers("History_GUI.xml")
GUI:LoadLayers("EndGame_GUI.xml")

-- Animation
--LoadResource("FieldResource.xml")
LoadParticles("MultEffects.xml")
LoadEffects("MultEffects.xml")
LoadAnimation()
GUI:LoadLayers("MultTest.xml")

-- Profiles
if not gameInfo:NoPlayers() then

	local namesWidget = GUI:getLayer("Profiles"):getWidget("Names")
	local numOfPlayers = gameInfo:getNumberOfPlayers()-1
	for i = 0, numOfPlayers do
		local playerName = gameInfo:getPlayerName(i)
		namesWidget:AcceptMessage(Message("Add", playerName))
	end

	namesWidget:AcceptMessage(Message("Select", gameInfo:getName()))
	GUI:getLayer("Menu"):getWidget("ProfileButton"):AcceptMessage(Message("SetProfile", gameInfo:getName()))
	SetShowHints()
end

local sndVolume = gameInfo:getSoundVolume()
local musVolume = gameInfo:getMusicVolume()

Sound:SetSoundVolume(sndVolume)
Sound:SetMusicVolume(musVolume)

local iFullscreen = "0"
if gameInfo:getFullscreen() then
	iFullscreen = "1"
end

local iCCursor = "0"
if gameInfo:getCustomCursor() then
	iCCursor = "1"
end


GUI:getLayer("Options"):getWidget("Music"):AcceptMessage(Message("SetPos", tostring(musVolume)))
GUI:getLayer("Options"):getWidget("Sound"):AcceptMessage(Message("SetPos", tostring(sndVolume)))
GUI:getLayer("Options"):getWidget("Fullscreen"):AcceptMessage(Message("SetState", iFullscreen))
GUI:getLayer("Options"):getWidget("CustomCursor"):AcceptMessage(Message("SetState", iCCursor))

if gameInfo:getUseExternalScreensaver() then
	GUI:getLayer("ScreensaverReady"):getWidget("Download"):AcceptMessage(Message("SetText", "Download"))
	GUI:getLayer("ScreensaverReady"):getWidget("SaverReady2"):AcceptMessage(Message("SetText", "ScreensaverReady2Ext"))
	GUI:getLayer("GetScreensaver"):getWidget("Caption"):AcceptMessage(Message("SetString", "Download screensaver?"))
else
	GUI:getLayer("ScreensaverReady"):getWidget("Download"):AcceptMessage(Message("SetText", "Install"))
	GUI:getLayer("ScreensaverReady"):getWidget("SaverReady2"):AcceptMessage(Message("SetText", "ScreensaverReady2"))
	GUI:getLayer("GetScreensaver"):getWidget("Caption"):AcceptMessage(Message("SetString", "Install screensaver?"))

end

if not gameInfo:getUseScreensaver() then
	GUI:getLayer("Menu"):getWidget("GetSaver"):AcceptMessage(Message("Active", 0))
end

DoMenu()
--DoGallery(7)
--DoTest()
--DoCompleteGame()

--GUI:getLayer("Statistics"):getWidget("GameStatistics"):AcceptMessage(Message("Start"))
