/*
	Copyright (c) 2004-2008, The Dojo Foundation
	All Rights Reserved.

	Licensed under the Academic Free License version 2.1 or above OR the
	modified BSD license. For more information on Dojo licensing, see:

		http://dojotoolkit.org/book/dojo-book-0-9/introduction/licensing
*/


window[(typeof (djConfig)!="undefined"&&djConfig.scopeMap&&djConfig.scopeMap[0][1])||"dojo"]._xdResourceLoaded({depends:[["provide","dojo.data.ItemFileReadStore"],["require","dojo.data.util.filter"],["require","dojo.data.util.simpleFetch"],["require","dojo.date.stamp"]],defineResource:function(_1,_2,_3){if(!_1._hasResource["dojo.data.ItemFileReadStore"]){_1._hasResource["dojo.data.ItemFileReadStore"]=true;_1.provide("dojo.data.ItemFileReadStore");_1.require("dojo.data.util.filter");_1.require("dojo.data.util.simpleFetch");_1.require("dojo.date.stamp");_1.declare("dojo.data.ItemFileReadStore",null,{constructor:function(_4){this._arrayOfAllItems=[];this._arrayOfTopLevelItems=[];this._loadFinished=false;this._jsonFileUrl=_4.url;this._jsonData=_4.data;this._datatypeMap=_4.typeMap||{};if(!this._datatypeMap["Date"]){this._datatypeMap["Date"]={type:Date,deserialize:function(_5){return _1.date.stamp.fromISOString(_5);}};}this._features={"dojo.data.api.Read":true,"dojo.data.api.Identity":true};this._itemsByIdentity=null;this._storeRefPropName="_S";this._itemNumPropName="_0";this._rootItemPropName="_RI";this._reverseRefMap="_RRM";this._loadInProgress=false;this._queuedFetches=[];},url:"",_assertIsItem:function(_6){if(!this.isItem(_6)){throw new Error("dojo.data.ItemFileReadStore: Invalid item argument.");}},_assertIsAttribute:function(_7){if(typeof _7!=="string"){throw new Error("dojo.data.ItemFileReadStore: Invalid attribute argument.");}},getValue:function(_8,_9,_a){var _b=this.getValues(_8,_9);return (_b.length>0)?_b[0]:_a;},getValues:function(_c,_d){this._assertIsItem(_c);this._assertIsAttribute(_d);return _c[_d]||[];},getAttributes:function(_e){this._assertIsItem(_e);var _f=[];for(var key in _e){if((key!==this._storeRefPropName)&&(key!==this._itemNumPropName)&&(key!==this._rootItemPropName)&&(key!==this._reverseRefMap)){_f.push(key);}}return _f;},hasAttribute:function(_11,_12){return this.getValues(_11,_12).length>0;},containsValue:function(_13,_14,_15){var _16=undefined;if(typeof _15==="string"){_16=_1.data.util.filter.patternToRegExp(_15,false);}return this._containsValue(_13,_14,_15,_16);},_containsValue:function(_17,_18,_19,_1a){return _1.some(this.getValues(_17,_18),function(_1b){if(_1b!==null&&!_1.isObject(_1b)&&_1a){if(_1b.toString().match(_1a)){return true;}}else{if(_19===_1b){return true;}}});},isItem:function(_1c){if(_1c&&_1c[this._storeRefPropName]===this){if(this._arrayOfAllItems[_1c[this._itemNumPropName]]===_1c){return true;}}return false;},isItemLoaded:function(_1d){return this.isItem(_1d);},loadItem:function(_1e){this._assertIsItem(_1e.item);},getFeatures:function(){return this._features;},getLabel:function(_1f){if(this._labelAttr&&this.isItem(_1f)){return this.getValue(_1f,this._labelAttr);}return undefined;},getLabelAttributes:function(_20){if(this._labelAttr){return [this._labelAttr];}return null;},_fetchItems:function(_21,_22,_23){var _24=this;var _25=function(_26,_27){var _28=[];if(_26.query){var _29=_26.queryOptions?_26.queryOptions.ignoreCase:false;var _2a={};for(var key in _26.query){var _2c=_26.query[key];if(typeof _2c==="string"){_2a[key]=_1.data.util.filter.patternToRegExp(_2c,_29);}}for(var i=0;i<_27.length;++i){var _2e=true;var _2f=_27[i];if(_2f===null){_2e=false;}else{for(var key in _26.query){var _2c=_26.query[key];if(!_24._containsValue(_2f,key,_2c,_2a[key])){_2e=false;}}}if(_2e){_28.push(_2f);}}_22(_28,_26);}else{for(var i=0;i<_27.length;++i){var _30=_27[i];if(_30!==null){_28.push(_30);}}_22(_28,_26);}};if(this._loadFinished){_25(_21,this._getItemsArray(_21.queryOptions));}else{if(this._jsonFileUrl){if(this._loadInProgress){this._queuedFetches.push({args:_21,filter:_25});}else{this._loadInProgress=true;var _31={url:_24._jsonFileUrl,handleAs:"json-comment-optional"};var _32=_1.xhrGet(_31);_32.addCallback(function(_33){try{_24._getItemsFromLoadedData(_33);_24._loadFinished=true;_24._loadInProgress=false;_25(_21,_24._getItemsArray(_21.queryOptions));_24._handleQueuedFetches();}catch(e){_24._loadFinished=true;_24._loadInProgress=false;_23(e,_21);}});_32.addErrback(function(_34){_24._loadInProgress=false;_23(_34,_21);});}}else{if(this._jsonData){try{this._loadFinished=true;this._getItemsFromLoadedData(this._jsonData);this._jsonData=null;_25(_21,this._getItemsArray(_21.queryOptions));}catch(e){_23(e,_21);}}else{_23(new Error("dojo.data.ItemFileReadStore: No JSON source data was provided as either URL or a nested Javascript object."),_21);}}}},_handleQueuedFetches:function(){if(this._queuedFetches.length>0){for(var i=0;i<this._queuedFetches.length;i++){var _36=this._queuedFetches[i];var _37=_36.args;var _38=_36.filter;if(_38){_38(_37,this._getItemsArray(_37.queryOptions));}else{this.fetchItemByIdentity(_37);}}this._queuedFetches=[];}},_getItemsArray:function(_39){if(_39&&_39.deep){return this._arrayOfAllItems;}return this._arrayOfTopLevelItems;},close:function(_3a){},_getItemsFromLoadedData:function(_3b){function valueIsAnItem(_3c){var _3d=((_3c!=null)&&(typeof _3c=="object")&&(!_1.isArray(_3c))&&(!_1.isFunction(_3c))&&(_3c.constructor==Object)&&(typeof _3c._reference=="undefined")&&(typeof _3c._type=="undefined")&&(typeof _3c._value=="undefined"));return _3d;};var _3e=this;function addItemAndSubItemsToArrayOfAllItems(_3f){_3e._arrayOfAllItems.push(_3f);for(var _40 in _3f){var _41=_3f[_40];if(_41){if(_1.isArray(_41)){var _42=_41;for(var k=0;k<_42.length;++k){var _44=_42[k];if(valueIsAnItem(_44)){addItemAndSubItemsToArrayOfAllItems(_44);}}}else{if(valueIsAnItem(_41)){addItemAndSubItemsToArrayOfAllItems(_41);}}}}};this._labelAttr=_3b.label;var i;var _46;this._arrayOfAllItems=[];this._arrayOfTopLevelItems=_3b.items;for(i=0;i<this._arrayOfTopLevelItems.length;++i){_46=this._arrayOfTopLevelItems[i];addItemAndSubItemsToArrayOfAllItems(_46);_46[this._rootItemPropName]=true;}var _47={};var key;for(i=0;i<this._arrayOfAllItems.length;++i){_46=this._arrayOfAllItems[i];for(key in _46){if(key!==this._rootItemPropName){var _49=_46[key];if(_49!==null){if(!_1.isArray(_49)){_46[key]=[_49];}}else{_46[key]=[null];}}_47[key]=key;}}while(_47[this._storeRefPropName]){this._storeRefPropName+="_";}while(_47[this._itemNumPropName]){this._itemNumPropName+="_";}while(_47[this._reverseRefMap]){this._reverseRefMap+="_";}var _4a;var _4b=_3b.identifier;if(_4b){this._itemsByIdentity={};this._features["dojo.data.api.Identity"]=_4b;for(i=0;i<this._arrayOfAllItems.length;++i){_46=this._arrayOfAllItems[i];_4a=_46[_4b];var _4c=_4a[0];if(!this._itemsByIdentity[_4c]){this._itemsByIdentity[_4c]=_46;}else{if(this._jsonFileUrl){throw new Error("dojo.data.ItemFileReadStore:  The json data as specified by: ["+this._jsonFileUrl+"] is malformed.  Items within the list have identifier: ["+_4b+"].  Value collided: ["+_4c+"]");}else{if(this._jsonData){throw new Error("dojo.data.ItemFileReadStore:  The json data provided by the creation arguments is malformed.  Items within the list have identifier: ["+_4b+"].  Value collided: ["+_4c+"]");}}}}}else{this._features["dojo.data.api.Identity"]=Number;}for(i=0;i<this._arrayOfAllItems.length;++i){_46=this._arrayOfAllItems[i];_46[this._storeRefPropName]=this;_46[this._itemNumPropName]=i;}for(i=0;i<this._arrayOfAllItems.length;++i){_46=this._arrayOfAllItems[i];for(key in _46){_4a=_46[key];for(var j=0;j<_4a.length;++j){_49=_4a[j];if(_49!==null&&typeof _49=="object"){if(_49._type&&_49._value){var _4e=_49._type;var _4f=this._datatypeMap[_4e];if(!_4f){throw new Error("dojo.data.ItemFileReadStore: in the typeMap constructor arg, no object class was specified for the datatype '"+_4e+"'");}else{if(_1.isFunction(_4f)){_4a[j]=new _4f(_49._value);}else{if(_1.isFunction(_4f.deserialize)){_4a[j]=_4f.deserialize(_49._value);}else{throw new Error("dojo.data.ItemFileReadStore: Value provided in typeMap was neither a constructor, nor a an object with a deserialize function");}}}}if(_49._reference){var _50=_49._reference;if(!_1.isObject(_50)){_4a[j]=this._itemsByIdentity[_50];}else{for(var k=0;k<this._arrayOfAllItems.length;++k){var _52=this._arrayOfAllItems[k];var _53=true;for(var _54 in _50){if(_52[_54]!=_50[_54]){_53=false;}}if(_53){_4a[j]=_52;}}}if(this.referenceIntegrity){var _55=_4a[j];if(this.isItem(_55)){this._addReferenceToMap(_55,_46,key);}}}else{if(this.isItem(_49)){if(this.referenceIntegrity){this._addReferenceToMap(_49,_46,key);}}}}}}}},_addReferenceToMap:function(_56,_57,_58){},getIdentity:function(_59){var _5a=this._features["dojo.data.api.Identity"];if(_5a===Number){return _59[this._itemNumPropName];}else{var _5b=_59[_5a];if(_5b){return _5b[0];}}return null;},fetchItemByIdentity:function(_5c){if(!this._loadFinished){var _5d=this;if(this._jsonFileUrl){if(this._loadInProgress){this._queuedFetches.push({args:_5c});}else{this._loadInProgress=true;var _5e={url:_5d._jsonFileUrl,handleAs:"json-comment-optional"};var _5f=_1.xhrGet(_5e);_5f.addCallback(function(_60){var _61=_5c.scope?_5c.scope:_1.global;try{_5d._getItemsFromLoadedData(_60);_5d._loadFinished=true;_5d._loadInProgress=false;var _62=_5d._getItemByIdentity(_5c.identity);if(_5c.onItem){_5c.onItem.call(_61,_62);}_5d._handleQueuedFetches();}catch(error){_5d._loadInProgress=false;if(_5c.onError){_5c.onError.call(_61,error);}}});_5f.addErrback(function(_63){_5d._loadInProgress=false;if(_5c.onError){var _64=_5c.scope?_5c.scope:_1.global;_5c.onError.call(_64,_63);}});}}else{if(this._jsonData){_5d._getItemsFromLoadedData(_5d._jsonData);_5d._jsonData=null;_5d._loadFinished=true;var _65=_5d._getItemByIdentity(_5c.identity);if(_5c.onItem){var _66=_5c.scope?_5c.scope:_1.global;_5c.onItem.call(_66,_65);}}}}else{var _65=this._getItemByIdentity(_5c.identity);if(_5c.onItem){var _66=_5c.scope?_5c.scope:_1.global;_5c.onItem.call(_66,_65);}}},_getItemByIdentity:function(_67){var _68=null;if(this._itemsByIdentity){_68=this._itemsByIdentity[_67];}else{_68=this._arrayOfAllItems[_67];}if(_68===undefined){_68=null;}return _68;},getIdentityAttributes:function(_69){var _6a=this._features["dojo.data.api.Identity"];if(_6a===Number){return null;}else{return [_6a];}},_forceLoad:function(){var _6b=this;if(this._jsonFileUrl){var _6c={url:_6b._jsonFileUrl,handleAs:"json-comment-optional",sync:true};var _6d=_1.xhrGet(_6c);_6d.addCallback(function(_6e){try{if(_6b._loadInProgress!==true&&!_6b._loadFinished){_6b._getItemsFromLoadedData(_6e);_6b._loadFinished=true;}}catch(e){console.log(e);throw e;}});_6d.addErrback(function(_6f){throw _6f;});}else{if(this._jsonData){_6b._getItemsFromLoadedData(_6b._jsonData);_6b._jsonData=null;_6b._loadFinished=true;}}}});_1.extend(_1.data.ItemFileReadStore,_1.data.util.simpleFetch);}}});
