
local PANEL = {}
local SLIDE_SPEED = 0.1

/*---------------------------------------------------------
   Name: Init
---------------------------------------------------------*/
function PANEL:Init()

	self.Label = vgui.Create( "Label", self )
		self.Label:SetText( "Unnammed Group" )
		self.Label:SetMouseInputEnabled( false )
		
	self.Items = {}
	self.HeaderColor = NamedColor( "HighlightDull" )
	self.HeaderColor.r = self.HeaderColor.r * 0.8
	self.HeaderColor.g = self.HeaderColor.g * 0.8
	self.HeaderColor.b = self.HeaderColor.b * 0.8
	
	self.Closed = false
	self.MovementEnd = 0

end

/*---------------------------------------------------------
   Name: Setup
---------------------------------------------------------*/
function PANEL:Setup( toolpanel, iparent, name, label, command, controlpanel, ToolTree, full_table )

	self.Label:SetText( label )
	self.ToolTree = ToolTree

end

/*---------------------------------------------------------
   Name: AddItem
---------------------------------------------------------*/
function PANEL:AddItem( item )

	table.insert( self.Items, item )
	
	item:SetParent( self )
	item:PerformLayout()
	self:PerformLayout()

end

/*---------------------------------------------------------
   Name: Think
---------------------------------------------------------*/
function PANEL:Think()

	// Perform Layout every frame if we're sliding
	if ( self.MovementEnd ) then
		if ( self.MovementEnd < RealTime() ) then self.MovementEnd = nil end
		self:PerformLayout()
	end

end

/*---------------------------------------------------------
   Name: PerformLayout
---------------------------------------------------------*/
function PANEL:PerformLayout()

	self.Label:SetPos( 4, 2 )
	self.Label:SetSize( self:GetParent():GetWide(), 18 )
	self.Label:SetContentAlignment( 7 )
	
	local y = 19
	
	for k, pnl in ipairs( self.Items ) do
	
		pnl:SetPos( 1, y )
		pnl:SetSize( self:GetWide() - 2, pnl:GetTall() )
		pnl.Num = k
		y = y + pnl:GetTall()
	
	end
	
	y = y + 6
	
	
	local MoveDelta = 0
	
	// This is what achieves the sliding motion
	if ( self.MovementEnd ) then
		MoveDelta = math.Clamp( (self.MovementEnd - RealTime()) / SLIDE_SPEED, 0, 1 )
		MoveDelta = MoveDelta ^ 2
	end
	
	if (!self.Closed) then MoveDelta = 1 - MoveDelta end
	
	// Set the label colour and size depending on MoveDelta
	self.Label:SetFGColor( 255*MoveDelta, 255*MoveDelta, 255*MoveDelta, 255 )
	self:SetSize( self:GetParent():GetWide(), 18 + (y-18) * MoveDelta )

end

/*---------------------------------------------------------
   Name: OnMousePressed
---------------------------------------------------------*/
function PANEL:OnMousePressed( mcode )

	self:RequestFocus()

	if ( mcode == MOUSE_LEFT ) then
	
		// Expand Contract
		self:ToggleClosed()
		surface.PlaySound( "common/bugreporter_succeeded.wav" )
		
		return
	end
	
	// Right mouse button means DRAG
	// This is a kind of lousy way to do this.. but it works.
	if ( mcode == MOUSE_RIGHT ) then
	
		local Parent = self
		while ( true ) do
		
			Parent = Parent:GetParent()
			if (!Parent) then return end
			
			if ( Parent.ClassName == "PanelList" && Parent.VBar ) then
				Parent.VBar:Grip()
				return
			end
		
		end
	end
	
end

/*---------------------------------------------------------
   Name: ToggleClosed
---------------------------------------------------------*/
function PANEL:ToggleClosed()

	self.Closed = !self.Closed
	
	self.MovementEnd = RealTime() + SLIDE_SPEED
	self.ToolTree.MovementEnd = RealTime() + SLIDE_SPEED + 0.5
	
	self:PerformLayout()

end

/*---------------------------------------------------------
   Name: ApplySchemeSettings
---------------------------------------------------------*/
function PANEL:ApplySchemeSettings()

	self.Label:SetFGColor( 255, 255, 255, 255 )
	self.Label:SetFont( "DefaultBold" )

end

local texGradient 	= surface.GetTextureID( "gui/gradient" )

/*---------------------------------------------------------
   Name: Paint
---------------------------------------------------------*/
function PANEL:Paint()
	
	draw.RoundedBox( 4, 0, 0, self:GetWide(), self:GetTall(), Color( 255, 255, 255, 80 ) )	
	return true
	
end

vgui.Register( "ToolTree_Group", PANEL, "Panel" )