--[[
Simple hook framework

StartupHook
SetupHook
OpenWindowHook
LoadHook
CloseWindowHook
CloseGroupHook
QuitHook
ExitHook
CloseTabHook
CreateTabHook
SwitchTabHook
ActivateWindowHook

]]

hook = {}

function hook.callhook(hook, ...)
	local i, j
	
	if hook then
		i, j  = next(hook, nil)
		while i do
			if type(j) == "function" then
				j(...)
			end
			i, j = next(hook, i)
		end
	end
end

function hook.add(h, f)
	if h and type(h) == "table" and f and type(f) == "function" then
		table.insert(h, f)
	end
end

function hook.remove(h, f)
	local k, v
	if h and type(h) == "table" and f and type(f) == "function" then
		for k, v in ipairs(h) do
			if v == f then
				table.remove(h, k)
				return
			end
		end
	end
end

StartupHook = {}
function OnStartup()
	hook.callhook(StartupHook)
	StartupHook = nil -- release memory
end

SetupHook = {}
function OnSetup()
	hook.callhook(SetupHook)
	SetupHook = nil -- release memory
end

OpenWindowHook = {}
function OnOpenWindow()
	hook.callhook(OpenWindowHook)
end

LoadHook = {}
function OnLoad()
	hook.callhook(LoadHook)
end

CloseWindowHook = {}
function OnCloseWindow()
	hook.callhook(CloseWindowHook)
end

CloseGroupHook = {}
function OnCloseGroup(window)
	hook.callhook(CloseGroupHook, window)
end

QuitHook = {}
function OnQuit()
	hook.callhook(QuitHook)
end 

ExitHook = {}
function OnExit()
	hook.callhook(ExitHook)
end

if KMEL_PLUGIN_VER and KMEL_PLUGIN_VER >= 514 then
	CloseTabHook = {}
	function OnCloseTab()
		hook.callhook(CloseTabHook)
	end
	
	CreateTabHook = {}
	function OnCreateTab()
		hook.callhook(CreateTabHook)
	end
	
--[[	SwitchTabHook = {}
	function OnSwitchTab()
		hook.callhook(SwitchTabHook)
	end
	
	ActivateWindowHook = {}
	function OnActivateWindow()
		hook.callhook(ActivateWindowHook)
	end --]]
end
