-- EVENTS --

local ResizeEvent = ParentContainer.Attribs.ResizeEvent

local RowCt = 5
local CellSize = 0.25
local Padding = 6
local ContainerPadding_Bottom = 32

function getNewCellSize()
	 CellSize = (Container.AbsoluteSize.X / RowCt) - Padding
end

function updateLayout()
	--[[
	if #Container.Cells:GetChildren() < 1 then
		Container.Parent.HeaderFrame.Visible = false
	else
		Container.Parent.HeaderFrame.Visible = true
	end
	]]
	getNewCellSize()

	local cells = Container.Cells:GetChildren()
	
	for i, cell in pairs(cells) do
		local index = i - 1
		local posX = ((index % RowCt) * (CellSize + Padding)) + (Padding / 2)
		local posY = (math.floor(index / RowCt) * (CellSize + Padding)) + (Padding / 2)

		cell.Size = UDim2.fromOffset(CellSize, CellSize)
		cell.Position = UDim2.fromOffset(posX, posY)
	end

	local rows = math.ceil((#cells / RowCt))

	local newSize = UDim2.new(1, -24, 0, (rows * CellSize + Padding) + ContainerPadding_Bottom)
	Container.Size = newSize

	ResizeEvent:Fire(ParentContainer.Name)
end


Container.Cells.ChildAdded:Connect(updateLayout)
Container.Cells.ChildRemoved:Connect(updateLayout)
Container:GetPropertyChangedSignal("AbsoluteSize"):Connect(updateLayout)
