Notes = new Meteor.Collection('notes');

if (Meteor.isClient) {
  Session.setDefault('editing', null);
  Session.setDefault('showPanel', null);
  Session.setDefault('currentNote', null);

  function updateContent(noteId, template) {
    var noteContent = template.find('.mtr_edit-content');

    if(noteContent.value !== ''){
      Meteor.call('updateNoteContent', noteId, {
        content: noteContent.value,
        updatedAt: Date.now()
      });
    }
  };

  UI.body.helpers({
    editing: function() {
      return Session.get('editing');
    }
  });

  Template.panel.helpers({
    showPanel: function() {
      return Session.get('showPanel');
    },

    note: function() {
      return Notes.find({});
    },

    isSelected: function() {
      return this._id === Session.get('currentNote') ? true : null;
    }
  });

  Template.panel.events({
    'click .mtr_toggle-panel': function(event, template) {
      Session.get('showPanel') ? Session.set('showPanel', null) : Session.set('showPanel', true);
    },

    'click .mtr_select-note': function() {
      Session.set('currentNote', this._id);
    }
  });

  Template.note.helpers({
    editing: function() {
      return Session.get('editing');
    },

    note: function() {
      return Notes.find({_id: Session.get('currentNote')});
    }
  });

  Template.note.events({
    'keyup .mtr_edit-content': function(event, template) {
      if (event.which == 32 || event.which == 8 || event.which == 13) {
        updateContent(this._id, template);
      }
    },

    'click .mtr_edit-toggle': function(event, template) {
      if(Session.get('editing')) {
        updateContent(this._id, template);
        Session.set('editing', null);
      } else {
        Session.set('editing', this._id);
      }
    }
  });
}

if (Meteor.isServer) {
  Meteor.methods({
    'updateNoteContent': function(noteId, args) {
      Notes.update(noteId, {$set: {
        content: args.content,
        updatedAt: args.updatedAt
      }});
    }
  })
}

@@ -1482,32 +1482,50 @@
 dateContent(
-this._id
+Session.get('currentNote')
 , template);
@@ -1591,32 +1591,85 @@
 nt, template) %7B%0A
+      var currentNote = Session.get('currentNote');%0A%0A
       if(Session
@@ -1709,24 +1709,27 @@
 Content(
-this._id
+currentNote
 , templa
@@ -1817,24 +1817,27 @@
 iting', 
-this._id
+currentNote
 );%0A     

Fix editing