/*   _                                
    ( )                               
   _| |   __   _ __   ___ ___     _ _ 
 /'_` | /'__`\( '__)/' _ ` _ `\ /'_` )
( (_| |(  ___/| |   | ( ) ( ) |( (_| |
`\__,_)`\____)(_)   (_) (_) (_)`\__,_) 

*/

g_OpenMenus = {}

function DermaMenu( parent )

	if (!parentmenu) then CloseDermaMenus() end
	
	local menu = vgui.Create( "DMenu" )
	return menu

end


function CloseDermaMenus()

	for k, menu in pairs( g_OpenMenus ) do
	
		menu:Remove()
	
	end	
	
	g_OpenMenus = {}

end

//
// Here we detect when the mouse is pressed on another panel
// This allows us to close the menus.
//
local function DermaDetectMenuFocus( panel, mousecode )

	// Clicking onto a menu shouldn't close all menus..
	if ( panel && panel.ClassName && panel.ClassName == "DMenu" ) then return end
	
	// Clicking onto a child of a menu shouldn't close all menus..
	local Parent = panel:GetParent()
	if ( Parent && Parent.ClassName && Parent.ClassName == "DMenu" ) then return end

	CloseDermaMenus()

end



hook.Add( "VGUIMousePressed", "DermaDetectMenuFocus", DermaDetectMenuFocus )
