
local table 	= table
local ipairs 	= ipairs
local debug 	= debug
local PrintTable = PrintTable

local spawnmenu_engine = spawnmenu

/*---------------------------------------------------------

---------------------------------------------------------*/

module("spawnmenu")


local g_ToolMenu = {}


function GetToolMenu( name )
	return g_ToolMenu[ name ]
end


function ClearToolMenus()
	g_ToolMenu = {}	
end

function AddToolTab( strName, strLabel )

	// Make a space on the toolmenu so we're all ready for action
	g_ToolMenu[ strName ] = g_ToolMenu[ strName ] or {}

	// Add the tab via the engine (todo: move to Lua)
	spawnmenu_engine.AddTab( strName, strLabel )

end


/*---------------------------------------------------------
  
---------------------------------------------------------*/
function AddToolCategory( tab, RealName, PrintName )
	
	table.insert( g_ToolMenu[ tab ], { Text = PrintName, ItemName = RealName } )

end

/*---------------------------------------------------------
  
---------------------------------------------------------*/
function AddToolMenuOption( tab, category, itemname, text, command, controls, cpanelfunction, TheTable )
	
	local Menu = g_ToolMenu[ tab ]
	local CategoryTable = nil
	
	for k, v in ipairs( Menu ) do
		if ( v.ItemName && v.ItemName == category ) then CategoryTable = v break end
	end
	
	// No table found.. lets create one
	if ( !CategoryTable ) then
		CategoryTable = { Text = "#"..category, ItemName = category }
		table.insert( Menu, CategoryTable )
	end
	
	TheTable = TheTable or {}
	
	TheTable.ItemName = itemname
	TheTable.Text = text
	TheTable.Command = command
	TheTable.Controls = controls
	TheTable.CPanelFunction = cpanelfunction
	
	table.insert( CategoryTable, TheTable )
	
	// Keep the table sorted
	table.SortByMember( CategoryTable, "Text", true )

end
