(function (context, $) {

  "use strict";

  context.JK = context.JK || {};
  context.JK.NetworkTestDialog = function (app) {
    var logger = context.JK.logger;

    var $dialog = null;
    var $btnClose = null;
    var $btnHelp = null;
    var networkTest = new context.JK.NetworkTest(app);

    function networkTestDone() {
      unfreezeInteractions();
    }

    function networkTestFail() {
      unfreezeInteractions();
    }

    function networkTestCancel() {
      unfreezeInteractions();
    }

    function networkTestStart() {
      freezeInteractions();
    }


    function freezeInteractions() {
      $btnClose.removeClass('button-orange').addClass('button-grey');
    }

    function unfreezeInteractions() {
      $btnClose.removeClass('button-grey').addClass('button-orange');
    }

    function onCancel() {
      // should we stop this if the test is going?
    }
    function events() {
      $btnClose.on('click', function(e) {
        if(!networkTest.isScoring()) {
          app.layout.closeDialog('network-test');
        }
        return false;
      })

     /** $btnHelp.on('click', function(e) {
        context.JK.Banner.showAlert('No help is yet available for the network test.');
        return false;
      })*/
    }

    function beforeShow() {
      networkTest.haltScoring();
      if(!networkTest.isScoring()) {
        networkTest.reset();
      }
    }

    function afterHide() {
      networkTest.resumeScoring();
    }

    function initialize() {

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

      app.bindDialog('network-test', dialogBindings);

      $dialog = $('#network-test-dialog');
      $btnHelp = $dialog.find('.btn-help');
      $btnClose = $dialog.find('.btn-close');

      networkTest.initialize($dialog, false);
      $(networkTest)
        .on(networkTest.NETWORK_TEST_DONE, networkTestDone)
        .on(networkTest.NETWORK_TEST_FAIL, networkTestFail)
        .on(networkTest.NETWORK_TEST_CANCEL, networkTestCancel)
        .on(networkTest.NETWORK_TEST_START, networkTestStart)

      events();
    }

    this.initialize = initialize;

    return this;
  };

})(window, jQuery);