function OnSceneLoad(screen)
    game_state = {}    
    LoadData(screen)
    local pf = play_field
    local level = pf:GetLevelScreen()
    game_state.custom_cursors = {}
    game_state.locked_objects = {}
    --level:AddTaskSafe(CreateTask('', '', ''))
    --zone = CreateZoomZone('')
    game_state.locker = ReferenceCounter()
    game_state.active_zone = nil
    game_state.inventory_zones = {}
	game_state.active_objects = {}
	
	local object = play_field:GetObject('sprajt_mehanizma_s_ruchkoj');
	inventory_winch = CreateInventory(object:GetX(),
                                        object:GetY(),
                                        object:GetZ() + 10,
                                        "winch_active_zone",
                                        CreateItem('knob_tool', 'knob_scaled'),
                                        CreateItem('gear_tool', 'gear_scaled'),
                                        CreateItem('spool_tool', 'spool'),
                                        play_field:GetTimer(),
										"INVENTORY_TASK_22_1");
										
	inventory_winch.state_changer = 'OnWinchComplete'
	
	inventory_winch.OnComplete = function()
		local obj = play_field:GetObject('dveri')
		obj:SetClipping(0, 346, 1024, 768)
		local af = CreateAnimationEffector()
		af:AttachTo(obj)
		af:SetKey(_TRACK_Y, 0, 457)
		af:SetKey(_TRACK_Y, 2, 243)
		af:Play()
		
		play_field:GetObject('sprajt_mehanizma_s_ruchkoj'):ChangeOpacity(0,1,0.3)
		RegisterZoomZone(zoom_luggage, 'active_zone_luggage')
		local sound = CreateSoundEntity('22_OpenGarage');
		sound:AttachTo(play_field)
		sound:Play(false)
		--PlaySound("22_OpenGarage")
	end
										
	CreateTool('knob', 'knob_scaled')
	CreateTool('gear', 'gear_scaled')
	CreateTool('spool', 'spool')
	 
	AddRandomTasks(17,
				CreateTask('BELL', '', 'bell'),                    
				CreateTask('BOWKNOT', '', 'bowknot'),
				CreateTask('BURGER', '', 'burger'),
				CreateTask('FRAME', '', 'frame'),
				CreateTask('GRAPE', '', 'grape'),
				CreateTask('HAMMER', '', 'hammer'),
				CreateTask('HANGER', '', 'hanger'),
				CreateTask('HORSESHOE', '', 'horseshoe'),
				CreateTask('LIGHTBULB', '', 'lightbulb'),					
				CreateTask('BROOM1', '', 'broom'),
				CreateTask('MUSHROOM', '', 'mushroom'),
				CreateTask('PADLOCK', '', 'padlock'),
				CreateTask('BUOY', '', 'ring_buoy'),
				CreateTask('SAW', '', 'saw'),
				CreateTask('SHIP', '', 'ship'),
				CreateTask('SPEAR', '', 'spear'),
				CreateTask('TURTLE', '', 'turtle'),
				CreateTask('VIOLIN', '', 'violin'));
				
	level:AddTaskSafe( CreateTask('REPAIR_KIT', '', 'repairkit') );
				
	zoom_luggage = CreateZoomZone('close_zone_luggage',
                                 '',
                                 'Zoom_back',
                                 'sprajt_sumki',
                                 'repairkit')
								 
	zoom_luggage.SetOffsetZ(120)
	
	InitChain1(screen);
	game_state.chain1.state_changer = 'OnStartingComicsComplete'
    InitChain2(screen);
    InitChain3(screen);
	game_state.chain3.final = true
    AddSheriffEyeLogic();
    
    play_field:GetObject('sprajt_mehanizma_s_ruchkoj'):SetOpacity(0)
	game_state.meta_object = 'cigar21'
	
	game_state.OnStartingComicsComplete = function()
		inventory_winch.Unlock()
	end
	
	game_state.OnWinchComplete = function()
		local obj = play_field:GetObject('dveri')
		obj:SetClipping(0, 346, 1024, 768)
		obj:SetY(243)		
		
		play_field:GetObject('sprajt_mehanizma_s_ruchkoj'):SetOpacity(1)
		RegisterZoomZone(zoom_luggage, 'active_zone_luggage')	
	end
	
	play_field:IgnoreOnTestPoint('dveri');
	
	game_state.CheckWinConditions = function()
		if IsNotebookClosed() and ArePanelTasksComplete() then
		    if game_state.chain2_shown then
			    game_state.chain3.Start()
			    return
			end
			game_state.chain2.Start()
		end
	end
	
	game_state.chain2_shown = false
end

function OnMinigameCompleted()
end

function InitChain1(screen)
    local comics1 = CreateComics(screen, 'Sheriff', 'eyes');
    local comics2 = CreateComics(screen, 'Barman');    
	
	comics2.OnHidden = function()
		comics2.DefaultOnHidden()
		inventory_winch.Unlock()
	end
    
    game_state.chain1 = CreateChain('pixel_comics_1',
                                    screen,
                                    comics1, GetString("COMICS_22_1"),
                                    comics2, GetString("COMICS_22_2"));
end

function InitChain2(screen)
    local comics1 = CreateComics(screen, 'Sheriff', 'eyes');
    local comics2 = CreateComics(screen, 'Barman');
    local comics3 = CreateComics(screen, 'Sheriff', 'eyes');
    comics3.OnHidden = function()
        comics3.DefaultOnHidden()
        game_state.chain2_shown = true
        game_state.CheckWinConditions()
    end
    
    game_state.chain2 = CreateChain('pixel_comics_2',
                                    screen,
                                    comics1, GetString("COMICS_22_3"),
                                    comics2, GetString("COMICS_22_4"),
                                    comics3, GetString("COMICS_22_5"));
end

function InitChain3(screen)
    local comics1 = CreateComics(screen, 'Sheriff', 'eyes');    
	
	comics1.OnHidden = function()
		OnWin()
	end
    
    game_state.chain3 = CreateChain('pixel_comics_3',
                                    screen,
                                    comics1, GetString("COMICS_22_6"));
end

function OnSceneFree(screen)
    FreeData(screen)
end

function OnNotebookClosed()
	game_state.chain3.Start()
end

function OnNotebookOpened()
end

function OnUpdate(dt)
end

function OnScreenShown()
	game_state.chain1.Start();
end

function OnExit()
end

function OnObjectMouseEnter(object)
    DefaultOnObjectMouseEnter(object)
end

function OnObjectMouseLeave(object)
    DefaultOnObjectMouseLeave(object)
end

function OnObjectMouseDown(object)
    obj_name = object:GetName()
    local pf = play_field
    local is_wrong_click = true

    is_wrong_click = DefaultOnObjectMouseDown(object)

    if is_wrong_click then
        --pf:ShowWrongObjectEffect()
    end

    return is_wrong_click;    
end

function OnObjectMouseUp(object)
end


function OnObjectWasFound(object)
    if object:GetName() == 'repairkit' then
        game_state.chain2.Start()
    end
end

function OnObjectWillBeFound(object)
	if object:GetName() == 'repairkit' then
        zoom_luggage.SetVisible(false)        
        game_state.locker:Release()
		UnregisterZoomZone(zoom_luggage)
    end
    if not play_field:GetLevelScreen():HasTaskObjects() then
		GetProfileManager():GetCurrentProfile():AddStateChanger('CheckWinConditions')
	end
end


function OnToolFound(tool)
end

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

function OnObjectReachPanel(object)
end

function OnButtonPress(title)
    DefaultOnButtonPress(title)
end

function OnMouseMove(previous_pos, current_pos)
end

function OnSetTool(tool)
end

function CanBeHinted(obj)
    if locked_object[obj] ~= true then
        return true
    else
        return false
    end
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/22_base_court_9/Layers/22_back.jpg',0,0,1024,768,1,0)
	anim:SetHotSpot(512,384)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('22_back',anim,512,385,90,0,1,1)
	local anim = CreateAnimation('data/Texture/Scenes/22_base_court_9/Layers/dveri.png',0,0,222,217,1,0)
	anim:SetHotSpot(111,108)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('dveri',anim,557,457,102,0,1,1)
	local anim = CreateAnimation('data/Texture/Scenes/22_base_court_9/Items/bowknot.png',0,0,50,32,1,0)
	anim:SetHotSpot(25,16)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('bowknot',anim,962,292,100,-15,1,1)
	local anim = CreateAnimation('data/Texture/Scenes/22_base_court_9/Items/burger.png',0,0,69,52,1,0)
	anim:SetHotSpot(34,26)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('burger',anim,868,670,100,0,1,1)
	local anim = CreateAnimation('data/Texture/Scenes/22_base_court_9/Items/frame.png',0,0,69,89,1,0)
	anim:SetHotSpot(34,44)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('frame',anim,397,496,100,40,1,1)
	local anim = CreateAnimation('data/Texture/Scenes/22_base_court_9/Items/gear.png',0,0,78,77,1,0)
	anim:SetHotSpot(39,38)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('gear',anim,549,401,110,0,1,1)
	local anim = CreateAnimation('data/Texture/Scenes/22_base_court_9/Items/grape.png',0,0,37,52,1,0)
	anim:SetHotSpot(18,26)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('grape',anim,854,370,100,0,1,1)
	local anim = CreateAnimation('data/Texture/Scenes/22_base_court_9/Items/hammer.png',0,0,102,39,1,0)
	anim:SetHotSpot(51,19)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('hammer',anim,706,618,100,15,1,1)
	local anim = CreateAnimation('data/Texture/Scenes/22_base_court_9/Items/hanger.png',0,0,92,51,1,0)
	anim:SetHotSpot(46,25)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('hanger',anim,381,112,105,0,1,1)
	local anim = CreateAnimation('data/Texture/Scenes/22_base_court_9/Items/horseshoe.png',0,0,60,67,1,0)
	anim:SetHotSpot(30,33)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('horseshoe',anim,533,223,110,0,1,1)
	local anim = CreateAnimation('data/Texture/Scenes/22_base_court_9/Items/knob.png',0,0,115,61,1,0)
	anim:SetHotSpot(57,30)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('knob',anim,311,737,100,0,1,1)
	local anim = CreateAnimation('data/Texture/Scenes/22_base_court_9/Items/knob_tool.png',0,0,111,59,1,0)
	anim:SetHotSpot(55,29)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('knob_tool',anim,493,63,100,20,0.6,0.6)
	local anim = CreateAnimation('data/Texture/Scenes/22_base_court_9/Items/knob_tool.png',0,0,111,59,1,0)
	anim:SetHotSpot(55,29)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('knob_scaled',anim,312,735,100,20,0.6,0.6)
	local anim = CreateAnimation('data/Texture/Scenes/22_base_court_9/Items/lightbulb.png',0,0,36,38,1,0)
	anim:SetHotSpot(18,19)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('lightbulb',anim,793,142,105,0,1,1)
	local anim = CreateAnimation('data/Texture/Scenes/22_base_court_9/Items/broom.png',0,0,52,183,1,0)
	anim:SetHotSpot(26,91)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('broom',anim,932,499,100,0,1,1)
	local anim = CreateAnimation('data/Texture/Scenes/22_base_court_9/Items/mushroom.png',0,0,72,48,1,0)
	anim:SetHotSpot(36,24)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('mushroom',anim,860,529,100,0,1,1)
	local anim = CreateAnimation('data/Texture/Scenes/22_base_court_9/Items/padlock.png',0,0,30,39,1,0)
	anim:SetHotSpot(15,19)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('padlock',anim,261,439,100,0,1,1)
	local anim = CreateAnimation('data/Texture/Scenes/22_base_court_9/Items/ring_buoy.png',0,0,65,87,1,0)
	anim:SetHotSpot(32,43)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('ring_buoy',anim,334,351,100,0,1,1)
	local anim = CreateAnimation('data/Texture/Scenes/22_base_court_9/Items/saw.png',0,0,147,38,1,0)
	anim:SetHotSpot(73,19)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('saw',anim,682,226,100,36,1,1)
	local anim = CreateAnimation('data/Texture/Scenes/22_base_court_9/Items/ship.png',0,0,54,55,1,0)
	anim:SetHotSpot(27,27)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('ship',anim,298,260,100,-20,1,1)
	local anim = CreateAnimation('data/Texture/Scenes/22_base_court_9/Items/spear.png',0,0,23,271,1,0)
	anim:SetHotSpot(11,135)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('spear',anim,729,452,102,-2,1,1)
	local anim = CreateAnimation('data/Texture/Scenes/22_base_court_9/Items/spool.png',0,0,87,56,1,0)
	anim:SetHotSpot(43,28)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('spool',anim,461,715,100,0,1,1)
	local anim = CreateAnimation('data/Texture/Scenes/22_base_court_9/Items/spool_tool.png',0,0,68,51,1,0)
	anim:SetHotSpot(34,25)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('spool_tool',anim,560,66,100,0,1,1)
	local anim = CreateAnimation('data/Texture/Scenes/22_base_court_9/Items/turtle.png',0,0,77,50,1,0)
	anim:SetHotSpot(38,25)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('turtle',anim,255,564,100,0,1,1)
	local anim = CreateAnimation('data/Texture/Scenes/22_base_court_9/Items/violin.png',0,0,113,44,1,0)
	anim:SetHotSpot(56,22)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('violin',anim,480,311,110,0,1,1)
	local anim = CreateAnimation('data/Texture/Scenes/22_base_court_9/Layers/sprajt_cherepahi.png',0,0,41,33,1,0)
	anim:SetHotSpot(20,16)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('sprajt_cherepahi',anim,232,573,102,0,1,1)
	local anim = CreateAnimation('data/Texture/Scenes/22_base_court_9/Layers/sprajt_gamburgera.png',0,0,26,63,1,0)
	anim:SetHotSpot(13,31)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('sprajt_gamburgera',anim,856,672,104,-30,1,1)
	local anim = CreateAnimation('data/Texture/Scenes/22_base_court_9/Layers/sprajt_mehanizma_s_ruchkoj.png',0,0,71,67,1,0)
	anim:SetHotSpot(35,33)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('sprajt_mehanizma_s_ruchkoj',anim,772,499,100,0,1,1)
	local anim = CreateAnimation('data/Texture/Scenes/22_base_court_9/Layers/sprajt_molotka.png',0,0,46,34,1,0)
	anim:SetHotSpot(23,17)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('sprajt_molotka',anim,674,605,105,-5,1,1)
	local anim = CreateAnimation('data/Texture/Scenes/22_base_court_9/Layers/sprajt_ramki.png',0,0,79,103,1,0)
	anim:SetHotSpot(39,51)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('sprajt_ramki',anim,400,493,105,37,1,1)
	local anim = CreateAnimation('data/Texture/Scenes/22_base_court_9/Layers/sprajt_dveri.png',0,0,250,112,1,0)
	anim:SetHotSpot(125,56)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('sprajt_dveri',anim,560,509,105,0,1,1)
	local anim = CreateAnimation('data/Texture/Scenes/Common/pixel.jpg',0,0,8,8,1,0)
	anim:SetHotSpot(4,4)
	anim:SetBlendMode(2)
	anim:SetColor('0x00FFFFFF')
	screen:CreateObject('pixel_comics_1',anim,512,384,401,0,128,96)
	local anim = CreateAnimation('data/Texture/Scenes/Common/pixel.jpg',0,0,8,8,1,0)
	anim:SetHotSpot(4,4)
	anim:SetBlendMode(2)
	anim:SetColor('0x00FFFFFF')
	screen:CreateObject('pixel_comics_2',anim,512,384,401,0,128,96)
	local anim = CreateAnimation('data/Texture/Scenes/Common/pixel.jpg',0,0,8,8,1,0)
	anim:SetHotSpot(4,4)
	anim:SetBlendMode(2)
	anim:SetColor('0x00FFFFFF')
	screen:CreateObject('pixel_comics_3',anim,512,384,401,0,128,96)
	local anim = CreateAnimation('data/Texture/Comics/Sheriff.dds',0,0,530,636,1,0)
	anim:SetHotSpot(266,256)
	anim:SetBlendMode(2)
	anim:SetColor('0x00FFFFFF')
	screen:CreateObject('Sheriff',anim,756,390,400,0,1,1)
	local anim = CreateAnimation('data/Texture/Comics/eyes.png',0,0,79,33,1,0)
	anim:SetHotSpot(39,16)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('eyes',anim,745,212,500,0,1,1)
	local anim = CreateAnimation('data/Texture/Comics/Barman.png',0,0,540,550,1,0)
	anim:SetHotSpot(270,275)
	anim:SetBlendMode(2)
	anim:SetColor('0x00FFFFFF')
	screen:CreateObject('Barman',anim,759,493,100,0,1,1)
	local anim = CreateAnimation('data/Texture/Scenes/22_base_court_9/Items/gear.png',0,0,78,77,1,0)
	anim:SetHotSpot(39,38)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('gear_tool',anim,633,64,100,0,0.8,0.8)
	local anim = CreateAnimation('data/Texture/Scenes/22_base_court_9/Items/gear.png',0,0,78,77,1,0)
	anim:SetHotSpot(39,38)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('gear_scaled',anim,551,404,100,0,0.8,0.8)
	local anim = CreateAnimation('data/Texture/Scenes/Common/pixel.jpg',0,0,8,8,1,0)
	anim:SetHotSpot(4,4)
	anim:SetBlendMode(2)
	anim:SetColor('0x01FFFFFF')
	screen:CreateObject('active_zone_luggage',anim,573,476,100,0,14,5)
	local anim = CreateAnimation('data/Texture/Scenes/22_base_court_9/Zoom/Zoom_back.jpg',0,0,640,480,1,0)
	anim:SetHotSpot(320,240)
	anim:SetBlendMode(2)
	anim:SetColor('0x00FFFFFF')
	screen:CreateObject('Zoom_back',anim,512,384,200,0,1,1)
	local anim = CreateAnimation('data/Texture/Scenes/22_base_court_9/Zoom/sprajt_sumki.png',0,0,127,113,1,0)
	anim:SetHotSpot(63,56)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('sprajt_sumki',anim,480,415,210,0,1,1)
	local anim = CreateAnimation('data/Texture/Scenes/22_base_court_9/Zoom/repair_kit.png',0,0,184,154,1,0)
	anim:SetHotSpot(92,77)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('repairkit',anim,435,392,203,0,1,1)
	local anim = CreateAnimation('data/Texture/Scenes/Common/pixel.jpg',0,0,8,8,1,0)
	anim:SetHotSpot(4,4)
	anim:SetBlendMode(2)
	anim:SetColor('0x01FFFFFF')
	screen:CreateObject('winch_active_zone',anim,768,496,101,0,10,10)
	local anim = CreateAnimation('data/Texture/Scenes/22_base_court_9/Layers/over_door.jpg',0,0,220,17,1,0)
	anim:SetHotSpot(110,8)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('over_door',anim,557,343,103,0,1,1)
	local anim = CreateAnimation('data/Texture/Scenes/22_base_court_9/Items/bell.png',0,0,42,80,1,0)
	anim:SetHotSpot(21,40)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('bell',anim,248,692,100,0,1,1)
	local anim = CreateAnimation('data/Texture/Scenes/21_ distant_dinner-wagon_9/cigar21.png',0,0,110,56,1,0)
	anim:SetHotSpot(55,28)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('cigar21',anim,473,669,100,0,1,1)
	local anim = CreateAnimation('data/Texture/Scenes/22_base_court_9/Animation/1.png',0,0,409,248,25,10)
	anim:SetHotSpot(205,124)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('1',anim,205,126,100,0,1,1)
	local anim = CreateAnimation('data/Texture/Scenes/22_base_court_9/Animation/2.png',0,0,582,169,25,10)
	anim:SetHotSpot(291,85)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('2',anim,445,77,100,0,1,1)
	local anim = CreateAnimation('data/Texture/Scenes/22_base_court_9/Animation/3.png',0,0,440,235,25,10)
	anim:SetHotSpot(220,118)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('3',anim,803,119,100,0,1,1)
end

function FreeData(screen)
end
-- CLEAR_ON_SAVE_END
