// 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', 'channels', 'underscore', 'backbone'], function(environ, mediator, channels, _, Backbone) {
  var Session, session;
  Session = (function(_super) {

    __extends(Session, _super);

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

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

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

    Session.prototype.initialize = function() {
      var _this = this;
      Session.__super__.initialize.apply(this, arguments);
      mediator.subscribe(channels.SESSION_SET, 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(channels.SESSION_SYNCED);
      });
      this.on('change', function() {
        return this.load();
      });
      return this.fetch();
    };

    Session.prototype.load = function() {
      var channel, data, json, key, _ref;
      json = this.get('json');
      _ref = json.session || {};
      for (key in _ref) {
        data = _ref[key];
        channel = _.template(channels.SESSION_LOAD, {
          key: key
        });
        mediator.publish(channel, data);
      }
    };

    return Session;

  })(Backbone.Model);
  session = new Session;
});
