/*
 * Copyright (C) 2013 Vanderbilt University, All rights reserved.
 * 
 * Author: Robert Kereskenyi
 */

"use strict";

/*
 * Utility helper functions for saving WebGME state and reload on browser back
 */

define(['jquery',
        'logManager'], function (_jquery,
                                 logManager) {

    var _client,
        STATE_SELECTED_OBJECT_ID = 'selectedObjectId',
        STATE_ACTIVE_SELECTION = 'activeSelection',
        _stateLoading = false,
        _initialized = false,
        logger = logManager.create("WebGME.History");

    var _saveState = function (stateObj) {
        if (_stateLoading === false) {
            window.history.pushState(stateObj, null, null);
        }
    };


    var _onLoadState = function (stateObj) {
        _stateLoading = true;

        //TODO: load state into WebGMEGlobal.State

        _stateLoading = false;
    };


    var _initialize = function () {
        if (_initialized) {
            return;
        }

        WebGMEGlobal.State.on("all", function(eventName) {
            var stateObj = {};
            //TODO: save state  ---> persist WebGMEGlobal.State into stateObj
            //_saveState(stateObj);
        });

        logger.error('!!! NOT YET IMPLEMENTED !!!');
    };

    if (WebGMEGlobal.history !== true) {
        WebGMEGlobal.history = true;
        $(window).on('popstate', function(event) {
            _onLoadState(event.originalEvent.state);
        });
    }


    //return utility functions
    return { initialize: _initialize};
});
@@ -322,132 +322,8 @@
 var 
-_client,%0A        STATE_SELECTED_OBJECT_ID = 'selectedObjectId',%0A        STATE_ACTIVE_SELECTION = 'activeSelection',%0A        
 _sta
@@ -504,24 +504,94 @@
 == false) %7B%0A
+            logger.debug('saving state:' + JSON.stringify(stateObj));%0A
             
@@ -747,29 +747,246 @@
   //
-TODO: 
+clear state in silent mode, it will not fire the clear event%0A        WebGMEGlobal.State.clear(%7B'silent': true%7D);%0A%0A        //set the attributes from the saved state%0A        logger.debug('
 load
+ing
  state
- into
+:' + JSON.stringify(stateObj));%0A       
  Web
@@ -1000,16 +1000,31 @@
 al.State
+.set(stateObj);
 %0A%0A      
@@ -1152,16 +1152,45 @@
      %7D%0A%0A
+        _initialized = true;%0A
         
@@ -1216,11 +1216,14 @@
 on(%22
-all
+change
 %22, f
@@ -1234,51 +1234,25 @@
 ion(
-eventName) %7B%0A            var stateObj = %7B%7D;
+model, options) %7B
 %0A   
@@ -1264,41 +1264,19 @@
     
-//TODO: 
+_
 save
- s
+S
 tate
-  ---%3E persist 
+(
 WebG
@@ -1293,80 +1293,28 @@
 tate
- into stateObj%0A            //_saveState(stateObj);%0A        %7D
+.toJSON()
 );%0A
-%0A
         
 logg
@@ -1313,50 +1313,9 @@
     
-logger.error('!!! NOT YET IMPLEMENTED !!!'
+%7D
 );%0A 
@@ -1362,32 +1362,54 @@
 true) %7B%0A        
+Object.defineProperty(
 WebGMEGlobal.his
@@ -1408,24 +1408,133 @@
 obal
-.
+, '
 history
- = true;
+', %7Bvalue : true,%0A            writable : false,%0A            enumerable : true,%0A            configurable : false%7D);%0A
 %0A   

implement history with the new state object