(function (context, $) {

  "use strict";

  context.JK = context.JK || {};

  context.JK.ShutdownDialog = function (app) {
    var logger = context.JK.logger;

    function show() {
      context.JK.Banner.showAlert(
        { title: "Close JamKazam Application",
          buttons: [
            {name: 'COMPLETELY SHUT DOWN THE APP', click: function() {
              logger.debug("'COMPLETELY SHUT DOWN THE APP' selected")
              context.jamClient.ShutdownApplication()
            }},
            {name: 'LET APP RUN IN BACKGROUND', click: function() {
              logger.debug("'LET APP RUN IN BACKGROUND' selected")
              context.jamClient.RegisterQuitCallback("window.JK.ShutdownDialogCallback");
              context.jamClient.LeaveSessionAndMinimize();
              context.JK.Banner.hide();
            }
        }
          ],
          html: $('#template-shutdown-prompt').html()});
    }

    function initialize() {
      // guard against old clients
      if(context.jamClient.RegisterQuitCallback) {
        context.jamClient.RegisterQuitCallback("window.JK.ShutdownDialogCallback");
      }
    }

    function quitCallback(options) {
      logger.debug("shutdown event");
      show();
    }

    this.initialize = initialize;

    context.JK.ShutdownDialogCallback = quitCallback;

    return this;

  }
})(window, jQuery);