init: function() {
     this._observers = {};
-    console.log('Created', this.guid());
   },
   
   description: function() {
   },
   
   addObserver: function(observer, property, callback) {
-    console.log('Add observer', observer.description(), 'to', this.description(), '[', property, ']');
+    // console.log('Add observer', observer.description(), 'to', this.description(), '[', property, ']');
     if (!this._observers[property]) {
       this._observers[property] = {}
     }
         this.removeObserver(observer, property)
       }
     } else {
-      console.log('Remove observer', observer.description(), 'from', this.description(), '[', property, ']');
+      // console.log('Remove observer', observer.description(), 'from', this.description(), '[', property, ']');
       delete this._observers[property][observer.guid()];
     }
     return this;
   notifyObservers: function(property) {
     var currentValue = this[property];
     for (var guid in this._observers[property]) {
-      console.log(this._observers[property][guid]);
-      console.log('Notifying', guid, 'of', this.description(), '[', property, ']');
+      // console.log(this._observers[property][guid]);
+      // console.log('Notifying', guid, 'of', this.description(), '[', property, ']');
       this._observers[property][guid](property, currentValue, this);
     }
     return this;
 
-/*globals Editor fileId SplitView PanelContainerView*/
+/*globals Editor fileId SplitView PanelContainerView EditorView*/
 var documentsUrl = '/api/documents/';
 
 
 });
 
 
+// Stany modelu:
+//
+// empty -> loading -> synced -> unsynced -> loading
+//                           \
+//                            -> dirty -> updating -> synced
+//
 Editor.XMLModel = Editor.Model.extend({
   _className: 'Editor.XMLModel',
   serverURL: null,
   data: '',
+  state: 'empty',
   
   init: function(serverURL) {
     this._super();
+    this.set('state', 'empty');
     this.serverURL = serverURL;
     this.toolbarButtonsModel = new Editor.ToolbarButtonsModel();
-  },
-  
-  getData: function() {
-    if (!this.data) {
-      this.reload();
-    }
-    return this.data;
+    this.addObserver(this, 'data', this.dataChanged.bind(this));
   },
   
   load: function() {
-    if (!this.get('synced')) {
+    if (this.get('state') == 'empty') {
+      this.set('state', 'loading');
       $.ajax({
         url: this.serverURL,
         dataType: 'text',
-        success: this.reloadSucceeded.bind(this)
+        success: this.loadingSucceeded.bind(this)
       });
     }
   },
   
-  reloadSucceeded: function(data) {
+  set: function(property, value) {
+    if (property == 'state') {
+      console.log(this.description(), ':', property, '=', value);
+    }
+    return this._super(property, value);
+  },
+  
+  dataChanged: function(property, value) {
+    if (this.get('state') == 'synced') {
+      this.set('state', 'dirty');
+    }
+  },
+  
+  loadingSucceeded: function(data) {
+    if (this.get('state') != 'loading') {
+      alert('erroneous state:', this.get('state'));
+    }
     this.set('data', data);
-    this.set('synced', true);
+    this.set('state', 'synced');
+  },
+  
+  dispose: function() {
+    this.removeObserver(this);
+    this._super();
   }
 });
 
   _className: 'Editor.HTMLModel',
   serverURL: null,
   data: '',
+  state: 'empty',
   
   init: function(serverURL) {
     this._super();
+    this.set('state', 'empty');
     this.serverURL = serverURL;
   },
   
   load: function() {
-    if (!this.get('synced')) {
+    if (this.get('state') == 'empty') {
+      this.set('state', 'loading');
       $.ajax({
         url: this.serverURL,
         dataType: 'text',
-        success: this.reloadSucceeded.bind(this)
+        success: this.loadingSucceeded.bind(this)
       });
     }
   },
   
-  reloadSucceeded: function(data) {
+  loadingSucceeded: function(data) {
+    if (this.get('state') != 'loading') {
+      alert('erroneous state:', this.get('state'));
+    }
     this.set('data', data);
-    this.set('synced', true);
+    this.set('state', 'synced');
+  },
+  
+  set: function(property, value) {
+    if (property == 'state') {
+      console.log(this.description(), ':', property, '=', value);
+    }
+    return this._super(property, value);
   }
 });
 
   _className: 'Editor.DocumentModel',
   data: null, // name, text_url, latest_rev, latest_shared_rev, parts_url, dc_url, size
   contentModels: {},
+  state: 'empty',
   
   init: function() {
     this._super();
+    this.set('state', 'empty');
     this.load();
   },
   
   load: function() {
-    console.log('DocumentModel#load');
-    $.ajax({
-      cache: false,
-      url: documentsUrl + fileId,
-      dataType: 'json',
-      success: this.successfulLoad.bind(this)
-    });
+    if (this.get('state') == 'empty') {
+      this.set('state', 'loading');
+      $.ajax({
+        cache: false,
+        url: documentsUrl + fileId,
+        dataType: 'json',
+        success: this.successfulLoad.bind(this)
+      });
+    }
   },
   
   successfulLoad: function(data) {
-    console.log('DocumentModel#successfulLoad:', data);
     this.set('data', data);
+    this.set('state', 'synced');
     this.contentModels = {
       'xml': new Editor.XMLModel(data.text_url),
       'html': new Editor.HTMLModel(data.html_url)
     };
     for (var key in this.contentModels) {
-      this.contentModels[key].addObserver(this, 'data', this.contentModelDataChanged.bind(this));
+      this.contentModels[key].addObserver(this, 'state', this.contentModelStateChanged.bind(this));
     }
   },
   
-  contentModelDataChanged: function(property, value, contentModel) {
-    console.log('data of', contentModel.description(), 'changed!');
-    for (var key in this.contentModels) {
-      if (this.contentModels[key].guid() != contentModel.guid()) {
-        console.log(this.contentModels[key].description(), 'frozen');
-        this.contentModels[key].set('synced', false);
+  contentModelStateChanged: function(property, value, contentModel) {
+    if (value == 'dirty') {
+      for (var key in this.contentModels) {
+        if (this.contentModels[key].guid() != contentModel.guid()) {
+          // console.log(this.contentModels[key].description(), 'frozen');
+          this.contentModels[key].set('state', 'unsynced');
+        }
       }
     }
   }
 
 $(function() {
   doc = new Editor.DocumentModel();
-  var editor = new EditorView('body', doc);
+  var editor = new EditorView('#body-wrap', doc);
   var splitView = new SplitView('#splitview', doc);
   leftPanelView = new PanelContainerView('#left-panel-container', doc);
   rightPanelContainer = new PanelContainerView('#right-panel-container', doc);
 
     
     this.model
       .addObserver(this, 'data', this.modelDataChanged.bind(this))
-      .addObserver(this, 'synced', this.modelSyncChanged.bind(this));
+      .addObserver(this, 'state', this.modelStateChanged.bind(this));
       
     $('.htmlview', this.element).html(this.model.get('data'));
-    if (!this.model.get('synced')) {
-      this.parent.freeze('Niezsynchronizowany...');
-      this.model.load();
-    }
+    this.modelStateChanged('state', this.model.get('state'));
+    this.model.load();
   },
   
   modelDataChanged: function(property, value) {
     $('.htmlview', this.element).html(value);
   },
   
-  modelSyncChanged: function(property, value) {
-    if (value) {
-      this.parent.unfreeze();
-    } else {
-      this.parent.freeze('Niezsynchronizowany...');
+  modelStateChanged: function(property, value) {
+    if (value == 'synced' || value == 'dirty') {
+      this.unfreeze();
+    } else if (value == 'unsynced') {
+      this.freeze('Niezsynchronizowany...');
+    } else if (value == 'loading') {
+      this.freeze('Ładowanie...');
+    } else if (value == 'saving') {
+      this.freeze('Zapisywanie...');
     }
   },
   
 
   
   resized: function(event) {
     var height = this.element.height() - $('.xmlview-toolbar', this.element).outerHeight();
-    console.log('.xmlview height =', height);
     $('.xmlview', this.element).height(height);
   },
   
     $(editor.frame).css({width: '100%', height: '100%'});
     this.model
       .addObserver(this, 'data', this.modelDataChanged.bind(this))
-      .addObserver(this, 'synced', this.modelSyncChanged.bind(this));
+      .addObserver(this, 'state', this.modelStateChanged.bind(this))
+      .load();
     
     this.parent.unfreeze();
       
     this.editor.setCode(this.model.get('data'));
-    if (!this.model.get('synced')) {
-      this.parent.freeze('Niezsynchronizowany...');
-      this.model.load();
-    }
-    
+    this.modelStateChanged('state', this.model.get('state'));
+        
     // editor.grabKeys(
     //   $.fbind(self, self.hotkeyPressed),
     //   $.fbind(self, self.isHotkey)
   },
   
   modelDataChanged: function(property, value) {
+    console.log('modelDataChanged');
     if (this.editor.getCode() != value) {
       this.editor.setCode(value);
     }
   },
   
-  modelSyncChanged: function(property, value) {
-    if (value) {
-      this.parent.unfreeze();
-    } else {
-      this.parent.freeze('Niezsynchronizowany...');
+  modelStateChanged: function(property, value) {
+    if (value == 'synced' || value == 'dirty') {
+      this.unfreeze();
+    } else if (value == 'unsynced') {
+      this.freeze('Niezsynchronizowany...');
+    } else if (value == 'loading') {
+      this.freeze('Ładowanie...');
+    } else if (value == 'saving') {
+      this.freeze('Zapisywanie...');
     }
   },