var chat = chat || {};

chat.User = Backbone.Model.extend({

});

chat.firebaseRef = new Firebase(firebaseRootUrl + '/people');

chat.UserList = Backbone.Firebase.Collection.extend({
  model: chat.User,

  firebase: chat.firebaseRef // pass the ref here instead of string so we can listen for disconnect.
});

chat.presenceList = new chat.UserList();

chat.UserView = Backbone.View.extend({
  tagName: 'li',

  template: _.template($('#user-presence-template').html()),

  initialize: function() {
    this.listenTo(this.model, 'change', this.render);
    this.listenTo(this.model, 'remove', this.remove);
  },

  render: function() {
    console.log("rendering user with icon at ", this.model.get('iconSrc'));
    var data = _.extend({
      'name': this.model.get('vanityName'),
      'iconSrc': this.model.get('iconSrc')
    });
    this.$el.html(this.template(data));
    this.$el.show();
    return this;
  }
});


chat.UserListView = Backbone.View.extend({
  el: '#user-list',

  initialize: function() {
    this.presenceStats = $('#presence-stats');

    this.listenTo(chat.presenceList, 'change', this.onListChanged);
    this.listenTo(chat.presenceList, 'all', this.render);

    //probably should render the presenceList on init so we have a starting point too.
    console.log("render all the users here?");
    this.redraw(chat.presenceList, {});
  },

  drawUser: function(model, collection, options) {
    if (model.get('presenceStatus') != 'offline') {
      var view = new chat.UserView({ model: model });
      this.$el.append(view.render().el);
    }
  },

  redraw: function(collection, options) {
    this.$el.empty();
    collection.each(this.drawUser, this);
  },

  onListChanged: function(model, options) {
    console.log("list changed with model: ", model);
    // this is inefficient, but we don't have another hook to the dom element.
    this.redraw(chat.presenceList, {});
  }

});


var connectedRef = new Firebase(firebaseRootUrl + '/info/connected');

R.ready(function() {
  var userName = R.currentUser.get('vanityName');
  var userIcon = R.currentUser.get('icon');
  var userKey = R.currentUser.get('key');

  var userListView = new chat.UserListView();


  // add current user to chat list, if they're not already
  //
  var exists = chat.presenceList.any(function(user) {
    return user.get('id') == userKey;
  });
  if (!exists) {
    chat.presenceList.add({
      id: userKey,
      vanityName: userName,
      iconSrc: userIcon,
      presenceStatus: 'offline'
    });
  }

  connectedRef.on('value', function(isOnline) {
    var user = chat.presenceList.find(function(user) {
      return user.get('id') == userKey;
    });

    if (isOnline.val()) {
      user.set('presenceStatus', 'online');
    } else {
      user.set('presenceStatus', 'offline');
    }
  });

  chat.firebaseRef.onDisconnect(function() {
    var user = chat.presenceList.find(function(user) {
      return user.get('id') == userKey;
    });

    user.set('presenceStatus', 'offline');
  });
});


@@ -1912,79 +1912,8 @@
 ;%0A%0A%0A
-var connectedRef = new Firebase(firebaseRootUrl + '/info/connected');%0A%0A
 R.re
@@ -2179,23 +2179,16 @@
 y%0A  
-//%0A  var exists
+var user
  = c
@@ -2208,86 +2208,46 @@
 ist.
-any(function(user) %7B%0A    return user.get('id')
+get(userKey);%0A  if (user
  ==
+=
  u
-serKey;%0A  %7D);%0A  if (!exists
+ndefined
 ) %7B%0A
@@ -2397,456 +2397,233 @@
 %0A%0A  
-connectedRef.on('value', function(isOnline) %7B%0A    var user = chat.presenceList.find(function(user) %7B%0A      return user.get('id') == userKey;%0A    %7D);%0A%0A    if (isOnline.val()) %7B%0A      user.set('presenceStatus', 'online');%0A    %7D else %7B%0A      user.set('presenceStatus', 'offline');%0A    %7D%0A  %7D);%0A%0A  chat.firebaseRef.onDisconnect(function() %7B%0A    var user = chat.presenceList.find(function(user) %7B%0A      return user.get('id') == userKey;%0A    %7D);%0A%0A    user
+var presenceRef = chat.presenceList.firebase.child(userKey).child('presenceStatus');%0A%0A  presenceRef.onDisconnect(function() %7B%0A    presenceRef.set('presenceStatus', 'offline');%0A  %7D);%0A%0A  // Mark yourself as online%0A  presenceRef
 .set
@@ -2639,33 +2639,32 @@
 eStatus', 'o
-ff
+n
 line');%0A
   %7D);%0A%7D);%0A%0A
@@ -2656,15 +2656,9 @@
 ');%0A
-  %7D);
 %0A%7D);%0A
-%0A

Improve persistence