
function filesort(a, b)
  if case_sorting == 0 then
    a_name = string.lower(a.name)
    b_name = string.lower(b.name)
  else
    a_name = a.name
    b_name = b.name
  end
  if a.directory == b.directory then
    return a_name < b_name
  else
    return a.directory
  end
end

function mp_nextfile()
  if menu_pointer + 1 <= num_files then
    menu_pointer = menu_pointer + 1
  else
    menu_pointer = 1
    start_pointer = 1
  end
  if (num_files > num_listings) and (menu_pointer == (start_pointer + num_listings)) then
    start_pointer = start_pointer + 1
  end
end

function mp_prevfile()
  if (num_files > num_listings) and (menu_pointer == start_pointer) then
    start_pointer = start_pointer - 1
  end
  if menu_pointer - 1 >= 1 then
    menu_pointer = menu_pointer - 1
  else
    menu_pointer = num_files
    if (num_files > num_listings) then
      start_pointer = ((num_files - num_listings) + 1)
    else
      start_pointer = 1
    end      
  end
end

function test_filetype(filename, extension)
--  if string.lower(string.sub(filename, -3)) == extension then return true end
  local s,v,r = string.find(filename, "\.(%w*)$")
  if string.lower(r) == extension then return true end
end

function update_files()

  -- get current working directory
  currentdir = System.currentDirectory()

  -- get directory listing
  files = System.listDirectory()
  table.sort(files, filesort)
  num_files = table.getn(files)

  -- add actions
  for i,file in files do
    if file.directory then
      directory = 1
    else
      directory = 0
    end
    file.action_cross = "file_action(\"" .. file.name .. "\", " .. directory .. ", \"execute\")"
    file.action_square = "file_action(\"" .. file.name .. "\", " .. directory .. ", \"operation\")"
    file.action_triangle = "file_select(\"" .. currentdir .. "\", \"" .. file.name .. "\", \"toggle\")"
  end

  -- update menu pointer
  if menu_pointer >= (num_files + 1) then
    menu_pointer = 1
  end

  -- reset start pointer
  start_pointer = 1

  -- reset selected files array
  selected_files = {}

end

function is_dir(path, name)
  local tmpfiles = System.listDirectory(path)
  local found_needle = false
  for k,v in pairs(tmpfiles) do
    if v.name == name and v.directory then found_needle = true end
  end
  return found_needle
end

function get_filename(path_name)
  return string.gsub(path_name, ".*/(.*[^\/])", "%1")
end

function get_filepath(path_name)
  return string.gsub(path_name, "^(.+)/.*$", "%1")
end

function scan_dir(path, name)

  -- set path
  local path_name = remove_trailing_slash(path) .. "/" .. name

  -- add dir to scan array
  table.insert(scan_dirs, path_name)

  -- get listing
  tmplist = System.listDirectory(path_name)
  table.sort(tmplist, filesort)
  scan_tmp[path_name] = tmplist

  -- loop listing
  for k,v in pairs(scan_tmp[path_name]) do

    -- skip ./..
    if v.name ~= "." and v.name ~= ".." then

      -- is dir
      if is_dir(path_name, v.name) then

	-- scan dir
	scan_dir(path_name, v.name)

      -- is file
      else

        -- add file to scan array
	table.insert(scan_files, path_name .. "/" .. v.name)

      end

    end

  end

end
