function OnSceneLoad(screen)	
	play_field.game_state = game_state
		
    game_state = {}    
    
    game_state.elements_to_finish = 0
       
    LoadData(screen)
    local pf = play_field
    local level = pf:GetLevelScreen()
    game_state.custom_cursors = {}
    game_state.locked_objects = {}
    game_state.active_objects = {}
    --level:AddTaskSafe(CreateTask('', '', ''))
    --zone = CreateZoomZone('')
    game_state.locker = ReferenceCounter()
    game_state.active_zone = nil
    game_state.game_skip = false;
    game_state.screen = screen;
    game_state.HELP_STRING = "HELP_MINIGAME_04";
    
    z = 200
	
	game_state.elements_to_finish = 0
		
	game_state.locked_objects['BG'] = true
	
	RegisterElement(screen:GetObject('pazl_1'))
	RegisterElement(screen:GetObject('pazl_2'))
	RegisterElement(screen:GetObject('pazl_3'))
	RegisterElement(screen:GetObject('pazl_4'))
	RegisterElement(screen:GetObject('pazl_5'))
	RegisterElement(screen:GetObject('pazl_6'))
	RegisterElement(screen:GetObject('pazl_7'))
	RegisterElement(screen:GetObject('pazl_8'))
	RegisterElement(screen:GetObject('pazl_9'))
	RegisterElement(screen:GetObject('pazl_10'))
	RegisterElement(screen:GetObject('pazl_11'))
	RegisterElement(screen:GetObject('pazl_12'))
	RegisterElement(screen:GetObject('pazl_13'))
	RegisterElement(screen:GetObject('pazl_14'))
	RegisterElement(screen:GetObject('pazl_15'))
	RegisterElement(screen:GetObject('pazl_16'))
	
	game_state.timer = screen:GetTimer()
	AddMiniGamesGUI(screen);
	
	game_state.active_object = nil
	
	InitChain1(screen)	
	AddSheriffEyeLogic(screen)
	
	game_state.screen:SetLayer(2)
end

function InitChain1(screen)
    local comics1 = CreateComics(screen,                                                                  
                                 'Sheriff',
								 'eyes')
                                     
    comics1.OnHidden = function()
		game_state.locker:Release()    
		OnCompletedGame();
    end	               
    
    game_state.finish_dialog = CreateChain('pixel',
                                    screen,
                                    comics1, GetString("COMICS_4_10"))
                                    
    comics1.OnShown = function()
        OnMinigameComicsShown(comics1)
    end
end

function OnScreenShown()   
end

function OnAllPiecesReachedPositions()
    ShowDoneButton();
end

function RegisterElement(object, position)
    local element_data = {}
	if not game_state.elements then
        game_state.elements = {}
    end
	element_data.name = object:GetName()
	element_data.pos = {}
	element_data.pos.x = object:GetX()
	element_data.pos.y = object:GetY()
	local randY = math.random(2) - 1;
	local randX = math.random(randY + 1) - 1;
	
	if(object:GetName() ~= "pazl_1")  then
		object:SetXY(100 + math.random(50) + 800 * randX, 100 + math.random(50) + 500 * randY)
	
		local GetRandomAngle = function()
			local rnd = math.random(0, 6)
			if rnd == 0 then
				return 0;
			end
			if rnd == 1 then
				return M_PI_OVER_3;
			end
			if rnd == 2 then
				return 2 * M_PI_OVER_3;
			end
			if rnd == 3 then
				return M_PI;
			end
			if rnd == 4 then
				return M_PI + M_PI_OVER_3;
			end
			if rnd == 5 then
				return M_PI + 2 * M_PI_OVER_3;
			end
			if rnd == 6 then
				return 0;
			end
		end
		
		object:SetZ(z);
		z = z + 1
		table.insert(game_state.elements, element_data)
		game_state.elements_to_finish = game_state.elements_to_finish + 1
	end
	--object:SetAngle(GetRandomAngle())	
	
end

function OnSceneFree(screen)
    FreeData(screen)
end

function OnUpdate(dt)
	CheckPuzzlePiecePos(game_state.active_object)
    if game_state.active_object then
        game_state.active_object:SetXY(GetMouseX(), GetMouseY())
    end
    oldMouseX = GetMouseX()
    oldMouseY = GetMouseY()
end

function OnExit()
end

function OnObjectMouseEnter(object)
    DefaultOnObjectMouseEnter(object)
end

function OnObjectMouseLeave(object)
    DefaultOnObjectMouseLeave(object)
end

function OnObjectMouseDown(object)
	DefaultOnObjectMouseDown(object)
    local object_name = object:GetName()
    local is_wrong_click = true
    local pf = game_state.screen
    
    if game_state.locked_objects[object_name] then
        return
    end
    
    if game_state.elements and game_state.game_skip == false then
	    for i, v in ipairs(game_state.elements) do        
		    if object_name == v.name then
				PlaySound("ToolSet");
    		    game_state.active_object = pf:GetObject(v.name)
    		    game_state.active_object:SetZ(game_state.active_object:GetZ() + 100)
	    	    is_wrong_click = false;
		    end
        end
    end    
end

function OnObjectRightMouseDown(object)
    --[[if game_state.locked_objects[object:GetName()] then
        return
    end
    
    local af = CreateAnimationEffector()
    af:AttachTo(object)
    af:SetMode(_ANIM_FWD)
    
    local GetDesiredAngle = function(base)
        
        if base < 0.000001 then return M_PI_OVER_3 end
        if math.abs(base - M_PI_OVER_3) < 0.000001 then return 2 * M_PI_OVER_3 end
        if math.abs(base - 2 * M_PI_OVER_3) < 0.000001 then return M_PI end
        if math.abs(base - M_PI) < 0.000001 then return 4 * M_PI_OVER_3 end
        if math.abs(base - M_PI_OVER_3 - M_PI) < 0.000001 then return 5 * M_PI_OVER_3 end
        if math.abs(base - 2 * M_PI_OVER_3 - M_PI) < 0.000001 then return M_TWO_PI end       
    end
    
    local angle = object:GetAngle()
    assert(angle)
    
    desired_angle = GetDesiredAngle(object:GetAngle())
    af:SetKey(_TRACK_A, 0.0, object:GetAngle())
    af:SetKey(_TRACK_A, 0.2, desired_angle)
    af:Play()	
    if math.abs(desired_angle - M_TWO_PI) < M_PI_OVER_3 / 2 then
	    desired_angle = 0
	end
	game_state.locked_objects[object:GetName()] = true;
	GTimerDoString('puzzle:GetObject("'..object:GetName()..'"):SetAngle('..desired_angle..')', 0.2)
	GTimerDoString('game_state.locked_objects["'..object:GetName()..'"] = false', 0.2)	
	GTimerDoString('CheckElementPos("'..object:GetName()..'")', 0.2);]]
end

function CheckElementPos(element_name)
    local object = game_state.screen:GetObject(element_name)
    local found = false
    for i, v in ipairs(game_state.elements) do
		if object:GetName() == v.name then
		    object:SetAngle(fix_angle(object:GetAngle()))
			if math.abs(v.pos.x - object:GetX()) < 10 and
                  math.abs(v.pos.y - object:GetY()) < 10 and
                  object:GetAngle() == 0 then
				PlaySound("ToolApply");
				object:SetX(v.pos.x);
				object:SetY(v.pos.y);
				object:SetZ(object:GetZ() - 200);
				game_state.elements_to_finish = game_state.elements_to_finish - 1
				game_state.locked_objects[object:GetName()] = true;
				found = true
                break
			end
		end
	end
	return found
end


function OnObjectRightMouseUp(object)
end

function OnObjectMouseUp(object)
    if game_state.active_object then
		local found = false
		local active_obj = game_state.active_object
		found = CheckElementPos(active_obj:GetName())
		if not found then
			local obj = game_state.active_object
			obj:SetZ(obj:GetZ() - 100)			
		end
		if game_state.elements_to_finish == 0 and game_state.game_skip == false then
			ShowDoneButton();
        end
		game_state.active_object = nil
	end
end

function OnObjectWillBeFound(object)
end

function OnToolFound(tool)
end

function OnToolApply(tool, object)
    ShowToolFailEffect()
end

function OnObjectReachPanel(object)
end

function OnButtonPress(title)
    DefaultOnButtonPress(title)
    OnMiniGameButtonPress(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

function OnLostFocus()
end

function OnGainFocus()
end


function SkipMiniGame()
	if(game_state.game_skip == false) then
		game_state.game_skip = true
		local gathering_time = 1    
		for i, v in ipairs(game_state.elements) do
			local obj = game_state.screen:GetObject(v.name)
			obj:SetZ(obj:GetZ() + 100)
			
			local af = CreateAnimationEffector()        
			af:AttachTo(obj)
			af:SetMode(_ANIM_FWD)
			
			af:SetKey(_TRACK_X, 0.0, obj:GetX())
			af:SetKey(_TRACK_X, gathering_time, v.pos.x)
			af:SetKey(_TRACK_Y, 0.0, obj:GetY())        
			af:SetKey(_TRACK_Y, gathering_time, v.pos.y)        
			af:SetKey(_TRACK_A, 0.0, obj:GetAngle())
			af:SetKey(_TRACK_A, gathering_time, 0)
			af:Play()        
		end
		TimerDo(game_state.timer,
				gathering_time,
				OnAllPiecesReachedPositions)
				
		return 1.0;
	end
	
	return -1.0;
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/Minigames/4_Triangles/pazl_1.png',0,0,156,138,1,0)
	anim:SetHotSpot(78,69)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('pazl_1',anim,508,366,100,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/4_Triangles/pazl_2.png',0,0,157,138,1,0)
	anim:SetHotSpot(78,69)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('pazl_2',anim,508,227,100,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/4_Triangles/pazl_3.png',0,0,156,140,1,0)
	anim:SetHotSpot(78,70)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('pazl_3',anim,586,365,100,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/4_Triangles/pazl_4.png',0,0,155,140,1,0)
	anim:SetHotSpot(77,70)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('pazl_4',anim,663,500,100,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/4_Triangles/pazl_5.png',0,0,156,137,1,0)
	anim:SetHotSpot(78,68)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('pazl_5',anim,510,499,100,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/4_Triangles/pazl_6.png',0,0,157,138,1,0)
	anim:SetHotSpot(78,69)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('pazl_6',anim,354,499,100,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/4_Triangles/pazl_7.png',0,0,155,137,1,0)
	anim:SetHotSpot(77,68)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('pazl_7',anim,429,363,100,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/4_Triangles/pazl_8.png',0,0,155,138,1,0)
	anim:SetHotSpot(77,69)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('pazl_8',anim,507,92,100,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/4_Triangles/pazl_9.png',0,0,156,139,1,0)
	anim:SetHotSpot(78,69)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('pazl_9',anim,585,229,100,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/4_Triangles/pazl_10.png',0,0,157,140,1,0)
	anim:SetHotSpot(78,70)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('pazl_10',anim,431,230,100,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/4_Triangles/pazl_11.png',0,0,157,139,1,0)
	anim:SetHotSpot(78,69)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('pazl_11',anim,354,367,100,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/4_Triangles/pazl_12.png',0,0,159,140,1,0)
	anim:SetHotSpot(79,70)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('pazl_12',anim,664,366,100,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/4_Triangles/pazl_13.png',0,0,156,141,1,0)
	anim:SetHotSpot(78,70)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('pazl_13',anim,740,504,100,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/4_Triangles/pazl_14.png',0,0,159,142,1,0)
	anim:SetHotSpot(79,71)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('pazl_14',anim,588,503,100,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/4_Triangles/pazl_15.png',0,0,160,141,1,0)
	anim:SetHotSpot(80,70)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('pazl_15',anim,433,501,100,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/4_Triangles/pazl_16.png',0,0,160,137,1,0)
	anim:SetHotSpot(80,68)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('pazl_16',anim,282,499,100,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/4_Triangles/BG.jpg',0,0,1024,768,1,0)
	anim:SetHotSpot(512,384)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('BG',anim,512,384,90,0,1,1)
	local anim = CreateAnimation('data/Texture/Comics/Sheriff.dds',0,0,530,636,1,0)
	anim:SetHotSpot(266,256)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('Sheriff',anim,734,390,400,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',anim,512,384,501,0,128,96)
	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,727,212,500,0,1,1)
end

function FreeData(screen)
end
-- CLEAR_ON_SAVE_END
