(function(context,$) {

  "use strict";
  context.JK = context.JK || {};
  context.JK.RecordingSelectorDialog = function(app, selectedRecordings, selectCallback) {
    var logger = context.JK.logger;
    var rest = context.JK.Rest();
    var recordingUtils = context.JK.RecordingUtils;
    var $dialog = null;
    var dialogId = 'recording-selector-dialog';
    var $screen = $('#' + dialogId);
    var $btnSelect = $screen.find(".btn-select-recordings");
    var $recordings = $screen.find('.recordings');
    var $paginatorHolder = null;
    var feedHelper = new context.JK.Feed(app);
    var $scroller = $recordings;
    var $content = $recordings;
    var $recordsHolder = $screen.find('.recordings-content');
    var $noMoreFeeds = $screen.find('.end-of-list');
    var $empty = $();
    feedHelper.initialize($screen, $scroller, $recordsHolder, $noMoreFeeds, $empty, $empty, $empty, $empty, {sort: 'date', time_range: 'all', type: 'recording', show_checkbox: true, hide_avatar: true});

    function beforeShow(data) {

    }

    function afterShow(data) {

      feedHelper.setUser(context.JK.currentUserId)
      feedHelper.refresh()

      // hide the avatars
      //$screen.find('.avatar-small.ib').hide();
    }

    function afterHide() {
    }

    function showDialog() {
      return app.layout.showDialog(dialogId);
    }

    function events() {
      $btnSelect.off('click').on('click', function(evt) {
        evt.preventDefault();
        var preSelectedRecordings = [];
        $recordings.find('.select-box input[type=checkbox]:checked').each(function(index) {
          preSelectedRecordings.push({
            "id": $(this).attr('data-recording-id'),
            "name": $(this).attr('data-recording-title')
          });
        });

        if (selectCallback) {
          console.log("calling selectCallback", preSelectedRecordings)
          selectCallback(preSelectedRecordings);
        }

        app.layout.closeDialog(dialogId);

        return false;

      });
    }

    function initialize() {
      var dialogBindings = {
        'beforeShow' : beforeShow,
        'afterShow' : afterShow,
        'afterHide': afterHide
      };

      app.bindDialog(dialogId, dialogBindings);

      events();
    }

    this.initialize = initialize;
    this.showDialog = showDialog;
  }

  return this;
})(window,jQuery);