function CreateComics(screen,                      
                      ...)    
    local comics = {}
    comics.screen = screen
    local objects = {...}    
    comics.objects = {}
    for i, v in ipairs(objects) do
        local obj = screen:GetObject(v)
        comics.objects[v] = obj
        obj:GetAnimation():SetColor('0xFFFFFFFF')
        obj:SetOpacity(0)
        local new_layer = COMICS_LAYER
        obj:SetLayer(new_layer)
        --obj:SetZ(obj:GetZ() + play_field:GetMaxObjectsZ() + HINT_OFFSET_Z + 1)
    end
    
    local te = CreateTextEntity("Comics", "")
	te:SetXY(20, 40)
	te:SetZ(200)
	te:SetTextAlignment(HGETEXT_TOP + HGETEXT_LEFT)
	--te:AttachTo(comics.objects[bubble])
	
	local ProceedHint = CreateTextEntity("Comics", GetString("PRESS_ANY_KEY"))
	ProceedHint:AttachTo(screen)
	ProceedHint:SetXY(571, 759)
	ProceedHint:SetLayer(COMICS_LAYER)
	ProceedHint:SetZ(500)
	ProceedHint:SetScale(0.5)	
	local af = CreateAnimationEffector()
	af:AttachTo(ProceedHint)
	af:SetMode(_ANIM_PINGPONG)
	af:SetKey(_TRACK_O, 0, 0)
	af:SetKey(_TRACK_O, 1, 1)
	af:Play()	
	ProceedHint:Hide()
	
	te:SetOpacity(0)
	
	comics.text = te
	comics.proceed = ProceedHint
	comics.timer = screen:GetTimer()
	
	comics.SetVisible = function(is_visible)	    
        for i, v in pairs(comics.objects) do
            obj = v
            if is_visible then
                obj:ChangeOpacity(0., 1., ZOOM_ZONE_APPEARING_TIME)
            else
                obj:ChangeOpacity(1., 0., ZOOM_ZONE_APPEARING_TIME)
            end
        end
        if is_visible then
            comics.text:ChangeOpacity(0., 1., ZOOM_ZONE_APPEARING_TIME)
        else
            comics.text:ChangeOpacity(1., 0., ZOOM_ZONE_APPEARING_TIME)
			comics.proceed:Hide()
        end
        if is_visible then
			TimerDo(comics.timer, 
					ZOOM_ZONE_APPEARING_TIME,
					comics.OnShown)
        else
			TimerDo(comics.timer, 
					ZOOM_ZONE_APPEARING_TIME,
					comics.OnHidden)
        end
    end
    
	comics.DefaultOnShown = function()
		game_state.locker:AddRef()
		if(comics.screen.GetLevelScreen ~= nil) then
		    --play_field:GetLevelScreen():GetPanel():SetAllowInput(false);
			comics.screen:GetLevelScreen():GetPanel():Stop()
			
		end
		comics.proceed:Show()
		--[[if(minigames_gui ~= nil and minigames_gui.screen ~= nil) then
			minigames_gui.screen:Stop();
		end]]
	end
	
    comics.OnShown = function()
        comics.DefaultOnShown()
    end
    
	comics.DefaultOnHidden = function()
        game_state.locker:Release()
        if(comics.screen.GetLevelScreen ~= nil) then
			comics.screen:GetLevelScreen():GetPanel():Go()         
		end
		--[[if(minigames_gui ~= nil and minigames_gui.screen ~= nil) then
			minigames_gui.screen:Play();
		end]]
    end
	
    comics.OnHidden = function()
        comics.DefaultOnHidden()
    end
    
    comics.OnClick = function()
        local visible = false
        for i, v in pairs(comics.objects) do
            obj = v
            if obj:GetOpacity() == 1 then
                visible = true
            end
            break
        end
        if visible then
            comics.SetVisible(false)
        end
    end        
    
    return comics
end

function CreateChain(active_object, screen, ...)
    local chain1 = {}
    local arg = {...}
    chain1.comicslist = {}
    chain1.screen = screen
    chain1.allow_click = true
	
	if not screen:HasObject('Bubble') then
		local anim = CreateAnimation('data/Texture/Comics/Bubble.png',0,0,821,159,1,0)
		anim:SetHotSpot(0,0)
		anim:SetBlendMode(2)
		anim:SetColor('0xFFFFFFFF')
		local obj = screen:CreateObject('Bubble',anim,189,607,410,0,1,1)
		obj:SetOpacity(0)
		local new_layer = COMICS_LAYER
		obj:SetLayer(new_layer)
		screen:IgnoreOnTestPoint('Bubble')
		--obj:SetZ(play_field:GetObject('Sheriff'):GetZ() + 5)
	end
           
    for i = 1, table.getn(arg), 2 do
       local comics = arg[i]
       local text = arg[i + 1]           
       local size = Size(780, 100)--arg[i + 2]
       
       local comics_info = {}
       comics_info.text_lines = {} 
       
       comics_info.text_lines = comics.text:ExplodeText(text, size.w, size.h)
	   comics.text:SetBorderSize(size.w, size.h)
       comics_info.comics = comics
              
       table.insert(chain1.comicslist, comics_info)
    end
    
    local OnHidden = function(comics)
        comics:SetVisible(true)
        game_state.locker:Release()
    end
    
    local OnShown = function(comics, chain)
        comics.DefaultOnShown()
        chain.allow_click = true
    end
    
    for i,v in ipairs(chain1.comicslist) do
        local c = nil
        if i ~= table.getn(chain1.comicslist) then
            c = chain1.comicslist[i + 1].comics  
            local id = FunctionDispatcher.AddFunction(OnHidden, c)
            v.comics.OnHidden = function()
                FunctionDispatcher.Call(id)
            end
        end
       
        c = v.comics
		
		c.bubble = screen:GetObject('Bubble')
		c.text:AttachTo(c.bubble)
        local id = FunctionDispatcher.AddFunction(OnShown, c, chain1)
        v.comics.OnShown = function()
            FunctionDispatcher.Call(id)
        end
    end
    
    chain1.comics_id = 1
    chain1.text_line_id = 0
    local current_info = chain1.comicslist[chain1.comics_id]
    local current_comics = current_info.comics
    local x = current_info.text_lines:size()
    current_comics.text:SetText(current_info.text_lines:get(chain1.text_line_id))
    
    chain1.Start = function()
        play_field:GetLevelScreen():GetPanel():Stop()
        chain1.screen:GetObject(active_object):GetAnimation():SetColor('0x01FFFFFF')
        chain1.screen:GetObject(active_object):ChangeOpacity(0., 1., ZOOM_ZONE_APPEARING_TIME)
        chain1.comicslist[1].comics.SetVisible(true)
        screen:GetObject('Bubble'):ChangeOpacity(0, 1, ZOOM_ZONE_APPEARING_TIME)
        if chain1.final then
            local pm = GetProfileManager();
            local currentProfile = pm:GetCurrentProfile();
            currentProfile:AddStateChanger('OnWin')
        end
        if chain1.state_changer then
            local pm = GetProfileManager();
            local currentProfile = pm:GetCurrentProfile();
            currentProfile:AddStateChanger(chain1.state_changer)
        end
        OnSceneStateChanged(play_field);
    end
    
    chain1.OnClick = function()
        if not chain1.allow_click then
            return
        end
        play_field:ResetWrongClicksCount();
        local cur_comics = chain1.comicslist[chain1.comics_id]        
        chain1.allow_click = false
        chain1.text_line_id = chain1.text_line_id + 1
        if chain1.text_line_id < cur_comics.text_lines:size() then
            cur_comics.comics.text:SetText(cur_comics.text_lines:get(chain1.text_line_id))
            chain1.allow_click = true
        else
            chain1.text_line_id = 0
            cur_comics.comics.OnClick()
            chain1.comics_id = chain1.comics_id + 1
            local cur_comics = chain1.comicslist[chain1.comics_id]
            if cur_comics ~= nil then
                cur_comics.comics.text:SetText(cur_comics.text_lines:get(chain1.text_line_id))
            else                
                chain1.screen:GetObject(active_object):ChangeOpacity(1, 0, ZOOM_ZONE_APPEARING_TIME)
                screen:GetObject('Bubble'):ChangeOpacity(1, 0, ZOOM_ZONE_APPEARING_TIME)
            end
        end
    end
    
    chain1.SetLayer = function(layer)
        for i,v in pairs(chain1.comicslist) do
            v.SetLayer(layer)
        end
        screen:GetObject('Bubble'):SetLayer(layer)
    end
    
    game_state.active_objects[active_object] = chain1.OnClick
    
    return chain1
end

function CreatePos(x, y)
	local pos = {}
	pos.x = x
	pos.y = y
	return pos
end

function AddSheriffEyeLogic(screen)
    game_state.sheriffs_eye_positions = {}
	local ep = game_state.sheriffs_eye_positions
	
	local cur_screen;
	
	if(screen ~= nil) then
		cur_screen = screen;
	else
		cur_screen = play_field;
	end
	
	local base_x = cur_screen:GetObject('Sheriff'):GetX()
	local base_y = cur_screen:GetObject('Sheriff'):GetY()
	
	--table.insert(ep, CreatePos(499, 276))
	table.insert(ep, CreatePos(base_x - 6, base_y - 178))
	--table.insert(ep, CreatePos(513, 276))
	table.insert(ep, CreatePos(base_x + 8, base_y - 178))
	--table.insert(ep, CreatePos(494, 280))
	table.insert(ep, CreatePos(base_x - 9, base_y - 181))
	--table.insert(ep, CreatePos(489, 273))
	table.insert(ep, CreatePos(base_x - 16, base_y - 174))
	--table.insert(ep, CreatePos(504, 276))
	table.insert(ep, CreatePos(base_x - 1, base_y - 178))
	
	
	local comics_timer = cur_screen:GetTimer()
	
	local SetSheriffNextEyePos
    SetSheriffNextEyePos = function()
	    local r = math.random(1, 5)
	    if game_state.sheriffs_eye_positions then
            local pos = game_state.sheriffs_eye_positions[r]
            local obj = cur_screen:GetObject('eyes')
            local af = CreateAnimationEffector()
            af:AttachTo(obj)
            af:SetKey(_TRACK_X, 0, obj:GetX())
            af:SetKey(_TRACK_X, 1, pos.x)
            af:SetKey(_TRACK_Y, 0, obj:GetY())
            af:SetKey(_TRACK_Y, 1, pos.y)
            
            af:SetKey(_TRACK_S, 0, 1)
            af:SetKey(_TRACK_S, 0.5, 1.05)
            af:SetKey(_TRACK_S, 1, 1)
            
            --[[af:SetKey(_TRACK_SY, 0, 1)
            af:SetKey(_TRACK_SY, 0.5, 2)
            af:SetKey(_TRACK_SY, 1, 1)]]
            
            af:Play()
        end
	    --play_field:GetObject('eyes'):SetXY(pos.x, pos.y)
	    TimerDo(comics_timer,
                math.random(2, 4),
                SetSheriffNextEyePos)
	end
	
	SetSheriffNextEyePos()
end

function AddMDEyeLogic()
    game_state.eye_positions = {}
	local ep = game_state.eye_positions	
	table.insert(ep, CreatePos(509, 237))
	table.insert(ep, CreatePos(506, 233))
	table.insert(ep, CreatePos(503, 237))
	
	local comics_timer = play_field:GetTimer()
	
	local SetNextEyePos
    SetNextEyePos = function()
	    local r = math.random(1, 3)
	    if game_state.eye_positions then
            local pos = game_state.eye_positions[r]
            local obj = play_field:GetObject('eye_md')
            local af = CreateAnimationEffector()
            af:AttachTo(obj)
            af:SetKey(_TRACK_X, 0, obj:GetX())
            af:SetKey(_TRACK_X, 0.5, pos.x)
            af:SetKey(_TRACK_Y, 0, obj:GetY())
            af:SetKey(_TRACK_Y, 0.5, pos.y)
            
            af:SetKey(_TRACK_S, 0, 1)
            af:SetKey(_TRACK_S, 0.25, 1.1)
            af:SetKey(_TRACK_S, 0.5, 1)
            
            --[[af:SetKey(_TRACK_SY, 0, 1)
            af:SetKey(_TRACK_SY, 0.5, 2)
            af:SetKey(_TRACK_SY, 1, 1)]]
            
            af:Play()
            --play_field:GetObject('eyes'):SetXY(pos.x, pos.y)
        end
	    TimerDo(comics_timer,
                math.random(1, 2),
                SetNextEyePos)
	end
	
	SetNextEyePos()
end

function AddSMEyeLogic()
    game_state.sm_eye_positions = {}
	local ep = game_state.sm_eye_positions	
	
	local base_x = play_field:GetObject('Service_Manager'):GetX()
	local obj = play_field:GetObject('eyes_sm'):SetXY(base_x + 29, 163)
    		
	--table.insert(ep, CreatePos(727, 163))
	table.insert(ep, CreatePos(base_x + 29, 163))
	--table.insert(ep, CreatePos(720, 163))
	table.insert(ep, CreatePos(base_x + 22, 163))
	--table.insert(ep, CreatePos(738, 164))
	table.insert(ep, CreatePos(base_x + 40, 164))
	--table.insert(ep, CreatePos(733, 157))
	table.insert(ep, CreatePos(base_x + 35, 157))
	
	local comics_timer = play_field:GetTimer()
	
	local SetSMNextEyePos
    SetSMNextEyePos = function()
	    local r = math.random(1, 4)
	    if game_state.sm_eye_positions then
            local pos = game_state.sm_eye_positions[r]
            local obj = play_field:GetObject('eyes_sm')
            local af = CreateAnimationEffector()
            af:AttachTo(obj)
            af:SetKey(_TRACK_X, 0, obj:GetX())
            af:SetKey(_TRACK_X, 0.5, pos.x)
            af:SetKey(_TRACK_Y, 0, obj:GetY())
            af:SetKey(_TRACK_Y, 0.5, pos.y)
            
            af:Play()	    
        end
	    TimerDo(comics_timer,
                math.random(2, 4),
                SetSMNextEyePos)
	end
	
	SetSMNextEyePos()
end

function AddTOEyeLogic()
    game_state.to_eye_positions = {}
	local ep = game_state.to_eye_positions	
	
	local base_x = play_field:GetObject('Telephone_Operator'):GetX()
	local obj = play_field:GetObject('eyes_to'):SetXY(base_x - 36, 130)
    		
	table.insert(ep, CreatePos(base_x - 36, 130))
	table.insert(ep, CreatePos(base_x - 40, 129))
	table.insert(ep, CreatePos(base_x - 37, 123))
	table.insert(ep, CreatePos(base_x - 30, 132))
	
	local comics_timer = play_field:GetTimer()
	
	local SetTONextEyePos
    SetTONextEyePos = function()
	    local r = math.random(1, 4)
	    if game_state.to_eye_positions then
            local pos = game_state.to_eye_positions[r]
            local obj = play_field:GetObject('eyes_to')
            local af = CreateAnimationEffector()
            af:AttachTo(obj)
            af:SetKey(_TRACK_X, 0, obj:GetX())
            af:SetKey(_TRACK_X, 1, pos.x)
            af:SetKey(_TRACK_Y, 0, obj:GetY())
            af:SetKey(_TRACK_Y, 1, pos.y)
            af:Play()
	    end
	    
	    TimerDo(comics_timer,
                math.random(2, 4),
                SetTONextEyePos)
	end
	
	SetTONextEyePos()
end

function AddOctopusEyeLogic()
	local comics_timer = play_field:GetTimer()
	
	local SetToStart = function()
		play_field:GetObject('Octopus_eyes'):GetAnimation():SetFrame(0)
	end
	
	local Blink
    Blink = function()	    
	    local obj = play_field:GetObject('Octopus_eyes')
		obj:GetAnimation():Play()
	    TimerDo(comics_timer,
                math.random(3, 5),
                Blink)
		TimerDo(comics_timer,
				GetAnimationLength(obj:GetAnimation()),
				SetToStart)
	end
	
	Blink()
end