(function (context, $) {

  "use strict";
  context.JK = context.JK || {};
  context.JK.ChangeSearchLocationDialog = function (app) {
    var logger = context.JK.logger;
    var rest = context.JK.Rest();
    var $dialog = null;
    var initialized = false;
    var $countries = null;
    var $regions = null;
    var $cities = null;
    var selectLocation = null;
    var $resetLocation = null;
    var $btnSave = null;

    function getSelection() {
      var countryVal = $countries.val();
      var regionVal = $regions.val();
      var cityVal = $cities.val();

      if(countryVal && regionVal && cityVal) {
        // if any bit of info is not set, then null out info; user didn't pick a full set of data
        var $country = $($countries).find('option:selected');
        var $region = $($regions).find('option:selected');

        return {
          countrycode : countryVal,
          regioncode : regionVal,
          city: cityVal,
          country: $country.text(),
          region: $region.text()
        }
      }
      else
      {
        return null;
      }
    }

    function events() {
      $btnSave.click(function() {

        var selection = getSelection();

        app.user().done(function(user) {
          if(selection != null &&
            selection.countrycode == user.country &&
            selection.regioncode == user.state &&
            selection.city == user.city) {
            // if this is the same location on the user's account, suppress selection
            selection = null;
          }

          $dialog.data('result', selection);
          app.layout.closeDialog('change-search-location')
        })

        return false;
      })

      $resetLocation.click(function() {
        app.user().done(function(user) {
          selectLocation.load(user.country, user.state, user.city);
        })
        return false;
      })
    }

    function beforeShow() {
      $dialog.data('result', null);

      if(!initialized) {
        initialized = true;
        app.user().done(function(user) {
          selectLocation = new context.JK.SelectLocation($countries, $regions, $cities, app);
          selectLocation.load(user.country, user.state, user.city);
        })
      }
      else {

      }
    }

    function beforeHide() {

    }

    function getSelectedLocation() {
      return searchLocation;
    }

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

      app.bindDialog('change-search-location', dialogBindings);

      $dialog = $('#change-search-location-dialog');
      $countries = $dialog.find('select[name="country"]')
      $regions = $dialog.find('select[name="region"]')
      $cities = $dialog.find('select[name="city"]')
      $resetLocation = $dialog.find('.reset-location');
      $btnSave = $dialog.find('.btnSave')

      events();
    };

    this.initialize = initialize;
    this.getSelectedLocation = getSelectedLocation;
  }

  return this;
})(window, jQuery);