--------------------------------------------------------------------------------
dofile('data/lua/screens/Options.lua')
dofile('data/lua/screens/PlayerSelect.lua')
dofile('data/lua/Misc.lua')
--------------------------------------------------------------------------------
mainMenu = nil;
exitConfirmationScreen = nil;

changePlayerButton = nil;

helpScreen = nil;
ingameMenu = nil;
exitingApp = false;

--------------------------------------------------------------------------------
function BuildMainMenuScreen(screen)

    screen:SetLayer(BASE_LAYER);
    local anim = screen:CreateAnimation('data/Texture/Menu/BG_Main_Menu.jpg')
	anim:SetHotSpot(512, 384);
	anim:SetPosXY(512, 384);
	anim:SetPosZ(0);
	
	local anim = screen:CreateAnimation('data/Texture/Menu/Layer_Top.png')
	anim:SetHotSpot(512, 180);
	anim:SetPosXY(512, 180);
	anim:SetPosZ(5);

	local anim = screen:CreateAnimation('data/Texture/Menu/TM.png')
	anim:SetHotSpot(17, 15);
	anim:SetPosXY(396, 178);
	anim:SetPosZ(6);

	local anim = screen:CreateAnimation('data/Texture/Scenes/Common/Sky.jpg')
	anim:SetHotSpot(512, 200)
	anim:SetPosXY(512, 160);
	anim:SetPosZ(1);
	
	local anim = screen:CreateAnimation('data/Texture/Scenes/Common/Moon.png')
	anim:SetHotSpot(170, 160);
	anim:SetPosXY(580, 115);
	anim:SetPosZ(2);
	
	-- clouds 1
	local anim = screen:CreateAnimation('data/Texture/Scenes/Common/Clouds.png');
	anim:SetHotSpot(1024, 200);
	anim:SetPosXY(512, 160);
	anim:SetOpacity(0);
	anim:SetScaleXY(1, 1);
	anim:SetPosZ(4);
	
	local effector = CreateTextureEffector();
	effector:AttachToAnimation(anim);
	effector:SetSpeed(-16);
	effector:Play();
	
	local effector = CreateAnimationEffector();
	effector:AttachTo(anim);
	effector:SetKey(_TRACK_O, 0, 0.0);
	effector:SetKey(_TRACK_O, 1.5, 0.45);
	effector:Play();
	
	-- clouds 2
	local anim = screen:CreateAnimation('data/Texture/Scenes/Common/Clouds.png');
	anim:SetHotSpot(1024, 200);
	anim:SetPosXY(0, 160);
	anim:SetOpacity(0);
	anim:SetScaleXY(1, 1);
	anim:SetPosZ(3);
	
	local effector = CreateTextureEffector();
	effector:AttachToAnimation(anim);
	effector:SetSpeed(-4);
	effector:Play();
	
	local effector = CreateAnimationEffector();
	effector:AttachTo(anim);
	effector:SetKey(_TRACK_O, 0, 0.0);
	effector:SetKey(_TRACK_O, 1.5, 0.75);
	effector:Play();
	
	--[[local anim = screen:CreateAnimation('data/Texture/Menu/voroni_245x180_8fps.png')
	anim:SetHotSpot(122, 90);
	anim:SetPosXY(630, 110);
	anim:SetPosZ(2);]]
	
	--[[local anim = CreateAnimation('data/Texture/Menu/voroni_245x180_8fps.png',0,0,245,180,8,8)
	anim:SetHotSpot(122,90)
	screen:CreateObject('BG_Metrodotel_Hall',anim,512,384,40,0,1,1)]]
	
	-- street light glow
	local anim = screen:CreateAnimation('data/Texture/Menu/StreetLigthGlow.png')
	anim:SetHotSpot(135, 135);
    anim:SetPosXY(811, 332);
	anim:SetPosZ(10);
	
	local effector = CreateAnimationEffector();
	effector:SetMode(_ANIM_LOOP); -- loop
	effector:SetKey(_TRACK_O, 0.0, 0.6);
	effector:SetKey(_TRACK_O, 1.4, 0.8);
	effector:SetKey(_TRACK_O, 2.1, 0.6);
	effector:AttachTo(anim);
	effector:Play();
	
	-- the fog
	local anim = screen:CreateAnimation('data/Texture/Menu/fog.png')
	anim:SetHotSpot(512, 134);
    anim:SetPosXY(512, 634);
	anim:SetPosZ(10);
	--anim:SetColor('0xFFFF0000'); --will never work with CAnimEntity
	
	local effector = CreateAnimationEffector();
	effector:SetMode(_ANIM_LOOP); -- loop
	effector:SetKey(_TRACK_O, 0.0, 0.65);
	effector:SetKey(_TRACK_O, 1.4, 0.8);
	effector:SetKey(_TRACK_O, 2.8, 0.65);
	effector:AttachTo(anim);
	effector:Play();
	
	-- street light glow 2
	--local anim = screen:CreateAnimation('data/Texture/Menu/Layer_Small_Lantern.png')
	local anim = screen:CreateAnimation('data/Texture/Scenes/Common/radial_glow.png')
	anim:SetColor("0xAAFFE438");
	anim:SetHotSpot(50, 50);
    --anim:SetPosXY(283, 356);
    --anim:SetPosXY(283, 306);
    anim:SetPosXY(280, 280);
	anim:SetPosZ(10);
	anim:SetScale(1.4);
	anim:SetBlendMode(0)
	
	local megoLight = MegoLightEffector();
	megoLight:AttachTo(anim);
	megoLight:PlayAt(0.75);

end

--------------------------------------------------------------------------------
function UpdatePlayerWelcomeString()
    
    if (changePlayerButton ~= nil)
    then
        --local welcomeString = "Welcome, " .. GM:GetCurrPlayer().name .. "!\n" ..
        --                      "To change player click HERE";
        local welcomeString = GM:GetCurrPlayer().name;
        changePlayerButton:GetCaption():SetText(welcomeString);
    end
end

--------------------------------------------------------------------------------
function StartMainMenuContentScreen()

    local mainMenuContentScreen = CreateScreen();
    mainMenuContentScreen:SetLayer(ADDITIONAL_LAYER);
    local screen = mainMenuContentScreen;
    
    -- Tumba
	
	local tumba = screen:CreateAnimation('data/Texture/Menu/Tumba_Empty.png')
	tumba:SetHotSpot(197, 439);
    tumba:SetPosXY(734, 454);
	tumba:SetPosZ(20);
	
	local superTumbaSvet = screen:CreateAnimation('data/Texture/Menu/Layer_Tumba_Light.png')
	superTumbaSvet:SetHotSpot(278, 297);
    superTumbaSvet:SetPosXY(737, 479);
	superTumbaSvet:SetPosZ(30);
	
	local megoTumbaLightEffector = MegoLightEffector();
	megoTumbaLightEffector:AttachTo(superTumbaSvet);
	megoTumbaLightEffector:Play();
	
	-- Buttons
	
	local playButton = MakeButton(686, 325, 220, 100);
	--playButton:SetScale(1.40);
	playButton:SetSpriteUp("MainMenuPlayButton");
	playButton:SetSpriteOver("MainMenuPlayButtonOver");
	playButton:SetSpriteDown("MainMenuPlayButtonDown");
	playButton:SetActionSound("Menu_button_click");
	--playButton:SetRect(630, 320, 125, 60);
	--playButton:GetCaption():SetScale(1.0);
	--playButton:GetCaption():SetFont(MAIN_MENU_FONT);
	
	playButton:AttachTo(screen);
	playButton:SetAction("MenuPlayClick");
	--playButton:SetTitle("Play");
	playButton:SetZ(25);
	
	---
	
	local optionsButton = MakeButton(792, 437, 220, 50);
	--optionsButton:SetScale(1.40);
	optionsButton:SetSpriteUp("MainMenuOptionsButton");
	optionsButton:SetSpriteOver("MainMenuOptionsButtonOver");
	optionsButton:SetSpriteDown("MainMenuOptionsButtonDown");
	optionsButton:SetActionSound("Menu_button_click");
	--optionsButton:SetRect(700, 430, 140, 60);
	--optionsButton:GetCaption():SetFont(MAIN_MENU_FONT);
	
	optionsButton:AttachTo(screen);
	optionsButton:SetAction("MenuOptionsClick");
	--optionsButton:SetTitle("Options");
	optionsButton:SetZ(26);
	
	---
	
	local helpButton = MakeButton(696, 527, 220, 50);
	--helpButton:SetScale(1.40);
	helpButton:SetSpriteUp("MainMenuHelpButton");
	helpButton:SetSpriteOver("MainMenuHelpButtonOver");
	helpButton:SetSpriteDown("MainMenuHelpButtonDown");
	helpButton:SetActionSound("Menu_button_click");
	--helpButton:GetCaption():SetScale(1.0);
	--helpButton:SetRect(610, 515, 140, 60);
	--helpButton:GetCaption():SetFont(MAIN_MENU_FONT);
	
	helpButton:AttachTo(screen);
	helpButton:SetAction("MenuHelpClick");
	--helpButton:SetTitle("Help");
	helpButton:SetZ(25);
	
	---
	
	local exitButton = MakeButton(791, 674, 220, 50);
	--exitButton:SetScale(1.40);
	exitButton:SetSpriteUp("MainMenuExitButton");
	exitButton:SetSpriteOver("MainMenuExitButtonOver");
	exitButton:SetSpriteDown("MainMenuExitButtonDown");
	exitButton:SetActionSound("Menu_button_click");
	--exitButton:GetCaption():SetScale(1.0);
	--exitButton:GetCaption():SetFont(MAIN_MENU_FONT);
	--exitButton:SetRect(710, 615, 140, 60);
	
	exitButton:AttachTo(screen);
	exitButton:SetAction("MenuExitClick");
	--exitButton:SetTitle("Exit");
	exitButton:SetZ(25);
	
	--------
	
	local creditsButton = MakeButton(808, 577, 220, 50);
	--exitButton:SetScale(1.40);
	creditsButton:SetSpriteUp("MainMenuCreditsButton");
	creditsButton:SetSpriteOver("MainMenuCreditsButtonOver");
	creditsButton:SetSpriteDown("MainMenuCreditsButtonDown");
	creditsButton:SetActionSound("Menu_button_click");
	--exitButton:GetCaption():SetScale(1.0);
	--exitButton:GetCaption():SetFont(MAIN_MENU_FONT);
	--exitButton:SetRect(710, 615, 140, 60);
	
	creditsButton:AttachTo(screen);
	creditsButton:SetAction("MenuCreditsClick");
	--exitButton:SetTitle("Exit");
	creditsButton:SetZ(24);
	
	--------
	
	changePlayerButton = MakeButton(252, 350, 280, 50);
	changePlayerButton:SetRect(120, 310, 270, 90);
	changePlayerButton:GetCaption():SetScale(0.75);
	changePlayerButton:GetCaption():SetFont(PLAYER_NAME_FONT);
	changePlayerButton:SetActionSound("Menu_button_click");
	changePlayerButton:SetAngle(0.075);
	--changePlayerButton:GetCaption():SetColor("0xFFFFC438");
	--changePlayerButton:GetCaption():SetColor("0xFFAAEEAA");
	changePlayerButton:GetCaption():SetColor("0xFFFFE438");
	--changePlayerButton:GetCaption():SetHighlightColor("0xFFFF9438");
	changePlayerButton:GetCaption():SetHighlightColor("0xFFFFE438");
	UpdatePlayerWelcomeString();
	
	changePlayerButton:AttachTo(screen);
	changePlayerButton:SetZ(21);
	changePlayerButton:SetAction("ChangePlayer");
	--playButton:SetRect(630, 320, 125, 60);
	--playButton:GetCaption():SetScale(1.0);
	--playButton:GetCaption():SetFont(MAIN_MENU_FONT);
	
	moreInfoButton = MakeButton(355, 665, 280, 50);
	moreInfoButton:SetActionSound("Menu_button_click");
	moreInfoButton:SetSpriteUp("MainMenuMoreInfoButton");
	moreInfoButton:SetSpriteOver("MainMenuMoreInfoButtonOver");
	moreInfoButton:SetSpriteDown("MainMenuMoreInfoButtonDown");
	moreInfoButton:SetAction("ShowMoreInfo");
	moreInfoButton:AttachTo(screen);
	moreInfoButton:SetZ(21);
	
	clickHereButton = MakeButton(250, 390, 280, 50);
	clickHereButton:SetActionSound("Menu_button_click");
	clickHereButton:SetSpriteUp("MainMenuClickHereButton");
	clickHereButton:SetSpriteOver("MainMenuClickHereButtonOver");
	clickHereButton:SetSpriteDown("MainMenuClickHereButtonDown");
	--clickHereButton:SetAction("ChangePlayer");
	clickHereButton:AttachTo(screen);
	clickHereButton:SetZ(21);
	
	local anim = mainMenu:CreateAnimation('data/Texture/Menu/Buttons/More_Info.png')
	anim:SetHotSpot(62, 35);
	anim:GetAnimation():SetTextureRect(0, 0, 124, 70);
    --anim:SetPosXY(283, 356);
    --anim:SetPosXY(283, 306);
    anim:SetPosXY(355, 665);
	anim:SetPosZ(20);
	
    PopUpScreensMgr:Push(mainMenuContentScreen);
    
end

--------------------------------------------------------------------------------
function StartMainMenu()

    if (mainMenu == nil)
    then
        mainMenu = CreateMenuScreen("Main Menu Screen");
        BuildMainMenuScreen(mainMenu);
    end

    GM:SetCurrentScreen(mainMenu);

	local currentPlayer = GM:GetCurrPlayer();

	if GM.last_players_load_result_ == '' then
		if (currentPlayer == nil) then
			ShowInputPlayerNameScreen();
		else
			StartMainMenuContentScreen();
		end
	else
		local promt_create_profile = true
        local corrupted_name
		if currentPlayer then
            local corrupted_name_delimeter_pos = string.find(GM.last_players_load_result_, ':')
            if corrupted_name_delimeter_pos then
                corrupted_name = string.sub(GM.last_players_load_result_, corrupted_name_delimeter_pos + 1)
                if currentPlayer.name ~= corrupted_name then
                    promt_create_profile = false
                end
            end
        end
		if promt_create_profile then
			local scr = ShowInputPlayerNameScreen(GetString('Q_ERROR_PROFILE_IS_CORRUPTED')..'\n', 80)
			scr.doNotHideIfNameConfirmFailed = true
		else
			PopUpScreensMgr:Push(
				ShowLoadPlayersError("'"..corrupted_name..
									 "'\n "..GetString('Q_ERROR_PROFILE_IS_CORRUPTED_NOT_CURRENT')..'\n')
				)
		end
		GM.last_players_load_result_ = ''
	end

    PlayRandomAmbientMusic();

end

--------------------------------------------------------------------------------
function MenuPlayClick()

    if (mainMenu == nil) -- JJJJJJJJJJJJJJJJJJJJ
    then
        return;
    end;

    mainMenu:Finish();
    mainMenu:Destroy();
    mainMenu = nil;
    
    PopUpScreensMgr:DestroyAll();
    
    StartGame();
end

--------------------------------------------------------------------------------
function MenuOptionsClick()

    StartOptionsScreen();
    
    --[[if (optionsScreen == nil)
    then
        
        --
        
    else
        optionsScreen:Show();
    end]]
    
    

end

--------------------------------------------------------------------------------
function MenuHelpClick()

    StartHelpScreen(nil);

end

--------------------------------------------------------------------------------
function MenuExitClick()

    AppShouldExit();
    
end

--------------------------------------------------------------------------------
function MenuCreditsClick()

    local scr = BuildOKScreen("EMPTY", "PopCurrentPopUpScreen");
    scr.textBox:SetText("");
    
    local creditsScreen = CreateCreditsScreen();
    creditsScreen:AttachTo(scr);
    creditsScreen:SetZ(1);
    creditsScreen:SetScale(0.6);
    
    creditsScreen:SetRect(512-250, 435-250, 500, 380);
    
    PopUpScreensMgr:Push(scr);

end

--------------------------------------------------------------------------------
function ChangePlayer()

    ShowPlayerSelectScreen();
    
end

--------------------------------------------------------------------------------
-- Help
function ________________HELP() end
--------------------------------------------------------------------------------

--------------------------------------------------------------------------------
function StartHelpScreen(param)

    local zOffset = 50;
    local yOffset = -50;

    helpScreen = CreateScreen();
    helpScreen:SetLayer(POPUP_WINDOWS_LAYER);
    helpScreen.currentHelpTextTag = 0;
    helpScreen.maxHelpTextTag = 6;
        
    anim = helpScreen:CreateAnimation("data/Texture/GUI/Help/Help.png"); -- 642, 547
    anim:SetHotSpot(321, 274);
    anim:SetPosXY(512, 400 + yOffset);
    anim:SetPosZ(0 + zOffset);
    anim:SetScaleXY(1, 1);
    anim:SetAngle(0);
    anim:SetOpacity(1);
        
    local OKButton = MakeButton(512, 639 + yOffset, 100, 40);
    OKButton:GetCaption():SetFont(BUTTONS_FONT);
    OKButton:SetSpriteUp("OKButton");
	OKButton:SetSpriteOver("OKButtonOver");
	OKButton:SetSpriteDown("OKButtonDown");
    OKButton:SetZ(20 + zOffset);
    OKButton:SetActionSound("Menu_button_click");

    OKButton:AttachTo(helpScreen);
    OKButton:SetAction("DestroyHelpScreen");
    helpScreen.OKButton = OKButton;
    --OKButton:SetTitle("OK");
    
    local scrollUp = MakeButton(518, 248 + yOffset, 40, 40);
	scrollUp:SetZ(5 + zOffset);
	scrollUp:AttachTo(helpScreen);
	scrollUp:SetAction("PreviousHelpScreen");
	scrollUp:SetActionSound("Menu_button_click");
	scrollUp:SetSpriteUp("ScrollUpButton");
	scrollUp:SetSpriteOver("ScrollUpButtonOver");
	scrollUp:SetSpriteDown("ScrollUpButtonDown");
	scrollUp:SetEnabled(false);
	helpScreen.scrollUp = scrollUp;
	
	local scrollDown = MakeButton(518, 583 + yOffset, 40, 40);
	scrollDown:SetZ(5 + zOffset);
	scrollDown:AttachTo(helpScreen);
	scrollDown:SetAction("NextHelpScreen");
	scrollDown:SetActionSound("Menu_button_click");
	scrollDown:SetSpriteUp("ScrollDownButton");
	scrollDown:SetSpriteOver("ScrollDownButtonOver");
	scrollDown:SetSpriteDown("ScrollDownButtonDown");
	helpScreen.scrollDown = scrollDown;

    local contentTextBox = CreateTextEntity(INGAME_MENU_FONT, "HELP_TEXT_1");
	--textBox:SetHotSpot(298, 206);
	contentTextBox:SetXY(512, 392 + yOffset);
	contentTextBox:SetZ(2 + zOffset);
	contentTextBox:SetScale(.5);
	contentTextBox:SetRect(412, 264 + yOffset, 200, 100);
	contentTextBox:AttachTo(helpScreen);
	contentTextBox:SetTextAlignment(HGETEXT_CENTER + HGETEXT_MIDDLE);
	helpScreen.contentTextBox = contentTextBox;
	
	PopUpScreensMgr:Push(helpScreen);
    
end

--------------------------------------------------------------------------------
function PreviousHelpScreen()

    helpScreen.currentHelpTextTag = helpScreen.currentHelpTextTag - 1;

    helpScreen.scrollDown:SetEnabled(true);
    helpScreen.scrollUp:SetEnabled(
        helpScreen.currentHelpTextTag > 0);
        
    local helpTextKey = "HELP_TEXT_" .. helpScreen.currentHelpTextTag + 1;
	helpScreen.contentTextBox:SetText(GetString(helpTextKey));

end

--------------------------------------------------------------------------------
function NextHelpScreen()

    helpScreen.currentHelpTextTag = helpScreen.currentHelpTextTag + 1;

    helpScreen.scrollUp:SetEnabled(true);
    helpScreen.scrollDown:SetEnabled(
        helpScreen.currentHelpTextTag < helpScreen.maxHelpTextTag);

    local helpTextKey = "HELP_TEXT_" .. helpScreen.currentHelpTextTag + 1;
	helpScreen.contentTextBox:SetText(GetString(helpTextKey));

end

--------------------------------------------------------------------------------
function DestroyHelpScreen()

    if (helpScreen)
    then
        PopUpScreensMgr:Pop();
        helpScreen = nil;
    end
    
end

--------------------------------------------------------------------------------
-- Options
function ________________EXIT() end
--------------------------------------------------------------------------------

--------------------------------------------------------------------------------
-- Exiting confirmation
--------------------------------------------------------------------------------
function MenuExitConfirm()

    -- popping current screen in AppExit();
    exitConfirmationScreen = nil;
    
    AppExit();
end

--------------------------------------------------------------------------------
function MenuExitDiscard()

    PopUpScreensMgr:Pop();
    exitConfirmationScreen = nil;
    
end

--------------------------------------------------------------------------------
function AppShouldExit()

    -- temp
    
    if (exitConfirmationScreen ~= nil)
    then 
        return;
    end
    
    exitConfirmationScreen = BuildYesNoScreen("REALLY_QUIT_GAME",
                                      "MenuExitConfirm",
                                      "MenuExitDiscard");
                                      
    PopUpScreensMgr:Push(exitConfirmationScreen);

end

--------------------------------------------------------------------------------
function AppExit()

    exitingApp = true;
    
    CleanUpGameResources();
    
    APP:PerformExit();

end
--------------------------------------------------------------------------------
function ShowMoreInfo()

    local moreInfoScreen = BuildOKScreen('MoreInfo', 'PopCurrentPopUpScreen');
    moreInfoScreen.textBox:SetText("更多信息:\n\n\n\n");
    
    local zOffset = 100;
    
    local highlightColor = "0xFFf2a5a5";
    ---[[
    local moreAdventuresButton = MakeButton(512, 470, 380, 40);
    moreAdventuresButton:GetCaption():SetFont(INGAME_MENU_FONT);
    moreAdventuresButton:GetCaption():SetHighlightColor(highlightColor);
    --moreAdventuresButton:GetCaption():SetColor("0xFF666666");
    --moreAdventuresButton:GetCaption():SetHighlightColor("0xFF666666");
    moreAdventuresButton:SetZ(20 + zOffset);
    
    moreAdventuresButton:AttachTo(moreInfoScreen);
    moreAdventuresButton:SetActionSound("Menu_button_click");
    moreAdventuresButton:SetTitle("更多探险游戏");
    moreAdventuresButton:SetAction('GoToMoreAdventuresLink');
    --]]
    --local strategyGuideButton = MakeButton(512, 400, 360, 40);
    local strategyGuideButton = MakeButton(512, 330, 360, 40);
    strategyGuideButton:GetCaption():SetFont(INGAME_MENU_FONT);
    strategyGuideButton:GetCaption():SetHighlightColor(highlightColor);
    strategyGuideButton:SetZ(20 + zOffset);
    
    strategyGuideButton:AttachTo(moreInfoScreen);
    strategyGuideButton:SetActionSound("Menu_button_click");
    strategyGuideButton:SetTitle("攻略指南");
    strategyGuideButton:SetAction('GoToStrategyGuideLink');
    
    --local forumsButton = MakeButton(512, 470, 200, 40);
    local forumsButton = MakeButton(512, 400, 200, 40);
    forumsButton:GetCaption():SetFont(INGAME_MENU_FONT);
    forumsButton:GetCaption():SetHighlightColor(highlightColor);
    forumsButton:SetZ(20 + zOffset);
    
    forumsButton:AttachTo(moreInfoScreen);
    forumsButton:SetActionSound("Menu_button_click");
    forumsButton:SetTitle("大鱼论坛");
    forumsButton:SetAction('GoToForumLink');
    
    
    --More adventures (disabled)
    --Strategy guide
    --Forums

    
    PopUpScreensMgr:Push(moreInfoScreen);

end

--------------------------------------------------------------------------------
function GoToForumLink()

    if (APP:IsFullScreen())
    then
        APP:Minimize();
    end

    APP:OpenUrlFromFile('data/Links/ForumURL.txt')

end

--------------------------------------------------------------------------------
function GoToStrategyGuideLink()

    if (APP:IsFullScreen())
    then
        APP:Minimize();
    end

    APP:OpenUrlFromFile('data/Links/StrategyGuideURL.txt')

end

--------------------------------------------------------------------------------
function GoToMoreAdventuresLink()

    if (APP:IsFullScreen())
    then
        APP:Minimize();
    end

    APP:OpenUrlFromFile('data/Links/MoreAdventuresURL.txt')

end

--------------------------------------------------------------------------------
-- Ingame menu
function ________________IngameMenu() end
--------------------------------------------------------------------------------
ingameMenuBlocker = 8;
--------------------------------------------------------------------------------
function StartIngameMenu()
    if (ingameMenuBlocker ~= 1 and not ingameMenu)
    then
        if (DragObject)
        then
            StopDrag(true);
        end
        ingameMenuBlocker = 1;
        PendingIngameMenu = true;
        TimerDoString('DoStartIngameMenu();', 0.5, play_field:GetTimer())
        play_field.mouseBlocker:Push();
        prevForcedObject = game_state.forcedObject;
        game_state.forcedObject = nil;
        
        local fade = play_field:GetObject('zoom_fade');
		
		if active_zones:GetTop() and active_zones:GetTop().isFullscreen or
           game_state.comics and game_state.comics.isMonolog ~= true
		then
			fadeZToRestore = fade:GetZ();
			fade:SetZ(249)
			fade:SetOpacity(0);
		else
			fadeZToRestore = nil;
		end
        if (fade:GetOpacity() < 0.5)
        then
            hideFadeOnMenuDestroy = true;
            fade:ChangeOpacity(0., 0.7, ZOOM_ZONE_APPEARING_TIME);
        else
            hideFadeOnMenuDestroy = false;
        end
		
		if game_state.is_inventory_opened
		then
			HideInventory();
			TimerDoString('game_state.is_inventory_opened = false', 0.2, play_field:GetTimer());
		end
        
        if (play_field:GetLevelScreen():GetPanel():GetMode() == PM_COMICSMONOLOG)
        then
            UpdateComicsRequest();
        end
        
        if not game_state.scene_is_destroying -- fix for 1427
        then
            GTimerPause(); -- fix for PS-1072.2
        end
    end
end

function DoStartIngameMenu()
    ingameMenuBlocker = 0;
    if (ingameMenu)
    then
        return;
    end
    
    PendingIngameMenu = nil;
    if (game_state.forcedObject)
    then
        prevForcedObject = game_state.forcedObject;
        game_state.forcedObject = nil;
    end
    
    
    HideZoomZones();
    
    local zOffset = 50;
    
    ingameMenu = BuildOptionsScreenBack();
    
    -- Layer Front_1
    anim = ingameMenu:CreateAnimation("data/Texture/GUI/Options/front.png");
    anim:SetHotSpot(350, 234);
    anim:SetPosXY(512, 399.5);
    anim:SetPosZ(17 + zOffset);
    anim:SetScaleXY(1, 1);
    anim:SetAngle(0);
    anim:SetOpacity(1);
        
    --
    
    local highlightColor = "0xFFf2a5a5";
        
    local continueButton = MakeButton(512, 310, 200, 40);
    continueButton:GetCaption():SetFont(INGAME_MENU_FONT);
    continueButton:GetCaption():SetHighlightColor(highlightColor);
    continueButton:SetZ(20 + zOffset);
    
    continueButton:AttachTo(ingameMenu);
    continueButton:SetAction("DestroyIngameMenu");
    continueButton:SetActionSound("Menu_button_click");
    continueButton:SetTitle("继续");
    continueButton:SetKeyEquivalent(KEY_ESCAPE);
        
    --
        
    local optionsButton = MakeButton(512, 370, 170, 40);
    optionsButton:GetCaption():SetFont(INGAME_MENU_FONT);
    optionsButton:GetCaption():SetHighlightColor(highlightColor);
    optionsButton:SetZ(20 + zOffset);
    
    optionsButton:AttachTo(ingameMenu);
    optionsButton:SetAction("MenuOptionsClick");
    optionsButton:SetTitle("选项");
    optionsButton:SetActionSound("Menu_button_click");
        
    --
        
    local helpButton = MakeButton(512, 430, 120, 40);
    helpButton:GetCaption():SetFont(INGAME_MENU_FONT);
    helpButton:GetCaption():SetHighlightColor(highlightColor);
    helpButton:SetZ(20 + zOffset);
        
    helpButton:AttachTo(ingameMenu);
    helpButton:SetAction("MenuHelpClick");
    helpButton:SetTitle("帮助");
    helpButton:SetActionSound("Menu_button_click");
        
    --
        
    local exitButton = MakeButton(512, 490, 120, 40);
    exitButton:GetCaption():SetFont(INGAME_MENU_FONT);
    exitButton:GetCaption():SetHighlightColor(highlightColor);
    exitButton:SetZ(20 + zOffset);
        
    exitButton:AttachTo(ingameMenu);
    exitButton:SetAction("ExitToMainMenuClick");
    exitButton:SetTitle("退出");
    exitButton:SetActionSound("Menu_button_click");
	
	PopUpScreensMgr:Push(ingameMenu);
   
    if game_state.tool then
        DropTool(0.)
    end
end

--------------------------------------------------------------------------------
function DestroyIngameMenu()

    if (ingameMenu)
    then
    
        if (hideFadeOnMenuDestroy)
        then
            play_field:GetObject('zoom_fade'):ChangeOpacity(0.7, 0., ZOOM_ZONE_APPEARING_TIME);
        end
		
		if (fadeZToRestore)
		then
			TimerDoString("play_field:GetObject('zoom_fade'):SetZ(fadeZToRestore);", ZOOM_ZONE_APPEARING_TIME, play_field:GetTimer())
		end
    
        ingameMenu = nil;
        
        PopUpScreensMgr:Pop();
        
        ShowHiddenZoomZones();
        
        GTimerContinue();
    end
    
    play_field.mouseBlocker:Pop();
    game_state.forcedObject = prevForcedObject;

end

--------------------------------------------------------------------------------
function ExitToMainMenuClick()

    exitConfirmationScreen = BuildYesNoScreen("REALLY_QUIT_TO_MENU",
                                              "ExitToMainMenu",
                                              "MenuExitDiscard");
    
    PopUpScreensMgr:Push(exitConfirmationScreen);
    
end

--------------------------------------------------------------------------------
function ExitToMainMenu()

    if game_state.tool then
        game_state.tool:SetVisible(false)
        game_state.tool = nil
    end

    if (exitConfirmationScreen == nil)
    then
        return;
    end
    
    ingameMenu = nil;
    
    CleanUpGameResources()

    StartMainMenu();

end

--------------------------------------------------------------------------------