define("chy/store/Json", ["dojo/store/util/QueryResults", "dojo/_base/declare", "dojo/_base/lang", "dojo/_base/xhr", "dojo/store/util/SimpleQueryEngine","dojo/store/api/Store","dojo/when"],
        function(QueryResults, declare, lang, xhr, SimpleQueryEngine,Store,when){
    var base=null;
    //  Declare the initial store
    return declare("chy.store.Json",base, {
        idProperty: "id",
        queryEngine: SimpleQueryEngine,
        startLength:0,
        target: "",
 
        constructor: function(options){
            lang.mixin(this, options || {});
        },
        query: function(query, options){
		var hasQuestionMark = this.target.indexOf("?") > -1;
		if(query && typeof query == "object"){
			query = xhr.objectToQuery(query);
			query = query ? (hasQuestionMark ? "&" : "?") + query: "";
		}
		var queryVal=query.split('=');
		if (queryVal.length>=2)queryVal=queryVal[1];
		queryVal=queryVal.replace(/\*/g,'');
		
		
		if (queryVal.length>=this.startLength||queryVal.length==0) 
		{
			
	        	var results = xhr("GET", {
				url: this.target + (query || ""),
				handleAs: "json"
			}).then(function(data){
					var res;
					if (data.option){
						res= [].concat(data.option);
					} else {
						res=[];
					}
					0 && console.log("json chy res length", res.length);
					return res;
				});
			return QueryResults(results);    
		 }
		 return new Store.QueryResults();
            
        },
        getIdentity: function(object){
		// summary:
		//		Returns an object's identity
		// object: Object
		//		The object to get the identity from
		// returns: Number
		return object[this.idProperty];
	},
        get:function(id,options){
        	0 && console.log("Get called with id:" + id);
		var hasQuestionMark = this.target.indexOf("?") > -1;
		var query = (hasQuestionMark ? "&" : "?") + "id=" + id;
		return xhr("GET", {
				url: this.target + (query || ""),
				handleAs: "json"
			}).then(function(data){
					var res;
					if (data.option){
						res= [].concat(data.option);
					} else {
						res=[];
					}
					0 && console.log("json chy res length", res.length);
					if (res.length>0) return res[0];
					return res;
				});

	}
    });
});