/*
	Copyright (c) 2004-2011, The Dojo Foundation All Rights Reserved.
	Available via Academic Free License >= 2.1 OR the modified BSD license.
	see: http://dojotoolkit.org/license for details
*/


window[esri._dojoScopeName||"dojo"]._xdResourceLoaded(function(_1,_2,_3){return {depends:[["provide","dojox.rpc.OfflineRest"],["require","dojox.data.ClientFilter"],["require","dojox.rpc.Rest"],["require","dojox.storage"]],defineResource:function(_4,_5,_6){if(!_4._hasResource["dojox.rpc.OfflineRest"]){_4._hasResource["dojox.rpc.OfflineRest"]=true;_4.provide("dojox.rpc.OfflineRest");_4.require("dojox.data.ClientFilter");_4.require("dojox.rpc.Rest");_4.require("dojox.storage");var _7=_6.rpc.Rest;var _8="dojox_rpc_OfflineRest";var _9;var _a=_7._index;_6.storage.manager.addOnLoad(function(){_9=_6.storage.manager.available;for(var i in _a){_b(_a[i],i);}});var _c;function _d(_e){return _e.replace(/[^0-9A-Za-z_]/g,"_");};function _b(_f,id){if(_9&&!_c&&(id||(_f&&_f.__id))){_6.storage.put(_d(id||_f.__id),typeof _f=="object"?_6.json.ref.toJson(_f):_f,function(){},_8);}};function _10(_11){return _11 instanceof Error&&(_11.status==503||_11.status>12000||!_11.status);};function _12(){if(_9){var _13=_6.storage.get("dirty",_8);if(_13){for(var _14 in _13){_15(_14,_13);}}}};var _16;function _17(){_16.sendChanges();_16.downloadChanges();};var _18=setInterval(_17,15000);_4.connect(document,"ononline",_17);_16=_6.rpc.OfflineRest={turnOffAutoSync:function(){clearInterval(_18);},sync:_17,sendChanges:_12,downloadChanges:function(){},addStore:function(_19,_1a){_16.stores.push(_19);_19.fetch({queryOptions:{cache:true},query:_1a,onComplete:function(_1b,_1c){_19._localBaseResults=_1b;_19._localBaseFetch=_1c;}});}};_16.stores=[];var _1d=_7._get;_7._get=function(_1e,id){try{_12();if(window.navigator&&navigator.onLine===false){throw new Error();}var dfd=_1d(_1e,id);}catch(e){dfd=new _4.Deferred();dfd.errback(e);}var _1f=_6.rpc._sync;dfd.addCallback(function(_20){_b(_20,_1e._getRequest(id).url);return _20;});dfd.addErrback(function(_21){if(_9){if(_10(_21)){var _22={};var _23=function(id,_24){if(_22[id]){return _24;}var _25=_4.fromJson(_6.storage.get(_d(id),_8))||_24;_22[id]=_25;for(var i in _25){var val=_25[i];id=val&&val.$ref;if(id){if(id.substring&&id.substring(0,4)=="cid:"){id=id.substring(4);}_25[i]=_23(id,val);}}if(_25 instanceof Array){for(i=0;i<_25.length;i++){if(_25[i]===undefined){_25.splice(i--,1);}}}return _25;};_c=true;var _26=_23(_1e._getRequest(id).url);if(!_26){return _21;}_c=false;return _26;}else{return _21;}}else{if(_1f){return new Error("Storage manager not loaded, can not continue");}dfd=new _4.Deferred();dfd.addCallback(arguments.callee);_6.storage.manager.addOnLoad(function(){dfd.callback();});return dfd;}});return dfd;};function _27(_28,_29,_2a,_2b,_2c){if(_28=="delete"){_6.storage.remove(_d(_29),_8);}else{_6.storage.put(_d(_2a),_2b,function(){},_8);}var _2d=_2c&&_2c._store;if(_2d){_2d.updateResultSet(_2d._localBaseResults,_2d._localBaseFetch);_6.storage.put(_d(_2c._getRequest(_2d._localBaseFetch.query).url),_6.json.ref.toJson(_2d._localBaseResults),function(){},_8);}};_4.addOnLoad(function(){_4.connect(_6.data,"restListener",function(_2e){var _2f=_2e.channel;var _30=_2e.event.toLowerCase();var _31=_6.rpc.JsonRest&&_6.rpc.JsonRest.getServiceAndId(_2f).service;_27(_30,_2f,_30=="post"?_2f+_2e.result.id:_2f,_4.toJson(_2e.result),_31);});});var _32=_7._change;_7._change=function(_33,_34,id,_35){if(!_9){return _32.apply(this,arguments);}var _36=_34._getRequest(id).url;_27(_33,_36,_6.rpc.JsonRest._contentId,_35,_34);var _37=_6.storage.get("dirty",_8)||{};if(_33=="put"||_33=="delete"){var _38=_36;}else{_38=0;for(var i in _37){if(!isNaN(parseInt(i))){_38=i;}}_38++;}_37[_38]={method:_33,id:_36,content:_35};return _15(_38,_37);};function _15(_39,_3a){var _3b=_3a[_39];var _3c=_6.rpc.JsonRest.getServiceAndId(_3b.id);var _3d=_32(_3b.method,_3c.service,_3c.id,_3b.content);_3a[_39]=_3b;_6.storage.put("dirty",_3a,function(){},_8);_3d.addBoth(function(_3e){if(_10(_3e)){return null;}var _3f=_6.storage.get("dirty",_8)||{};delete _3f[_39];_6.storage.put("dirty",_3f,function(){},_8);return _3e;});return _3d;};_4.connect(_a,"onLoad",_b);_4.connect(_a,"onUpdate",_b);}}};});