var cargo_ships_keyboard_navigation_loop = function (elem) {
  var cargo_ships_tabbable = elem.find('select, input, textarea, button, a').filter(':visible');
  var cargo_ships_firstTabbable = cargo_ships_tabbable.first();
  var cargo_ships_lastTabbable = cargo_ships_tabbable.last();
  cargo_ships_firstTabbable.focus();

  cargo_ships_lastTabbable.on('keydown', function (e) {
    if ((e.which === 9 && !e.shiftKey)) {
      e.preventDefault();
      cargo_ships_firstTabbable.focus();
    }
  });

  cargo_ships_firstTabbable.on('keydown', function (e) {
    if ((e.which === 9 && e.shiftKey)) {
      e.preventDefault();
      cargo_ships_lastTabbable.focus();
    }
  });

  elem.on('keyup', function (e) {
    if (e.keyCode === 27) {
      elem.hide();
    };
  });
};