local zoom_chest;
local zoom_window;
local zoom_bell;

function OnSceneLoad(screen)
    LoadData(screen)
    DefaultOnSceneLoad('data/Scenes/14_Corridor_Main.lua', 'quest', 'data/Scenes/13_Crimeplace_Main.lua')
    
    play_field:GetObject('bell_small_shadow_layer'  ):SetOpacity(0);
    play_field:GetObject('bell_layer'               ):SetOpacity(0);
    play_field:GetObject('door_open_layer'          ):SetOpacity(0);
    
    play_field:GetObject('area_doctor_room' ):SetVisible(false);
    play_field:GetObject('area_chest'       ):SetVisible(false);
    
    play_field:GetObject('Bag_anim'):GetAnimation():SetMode(HGEANIM_FWD + HGEANIM_NOLOOP);
    play_field:GetObject('Bag_anim'):GetAnimation():Stop();
    
    zoom_chest = CreateZoomZone(    'area_chest',
                                    'CloseZoneChest',
                                    'ZOOM_chest',       true,
                                    'button_layer',     true);
    
    zoom_chest.on_open = function()
--        ShowMonolog('C_14_CHEST_HINT', 'C_14_CHEST_HINT')
    end
    
    zoom_window = CreateZoomZone(   'area_window',
                                    'CloseZoneWindow',
                                    'ZOOM_window',      true,
                                    'scissors_layer',   true);
    
    zoom_window.on_open = function()
--        ShowMonolog('C_14_WINDOW_HINT', 'C_14_WINDOW_HINT');
    end
    
    zoom_bell = CreateZoomZone(     'area_zz_bell',
                                    'CloseZoneBell',
                                    'ZOOM_bell',            true,
                                    'bell_shadow_layer',    false,
                                    'bell_big_layer',       false,
                                    'area_bell',            true);
    
    zoom_bell.on_open = function()
--        ShowMonolog('C_14_BELL_HINT', 'C_14_BELL_HINT');
    end
                                    
    play_field:GetObject('area_bell'):GetAnimation():SetColor('0x01FFFFFF');
    
    RegisterToolFindInfo('14_button', 'button_layer',           function(tool_name)
                                                                    zoom_chest.Disable();
                                                                    
                                                                    AddNoteToNotebook('UPSTAIRS_FIRST_VISIT');
                                                                end,
                                                                function(tool_name)
                                                                    zoom_chest.Disable();
                                                                end);
            
    RegisterToolFindInfo('14_scissors', 'scissors_layer',       function(tool_name)
                                                                    zoom_window.Disable();
                                                                end,
                                                                function(tool_name)
                                                                    zoom_window.Disable();
                                                                end);
    
    RegisterToolApplyInfo('14_scissors', 'area_bag', nil, nil,  function(tool_name)
                                                                    play_field:GetObject('area_bag'  ):SetVisible(false);
                                                                    play_field:GetObject('area_chest'):SetVisible(true);
                                                                    
                                                                    play_field:GetObject('Bag_anim'):GetAnimation():Play();
                                                                    
                                                                    PlaySound('14_CutBag');
                                                                end,
                                                                function(tool_name)
                                                                    play_field:GetObject('area_chest'):SetVisible(true);
                                                                    play_field:GetObject('area_bag'  ):SetVisible(false);
                                                                    
                                                                    local bag_anim = play_field:GetObject('Bag_anim'):GetAnimation();
                                                                    bag_anim:SetFrame(bag_anim:GetFrames() - 1);
                                                                end);
    
    RegisterToolApplyInfo('18_bell', 'area_bell', nil, nil,     function(tool_name)
                                                                    local time = 0.5;
                                                                    
                                                                    zoom_bell.SetObjectVisible('area_bell',         false);
                                                                    zoom_bell.SetObjectVisible('bell_shadow_layer', true, time);
                                                                    zoom_bell.SetObjectVisible('bell_big_layer',    true, time);
                                                                    
                                                                    play_field:GetObject('bell_small_shadow_layer'  ):ChangeOpacity(0., 1., time);
                                                                    play_field:GetObject('bell_layer'               ):ChangeOpacity(0., 1., time);
                                                                    game_state.bell_applied = true;
                                                                end,
                                                                function(tool_name)
                                                                    zoom_bell.SetObjectVisible('area_bell',         false);
                                                                    zoom_bell.SetObjectVisible('bell_shadow_layer', true);
                                                                    zoom_bell.SetObjectVisible('bell_big_layer',    true);
                                                                    
                                                                    play_field:GetObject('bell_small_shadow_layer'  ):SetOpacity(1);
                                                                    play_field:GetObject('bell_layer'               ):SetOpacity(1);
                                                                    game_state.bell_applied = true;
                                                                end);
    
    RegisterActiveObject('bell_big_layer',                      function(obj_name)
                                                                    zoom_bell.Disable();
                                                                    zoom_bell.SetVisible(false);
                                                                    play_field:GetObject('area_zz_bell'):SetVisible(false);
                                                                    
                                                                    TimerDoString("BellSwing();", ZOOM_ZONE_APPEARING_TIME, play_field:GetTimer());
                                                                end,
                                                                function(obj_name)
                                                                    zoom_bell.Disable();
                                                                    play_field:GetObject('area_zz_bell'):SetVisible(false);
                                                                    
                                                                    if not HasGameState('ActionCompleted', game_state.scene_path.."(BellSwing)") then
                                                                        TimerDoString("BellSwing();", ZOOM_ZONE_APPEARING_TIME, play_field:GetTimer());
                                                                    else
                                                                        play_field:GetObject('door_open_layer'):SetOpacity(1);
                                                                        play_field:GetObject('area_doctor_room'):SetVisible(true);
                                                                    end
                                                                end);
    
    LinkScene(  'area_doctor_room',
                'CursorLeft',
                'data/Scenes/16_Doctor_Main.lua',
                false);
                
    LinkScene(  'area_room',
                'CursorRight',
                'data/Scenes/14_Corridor_Room.lua',
                false);
    
    LinkScene(  'area_lock_puzzle',
                'CursorActive',
                'data/Scenes/Minigames/14_puzzle.lua',
                false);
                
    if HasGameState('MiniGameCompleted', '14_puzzle') then
        local value = game_state.scene_path.."(door2_open)"
        
        if not HasGameState('ActionCompleted', value) then
            AddGameState('ActionCompleted', value)
            
            TimerDoString("OpenDoor2()", ZOOM_ZONE_APPEARING_TIME, play_field:GetTimer());
        end
        
        play_field:GetObject('area_lock_puzzle'):SetVisible(false);
        
        LinkScene(  'area_bedroom',
                    'CursorUpLeft',
                    'data/Scenes/17_Bedroom_Main.lua',
                    false);
    else
        play_field:GetObject('area_bedroom'):SetVisible(false);
        play_field:GetObject('door2_open_layer'):SetOpacity(0);
    end
end

function OpenDoor2()
    play_field:GetObject('door2_open_layer'):ChangeOpacity(0, 1, OPEN_DOOR_TIME);
    PlaySound('14_OpenBedroomDoor');
end

function BellSwing()
    local bell   = play_field:GetObject('bell_layer');
    local time   = 2;
    local amplit = 15 * M_PI/180;
    local a      = bell:GetAngle();
    
    local eff = CreateAnimationEffector();
    eff:AttachTo( bell );
    
    eff:SetKey(_TRACK_A, 0, a);
    eff:SetKey(_TRACK_A, time * 0.15, a + amplit/2);
    eff:SetKey(_TRACK_A, time * 0.45, a - amplit/2);
    eff:SetKey(_TRACK_A, time * 0.75, a + amplit/2);
    eff:SetKey(_TRACK_A, time * 0.95, a - amplit / 100);
    eff:SetKey(_TRACK_A, time,        a);
    eff:Play();
    
    PlaySound('14_Bell')
    
    TimerDoString("OnBellSwingCompleted();", time, play_field:GetTimer());
end

function OnBellSwingCompleted()
    AddGameState('ActionCompleted', game_state.scene_path.."(BellSwing)");
    
    play_field:GetObject('door_open_layer'):ChangeOpacity(0, 1, OPEN_DOOR_TIME);
    play_field:GetObject('area_doctor_room'):SetVisible(true);
    
    PlaySound('13_OpenDoor')
end

function OnSceneFree(screen)
    DefaultOnSceneFree()
    FreeData(screen)
end

function OnUpdate(dt)
    DefaultOnUpdate(dt)
end

function OnAllItemsFound()
    DefaultOnAllItemsFound()
end

function OnLose()
    DefaultOnLose()
end

function OnExit()
    DefaultOnExit()
end

function OnClose()
    DefaultOnClose()
end

function OnObjectMouseEnter(object)
    DefaultOnObjectMouseEnter(object)
end

function OnObjectMouseLeave(object)
    DefaultOnObjectMouseLeave(object)
end

function OnObjectMouseDown(object)
    local is_wrong_click = not DefaultOnObjectMouseDown(object)
    local obj_name = object:GetName()
    local pf = play_field
    
    if obj_name == 'ZOOM_chest' then
        ShowMonolog('C_14_CHEST_HINT', 'C_14_CHEST_HINT')
    end
    
    if obj_name == 'ZOOM_window' then
        ShowMonolog('C_14_WINDOW_HINT', 'C_14_WINDOW_HINT');
    end
    
    if (obj_name == 'ZOOM_bell' or obj_name == 'area_bell') and (not game_state.bell_applied)
    then
        ShowMonolog('C_14_BELL_HINT', 'C_14_BELL_HINT');
    end
    
    if obj_name == 'area_bag' then
        ShowMonolog('C_14_BAG_HINT', 'C_14_BAG_HINT');
    end

    DefaultOnObjectMouseDownProcessed(is_wrong_click);
end

function OnObjectMouseUp(object)
    DefaultOnObjectMouseUp(object)
end

function OnObjectFound(object)
    DefaultOnObjectFound(object)
end

function OnToolFound(tool)
    DefaultOnToolFound(tool)
end

function OnToolApply(tool, object)
    DefaultOnToolApply(tool, object)
end

function OnObjectReachPanel(object)
    DefaultOnObjectReachPanel(object)
end

function OnButtonPress(title)
    DefaultOnButtonPress(title)
end

function OnMouseMove(previous_pos, current_pos)
    DefaultOnMouseMove(previous_pos, current_pos)
end

function OnSetTool(tool)
    DefaultOnSetTool(tool)
end

function LevelLoaded(level)
end

function OnSceneDestroy(playfield)
    DefaultOnSceneDestroy(playfield)
end

--------------------------------------------------------------------------------
-- CLEAR_ON_SAVE_BEGIN
-- Do not edit code listed below (generated by the editor on each save)
function LoadData(screen)
	local anim = CreateAnimation('data/Texture/Scenes/14_Corridor/Zone_main/Layers/background.jpg',0,0,1024,768,1,0)
	anim:SetHotSpot(512,384)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('background',anim,512,384,100,0,1,1)
	local anim = CreateAnimation('data/Texture/Scenes/Common/area.png',0,0,8,8,1,0)
	anim:SetHotSpot(4,4)
	anim:SetBlendMode(2)
	anim:SetColor('0x01FFFFFF')
	screen:CreateObject('area_chest',anim,324,462,101,0,10,15)
	local anim = CreateAnimation('data/Texture/Scenes/Common/area.png',0,0,8,8,1,0)
	anim:SetHotSpot(4,4)
	anim:SetBlendMode(2)
	anim:SetColor('0x01FFFFFF')
	screen:CreateObject('area_window',anim,586,295,101,0,10,15)
	local anim = CreateAnimation('data/Texture/Scenes/14_Corridor/Zone_main/Zoom_chest/ZOOM_chest.jpg',0,0,640,480,1,0)
	anim:SetHotSpot(320,240)
	anim:SetBlendMode(2)
	anim:SetColor('0x00FFFFFF')
	screen:CreateObject('ZOOM_chest',anim,512,384,150,0,1,1)
	local anim = CreateAnimation('data/Texture/Scenes/14_Corridor/Zone_main/Zoom_window/ZOOM_window.jpg',0,0,640,480,1,0)
	anim:SetHotSpot(320,240)
	anim:SetBlendMode(2)
	anim:SetColor('0x00FFFFFF')
	screen:CreateObject('ZOOM_window',anim,512,384,160,0,1,1)
	local anim = CreateAnimation('data/Texture/Scenes/14_Corridor/Zone_main/Zoom_chest/button_layer.png',0,0,64,45,1,0)
	anim:SetHotSpot(32,22)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('button_layer',anim,471,315,151,0,1,1)
	local anim = CreateAnimation('data/Texture/Scenes/14_Corridor/Zone_main/Zoom_window/scissors_layer.png',0,0,55,85,1,0)
	anim:SetHotSpot(27,42)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('scissors_layer',anim,551,398,161,0,1,1)
	local anim = CreateAnimation('data/Texture/Scenes/14_Corridor/Zone_main/Layers/bell_layer.png',0,0,33,90,1,0)
	anim:SetHotSpot(16,0)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('bell_layer',anim,282,114,102,0,1,1)
	local anim = CreateAnimation('data/Texture/Scenes/14_Corridor/Zone_main/Layers/bell_small_shadow_layer.png',0,0,25,76,1,0)
	anim:SetHotSpot(12,38)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('bell_small_shadow_layer',anim,199,114,102,0,1,1)
	local anim = CreateAnimation('data/Texture/Scenes/14_Corridor/Zone_main/Zoom_bell/ZOOM_bell.jpg',0,0,640,480,1,0)
	anim:SetHotSpot(320,240)
	anim:SetBlendMode(2)
	anim:SetColor('0x00FFFFFF')
	screen:CreateObject('ZOOM_bell',anim,512,384,170,0,1,1)
	local anim = CreateAnimation('data/Texture/Scenes/14_Corridor/Zone_main/Zoom_bell/bell_shadow_layer.png',0,0,97,225,1,0)
	anim:SetHotSpot(48,112)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('bell_shadow_layer',anim,291,310,171,0,1,1)
	local anim = CreateAnimation('data/Texture/Scenes/14_Corridor/Zone_main/Zoom_bell/bell_big_layer.png',0,0,79,255,1,0)
	anim:SetHotSpot(39,127)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('bell_big_layer',anim,551,433,172,0,1,1)
	local anim = CreateAnimation('data/Texture/Scenes/Common/area.png',0,0,8,8,1,0)
	anim:SetHotSpot(4,4)
	anim:SetBlendMode(2)
	anim:SetColor('0x01FFFFFF')
	screen:CreateObject('area_bell',anim,551,433,173,0,20,34)
	local anim = CreateAnimation('data/Texture/Scenes/Common/area.png',0,0,8,8,1,0)
	anim:SetHotSpot(4,4)
	anim:SetBlendMode(2)
	anim:SetColor('0x01FFFFFF')
	screen:CreateObject('area_zz_bell',anim,293,150,103,0,10,15)
	local anim = CreateAnimation('data/Texture/Scenes/14_Corridor/Zone_main/Layers/door_open_layer.png',0,0,113,528,1,0)
	anim:SetHotSpot(56,264)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('door_open_layer',anim,186,340,103,0,1,1)
	local anim = CreateAnimation('data/Texture/Scenes/Common/area.png',0,0,8,8,1,0)
	anim:SetHotSpot(4,4)
	anim:SetBlendMode(2)
	anim:SetColor('0x01FFFFFF')
	screen:CreateObject('area_doctor_room',anim,183,348,104,0,13,50)
	local anim = CreateAnimation('data/Texture/Scenes/Common/area.png',0,0,8,8,1,0)
	anim:SetHotSpot(4,4)
	anim:SetBlendMode(2)
	anim:SetColor('0x01FFFFFF')
	screen:CreateObject('area_lock_puzzle',anim,396,367,120,0,10,10)
	local anim = CreateAnimation('data/Texture/Scenes/14_Corridor/Zone_main/Animations/Bag_anim.png',0,0,348,218,10,10)
	anim:SetHotSpot(174,109)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('Bag_anim',anim,384,539,102,0,1,1)
	local anim = CreateAnimation('data/Texture/Scenes/Common/area.png',0,0,8,8,1,0)
	anim:SetHotSpot(4,4)
	anim:SetBlendMode(2)
	anim:SetColor('0x01FFFFFF')
	screen:CreateObject('area_room',anim,892,386,101,0,7,50)
	local anim = CreateAnimation('data/Texture/Scenes/Common/area.png',0,0,8,8,1,0)
	anim:SetHotSpot(4,4)
	anim:SetBlendMode(2)
	anim:SetColor('0x01FFFFFF')
	screen:CreateObject('area_bag',anim,374,517,103,0,10,7)
	local anim = CreateAnimation('data/Texture/Scenes/14_Corridor/Zone_main/Layers/door2_open_layer.png',0,0,41,289,1,0)
	anim:SetHotSpot(20,144)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('door2_open_layer',anim,384,354,101,0,1,1)
	local anim = CreateAnimation('data/Texture/Scenes/Common/area.png',0,0,8,8,1,0)
	anim:SetHotSpot(4,4)
	anim:SetBlendMode(2)
	anim:SetColor('0x01FFFFFF')
	screen:CreateObject('area_bedroom',anim,385,357,102,0,5,30)
end

function FreeData(screen)
end
-- CLEAR_ON_SAVE_END
