
function recordingContextMenu(r)
{
  var menu = new Menu(r.fullTitle);
  menu.createMenuItem(gSBRec.GetStringFromName("startVideo.item"), r, startVideo);
  if (r.seen)
    menu.createMenuItem(gSBRec.GetStringFromName("unmarkSeen.item"), r, recordingUnMarkSeen);
  else
    menu.createMenuItem(gSBRec.GetStringFromName("markSeen.item"), r, recordingMarkSeen);
  menu.createMenuItem(gSBRec.GetStringFromName("delete.item"), r, recordingDeleteAreYouSure);
  if (r.keep)
    menu.createMenuItem(gSBRec.GetStringFromName("unmarkKeep.item"), r, recordingUnMarkKeep);
  else
    menu.createMenuItem(gSBRec.GetStringFromName("markKeep.item"), r, recordingMarkKeep);
  menu.load();

  try {
    askServer({
      details: "true",
      filter: "id-eq-" + r.id
    }, function(recordings) {
      assert(recordings.length == 1, "expected exactly one recording with ID " + r.id);
      r = recordings[0];
      menu.createMenuItem("---");
      menu.createMenuItem(gSBRec.GetStringFromName("genre.label") + ": " + r.genre +
                  "  " + gSBRec.GetStringFromName("firstshownyear.label") + ": " + r.firstshown);
      if (r.description)
      {
        menu.createMenuItem(gSBRec.GetStringFromName("description.label") + ":");
        menu.createMenuItem(r.description);
      }
    });
  } catch (e) { errorCritical(e); }
}

function recordingDeleteAreYouSure(r)
{
  if (r.keep)
  {
    errorCritical(gSBRec.GetStringFromName("deleteNever.error"));
    return;
  }

  var menu = new Menu(gSBRec.formatStringFromName("delete.title", [ r.fullTitle ], 1));
  menu.createMenuItem(gSBRec.GetStringFromName("deleteNo.item"), null, actionBack);
  menu.createMenuItem(gSBRec.formatStringFromName("deleteYes.item", [ r.fullTitle ], 1), r, recordingDelete);
  menu.createMenuItem(gSBRec.GetStringFromName("deleteBroken.item"), r, recordingBroken);
  menu.load();
}

function recordingDelete(r)
{
  r.deleteVideo(function()
  {
    // HACK: remove recording from menu list
    var menu = gMenuStack[gMenuStack.length - 3]; // menu with list of recordings (2 before current)
    ArrayRemove(menu.items, r, false);
    actionBack();
    actionBack();
  },
  errorCritical);
}

function recordingBroken(r)
{
  r.deleteVideo(function()
  {
    r.markBroken(true, function()
    {
      // HACK: remove recording from menu list
      var menu = gMenuStack[gMenuStack.length - 3]; // menu with list of recordings (2 before current)
      ArrayRemove(menu.items, r, false);
      actionBack();
      actionBack();
    },
    errorCritical);
  },
  errorCritical);
}

function recordingMarkSeen(r)
{
  r.markSeen(true, function()
  {
    actionBack();
    recordingContextMenu(r);
  },
  errorCritical);
}

function recordingUnMarkSeen(r)
{
  r.markSeen(false, function()
  {
    actionBack();
    recordingContextMenu(r);
  },
  errorCritical);
}

function recordingMarkKeep(r)
{
  r.markKeep(true, function()
  {
    actionBack();
    recordingContextMenu(r);
  },
  errorCritical);
}

function recordingUnMarkKeep(r)
{
  r.markKeep(false, function()
  {
    actionBack();
    recordingContextMenu(r);
  },
  errorCritical);
}
