-- inventory

inventory_ = {}
inventory_.elems = {}
inventory_.first_index = 1
inventory_.pos0 = nil
inventory_.slots_count = 3

inventory_.elem_size = { width = 68, height = 68}

----------------------------------------------------------------------------------------------------
function add_element_to_inventory(name)
	local elem = level_mgr:find_element(name)
	
	if elem ~= nil then
		elem:setActive(false)
		inventory_:add_elem(elem, false)
		el:setActive(true)
	end
end

----------------------------------------------------------------------------------------------------
function inventory_:is_elem_exists(name)

	for i = 1, #self.elems do
		if self.elems[i].element_name == name then
			return true
		end
	end
	
	return false		
end

----------------------------------------------------------------------------------------------------
function inventory_:scroll(val) -- <0 - left, >0 - right

	if (inventory_.first_index == 1 and val < 0) or (inventory_.first_index == #inventory_.elems and val > 0) then
		return
	end
	
	inventory_.first_index = inventory_.first_index + val
	inventory_:update()
	
end

----------------------------------------------------------------------------------------------------
function inventory_:update()

	for i = 1, #self.elems do
	
		if i < self.first_index or i > self.slots_count + self.first_index - 1 then
			self.elems[i].elem_obj:setVisible(false)
		else
			self.elems[i].elem_obj:setVisible(true)
			
			local posNew = Vec2(self.pos0.x + (i - self.first_index) * inventory_.elem_size.width, self.pos0.y)
			self.elems[i].elem_obj:setPos(posNew)		
		end
		
	end

end

----------------------------------------------------------------------------------------------------
function inventory_:init()
	if Panel ~= nil then
		--self.pos0 = Panel.parent:getElement("mp_elems_anchor"):getPos(false)
	end
end

----------------------------------------------------------------------------------------------------
function inventory_:resize_elem(elem)
	local size = elem:getSize()
	elem:setScale(math.min(inventory_.elem_size.width / size.x, inventory_.elem_size.height / size.y))
end

----------------------------------------------------------------------------------------------------
function inventory_:get_element(name)

	for i = 1, #inventory_.elems do
		if inventory_.elems[i].element_name == name then
			return inventory_.elems[i].elem_obj
		end
	end
	
	return nil
	
end

----------------------------------------------------------------------------------------------------
function inventory_:element_click(name)

	local elem = inventory_:get_element(name)
	
	if elem ~= nil then
		mouse:setConnCharacter(elem:getCharacter())
		play_clip(elem:getCharacter())
		
		--main_panel:set_element_eh("mainpanel",
		--function()
		--	mouse:setConnCharacter(nil)
		--	main_panel:get_element("mainpanel"):setActive(false)
		--end)
	end

end

----------------------------------------------------------------------------------------------------
function inventory_:add_elem(elem)
	
	local item_index = #inventory_.elems + 1;

	local invname = elem:getName()
	
	table.insert(inventory_.elems, item_index, {name = invname})
	
	inventory_.elems[item_index].element_name = elem:getName()
	inventory_.elems[item_index].elem_obj = elem
	
	for i = 0, eiUpdate do
		elem:setEventHandler(i, "")
	end
	
	elem:setEventHandler(eiLeftClick, "inventory_:element_click('"..invname.."')")
	--elem:setParent(main_panel:get_parent():getElement("mainpanel"):getCharacter())
	
	elem:setActive(true)
	play_clip(elem:getCharacter())
	
	inventory_:update()
	
end