(function(context,$) {

  "use strict";
  context.JK = context.JK || {};
  context.JK.SessionCancelDialog = function(app, sessionId) {
    var logger = context.JK.logger;
    var rest = context.JK.Rest();
    var $screen = null;
    var dialogId = 'session-cancel-dialog';
    var $btnCancel = null;

    function beforeShow(data) {
      console.log(sessionId);
    }

    function afterShow(data) {
    }

    function events() {
      $btnCancel.unbind('click');
      $btnCancel.click(function(e) {
        e.preventDefault();

        var error = false;
        rest.cancelSession({session_id: sessionId})
          .done(function(response) {
            if (!error) {
              app.layout.closeDialog(dialogId);
              $btnCancel.trigger("sessionCancelEvent");
            }
          })
          .fail(app.ajaxError);
      });
    }

    function initialize() {

      var dialogBindings = {
        'beforeShow' : beforeShow,
        'afterShow' : afterShow
      };

      app.bindDialog(dialogId, dialogBindings);

      $screen = $('[layout-id="' + dialogId + '"]');
      $btnCancel = $screen.find("#btnCancelSession");

      events();
    }

    this.initialize = initialize;
  };
})(window,jQuery);