INVENTORY_ZONE_FADE_TIME = 0.3

function ShowElementFade(element, delay)
    local af = CreateAnimationEffector()
    af:AttachTo(element)
    af:SetMode(_ANIM_FWD)
    af:SetKey(_TRACK_O, 0.0, 0.0)
    af:SetKey(_TRACK_O, delay, 1.0)
    af:Play()
end

function HideElementFade(element, active_zone)
    local af = CreateAnimationEffector()
    af:AttachTo(element)
    af:SetMode(_ANIM_FWD)
    af:SetKey(_TRACK_O, 0.0, element:GetOpacity())
    af:SetKey(_TRACK_O, INVENTORY_ZONE_FADE_TIME, 0.0)
    af:Play()
    active_zone.timer:Clear()
end

function OnActiveZoneMouseEnter(play_field, active_zone, object_name)
    --[[local tool_name = play_field:GetToolName()
    if tool_name == '' then
        return
    end
    
    if object_name == active_zone.bound_object and 
       not active_zone.visible then
           active_zone.Show(true);
    end
	
    if object_name == active_zone['cyclic_anim_left']:GetName() or
       object_name == active_zone['cyclic_anim_middle']:GetName() or
       object_name == active_zone['cyclic_anim_right']:GetName() 
    then
        active_zone.timer:Clear()
    end]]
end

function OnActiveZoneMouseLeave(play_field, active_zone, object_name)
    --[[local obj_under_mouse = play_field:FindObjectByPos(Vector(GetMouseX(), GetMouseY()))
	local HideZone = function(zone, fade)
		zone.Hide(fade)
	end
    if object_name == active_zone.bound_object and
       active_zone.visible and
       active_zone.appeared_smoothly and
       play_field:GetToolName() ~= '' and
       obj_under_mouse and obj_under_mouse:GetName() ~= active_zone['inventory']:GetName() then
			TimerDo(active_zone.timer,
					0.4,
					HideZone,
					active_zone,
					true);       
    end
    
	local is_zone_item = false
	is_zone_item = object_name == active_zone['cyclic_anim_left']:GetName()
	is_zone_item = is_zone_item or object_name == active_zone['cyclic_anim_middle']:GetName()
	is_zone_item = is_zone_item or object_name == active_zone['cyclic_anim_right']:GetName()
	is_zone_item = is_zone_item or object_name == active_zone['cyclic_anim_right']:GetName()
	
    if is_zone_item and
       active_zone.visible and
       active_zone.appeared_smoothly and
       play_field:GetToolName() ~= '' then       
       active_zone.Hide(true)
    end]]
end

function PlayNextEmotion(active_zone)
    local item_name = ''
    local random_number = math.random(0, 100)
    if random_number >= 0 and random_number < 5 then
        item_name = 'eyes1'
    elseif random_number >= 5 and random_number < 10 then
        item_name = 'eyes2'
    elseif random_number >= 10 and random_number < 15 then
        item_name = 'eyes3'
    elseif random_number >= 15 and random_number < 20 then
        item_name = 'joking'
    elseif random_number >= 15 and random_number < 20 then
        item_name = 'tongue1'
    elseif random_number >= 20 and random_number < 25 then
        item_name = 'tongue2'                            
    else
        item_name = 'eyes'
    end
    local obj = active_zone[item_name];
    if active_zone.visible then
        PlayEmotionAnimation(active_zone, item_name);
    end
    TimerDo(active_zone.timer,
            GetAnimationLength(active_zone[item_name]:GetAnimation()) + 0.1,
            PlayNextEmotion,
            active_zone)    
end

function PlayEmotionAnimation(active_zone, element_name)
    if active_zone.current_animation then
        active_zone[active_zone.current_animation]:SetOpacity(0)
    end 
    active_zone.current_animation = element_name
    active_zone[element_name]:SetOpacity(1)
	active_zone[element_name]:GetAnimation():SetFrame(0);
	active_zone[element_name]:GetAnimation():Play()			
	
	local SetOpacity = function(obj, val)
	    obj:SetOpacity(val)
	end
	
	TimerDo(active_zone.timer,
			GetAnimationLength(active_zone[element_name]:GetAnimation()),
			SetOpacity,
			active_zone[element_name],
			0)	
end

function CheckItem(active_zone,
                   object,
                   tool_name,
                   zone_tool_name,
                   cyclic_animation_name, 
                   finishing_animation_name,
                   flag_name,
                   item_name,
                   light_name)
                   
	if string.find(zone_tool_name, tool_name) ~= nil and 
       not active_zone[flag_name] then	
	    local timer = active_zone.timer
		local delay = 0
		local object_name = object:GetName()
		local equal_to_cyclic_name = object_name == active_zone[cyclic_animation_name]:GetName()
		
		if equal_to_cyclic_name or object_name == active_zone['inventory']:GetName() then --or object_name == active_zone['region']:GetName() then
		    if equal_to_cyclic_name then
			    object:SetOpacity(0)
			end
			active_zone[cyclic_animation_name]:SetVisible(false)
			active_zone[finishing_animation_name]:SetOpacity(1)
			active_zone[finishing_animation_name]:GetAnimation():Play()
			delay = 0.6
			active_zone.items_to_use = active_zone.items_to_use - 1
			active_zone[flag_name] = true
			active_zone[item_name]:SetVisible(false)
			active_zone[light_name]:SetVisible(false)
			
			local index = math.random(1, 2)
			
			PlayEmotionAnimation(active_zone, 'delight'..index)
			local delay_for_complete_callback = GetAnimationLength(active_zone['delight'..index]:GetAnimation())
			for i, v in ipairs(game_state.tools) do
                if v.tool_object_name == tool_name then
                    GetProfileManager():GetCurrentProfile():AddFoundObject(v.scene_object_name)
                end
            end
			if not active_zone.CheckIfComplete(delay_for_complete_callback) then
                OnSceneStateChanged(play_field)
			end
--			PlaySound("ToolApply");
			sound_tool = CreateSoundEntity('ToolApply');
		    sound_tool:AttachTo(play_field);
		    sound_tool:StopSound();
		    sound_tool:Play(false);
			return true
		end
		return false		
	end
end

function OnActiveZoneToolApply(active_zone, tool_name, object)
	if active_zone.visible then
	    if CheckItem(active_zone,
                 object,
                 tool_name,
                 active_zone.tool_1_name,
                 'cyclic_anim_left',
                 'finishing_anim_left',
                 'item_1_found',
                 'item1',
                 'light1') then
            return true
        end
        if CheckItem(active_zone,
                 object,
                 tool_name,
                 active_zone.tool_2_name,
                 'cyclic_anim_middle',
                 'finishing_anim_middle',
                 'item_2_found',
                 'item2',
                 'light2') then
            return true
        end
        if CheckItem(active_zone,
                 object,
                 tool_name,
                 active_zone.tool_3_name,
                 'cyclic_anim_right',
                 'finishing_anim_right',
                 'item_3_found',
                 'item3',
                 'light3') then
            return true
        end	
	end
    return false;
end

function ShowToolFailEffect(tool_name)
    delay = 0.4
	local af = CreateAnimationEffector()
	local obj = play_field:GetObject(tool_name)
    af:AttachTo(obj)
    af:SetMode(_ANIM_FWD)
    af:SetKey(_TRACK_O, 0.0, obj:GetOpacity())
    af:SetKey(_TRACK_O, delay - 0.1, 0.0)
    af:SetKey(_TRACK_O, delay, 1.0)
    af:Play()
    play_field:ShowParticleLine(GetMouseX(),
                                GetMouseY(),
                                play_field:GetOldToolX(),
                                play_field:GetOldToolY(),
                                play_field:GetMaxObjectsZ())
end

function CreateInventory(x, y, z, bound_object, item_1, item_2, item_3, timer, task_text_alias)
    local active_zone = {}
	
	active_zone.request_notebook_on_complete = false
	
	AssignInventoryZoneMethods(active_zone)
	
	active_zone.task_text_alias = task_text_alias
	
    active_zone.z = z
    local name = bound_object
	
	--[[local anim = CreateAnimation('data/Texture/Scenes/Common/inventory/Region.png',0,0,340,340,1,0)
	anim:SetHotSpot(170,170)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	local obj_name = name..'region';
	play_field:CreateObject(obj_name,anim,x-31,y-45,z+20,0,1,1)
	AddInventoryZoneElement(obj_name,  active_zone, "region", false);]]
	
	local anim = CreateAnimation('data/Texture/Scenes/Common/inventory/inventory.png',0,0,91,120,1,0)
	anim:SetHotSpot(75,95)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	local obj_name = name..'inventory';
	play_field:CreateObject(obj_name,anim,x,y,z,0,1,1)
	active_zone.AddElement(obj_name, "inventory", false);
	local anim = CreateAnimation('data/Texture/Scenes/Common/inventory/left/starting.png',0,0,27,22,3,6)
	anim:SetHotSpot(14,11)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	obj_name = name..'starting'
	play_field:CreateObject(obj_name,anim,x - 60,y - 74,z,0,1,1)
	active_zone.AddElement(obj_name, "starting_anim_left", true);
	local anim = CreateAnimation('data/Texture/Scenes/Common/inventory/left/cyclic.png',0,0,119,84,12,12)
	anim:SetHotSpot(60,42)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	obj_name = name..'cyclic'
	play_field:CreateObject(obj_name,anim,x - 106, y - 102,z,0,1,1)
	active_zone.AddElement(obj_name, "cyclic_anim_left", false);
	local anim = CreateAnimation('data/Texture/Scenes/Common/inventory/left/finishing.png',0,0,177,165,6,10)
	anim:SetMode(HGEANIM_NOLOOP)
	anim:SetHotSpot(39,83)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	obj_name = name..'finishing'
	play_field:CreateObject(obj_name,anim,x - 167,y - 84,z,0,1,1)
	active_zone.AddElement(obj_name, "finishing_anim_left", true);
	local anim = CreateAnimation('data/Texture/Scenes/Common/inventory/middle/middlestarting.png',0,0,18,32,3,3)
	anim:SetMode(HGEANIM_NOLOOP)
	anim:SetHotSpot(9,16)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	obj_name = name..'middlestarting'
	play_field:CreateObject(obj_name,anim, x - 35, y - 106,z,0,1,1)
	active_zone.AddElement(obj_name, "starting_anim_middle", true);
	local anim = CreateAnimation('data/Texture/Scenes/Common/inventory/middle/middlecyclic.png',0,0,91,125,12,12)
	anim:SetHotSpot(46,63)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	obj_name = name..'middlecyclic'
	play_field:CreateObject(obj_name,anim,x - 32,y - 151,z,0,1,1)
	active_zone.AddElement(obj_name, "cyclic_anim_middle", false);
	local anim = CreateAnimation('data/Texture/Scenes/Common/inventory/middle/middlefinish.png',0,0,191,165,6,10)
	anim:SetMode(HGEANIM_NOLOOP)
	anim:SetHotSpot(96,83)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	obj_name = name..'middlefinish'
	play_field:CreateObject(obj_name,anim,x - 32,y - 176,z,0,1,1)
	active_zone.AddElement(obj_name, "finishing_anim_middle", true);
	local anim = CreateAnimation('data/Texture/Scenes/Common/inventory/right/rightstarting.png',0,0,26,22,3,3)
	anim:SetMode(HGEANIM_NOLOOP)
	anim:SetHotSpot(13,11)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	obj_name = name..'rightstarting'
	play_field:CreateObject(obj_name,anim,x + 6,y - 77,z,0,1,1)
	active_zone.AddElement(obj_name, "starting_anim_right", true);
	local anim = CreateAnimation('data/Texture/Scenes/Common/inventory/right/rightcyclic.png',0,0,118,84,12,12)
	anim:SetHotSpot(59,42)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	obj_name = name..'rightcyclic'
	play_field:CreateObject(obj_name,anim,x + 50, y-105,z,0,1,1)
	active_zone.AddElement(obj_name, "cyclic_anim_right", false);
	local anim = CreateAnimation('data/Texture/Scenes/Common/inventory/right/rightfinishing.png',0,0,177,165,6,10)
	anim:SetMode(HGEANIM_NOLOOP)
	anim:SetHotSpot(88,83)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	obj_name = name..'rightfinishing'
	play_field:CreateObject(obj_name,anim,x + 60,y - 92,z,0,1,1)
	active_zone.AddElement(obj_name, "finishing_anim_right", true);
	local anim = CreateAnimation('data/Texture/Scenes/Common/inventory/light.png',0,0,97,88,1,0)
	anim:SetMode(HGEANIM_NOLOOP)
	anim:SetHotSpot(49,40)
	anim:SetBlendMode(1)
	anim:SetColor('0xFFFFFFFF')
	obj_name = name..'light1'
	local obj = play_field:CreateObject(obj_name,anim,x - 30, y - 35, z + 1009, 0, 1, 1)
	obj:SetOpacity(0)
	active_zone.AddItem(obj_name, "light1");	
	local anim = CreateAnimation('data/Texture/Scenes/Common/inventory/light.png',0,0,97,88,1,0)
	anim:SetMode(HGEANIM_NOLOOP)
	anim:SetHotSpot(49,40)
	anim:SetBlendMode(1)
	anim:SetColor('0xFFFFFFFF')
	obj_name = name..'light2'
	local obj = play_field:CreateObject(obj_name,anim,x - 30, y - 35, z + 1009, 0, 1, 1)
	obj:SetOpacity(0)
	active_zone.AddItem(obj_name, "light2");	
	local anim = CreateAnimation('data/Texture/Scenes/Common/inventory/light.png',0,0,97,88,1,0)
	anim:SetMode(HGEANIM_NOLOOP)
	anim:SetHotSpot(49,40)
	anim:SetBlendMode(1)
	anim:SetColor('0xFFFFFFFF')
	obj_name = name..'light3'
	local obj = play_field:CreateObject(obj_name,anim,x - 30, y - 35, z + 1009, 0, 1, 1)
	obj:SetOpacity(0)
	active_zone.AddItem(obj_name, "light3");	
	
	local k = 2
	
	local anim = CreateAnimation('data/Texture/Scenes/Common/inventory/head/delight/delight1.png',0,0,91,120,6,2 * k)
	anim:SetMode(HGEANIM_NOLOOP)
	anim:SetHotSpot(45,60)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	obj_name = name..'delight1'
	local obj = play_field:CreateObject(obj_name,anim,x - 30, y - 35, z + 2, 0, 1, 1)
	obj:SetOpacity(0)
	obj.play_time = 3
	active_zone.AddElement(obj_name, "delight1", true);
	local anim = CreateAnimation('data/Texture/Scenes/Common/inventory/head/delight/delight2.png',0,0,91,120,6,2 * k)
	anim:SetMode(HGEANIM_NOLOOP)
	anim:SetHotSpot(45,60)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	obj_name = name..'delight2'
	local obj = play_field:CreateObject(obj_name,anim,x - 30, y - 35, z + 2, 0, 1, 1)
	obj:SetOpacity(0)
	active_zone.AddElement(obj_name, "delight2", true);
	local anim = CreateAnimation('data/Texture/Scenes/Common/inventory/head/eyes/eyes1.png',0,0,91,120,5,2 * k)
	anim:SetMode(HGEANIM_NOLOOP)
	anim:SetHotSpot(45,60)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	obj_name = name..'eyes1'
	local obj = play_field:CreateObject(obj_name,anim,x - 30, y - 35, z + 1, 0, 1, 1)
	obj:SetOpacity(0)
	active_zone.AddElement(obj_name, "eyes1", true);
	local anim = CreateAnimation('data/Texture/Scenes/Common/inventory/head/eyes/eyes2.png',0,0,91,120,4,2 * k)
	anim:SetMode(HGEANIM_NOLOOP)
	anim:SetHotSpot(45,60)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	obj_name = name..'eyes2'
	local obj = play_field:CreateObject(obj_name,anim,x - 30, y - 35, z + 1, 0, 1, 1)
	obj:SetOpacity(0)
	active_zone.AddElement(obj_name, "eyes2", true);
	local anim = CreateAnimation('data/Texture/Scenes/Common/inventory/head/eyes/eyes3.png',0,0,91,120,10,2 * k)
	anim:SetMode(HGEANIM_NOLOOP)
	anim:SetHotSpot(45,60)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	obj_name = name..'eyes3'
	local obj = play_field:CreateObject(obj_name,anim,x - 30, y - 35, z + 1, 0, 1, 1)
	obj:SetOpacity(0)
	active_zone.AddElement(obj_name, "eyes3", true);
	local anim = CreateAnimation('data/Texture/Scenes/Common/inventory/head/eyes/eyes.png',0,0,91,120,2,0.5 * k)
	anim:SetMode(HGEANIM_NOLOOP)
	anim:SetHotSpot(45,60)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	obj_name = name..'eyes'
	local obj = play_field:CreateObject(obj_name,anim,x - 30, y - 35, z + 1, 0, 1, 1)
	obj:SetOpacity(0)
	active_zone.AddElement(obj_name, "eyes", true);
	local anim = CreateAnimation('data/Texture/Scenes/Common/inventory/head/tongue1.png',0,0,91,120,6,2 * k)
	anim:SetMode(HGEANIM_NOLOOP)
	anim:SetHotSpot(45,60)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	obj_name = name..'tongue1'
	local obj = play_field:CreateObject(obj_name,anim,x - 30, y - 35, z + 1, 0, 1, 1)
	obj:SetOpacity(0)
	active_zone.AddElement(obj_name, "tongue1", true);
	local anim = CreateAnimation('data/Texture/Scenes/Common/inventory/head/tongue2.png',0,0,91,120,7,2 * k)
	anim:SetMode(HGEANIM_NOLOOP)
	anim:SetHotSpot(45,60)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	obj_name = name..'tongue2'
	local obj = play_field:CreateObject(obj_name,anim,x - 30, y - 35, z + 1, 0, 1, 1)
	obj:SetOpacity(0)
	active_zone.AddElement(obj_name, "tongue2", true);
	local anim = CreateAnimation('data/Texture/Scenes/Common/inventory/head/joking.png',0,0,91,120,12,2 * k)
	anim:SetMode(HGEANIM_NOLOOP)
	anim:SetHotSpot(45,60)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	obj_name = name..'joking'
	local obj = play_field:CreateObject(obj_name,anim,x - 30, y - 35, z + 1, 0, 1, 1)
	obj:SetOpacity(0)
	active_zone.AddElement(obj_name, "joking", true);
	PlayNextEmotion(active_zone)
	
	active_zone.bound_object = bound_object
	
	game_state.custom_cursors[bound_object] = 'CursorMech'
	
	active_zone.visible = false
	active_zone.appeared_smoothly = false
	
	RegisterInventory(active_zone)
	
	active_zone.items_to_use = 0;
	active_zone.tool_1_name = ''
	active_zone.tool_2_name = ''
	active_zone.tool_3_name = ''
	active_zone.item_1_found=true;
    active_zone.item_2_found=true;
    active_zone.item_3_found=true; 
	
	if item_1 ~= nil then
		active_zone.items_to_use = active_zone.items_to_use + 1
		active_zone.item_1_found = false
		active_zone.tool_1_name = item_1.tool_name
		active_zone.AddItem(item_1.name, 'item1')
	end
	
	if item_2 ~= nil then
		active_zone.items_to_use = active_zone.items_to_use + 1
		active_zone.item_2_found = false
		active_zone.tool_2_name = item_2.tool_name
		active_zone.AddItem(item_2.name, 'item2')
	end
	
	if item_3 ~= nil then
		active_zone.items_to_use = active_zone.items_to_use + 1
		active_zone.item_3_found = false
		active_zone.tool_3_name = item_3.tool_name
		active_zone.AddItem(item_3.name, 'item3')
	end
	
	active_zone.timer = timer
	
	local object = play_field:GetObject(bound_object)
	active_zone.bubbles = AddBubblesAnimation(play_field, 3, 0, 0, 0, 0.25 / object:GetScaleX(), 0.25 / object:GetScaleY(), math.random(0, 5))	
	active_zone.bubbles:AttachTo(object)
	
    active_zone.Lock()
	
    return active_zone
end

function CreateItem(object_name, tool_name)
	local item = {}
	item.tool_name = tool_name	
	item.name = object_name
	return item
end

function RegisterInventory(zone)
    if not game_state.inventory_zones then
        game_state.inventory_zones = {}
    end
    table.insert(game_state.inventory_zones, zone)    
end

function AssignInventoryZoneMethods(inventory_zone)
	inventory_zone.AddElement = function(name, member_alias, ignore)
		local obj = play_field:GetObject(name)
		obj:GetAnimation():Stop()
		obj:SetOpacity(0);
		if ignore then
			play_field:IgnoreOnTestPoint(name)
		end
		inventory_zone[member_alias]=obj
	end
	
	inventory_zone.AddItem = function(name, member)
		assert(member == 'item1' or member == 'item2' or member == 'item3' or member == 'light1' or member == 'light2' or member == 'light3')
		inventory_zone.AddElement(name, member, true)
		local object = play_field:GetObject(name)
		object:SetZ(inventory_zone.z + 10)
		local anim = object:GetAnimation()
		if member == 'item1' then
			object:SetXY(inventory_zone['inventory']:GetX() - 120, inventory_zone['inventory']:GetY() - 104);
		end
		if member == 'light1' then
			object:SetXY(inventory_zone['inventory']:GetX() - 120, inventory_zone['inventory']:GetY() - 104);
			object:SetZ(inventory_zone.z + 8)
		end
		if member == 'item2' then
			object:SetXY(inventory_zone['inventory']:GetX() - 30, inventory_zone['inventory']:GetY() - 167);
		end
		if member == 'light2' then
			object:SetXY(inventory_zone['inventory']:GetX() - 30, inventory_zone['inventory']:GetY() - 167);
			object:SetZ(inventory_zone.z + 8)
		end
		if member == 'item3' then
			object:SetXY(inventory_zone['inventory']:GetX() + 65, inventory_zone['inventory']:GetY() - 106);
		end
		if member == 'light3' then
			object:SetXY(inventory_zone['inventory']:GetX() + 65, inventory_zone['inventory']:GetY() - 106);
			object:SetZ(inventory_zone.z + 8)
		end
	end
	
	inventory_zone.Hide = function(faded)
		if not inventory_zone.visible then
			return
		end
		inventory_zone.OnHiddenImmediate()
		local elements = {}
		table.insert(elements, inventory_zone['finishing_anim_left'])
		table.insert(elements, inventory_zone['starting_anim_left'])
		table.insert(elements, inventory_zone['cyclic_anim_left'])
		table.insert(elements, inventory_zone['finishing_anim_middle'])
		table.insert(elements, inventory_zone['starting_anim_middle'])
		table.insert(elements, inventory_zone['cyclic_anim_middle'])
		table.insert(elements, inventory_zone['finishing_anim_right'])
		table.insert(elements, inventory_zone['starting_anim_right'])
		table.insert(elements, inventory_zone['cyclic_anim_right'])
		table.insert(elements, inventory_zone['item1'])
		table.insert(elements, inventory_zone['item2'])
		table.insert(elements, inventory_zone['item3'])
		table.insert(elements, inventory_zone['inventory'])
		--table.insert(elements, inventory_zone['region'])
		table.insert(elements, inventory_zone['delight1'])
		table.insert(elements, inventory_zone['delight2'])
		table.insert(elements, inventory_zone['eyes'])
		table.insert(elements, inventory_zone['eyes1'])
		table.insert(elements, inventory_zone['eyes2'])
		table.insert(elements, inventory_zone['eyes3'])
		table.insert(elements, inventory_zone['tongue1'])
		table.insert(elements, inventory_zone['tongue2'])
		table.insert(elements, inventory_zone['joking'])
		table.insert(elements, inventory_zone['light1'])
		table.insert(elements, inventory_zone['light2'])
		table.insert(elements, inventory_zone['light3'])
		
		for i, v in pairs(elements) do
			if faded then
				if v:GetOpacity() ~= 0 then
					HideElementFade(v, inventory_zone)
				end
			else
				v:SetOpacity(0)
				inventory_zone.timer:Clear()
			end
		end

		inventory_zone.visible=false
		inventory_zone.appeared_smoothly=false
		if (not inventory_zone.item_1_found or 
		   not inventory_zone.item_2_found or 
		   not inventory_zone.item_3_found) and not inventory_zone.IsLocked() then
			inventory_zone.bubbles:Show()
		end		
	end
	
	inventory_zone.Show = function(fade)
		if inventory_zone.visible then
			return
		end
		if inventory_zone.IsCompleted() then
			return
		end
		
		if fade then
			ShowElementFade(inventory_zone['inventory'], INVENTORY_ZONE_FADE_TIME);		
		else
			inventory_zone['inventory']:SetOpacity(1)		
		end
		
		if inventory_zone.bubbles_explosion then
            inventory_zone.bubbles_explosion:Hide()
			StopSound("BubbleExplosion")
        end
		
		--inventory_zone['region']:SetOpacity(0.1)
		
		local ActivateStartingAnimation = function(inventory_zone, item_found, anim_name)
			if(not item_found) then
				if fade then
					ShowElementFade(inventory_zone[anim_name], INVENTORY_ZONE_FADE_TIME);
				else
					inventory_zone[anim_name]:SetOpacity(1)
				end        
				inventory_zone[anim_name]:GetAnimation():SetFrame(0)
				inventory_zone[anim_name]:GetAnimation():Play()
			end
		end
		
		ActivateStartingAnimation(inventory_zone, inventory_zone.item_1_found, 'starting_anim_left')
		ActivateStartingAnimation(inventory_zone, inventory_zone.item_2_found, 'starting_anim_middle')
		ActivateStartingAnimation(inventory_zone, inventory_zone.item_3_found, 'starting_anim_right')
			
		local ActivateCyclicAnimation = function(inventory_zone, anim_to_stop_name, anim_to_start_name, item_name, light_name)
			inventory_zone[anim_to_stop_name]:SetOpacity(0)
			inventory_zone[anim_to_stop_name]:GetAnimation():Stop()
			inventory_zone[anim_to_start_name]:SetOpacity(1)
			inventory_zone[anim_to_start_name]:GetAnimation():Play()
			inventory_zone[item_name]:SetOpacity(1)
			inventory_zone[light_name]:SetOpacity(0.75)
		end
		
		local timer = inventory_zone.timer
		local delay = 0.5
		
		if(not inventory_zone.item_1_found) then
			TimerDo(timer,
					delay,
					ActivateCyclicAnimation,
					inventory_zone,
					'starting_anim_left',
					'cyclic_anim_left',
					'item1',
					'light1')
		end
		
		if(not inventory_zone.item_2_found) then		
			TimerDo(timer,
					delay,
					ActivateCyclicAnimation,
					inventory_zone,
					'starting_anim_middle',
					'cyclic_anim_middle',
					'item2',
					'light2')
		end
		
		if(not inventory_zone.item_3_found) then		
			TimerDo(timer,
					delay,
					ActivateCyclicAnimation,
					inventory_zone,
					'starting_anim_right',
					'cyclic_anim_right',
					'item3',
					'light3')
		end
		
		inventory_zone.visible=true
		inventory_zone.appeared_smoothly=fade
		
		inventory_zone.bubbles:Hide()		
		inventory_zone.OnShown()
	end
	
	inventory_zone.Unlock = function()
	    if not inventory_zone.IsLocked() or inventory_zone.items_to_use == 0 then
	        return
	    end
		local obj = play_field:GetObject(inventory_zone.bound_object)
		if obj:GetOpacity() > 0 then
			inventory_zone.bubbles_explosion = AddBubblesExplosion(obj, play_field, 1, obj:GetZ() + 10, 0, 0, 1 / obj:GetScaleX(), 1 / obj:GetScaleY(), 1)
		end
		inventory_zone.locked = false
		inventory_zone.bubbles:Show()
		if play_field:HasObject(inventory_zone.bound_object) then
		    play_field:DoNotIgnoreOnTestPoint(inventory_zone.bound_object)
		end
		play_field:GetLevelScreen():GetTaskList():SetExtraTaskText(GetString(inventory_zone.task_text_alias))
	end
	
	inventory_zone.Lock = function()
	    if inventory_zone.IsLocked() then
	        return
	    end
		inventory_zone.locked = true
		inventory_zone.bubbles:Hide()
		if play_field:HasObject(inventory_zone.bound_object) then
		    play_field:IgnoreOnTestPoint(inventory_zone.bound_object)
		end
		play_field:GetLevelScreen():GetTaskList():SetExtraTaskText("")
	end
	
	inventory_zone.IsLocked = function()
		return inventory_zone.locked
	end
	
	inventory_zone.IsCompleted = function()
	    return inventory_zone.item_1_found and inventory_zone.item_2_found and inventory_zone.item_3_found
	end
	
	inventory_zone.NeedsTool = function(tool)
		if string.find(inventory_zone.tool_1_name, tool.tool_object_name) ~= nil or 
		   string.find(inventory_zone.tool_2_name, tool.tool_object_name) ~= nil or 
           string.find(inventory_zone.tool_3_name, tool.tool_object_name) ~= nil  then
		   return true
		end
		return false
	end
	
	inventory_zone.OnCompleteImmediate = function()
	end
	
	inventory_zone.CheckIfComplete = function(delay_for_complete_callback)
		if inventory_zone.items_to_use == 0 then
			local timer = play_field:GetTimer()--GetGameManager():GetTimer()
			local HideZone = function(zone, fade)
				zone.Hide(fade)
			end
			TimerDo(timer,
					delay_for_complete_callback,
					HideZone,
					inventory_zone,
					true)
			
			--HideInventory(inventory_zone, true)
			--play_field:IgnoreOnTestPoint(inventory_zone.bound_object)
			--game_state.custom_cursors[inventory_zone.bound_object] = ''
			inventory_zone.Lock()
			local pm = GetProfileManager();
            local currentProfile = pm:GetCurrentProfile();
		    currentProfile:AddStateChanger(inventory_zone.state_changer)
			OnSceneStateChanged(play_field);
			inventory_zone.OnCompleteImmediate()
			
			if inventory_zone.request_notebook_on_complete then
			    OnAllItemsFound()
			end
			
			TimerDo(timer,
					delay_for_complete_callback + ZOOM_ZONE_APPEARING_TIME,
					inventory_zone.DefaultOnComplete)        
		    return true
		end
		return false
	end
	
	inventory_zone.DefaultOnComplete = function()
	    inventory_zone.OnComplete();
	end
	
	inventory_zone.OnShown = function()
	end
	
	inventory_zone.OnHiddenImmediate = function()
	end
end