function OnSceneLoad(screen)
	ELEMENTS_AMOUNT = 18;
	
	play_field.game_state = game_state;
	
    game_state = {}    
    LoadData(screen)
    
    game_state.custom_cursors = {}
    game_state.locked_objects = {}
    game_state.active_objects = {}
    game_state.locker = ReferenceCounter()
    game_state.game_skip = false;
	game_state.timer = screen:GetTimer();
	game_state.screen = screen;
    
    game_state.HELP_STRING = "HELP_MINIGAME_28";
    
	AddMiniGamesGUI(screen);
     
	game_state.screen:SetLayer(2);
	
	Objects = {};
	for i = 1, ELEMENTS_AMOUNT do
		Objects[i] = {};
		Objects[i].Obj = screen:GetObject(""..i.."");
		Objects[i].NeedPos = Objects[i].Obj:GetPosition();
		Objects[i].Active = true;
		Objects[i].Locked = false;
		Objects[i].Index = i;
		Objects[i].Name = Objects[i].Obj:GetName();
		------------------------------------------------
		Objects[i].Obj:SetAngle(M_PI_OVER_2 * (math.random(4) - 1));
		Objects[i].Obj:SetZ(250 + i);
		Objects[i].Obj:SetXY(math.random(200.0) + 50.0, math.random(300.0) + 50.0);
		------------------------------------------------
		Objects[i].StartPos = Objects[i].Obj:GetPosition();
		Objects[i].Z = Objects[i].Obj:GetZ();
	end
	
	InitChain1(screen);
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_28_3"))
                                    
    comics1.OnShown = function()
       OnMinigameComicsShown(comics1)
    end
end


function GetObjectByName(name)
	for i = 1, ELEMENTS_AMOUNT do
		if(Objects[i].Name == name) then
			return  Objects[i];
		end
	end
	
	return nil
end

function IsComplited()
	for i = 1, ELEMENTS_AMOUNT do
		if(Objects[i].Active == true) then
			return false;
		end	
	end
	
	return true;
end

function StartDrag(object)
	DragObject = object;
	DragObject.Obj:SetZ(DragObject.Z + 100);
end

function UpdateDrag()
	if(DragObject ~= nil) then
		DragObject.Obj:SetXY(GetMouseX(), GetMouseY());
	end
end

function StopDrag()
	if(DragObject ~= nil) then
		if(SetElement(DragObject) == false) then
			DragObject.Obj:SetZ(DragObject.Z);
		end
		
		DragObject = nil;
	end
end

function SetElement(Object)	
	if(Object.Obj:GetAngle() > M_TWO_PI) then
		local d =0;
	end
	
	if(math.abs(Object.Obj:GetX() - Object.NeedPos.x) < 50 and math.abs(Object.Obj:GetY() - Object.NeedPos.y) < 50 and Object.Obj:GetAngle() == 0) then
		Object.Obj:Move(Object.NeedPos.x - Object.Obj:GetX(), Object.NeedPos.y - Object.Obj:GetY(), 0.2);
		Object.Active = false;
		Object.Obj:SetZ(Object.Z - 100);
		Object.Obj:GetAnimation():SetColor("0xFFBBBBBB");
		
		if(IsComplited()) then
			for i = 1, ELEMENTS_AMOUNT do
				Objects[i].Obj:GetAnimation():SetColor("0xFFFFFFFF");
				Objects[i].Obj:ChangeOpacity(0.0, 1.0, 2.0);
			end
			TimerDo(game_state.timer, 2.0, ShowDoneButton);
		end
		
		return true;
	end
	
	return false;
end

function SkipMiniGame()
	if(game_state.game_skip == false) then
		game_state.game_skip = true;
		
		for i = 1, ELEMENTS_AMOUNT do
			if(Objects[i].Active == true) then
				Objects[i].Obj:GetAnimation():SetColor("0xFFFFFFFF");
				Objects[i].Obj:Move(Objects[i].NeedPos.x - Objects[i].Obj:GetX(), Objects[i].NeedPos.y - Objects[i].Obj:GetY(), 1.0);
				Objects[i].Obj:Rotate(M_TWO_PI - Objects[i].Obj:GetAngle(), 1.0);
				Objects[i].Active = false;
			end
			TimerDo(game_state.timer, 1.0, UnHide, i);
		end
		
		return 3.0;
	end
	
	return -1.0;
end

function UnHide(i)
	Objects[i].Obj:ChangeOpacity(0.0, 1.0, 2.0);
end

function OnScreenShown()   
end

function OnSceneFree(screen)
    FreeData(screen)
end

function OnUpdate(dt)
	CheckPuzzlePiecePos(DragObject)	
	UpdateDrag();
	oldMouseX = GetMouseX()
    oldMouseY = GetMouseY()	
end

function OnLose()
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

	local Obj = GetObjectByName(obj_name);
	
	if(Obj ~= nil and Obj.Active == true) then
		StartDrag(Obj);
	end	

    is_wrong_click = DefaultOnObjectMouseDown(object)

    if is_wrong_click then
        --pf:ShowWrongObjectEffect()
    end    
end

function OnObjectRightMouseDown(object)
	obj_name = object:GetName()
	local Object = GetObjectByName(obj_name);
	
	if(Object ~= nil and Object.Locked == false and Object.Active == true) then
		local af = CreateAnimationEffector()
		
		af:AttachTo(Object.Obj)
		af:SetMode(_ANIM_FWD)
		af:SetKey(_TRACK_A, 0.0, Object.Obj:GetAngle())
		af:SetKey(_TRACK_A, 0.15, Object.Obj:GetAngle() + M_PI_OVER_2)
		af:Play()
		
		Object.Locked = true;
		
		if(DragObject == nil) then
			flag = 1;
		else
			flag = 0;
		end
		
		TimerDo(game_state.timer, 0.17, SetElem, flag, Object.Index);
    end
end

function SetElem(flag, Index)
	Objects[Index].Locked = false;
	CorrectAngle(Objects[Index]);
	if(flag == 1) then
		SetElement(Objects[Index]);
	end
end

function OnObjectRightMouseUp(object)
end

function OnObjectMouseUp(object)
	StopDrag();
end

function OnObjectWillBeFound(object)
end

function OnToolFound(tool)
end

function OnToolApply(tool, object)
    DefaultOnToolApply(tool, object)
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

--хака для исправления недоповорота на малых FPS
function CorrectAngle(Object)
	local angle = Object.Obj:GetAngle();
	Object.Obj:SetAngle(fix_angle(Object.Obj:GetAngle()))
	Object.Obj:SetAngle(_correct(angle, 0, 					M_PI_OVER_2));
	Object.Obj:SetAngle(_correct(angle, M_PI_OVER_2, 		M_PI));
	Object.Obj:SetAngle(_correct(angle, M_PI, 				M_PI + M_PI_OVER_2));
	Object.Obj:SetAngle(_correct(angle, M_PI + M_PI_OVER_2, M_TWO_PI));
	Object.Obj:SetAngle(fix_angle(Object.Obj:GetAngle()))
	
	if(Object.Obj:GetAngle() > M_TWO_PI) then
		local d =0;
	end
end

function _correct(angle, value1, value2)
	if (angle > value1 and angle < value2) then
		return value2;
	end
	
	return angle;
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/28_puzzle_book/bak.jpg',0,0,1024,768,1,0)
	anim:SetHotSpot(512,384)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('bak',anim,512,384,99,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/28_puzzle_book/1.png',0,0,192,262,1,0)
	anim:SetHotSpot(96,131)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('1',anim,824,351,100,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/28_puzzle_book/2.png',0,0,172,242,1,0)
	anim:SetHotSpot(86,121)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('2',anim,776,547,100,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/28_puzzle_book/3.png',0,0,216,211,1,0)
	anim:SetHotSpot(108,105)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('3',anim,706,569,100,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/28_puzzle_book/4.png',0,0,191,201,1,0)
	anim:SetHotSpot(95,100)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('4',anim,665,457,100,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/28_puzzle_book/5.png',0,0,270,212,1,0)
	anim:SetHotSpot(135,106)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('5',anim,505,542,100,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/28_puzzle_book/6.png',0,0,123,190,1,0)
	anim:SetHotSpot(61,95)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('6',anim,719,336,100,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/28_puzzle_book/7.png',0,0,170,135,1,0)
	anim:SetHotSpot(85,67)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('7',anim,600,386,100,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/28_puzzle_book/8.png',0,0,205,198,1,0)
	anim:SetHotSpot(102,99)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('8',anim,487,450,100,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/28_puzzle_book/9.png',0,0,175,148,1,0)
	anim:SetHotSpot(87,74)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('9',anim,650,308,100,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/28_puzzle_book/10.png',0,0,127,117,1,0)
	anim:SetHotSpot(63,58)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('10',anim,532,319,100,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/28_puzzle_book/11.png',0,0,150,163,1,0)
	anim:SetHotSpot(75,81)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('11',anim,614,241,100,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/28_puzzle_book/12.png',0,0,202,150,1,0)
	anim:SetHotSpot(101,75)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('12',anim,436,365,100,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/28_puzzle_book/13.png',0,0,315,207,1,0)
	anim:SetHotSpot(157,103)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('13',anim,798,166,100,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/28_puzzle_book/14.png',0,0,183,215,1,0)
	anim:SetHotSpot(91,107)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('14',anim,523,162,100,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/28_puzzle_book/15.png',0,0,244,226,1,0)
	anim:SetHotSpot(122,113)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('15',anim,449,265,100,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/28_puzzle_book/16.png',0,0,158,170,1,0)
	anim:SetHotSpot(79,85)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('16',anim,655,140,100,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/28_puzzle_book/17.png',0,0,158,148,1,0)
	anim:SetHotSpot(79,74)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('17',anim,326,426,100,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/28_puzzle_book/18.png',0,0,257,206,1,0)
	anim:SetHotSpot(128,103)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('18',anim,305,514,100,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,756,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,750,212,500,0,1,1)
end

function FreeData(screen)
end
-- CLEAR_ON_SAVE_END
