local on_enter_comics = nil;
local zoom_faucet;

function ActivateAreaHO()
    LinkScene(  'area_table',
                'CursorGlass',
                'data/Scenes/08_Bank_Table_HO.lua',
                true);
end

function ActivateAreaDoor()
    play_field:GetObject('area_door'):SetVisible(true);
    
    LinkScene('area_door',
              'CursorUp',
              'data/Scenes/08_Bank_Door.lua',
              false);
end

function ActivateAreaOffice()
    play_field:GetObject('area_office'):SetVisible(true);
    
    LinkScene('area_office',
              'CursorUp',
              'data/Scenes/09_Bank_Office_Main.lua',
              false);
end

function RegisterHints()
    RegisterMonologObject('area_painting',  'C_08_PAINTING_HINT');
    RegisterMonologObject('area_ship',      'C_08_SHIP_PAINTING_HINT');
    RegisterMonologObject('area_mona_lisa', 'C_08_MONA_LISA_HINT');
end

function OnSceneLoad(screen)
    LoadData(screen)
    DefaultOnSceneLoad('data/Scenes/08_Bank_Main.lua', 'quest')
    
    play_field:GetObject('open_door_light'  ):SetOpacity(0.);
    play_field:GetObject('area_office'      ):SetVisible(false);
    play_field:GetObject('area_door'        ):SetVisible(false);
    
    if HasGameState('ToolApplied', '08_doorknob') then
        play_field:GetObject('layer_without_doorknob'):SetVisible(false);
    end
    
    zoom_faucet = CreateZoomZone('area_faucet',
                                 'CloseZoneFaucet',
                                 'ZOOM_faucet',         true,
                                 'Layer_lever_1',       false,
                                 'Layer_lever_2',       false,
                                 'layer_over',          true,
                                 'area_lever_1',        true,
                                 'Fill_water_anim',     false,
                                 'area_duster_apply',   false);
    
    play_field:GetObject('Fill_water_anim'):GetAnimation():SetMode(HGEANIM_FWD + HGEANIM_NOLOOP);
    play_field:GetObject('Fill_water_anim'):GetAnimation():Stop();
    
    RegisterToolApplyInfo('09_lever', 'area_lever_1', nil, nil,         function(tool_name)
                                                                            zoom_faucet.SetObjectVisible('Layer_lever_1', true, 0.3);
                                                                            zoom_faucet.SetObjectVisible('area_lever_1',  false);
                                                                            
                                                                            PlaySound('08_PlaceLever');
                                                                        end,
                                                                        function(tool_name)
                                                                            zoom_faucet.SetObjectVisible('Layer_lever_1', true);
                                                                            zoom_faucet.SetObjectVisible('area_lever_1',  false);
                                                                        end,
                                                                        function()
                                                                            ShowMonolog('C_08_LEVER_APPLY_HINT', 'C_08_LEVER_APPLY_HINT');
                                                                        end);
                                                                
    RegisterActiveObject('Layer_lever_1',                               function(obj_name)
                                                                            zoom_faucet.SetObjectVisible('Layer_lever_1', false, 0.3);
                                                                            zoom_faucet.SetObjectVisible('Layer_lever_2', true,  0.3);
                                                                            zoom_faucet.SetObjectVisible('Fill_water_anim', true);
                                                                            
                                                                            local anim = play_field:GetObject('Fill_water_anim'):GetAnimation();
                                                                            anim:Play();
                                                                            PlaySound('22_Effluent');
                                                                            
                                                                            play_field:TimerDoString("OnFaterFulled()", GetAnimationLength(anim));
                                                                        end,
                                                                        function(obj_name)
                                                                            zoom_faucet.SetObjectVisible('Layer_lever_1',     false);
                                                                            zoom_faucet.SetObjectVisible('Layer_lever_2',     true);
                                                                            zoom_faucet.SetObjectVisible('Fill_water_anim',   true);
                                                                            zoom_faucet.SetObjectVisible('area_duster_apply', true);
                                                                            
                                                                            local anim = play_field:GetObject('Fill_water_anim'):GetAnimation();
                                                                            anim:SetFrame(anim:GetFrames() - 1);
                                                                        end);
    
    RegisterToolFindInfo('09_duster', 'area_duster',                    function(tool_name)
                                                                            play_field:GetObject('area_duster' ):SetVisible(false);
                                                                            play_field:GetObject('layer_duster'):SetVisible(false);
                                                                        end,
                                                                        function(tool_name)
                                                                            play_field:GetObject('area_duster' ):SetVisible(false);
                                                                            play_field:GetObject('layer_duster'):SetVisible(false);
                                                                        end);
                                                                
    RegisterToolApplyInfo('09_duster', 'area_duster_apply', nil, nil,   function(tool_name)
                                                                            zoom_faucet.Disable();
                                                                            zoom_faucet.SetVisible(false);
                                                                            
                                                                            FindTool('09_duster_wet');
                                                                            
                                                                        end,
                                                                        function(tool_name)
                                                                            zoom_faucet.Disable();
                                                                        end,
                                                                        function()
                                                                            ShowMonolog('C_08_DUSTER_APPLY_HINT', 'C_08_DUSTER_APPLY_HINT');
                                                                        end);
    
    on_enter_comics = CreateComics('08_OnEnter',
                                    CreateComicsFrame('Banker', 'C_08_START_1_BANKER',  'Banker', 'Banker_hand'),  
                                    CreateComicsFrame('Dupen',  'C_08_START_2_DUPEN',   'body',   'head1'),
                                    CreateComicsFrame('Banker', 'C_08_START_3_BANKER',  'Banker', 'Banker_hand'),
                                    CreateComicsFrame('Dupen',  'C_08_START_4_DUPEN',   'body',   'head2'),
                                    CreateComicsFrame('Banker', 'C_08_START_5_BANKER',  'Banker', 'Banker_hand'),
                                    CreateComicsFrame('Dupen',  'C_08_START_6_DUPEN',   'body',   'head1'),
                                    CreateComicsFrame('Banker', 'C_08_START_7_BANKER',  'Banker', 'Banker_hand'),
                                    CreateComicsFrame('Banker', 'C_08_START_7_BANKER_A','Banker', 'Banker_hand'),
                                    CreateComicsFrame('Dupen',  'C_08_START_8_DUPEN',   'body',   'head2'),
                                    CreateComicsFrame('Banker', 'C_08_START_9_BANKER',  'Banker', 'Banker_hand'),
                                    CreateComicsFrame('Banker', 'C_08_START_9_BANKER_A','Banker', 'Banker_hand'),
                                    CreateComicsFrame('Banker', 'C_08_START_9_BANKER_B','Banker', 'Banker_hand'),
                                    CreateComicsFrame('Banker', 'C_08_START_10_BANKER', 'Banker', 'Banker_hand'),
                                    CreateComicsFrame('Dupen',  'C_08_START_11_DUPEN',  'body',   'head1'),
                                    CreateComicsFrame('Banker', 'C_08_START_12_BANKER', 'Banker', 'Banker_hand'));

    if on_enter_comics then
        play_field:GetObject('area_duster'):SetVisible(false);
        play_field:GetObject('area_faucet'):SetVisible(false);
    
        TimerDoString('StartOnEnterComics()', 1.5, play_field:GetTimer())
    else
        if HasGameState('ObjectClicked', 'data/Scenes/08_Bank_Door.lua(area_open)') then
            local state_door_opened = game_state.scene_path..'(doorOpened)';
            
            if not HasGameState('ActionCompleted', state_door_opened) then
                AddGameState('ActionCompleted', state_door_opened);
                play_field:GetObject('open_door_light'):ChangeOpacity(0., 1., OPEN_DOOR_TIME);
                
                PlaySound('OpenDoor');
            else
                play_field:GetObject('open_door_light'):SetOpacity(1.);
            end
            ActivateAreaOffice();
        else
            ActivateAreaDoor();
        end
        
        ActivateAreaHO();
        RegisterHints();
        
    end
end

function OnFaterFulled()
    zoom_faucet.SetObjectVisible('area_duster_apply', true);
end

function StartOnEnterComics() 
    on_enter_comics.on_finish = function()
        play_field:GetObject('area_duster'):SetVisible(true);
        play_field:GetObject('area_faucet'):SetVisible(true);
        
        AddNoteToNotebook('MIGNAUD_ET_FILS_BANK');
        
        ActivateAreaHO();
        ActivateAreaDoor();
        
        RegisterHints();
    end
    on_enter_comics.SetVisible(true)
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_faucet'
        and play_field:GetObject('Layer_lever_1'):GetOpacity() == 1
        and play_field:GetObject('Layer_lever_2'):GetOpacity() == 0
    then
        ShowMonolog('C_08_HINT_FILL_WITH_WATE', 'C_08_HINT_FILL_WITH_WATE');
    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/08_Bank/Zone_main/Layer/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/Characters/Dupen/body.png',0,0,376,520,1,0)
	anim:SetHotSpot(188,260)
	anim:SetBlendMode(2)
	anim:SetColor('0x00FFFFFF')
	screen:CreateObject('body',anim,275,508,102,0,1,1)
	local anim = CreateAnimation('data/Texture/Characters/Dupen/head1.png',0,0,185,244,1,0)
	anim:SetHotSpot(92,122)
	anim:SetBlendMode(2)
	anim:SetColor('0x00FFFFFF')
	screen:CreateObject('head1',anim,308,202,104,0,1,1)
	local anim = CreateAnimation('data/Texture/Characters/Dupen/head2.png',0,0,214,252,1,0)
	anim:SetHotSpot(107,126)
	anim:SetBlendMode(2)
	anim:SetColor('0x00FFFFFF')
	screen:CreateObject('head2',anim,307,200,105,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_table',anim,931,547,101,0,20,20)
	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_office',anim,418,442,103,0,15,20)
	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_door',anim,418,442,104,0,15,20)
	local anim = CreateAnimation('data/Texture/Scenes/08_Bank/Zone_main/Layer/open_door_light.png',0,0,197,197,1,0)
	anim:SetHotSpot(98,98)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('open_door_light',anim,503,463,102,0,1,1)
	local anim = CreateAnimation('data/Texture/Characters/Banker/Banker.png',0,0,628,1164,1,0)
	anim:SetHotSpot(314,582)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('Banker',anim,780,560,102,0,1,1)
	local anim = CreateAnimation('data/Texture/Characters/Banker/Banker_hand.png',0,0,204,279,1,0)
	anim:SetHotSpot(102,139)
	anim:SetBlendMode(2)
	anim:SetColor('0x00FFFFFF')
	screen:CreateObject('Banker_hand',anim,594,486,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_painting',anim,123,338,101,0,9,23)
	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_ship',anim,641,383,102,0,15,10)
	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_mona_lisa',anim,888,384,100,0,8,10)
	local anim = CreateAnimation('data/Texture/Scenes/Common/Animations/candle.png',0,0,20,40,25,12)
	anim:SetHotSpot(10,20)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('candle1',anim,396,171,101,0,0.8,0.8)
	local anim = CreateAnimation('data/Texture/Scenes/Common/Animations/candle.png',0,0,20,40,25,12)
	anim:SetHotSpot(10,20)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('candle2',anim,440,146,101,0,1,1)
	local anim = CreateAnimation('data/Texture/Scenes/Common/Animations/candle.png',0,0,20,40,25,14)
	anim:SetHotSpot(10,20)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('candle3',anim,499,163,101,0,0.8,0.8)
	local anim = CreateAnimation('data/Texture/Scenes/08_Bank/Zone_main/Layer/sprajt_ruchki_main_loc.png',0,0,23,28,1,0)
	anim:SetHotSpot(11,14)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('layer_without_doorknob',anim,412,448,101,0,1,1)
	local anim = CreateAnimation('data/Texture/Scenes/08_Bank/Zone_main/Zoom_faucet/ZOOM_faucet.jpg',0,0,640,480,1,0)
	anim:SetHotSpot(320,240)
	anim:SetBlendMode(2)
	anim:SetColor('0x00FFFFFF')
	screen:CreateObject('ZOOM_faucet',anim,512,384,150,0,1,1)
	local anim = CreateAnimation('data/Texture/Scenes/08_Bank/Zone_main/Zoom_faucet/Layer_lever_1.jpg',0,0,108,71,1,0)
	anim:SetHotSpot(54,35)
	anim:SetBlendMode(2)
	anim:SetColor('0x00FFFFFF')
	screen:CreateObject('Layer_lever_1',anim,573,216,151,0,1,1)
	local anim = CreateAnimation('data/Texture/Scenes/08_Bank/Zone_main/Zoom_faucet/Layer_lever_2.jpg',0,0,108,71,1,0)
	anim:SetHotSpot(54,35)
	anim:SetBlendMode(2)
	anim:SetColor('0x00FFFFFF')
	screen:CreateObject('Layer_lever_2',anim,573,216,152,0,1,1)
	local anim = CreateAnimation('data/Texture/Scenes/08_Bank/Zone_main/Zoom_faucet/layer_over.png',0,0,26,44,1,0)
	anim:SetHotSpot(13,22)
	anim:SetBlendMode(2)
	anim:SetColor('0x00FFFFFF')
	screen:CreateObject('layer_over',anim,542,277,153,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_faucet',anim,281,493,101,0,10,15)
	local anim = CreateAnimation('data/Texture/Scenes/Common/area_circle.png',0,0,128,128,1,0)
	anim:SetHotSpot(64,64)
	anim:SetBlendMode(2)
	anim:SetColor('0x00FFFFFF')
	screen:CreateObject('area_lever_1',anim,574,213,154,0,1,1)
	local anim = CreateAnimation('data/Texture/Scenes/22_RiverPort/Zone_deck2/Zoom_faucet/Fill_water_anim.png',0,0,159,79,72,12)
	anim:SetHotSpot(79,39)
	anim:SetBlendMode(2)
	anim:SetColor('0x00FFFFFF')
	screen:CreateObject('Fill_water_anim',anim,572,313,151,0,1,1)
	local anim = CreateAnimation('data/Texture/Scenes/Common/area_circle.png',0,0,128,128,1,0)
	anim:SetHotSpot(64,64)
	anim:SetBlendMode(2)
	anim:SetColor('0x00FFFFFF')
	screen:CreateObject('area_duster_apply',anim,573,312,156,0,1.37,0.72)
	local anim = CreateAnimation('data/Texture/Scenes/08_Bank/Zone_main/Layer/layer_duster.png',0,0,61,88,1,0)
	anim:SetHotSpot(30,44)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('layer_duster',anim,61,500,101,0,1,1)
	local anim = CreateAnimation('data/Texture/Scenes/Common/area_circle.png',0,0,128,128,1,0)
	anim:SetHotSpot(64,64)
	anim:SetBlendMode(2)
	anim:SetColor('0x01FFFFFF')
	screen:CreateObject('area_duster',anim,67,485,102,0,1,1)
end

function FreeData(screen)
end
-- CLEAR_ON_SAVE_END
