function OnMiniGameInit(screen)
	LoadMiniGameData(screen);
	
	minigame_active = false;
	minigame_screen = screen;
	screen:IgnoreOnTestPoint('Layer_Gear_Over');
	screen:IgnoreOnTestPoint('Mouth_Open');
	
	OBJECTS_AMOUNT = 4;
	Objects = {}	
	AddObjects(1, screen:GetObject('Ear'), screen:GetObject('Ear_'));
	AddObjects(2, screen:GetObject('Eye'), screen:GetObject('Eye_'));
	AddObjects(3, screen:GetObject('Gear'), screen:GetObject('Gear_'));
	AddObjects(4, screen:GetObject('Skin'), screen:GetObject('Skin_'));


	zone_doll = CreateZoomZone('area_zoom_doll',
						   'CloseZoneDoll',
						   'BG_Zoom_Doll', true,
						   'Ear', true,
						   'Eye', true,
						   'Gear', true,
						   'Layer_Gear_Over', true,
						   'Layer_Skin_Over', true,
						   'Layer_Ear_Over', true,
						   'Skin', true,
						   'Ear_', false,
						   'Eye_', false,
						   'Gear_', false,
						   'Layer_Gear_Over_', true,
						   'Skin_', false,
						   'Mouth_', true,
						   'Mouth_Open',false,
						   'Pimpa_zoom', true)
		
	zone_doll.on_open = function()
		minigame_active = true;
				
		for i = 1,4 do
			if HasGameState('MINI_GAME_DOOL_STATE', ''..Objects[i].Index) == true then
				Objects[i].Active = false;
				Objects[i].Entity:SetVisible(false);
				Objects[i].Target:SetOpacity(1.0);
				zone_doll.SetObjectVisible(Objects[i].Target:GetName(), true);
			end
		end
		
		if(IsDollCreateComplited() == true) then
			OpenMouth();
			
			--[[minigame_screen:GetObject('Mouth_'):SetVisible(false);
			zone_doll.SetObjectVisible('Mouth_', false);
			zone_doll.SetObjectVisible('Mouth_Open', true);
			minigame_screen:GetObject('Pimpa_zoom'):SetOpacity(1.0);
			minigame_screen:GetObject('Mouth_Open'):SetOpacity(1.0);]]
		else
			game_state.level_type = 'mini_game';
			play_field:GetLevelScreen():GetPanel():SetMode(PM_MINIGAME);
			ShowMonolog('24_MiniGame', 'C_24_MINI_GAME2')
		end
    end
	
	zone_doll.on_close = function()
		minigame_active = false;
		game_state.level_type = 'quest';
		play_field:GetLevelScreen():GetPanel():SetMode(PM_QUEST)
		if (game_state.comics)
		then
		    game_state.comics.panel_mode_to_restore = PM_QUEST;
		end
    end
	
	minigame_screen:GetObject('Pimpa_zoom'):SetVisible(false);	
			
	on_find = function()
		zone_doll.Disable();
		game_state.ignore_on_highlight['area_zoom_doll'] = true
	end
	
	on_load_when_tool_found = function()
		zone_doll.Disable();
		game_state.ignore_on_highlight['area_zoom_doll'] = true
	end
	
	RegisterToolFindInfo('24_pimpa', 'Pimpa_zoom', on_find, on_load_when_tool_found)
	
	LoadSound('DollClick', 'data/Sound/24_Dolls/Doll_click.ogg');
	LoadSound('24_Doll_mouth_open', 'data/Sound/24_Dolls/Doll_mouth_open.ogg');
end

function SkipMiniGame()
	for i = 1,4 do
		if HasGameState('MINI_GAME_DOOL_STATE', ''..Objects[i].Index) == false then
			AddGameState('MINI_GAME_DOOL_STATE', ''..Objects[i].Index);
	
			Objects[i].Active = false;
			Objects[i].Entity:SetVisible(false);
			Objects[i].Target:SetVisible(true);
			Objects[i].Target:ChangeOpacity(0.0, 1.0, 0.2);
			Objects[i].Target:ChangeScale(0.5, 1.0, 0.2);
		end
	end
	
	game_state.level_type = 'quest';
	play_field:GetLevelScreen():GetPanel():SetMode(PM_QUEST)
		
	if(IsDollCreateComplited() == true) then
		TimerDoString("OpenMouth();"..
                      "MakeSoundEntity('24_Doll_mouth_open'):Play(false);",
                      0.5, play_field:GetTimer())
	end
end

function AddObjects(index, object, target)
	Objects[index] = {}
	Objects[index].Entity = object;
	Objects[index].Target = target;
	Objects[index].StartPos = Objects[index].Entity:GetPosition();
	Objects[index].Active = true;
	Objects[index].Index = index;
end

function GetObjectByName(object_name)
	for i = 1,OBJECTS_AMOUNT do
		if(object_name == Objects[i].Entity:GetName()) then
			return Objects[i];
		end
	end
	
	return nil;
end

function StartDrag(object)
	DragObject = object;
	SetCursor('CursorTool')
	DragObject.Entity:SetZ(DragObject.Entity:GetZ() + 200);
end

function StopDrag(doAbort)
	if(DragObject ~= nil) then
		if(math.abs(DragObject.Entity:GetX() - DragObject.Target:GetX()) < 40 and
           math.abs(DragObject.Entity:GetY() - DragObject.Target:GetY()) < 40) and not doAbort
        then
			DragObject.Active = false;
			DragObject.Entity:SetVisible(false);
			DragObject.Target:SetVisible(true);
			DragObject.Target:ChangeOpacity(0.0, 1.0, 0.2);
			DragObject.Target:ChangeScale(0.5, 1.0, 0.2);
			DragObject.Entity:SetZ(0);
			
			if HasGameState('MINI_GAME_DOOL_STATE', ''..DragObject.Index) == false then
				AddGameState('MINI_GAME_DOOL_STATE', ''..DragObject.Index);
				zone_doll.SetObjectVisible(DragObject.Target:GetName(), true);
				PlaySound('DollClick');
			end
			
			if(IsDollCreateComplited() == true) then
				TimerDoString("OpenMouth();"..
                              "MakeSoundEntity('24_Doll_mouth_open'):Play(false);",
                              0.5, play_field:GetTimer())
			end
		elseif (DragObject.Entity:GetName() == "Gear" and
                math.abs(DragObject.Entity:GetX() - DragObject.Target:GetX()) < 100 and
                math.abs(DragObject.Entity:GetY() - DragObject.Target:GetY()) < 100) and not doAbort
        then
			DragObject.Active = false;
			DragObject.Entity:SetVisible(false);
			DragObject.Target:SetVisible(true);
			DragObject.Target:ChangeOpacity(0.0, 1.0, 0.2);
			DragObject.Target:ChangeScale(0.5, 1.0, 0.2);
			DragObject.Entity:SetZ(0);
			
			if(IsDollCreateComplited() == true) then
				TimerDoString("OpenMouth();" ..
                              "MakeSoundEntity('24_Doll_mouth_open'):Play(false);",
                              0.5, play_field:GetTimer())
			end
		else
			GoHome(DragObject);
			--DragObject.Entity:Move(DragObject.StartPos.x - DragObject.Entity:GetX(), DragObject.StartPos.y - DragObject.Entity:GetY(), 0.2);
			DragObject.Entity:SetZ(DragObject.Entity:GetZ() - 200);
		end
		
		DragObject = nil;
		SetCursor('CursorDefault');
	end
end

function GoHome(obj)
	 local af = CreateAnimationEffector()--AnimationEffector()
	 af:AttachTo(obj.Entity)
	 
	 --[[
	 af:SetKey(_TRACK_O, 0.0, 1.0)
	 af:SetKey(_TRACK_O, 0.2, 0.0)
	 af:SetKey(_TRACK_X, 0.2, obj.Entity:GetX())
	 af:SetKey(_TRACK_Y, 0.2, obj.Entity:GetY())
	 af:SetKey(_TRACK_X, 0.25, obj.StartPos.x)
	 af:SetKey(_TRACK_Y, 0.25, obj.StartPos.y)
	 af:SetKey(_TRACK_O, 0.25, 0.0)
	 af:SetKey(_TRACK_O, 0.45, 1.0)
	 --]]
	 
	 --af:SetKey(_TRACK_O, 0.0, 1.0)
	 --af:SetKey(_TRACK_O, 0.2, 0.0)
	 af:SetKey(_TRACK_X, 0, obj.Entity:GetX())
	 af:SetKey(_TRACK_Y, 0, obj.Entity:GetY())
	 af:SetKey(_TRACK_X, 0.2, obj.StartPos.x)
	 af:SetKey(_TRACK_Y, 0.2, obj.StartPos.y)
	 --af:SetKey(_TRACK_O, 0.25, 0.0)
	 --af:SetKey(_TRACK_O, 0.45, 1.0)

	 af:Play()	
end

function OpenMouth()
	game_state.level_type = 'quest';
	play_field:GetLevelScreen():GetPanel():SetMode(PM_QUEST)

	zone_doll.SetObjectVisible('Mouth_Open', true);
	zone_doll.SetObjectVisible('Mouth_', false);

	minigame_screen:GetObject('Mouth_'):ChangeOpacity(1.0, 0.0, 0.3);
	minigame_screen:GetObject('Pimpa_zoom'):SetVisible(true);
	minigame_screen:GetObject('Mouth_Open'):ChangeOpacity(0.0, 1.0, 0.3);
end

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

function MG_Update(dt)
	if(minigame_active == false) then
		return;
	end
	
	if(DragObject ~= nil) then
		DragObject.Entity:SetXY(GetMousePos().x, GetMousePos().y);
	end
end

function MG_OnObjectMouseDown(object)
	if(minigame_active == false) then
		return;
	end
	
	obj = GetObjectByName(object:GetName());
	
	if(obj ~= nil and obj.Active == true) then
		StartDrag(obj);
	end
end

function MG_OnObjectMouseUp(object)
	if(minigame_active == false) then
		return;
	end
	
	StopDrag();
end

--------------------------------------------------------------------------------
-- CLEAR_ON_SAVE_BEGIN
-- Do not edit code listed below (generated by the editor on each save)
function LoadMiniGameData(screen)
	local anim = CreateAnimation('data/Texture/Minigames/Doll/BG_Zoom_Doll.jpg',0,0,640,480,1,0)
	anim:SetHotSpot(320,240)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('BG_Zoom_Doll',anim,512,384,159,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/Doll/Ear.PNG',0,0,108,90,1,0)
	anim:SetHotSpot(54,45)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('Ear',anim,625,189,160,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/Doll/Eye.PNG',0,0,90,100,1,0)
	anim:SetHotSpot(45,50)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('Eye',anim,727,254,160,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/Doll/Layer_Ear_Over.PNG',0,0,53,66,1,0)
	anim:SetHotSpot(26,33)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('Layer_Ear_Over',anim,670,190,165,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/Doll/Layer_Gear_Over.PNG',0,0,137,72,1,0)
	anim:SetHotSpot(68,36)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('Layer_Gear_Over',anim,214,227,165,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/Doll/Layer_Skin_Over.PNG',0,0,142,126,1,0)
	anim:SetHotSpot(71,63)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('Layer_Skin_Over',anim,285,502,165,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/Doll/Skin.PNG',0,0,132,69,1,0)
	anim:SetHotSpot(66,34)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('Skin',anim,241,475,160,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/Doll/Gear.PNG',0,0,71,65,1,0)
	anim:SetHotSpot(35,32)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('Gear',anim,239,247,160,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/Doll/Safe/Ear.PNG',0,0,75,81,1,0)
	anim:SetHotSpot(37,40)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('Ear_',anim,330,347,160,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/Doll/Safe/Eye.PNG',0,0,76,92,1,0)
	anim:SetHotSpot(38,46)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('Eye_',anim,373,273,160,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/Doll/Safe/Gear.PNG',0,0,52,58,1,0)
	anim:SetHotSpot(26,29)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('Gear_',anim,586,557,160,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/Doll/Safe/Skin.PNG',0,0,110,85,1,0)
	anim:SetHotSpot(55,42)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('Skin_',anim,420,512,160,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/Doll/Safe/Mouth.PNG',0,0,65,72,1,0)
	anim:SetHotSpot(32,36)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('Mouth_',anim,452,343,164,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/Doll/Safe/Layer_Gear_Over.PNG',0,0,58,73,1,0)
	anim:SetHotSpot(29,36)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('Layer_Gear_Over_',anim,601,566,160,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/Doll/Pimpa_zoom.PNG',0,0,37,37,1,0)
	anim:SetHotSpot(18,18)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('Pimpa_zoom',anim,448,333,160,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/Doll/Mouth_Open.PNG',0,0,82,113,1,0)
	anim:SetHotSpot(41,56)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('Mouth_Open',anim,460,355,161,0,1,1)
end