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