exports.sync = (function() {
  var backend = {
    function get(key, err, cb, timeout) {
      console.log('backend.get("'+key+'", "'+value+'", err, cb, '+timeout+');');
    }
    function set(key, value, err, cb, timeout) {
      console.log('backend.set("'+key+'", "'+value+'", err, cb, '+timeout+');');
    }
    function remove(key, err, cb, timeout) {
      console.log('backend.remove("'+key+'", "'+value+'", err, cb, '+timeout+');');
    }
  };
  function setBackend(backendToSet) {
    backend = backendToSet;
  }
  function start() {
    localStorage.setItem('_shadowSyncStatus', 'pulling');
    console.log('setting sync status to pulling');
  }
  function push(timestamp) {
    if(localStorage.getItem('_shadowSyncStatus') == 'pulling') {
      console.log('will push after pulling is completed');
    } else {
      localStorage.setItem('_shadowSyncStatus', 'pushing');
      localStorage.setItem('_shadowSyncPushRev', timestamp);
      console.log('setting sync status to pushing revision '+timestamp);
    }
  }
  function resumePulling(timeout, cb) {
    console.log('resume pulling');
    backend.get('_shadowLatestRevision', function(msg) {
      console.log('error retrieving _shadowLatestRevision');
    }, function(value) {
      localStorage.setItem('_shadowRemote', value);
      localStorage.setItem('_shadowSyncStatus') == 'idle';
      cb();
    }, timeout);
  }
  function getItemToPush(next) {
    var pushRev = localStorage.getItem('_shadowSyncPushRev');
    var index = JSON.parse(localStorage.getItem('_shadowRevision_'+pushRev));
    var entryToPush = localStorage.getItem('_shadowSyncCurrEntry');
    if(entryToPush == null) {
      entryToPush = 0;//leave as null in localStorage, no use updating that
    }
    if(next) {
      entryToPush++;
      localStorage.setItem('_shadowSyncCurrEntry', entryToPush);
    }
    if(entryToPush < index.length) {
      return index[entryToPush];
    } else if(entryToPush == index.length) {
      return '_shadowRevision_'+pushRev;
    } else {
      localStorage.removeItem('_shadowSyncCurrEntry');
      return false;
    }
  }

  function resumePushing(timeout, cb) {
    console.log('resume pushing');
    var itemToPush = getItemToPush(false);
    backend.set(nextItemToPush, localStorage.getItem(itemToPush), function(msg) {
      console.log('error putting '+itemToPush);
    }, function() {
      if(getItemToPush(true)) {
        var timeElapsed = (new Date().getTime()) - startTime;
        work(timeout - timeElapsed, cb);
      } else {
        localStorage.setItem('_shadowSyncStatus') == 'idle';
        cb();
      }
    }, timeout);
  }
  function work(timeout, cb) {
    var startTime = (new Date().getTime());
    console.log('sync working for '+timeout+' milliseconds:');
    if(localStorage.getItem('_shadowSyncStatus') == 'pulling') {
      resumePulling(timeout, cb);
    } else if(localStorage.getItem('_shadowSyncStatus') == 'pushing') {
      resumePushing(timeout, cb);
    } else {
      console.log('nothing to work on.');
    }
  }
  return {
    setBackend: setBackend,
    start: start,
    push: push,
    work: work
  };
})();

@@ -39,24 +39,29 @@
 kend = %7B%0A   
+ get:
  function ge
@@ -57,20 +57,16 @@
 function
- get
 (key, er
@@ -161,36 +161,42 @@
 out+');');%0A    %7D
+,
 %0A   
+ set:
  function set(ke
@@ -188,20 +188,16 @@
 function
- set
 (key, va
@@ -307,20 +307,29 @@
 );%0A    %7D
+,
 %0A   
+ remove:
  functio
@@ -329,23 +329,16 @@
 function
- remove
 (key, er
@@ -1378,32 +1378,390 @@
 , timeout);%0A  %7D%0A
+  function objLength(obj) %7B//FIXME: look up javascript lang ref when connected%0A    var i = 0;%0A    for(var j in obj) %7B%0A      i++;%0A    %7D%0A    return i;%0A  %7D%0A  function objKey(obj, i) %7B//FIXME: look up javascript lang ref when connected%0A    var j = 0;%0A    for(var k in obj) %7B%0A      if(i==j) %7B%0A        return k;%0A      %7D%0A      j++;%0A    %7D%0A    return undefined;%0A  %7D%0A%0A
   function getIt
@@ -2225,28 +2225,32 @@
 oPush %3C 
-index.length
+objLength(index)
 ) %7B%0A    
@@ -2262,14 +2262,22 @@
 urn 
+objKey(
 index
-%5B
+, 
 entr
@@ -2287,9 +2287,9 @@
 Push
-%5D
+)
 ;%0A  
@@ -2319,20 +2319,24 @@
  == 
-index.length
+objLength(index)
 ) %7B%0A
@@ -2613,13 +2613,9 @@
 set(
-nextI
+i
 temT

fix sync currEntry