function OnSceneLoad(screen)
    play_field.game_state = game_state;
    
    game_state = {}    
    LoadData(screen)
    
    MiniGameField = 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.inventory_zones = {}
	game_state.active_objects = {}
	game_state.game_skip = false
    game_state.screen = screen;
	game_state.timer = screen:GetTimer()
	
	AddMiniGamesGUI(screen);
	
	game_state.HELP_STRING = "HELP_MINIGAME_20";
	
	game_state.posX    = {};
	game_state.posX[1] = SCR_W/2 - 200;
	game_state.posX[2] = SCR_W/2;
	game_state.posX[3] = SCR_W/2 + 200;
	game_state.posY    = SCR_H / 2;
	
	game_state.target     = 0;
	game_state.moveCount  = 0;
	game_state.moveTarget = 0;
	
	game_state.choice  = 0;
	game_state.c1 = 0;
	game_state.c2 = 0;
	
	game_state.axisS     = 0;
	game_state.reverse   = 0;
	
	game_state.stage = 'wait';
	
	game_state.moveTime  = 0.8;
	game_state.currTime  = 0;
	game_state.xCoef     = 0;
	game_state.yCoef     = 0;
	
	game_state.oInfo     = {};
	game_state.oInfo.obj = nil;
	game_state.oInfo.sX  = 0;
	game_state.oInfo.sY  = 0;
	
	game_state.mInfo     = {};
	game_state.mInfo.obj = nil;
	game_state.mInfo.sX  = 0;
	game_state.mInfo.sY  = 0;
	
	game_state.wins = 0;
	
	
	MiniGameField:GetObject('1_bubble'):SetOpacity(0.0);
	MiniGameField:GetObject('2_bubble'):SetOpacity(0.0);
	MiniGameField:GetObject('3_bubble'):SetOpacity(0.0);
	
	InitText();
	
	cur_text = "";
	
	screen:SetLayer(2)
	
	auxResetGlassesPos();
	--auxStart();
	TimerDo(MiniGameField:GetTimer(), 1.0, auxStart);
	
	InitChain1(MiniGameField);
	AddSheriffEyeLogic(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_20_9"))
                                    
    comics1.OnShown = function()
        OnMinigameComicsShown(comics1)
    end
end

function OnSceneFree(screen)
    FreeData(screen)
end

function InitText()
	info = CreateTextEntity('Bookman24Orange','');
    info:AttachTo(MiniGameField);
	info:SetZ(350.0);
	info:SetTracking(-3.0);
	info:SetScale(1.5);
end

function TextUpdate(text)
	info:SetXY(500.0, 700.0);
	info:SetText(text);
end

function TextNull()
	cur_text = "";
end

------------------------------------------------------------
-- auxiliary functions
------------------------------------------------------------
function auxStart()
    game_state.stage = 'show_target';
    game_state.target = math.random(1, 3);
    
    local obj = MiniGameField:GetObject('bubble');
    obj:Move(MiniGameField:GetObject('stakan'..game_state.target):GetX() - obj:GetX(), MiniGameField:GetObject('stakan'..game_state.target):GetY() - obj:GetY(), 0.5);
    obj:SetVisible( true );
    
    local obj = MiniGameField:GetObject('stakan'..game_state.target);
    obj:GetAnimation():Play();
    
    auxOpenGlass(obj:GetName())
    cur_text = GetString("MINI_GAME_20_0");
	TimerDo(MiniGameField:GetTimer(), 1.0, TextNull);
end
------------------------------------------------------------
function auxShift()
    game_state.stage = 'shift';
    
    game_state.choice     = 0;
    game_state.moveCount  = 0;
    game_state.moveTarget = 7 + math.random(5,7);
    
    game_state.c1 = math.random(1,3);
    repeat
        game_state.c2 = math.random(1,3);
    until game_state.c1 ~= game_state.c2;
    
    auxStartBet();
end
------------------------------------------------------------
function auxStartBet()
    if game_state.moveCount < game_state.moveTarget then
		PlaySound("20_Thimble_out");
        game_state.moveCount = game_state.moveCount + 1;
        
        game_state.reverse = math.random(0,1)
        local prevC1 = game_state.c1;

        repeat
            game_state.c1 = math.random(1,3);
        until game_state.c1~=prevC1 and game_state.c1~=game_state.c2;
        
        game_state.c2 = prevC1;
        if game_state.reverse == 1 then
            game_state.c1, game_state.c2 = game_state.c2, game_state.c1;
        end
        
        local moved1 = MiniGameField:GetObject('stakan'..game_state.c1):GetX();
        local moved2 = MiniGameField:GetObject('stakan'..game_state.c2):GetX();
        
        game_state.currTime = 0;
        
        game_state.oInfo.obj  = MiniGameField:GetObject('stakan'..game_state.c1);
        game_state.oInfo.sX   = game_state.oInfo.obj:GetX();
        game_state.oInfo.sY   = game_state.oInfo.obj:GetY();
        
        game_state.mInfo.obj  = MiniGameField:GetObject('stakan'..game_state.c2);
        game_state.mInfo.sX   = game_state.mInfo.obj:GetX();
        game_state.mInfo.sY   = game_state.mInfo.obj:GetY();
        
        game_state.xCoef = (game_state.mInfo.sX - game_state.oInfo.sX) / game_state.moveTime;
	    game_state.yCoef = math.pi / game_state.moveTime;
	    
	    auxSetDepth(game_state.oInfo.obj:GetName(), game_state.mInfo.obj:GetName());
        
        game_state.axisS = math.floor(0.25 * math.abs(moved1 - moved2));
        game_state.stage = 'move_cups';
    else
        game_state.stage = 'choose';
        
        local obj = MiniGameField:GetObject('bubble');
        obj:SetX( MiniGameField:GetObject('stakan'..game_state.target):GetX() );
        obj:SetY( MiniGameField:GetObject('stakan'..game_state.target):GetY() );
        obj:SetVisible( true );
    end;
end
------------------------------------------------------------
function auxResetGlassesPos()
    for i=1, 3 do
        local obj = MiniGameField:GetObject('stakan'..i);
        obj:SetX(game_state.posX[i]);
	    obj:SetY(game_state.posY);
	    obj:GetAnimation():Stop();
    end
end
------------------------------------------------------------
function auxSetDepth(upper, lower)
    for i=1, 3 do
        local objName = 'stakan'..i
        local obj = MiniGameField:GetObject(objName);
        
        if objName == upper then
            obj:SetZ( 104 );
        elseif objName == lower then
            obj:SetZ( 102 );
        else
            obj:SetZ( 103 );
        end
    end
end
------------------------------------------------------------
function auxOpenGlass(glassName)
    local obj = MiniGameField:GetObject(glassName);
    local time = GetAnimationLength(obj:GetAnimation()) + 2;
    
    obj:GetAnimation():SetMode(HGEANIM_FWD + HGEANIM_NOLOOP);
    obj:GetAnimation():Play();
    
    TimerDo(MiniGameField:GetTimer(), time, auxCloseGlass, glassName);
end
------------------------------------------------------------
function auxCloseGlass(glassName)
	PlaySound("20_Thimble_in");
    local obj = MiniGameField:GetObject(glassName);
    local time = GetAnimationLength(obj:GetAnimation());
    
    obj:GetAnimation():SetMode(HGEANIM_REV + HGEANIM_NOLOOP);
    obj:GetAnimation():Play();
    
    TimerDo(MiniGameField:GetTimer(), time, auxOnGlassClosed, glassName);
end
------------------------------------------------------------
function auxOnGlassClosed()
    if game_state.stage == 'show_target' then
        MiniGameField:GetObject('bubble'):SetVisible( false );
        
        auxShift();
    elseif game_state.stage == 'not_found' then
        game_state.stage = 'show_correct';
        
        auxOpenGlass('stakan'..game_state.target);
        TimerDo(MiniGameField:GetTimer(), 0.5, BollGoHome);	
    elseif game_state.stage == 'found' then
        local dd = 1
        auxStart()
    elseif game_state.stage == 'show_correct' then
        local dd = 1
        auxStart()
    end
end
------------------------------------------------------------

function OnNotebookClosed()
end

function OnNotebookOpened()
end


function OnUpdate(dt)
    local finish_move = false;
    
	TextUpdate(cur_text);
   -- TextUpdate2();
    
    if game_state.stage == 'move_cups' and game_state.game_skip == false then
        game_state.currTime = game_state.currTime + dt;
        
        if game_state.currTime >= game_state.moveTime then
            game_state.currTime = game_state.moveTime;
            finish_move = true;
        end
        
        game_state.oInfo.obj:SetX( game_state.oInfo.sX + game_state.xCoef * game_state.currTime );
        game_state.mInfo.obj:SetX( game_state.mInfo.sX - game_state.xCoef * game_state.currTime );
        
        game_state.oInfo.obj:SetY( game_state.oInfo.sY + math.sin(game_state.yCoef * game_state.currTime) * game_state.axisS );
        game_state.mInfo.obj:SetY( game_state.mInfo.sY - math.sin(game_state.yCoef * game_state.currTime) * game_state.axisS );
        
        if finish_move == true then
            game_state.stage = 'shift';
            auxStartBet();
        end
    end
end

function OnScreenShown()
end

function OnLose()
end

function OnExit()
end

function OnObjectMouseEnter(object)
    DefaultOnObjectMouseEnter(object)
end

function OnObjectMouseLeave(object)
    DefaultOnObjectMouseLeave(object)
end

function BollGoHome()
	local obj = MiniGameField:GetObject('bubble');
	obj:Move(480 - obj:GetX(), 590 - obj:GetY(), 0.5);
end

function OnObjectMouseDown(object)
	DefaultOnObjectMouseDown(object)
	
    obj_name = object:GetName()
    local pf = play_field
    
    if game_state.stage == 'choose' then
        for i=1, 3 do
            if obj_name == 'stakan'..i then
                if game_state.target == i then
					TimerDo(MiniGameField:GetTimer(), 0.5, BollGoHome);					
			
                    game_state.stage = 'found';
                    game_state.wins = game_state.wins + 1;
                    
                    cur_text = GetString("MINI_GAME_20_1");
                    TimerDo(MiniGameField:GetTimer(), 2.0, TextNull);
                    if (game_state.wins < 4 and game_state.wins > 0) then
						MiniGameField:GetObject(game_state.wins..'_bubble'):ChangeOpacity(0.0, 1.0, 1.0);
					end
					
					if(game_state.wins == 3) then
						game_state.stage = "";
						 TimerDo(MiniGameField:GetTimer(), 2.0, ShowDoneButton);
					end
                else
                    game_state.stage = 'not_found';
                    cur_text = GetString("MINI_GAME_20_2");
                    TimerDo(MiniGameField:GetTimer(), 2.0, TextNull);
                end
                auxOpenGlass(obj_name);
                break;
            end
        end
    end
end

function OnObjectMouseUp(object)
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 OnObjectRightMouseDown(object)
end

function OnLostFocus()
end

function OnGainFocus()
end


function OnObjectRightMouseUp(object)
end

function SkipMiniGame()
	if(game_state.game_skip == false) then
		game_state.game_skip = true;
		return 0.1;
	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/20_thimble/fon.jpg',0,0,1024,768,1,0)
	anim:SetHotSpot(512,384)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('fon',anim,512,384,100,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/20_thimble/sh.png',0,0,111,71,1,0)
	anim:SetHotSpot(40,35)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('bubble',anim,530,609,101,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/20_thimble/glass.png',0,0,319,276,7,14)
	anim:SetHotSpot(110,230)
	anim:SetBlendMode(2)
	anim:SetColor('0x01FFFFFF')
	screen:CreateObject('glass1',anim,258,465,2,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/20_thimble/glass.png',0,0,319,276,7,14)
	anim:SetHotSpot(110,230)
	anim:SetBlendMode(2)
	anim:SetColor('0x01FFFFFF')
	screen:CreateObject('glass2',anim,511,486,3,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/20_thimble/glass.png',0,0,319,276,7,14)
	anim:SetHotSpot(110,230)
	anim:SetBlendMode(2)
	anim:SetColor('0x01FFFFFF')
	screen:CreateObject('glass3',anim,763,418,4,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,211,500,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/20_thimble/glass.png',0,0,176,287,5,6)
	anim:SetHotSpot(88,230)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('stakan1',anim,303,390,110,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/20_thimble/glass.png',0,0,176,287,5,6)
	anim:SetHotSpot(88,230)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('stakan2',anim,530,390,110,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/20_thimble/glass.png',0,0,176,287,5,6)
	anim:SetHotSpot(88,230)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('stakan3',anim,758,390,110,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/20_thimble/win.png',0,0,299,119,1,0)
	anim:SetHotSpot(149,59)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('win_2',anim,149,57,105,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/20_thimble/sh_2.png',0,0,111,71,1,0)
	anim:SetHotSpot(55,35)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('1_bubble',anim,70,62,200,0,0.9,0.9)
	local anim = CreateAnimation('data/Texture/Minigames/20_thimble/_sh.png',0,0,75,75,1,0)
	anim:SetHotSpot(37,37)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('3_bubble',anim,231,60,200,0,0.9,0.9)
	local anim = CreateAnimation('data/Texture/Minigames/20_thimble/sh_2.png',0,0,111,71,1,0)
	anim:SetHotSpot(55,35)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('2_bubble',anim,159,62,200,0,0.9,0.9)
end

function FreeData(screen)
end
-- CLEAR_ON_SAVE_END
