-- Draws a menu.
-- Inputs:
-- x, y: Position of top left
-- Items: Table of items
-- Title: Menu title
-- Key: If values of Items are tables, specifies the key of those tables to
--  print.
-- Notes: Sets global SelectedItem to the key of the selected item.
function DrawMenu(x, y, Items, key)
	local i, line = 0, 0
	for a, d in pairs(Items) do
		if i >= Skip and i < (Skip + NumLines) then
			BitmapFont_DrawString(FrameBuf, Font, x, y, FONT_DRAW_NORMAL,
				nil, key and d[key] or d)
			if i == Cursor then
				SelectedItem = a
				BitmapFont_DrawString(FrameBuf, Font, x - 8, y,
					FONT_DRAW_NORMAL, nil, ">")
			end
			y = y + LineHeight
			if y >= MenuYMax then break end
			
			line = line + 1
			if line >= NumLines then break end
		end
		i = i + 1
	end
end


-- Read current directory.
function ReadCurDir()
	Files, e = ReadDirectory(CurDir, true)
	if not Files then Files = {"Error reading directory:", e} end
	table.sort(Files)
	MenuNumOpts[FileMenuID] = TableSize(Files)
	MenuTable[FileMenuID] = Files
end


-- Change to parent directory in file select.
function PrevDir()
	local Idx = FindLast(CurDir, "/")
	if Idx == #CurDir then
		Idx = FindLast(string.sub(CurDir, 1, #CurDir - 1), "/")
	end
	
	if Idx then
		SoundEffect_Replay(BackSound)
		CurDir = string.sub(CurDir, 1, Idx - 1)
		print(CurDir)
		if(string.sub(CurDir, -1) == ":") then CurDir = CurDir .. "/" end
		ReadCurDir()
	end
end


-- Open selected file in file select.
function OpenThisFile()
	if not Files[SelectedItem] then return end
	SoundEffect_Replay(ActivateSound)
	
	-- The kernel will puke with error 80010016 if there are any "//" in
	-- the path of a program we try to execute.
	File = string.gsub(CurDir .. Files[SelectedItem], "//+", "/")
	print("File: " .. File)
	
	-- If this is a directory, switch to it.
	if(string.sub(Files[SelectedItem], 1, 1) == '/') then
		CurDir = CurDir .. Files[SelectedItem] .. "/"
		Cursor = 0
		SelectedItem = 1
		ReadCurDir()
		return
	end
	
	
	Ext = string.lower(string.sub(File, -4))
	if Ext == ".pbp" or Ext == ".prx" then -- Program; launch it
		e = LaunchProgram(File)
		if not e == true then
			ShowError("Error " .. string.format("%X", e) .. " launching program")
		end
	
	elseif Ext == ".mp3" then -- Music; play it
		if Music then MP3_Stop(Music) end
		Music = nil -- delete so the MP3 player is shut down properly
		Music, e = LoadMP3File(File)
		if Music then
			MP3_Loop(Music, -1) --loop forever
			MP3_Play(Music)
		else ShowError(e)
		end
	end
end


-- Run selected program in app select.
function RunThisApp()
	SoundEffect_Replay(ActivateSound)
	e = LaunchProgram(AppBase .. Apps[Cursor + 1].dir .. AppExt)
	if not e == true then
		ShowError("Error " .. string.format("%X", e) ..
			" launching program " .. tostring(Apps[Cursor + 1].dir))
	end
end


-- Run selected program in built-in app select.
function RunThisBuiltinApp()
	SoundEffect_Replay(ActivateSound)
	Result, e = RunBuiltinApp(BuiltinApps[Cursor + 1].id)
	if not Result == true then
		ShowError(e)
	end
end
