// Generated by CoffeeScript 1.3.3
var __hasProp = {}.hasOwnProperty,
  __extends = function(child, parent) { for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); child.__super__ = parent.prototype; return child; };

define(['environ', 'mediator', 'underscore', 'backbone'], function(environ, mediator, _, Backbone) {
  var Preferences;
  Preferences = (function(_super) {

    __extends(Preferences, _super);

    function Preferences() {
      return Preferences.__super__.constructor.apply(this, arguments);
    }

    Preferences.prototype.url = App.urls.preferences;

    Preferences.prototype.defaults = {
      json: {}
    };

    Preferences.prototype.initialize = function() {
      var _this = this;
      Preferences.__super__.initialize.apply(this, arguments);
      mediator.subscribe('session/changed', this.when(function(key, data) {
        var json, session;
        json = _this.get('json');
        if (!(session = json.session)) {
          session = json.session = {};
        }
        session[key] = data;
        return _this.save();
      }));
      this.on('sync', function() {
        return mediator.publish('session/synced');
      });
    };

    Preferences.prototype.load = function() {
      var data, json, key, _ref;
      json = this.get('json');
      _ref = json.session || {};
      for (key in _ref) {
        data = _ref[key];
        mediator.publish("session/load/" + key, data);
      }
    };

    return Preferences;

  })(Backbone.Model);
  App.preferences = new Preferences;
  return App.preferences.fetch();
});
