(function (context, $) {

  "use strict";

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

  context.JK.ClientPreferencesDialog = function (app) {
    var logger = context.JK.logger;
    var rest = context.JK.Rest();
    var dialogId = '#client-preferences-dialog';
    var $dialog = null;
    var $autoStartField = null;
    var $autoStartInput = null;
    var $btnSave = null;

    function events() {
      context.JK.checkbox($autoStartInput);

      $btnSave.click(function() {

        var autostart = $autoStartField.find('.icheckbox_minimal').is('.checked');
        context.jamClient.SetAutoStart(autostart);
        app.layout.closeDialog('client-preferences-dialog')
        context.jamClient.SaveSettings();
        return false;
      })
    }

    function beforeShow() {
      var autostart = context.jamClient.GetAutoStart();
      autostart ? $autoStartInput.iCheck('check') : $autoStartInput.iCheck('uncheck');
    }

    function afterHide() {

    }

    function initialize() {

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

      app.bindDialog('client-preferences-dialog', dialogBindings);

      $dialog = $(dialogId);
      $autoStartField = $dialog.find('.field[data-purpose="autostart"]')
      $autoStartInput = $dialog.find('input[name="autostart"]')
      $btnSave = $dialog.find('.btnSave')

      events();
    }

    this.initialize = initialize;

  }
})(window, jQuery);
