//>>built
define("dojox/rpc/OfflineRest",["dojo","dojox","dojox/data/ClientFilter","dojox/rpc/Rest","dojox/storage"],function(g,d){function k(a){return a.replace(/[^0-9A-Za-z_]/g,"_")}function m(a,c){if(l&&!o&&(c||a&&a.__id))d.storage.put(k(c||a.__id),"object"==typeof a?d.json.ref.toJson(a):a,function(){},j)}function q(a){return a instanceof Error&&(503==a.status||12E3<a.status||!a.status)}function r(){if(l){var a=d.storage.get("dirty",j);if(a)for(var c in a)s(c,a)}}function p(){f.sendChanges();f.downloadChanges()}
function t(a,c,b,e,h){"delete"==a?d.storage.remove(k(c),j):d.storage.put(k(b),e,function(){},j);if(a=h&&h._store)a.updateResultSet(a._localBaseResults,a._localBaseFetch),d.storage.put(k(h._getRequest(a._localBaseFetch.query).url),d.json.ref.toJson(a._localBaseResults),function(){},j)}function s(a,c){var b=c[a],e=d.rpc.JsonRest.getServiceAndId(b.id),e=u(b.method,e.service,e.id,b.content);c[a]=b;d.storage.put("dirty",c,function(){},j);e.addBoth(function(c){if(q(c))return null;var b=d.storage.get("dirty",
j)||{};delete b[a];d.storage.put("dirty",b,function(){},j);return c});return e}var i=d.rpc.Rest,j="dojox_rpc_OfflineRest",l,n=i._index;d.storage.manager.addOnLoad(function(){l=d.storage.manager.available;for(var a in n)m(n[a],a)});var o,f,w=setInterval(p,15E3);g.connect(document,"ononline",p);f=d.rpc.OfflineRest={turnOffAutoSync:function(){clearInterval(w)},sync:p,sendChanges:r,downloadChanges:function(){},addStore:function(a,c){f.stores.push(a);a.fetch({queryOptions:{cache:!0},query:c,onComplete:function(c,
d){a._localBaseResults=c;a._localBaseFetch=d}})}};f.stores=[];var x=i._get;i._get=function(a,c){try{r();if(window.navigator&&!1===navigator.onLine)throw Error();var b=x(a,c)}catch(e){b=new g.Deferred,b.errback(e)}var h=d.rpc._sync;b.addCallback(function(d){m(d,a._getRequest(c).url);return d});b.addErrback(function(e){if(l){if(q(e)){var v={},f=function(a,c){if(v[a])return c;var b=g.fromJson(d.storage.get(k(a),j))||c;v[a]=b;for(var e in b){var h=b[e];if(a=h&&h.$ref)a.substring&&"cid:"==a.substring(0,
4)&&(a=a.substring(4)),b[e]=f(a,h)}if(b instanceof Array)for(e=0;e<b.length;e++)void 0===b[e]&&b.splice(e--,1);return b};o=!0;var i=f(a._getRequest(c).url);if(!i)return e;o=!1;return i}return e}if(h)return Error("Storage manager not loaded, can not continue");b=new g.Deferred;b.addCallback(arguments.callee);d.storage.manager.addOnLoad(function(){b.callback()});return b});return b};g.addOnLoad(function(){g.connect(d.data,"restListener",function(a){var c=a.channel,b=a.event.toLowerCase(),e=d.rpc.JsonRest&&
d.rpc.JsonRest.getServiceAndId(c).service;t(b,c,"post"==b?c+a.result.id:c,g.toJson(a.result),e)})});var u=i._change;i._change=function(a,c,b,e){if(!l)return u.apply(this,arguments);var h=c._getRequest(b).url;t(a,h,d.rpc.JsonRest._contentId,e,c);var g=d.storage.get("dirty",j)||{};if("put"==a||"delete"==a)var f=h;else{var f=0,i;for(i in g)isNaN(parseInt(i))||(f=i);f++}g[f]={method:a,id:h,content:e};return s(f,g)};g.connect(n,"onLoad",m);g.connect(n,"onUpdate",m);return f});