AWP.Layer.Store = function(columns) {
	config = {
		remoteSort: true,
		reader: new Ext.data.JsonReader(null, columns),
		proxy: this.createProxy()
    };
	
    AWP.Layer.Store.superclass.constructor.call(this, config);
	
	this.reader.read = this.read;
	//this.addListener('beforeload', this.beforeLoad, this);
};
Ext.extend(AWP.Layer.Store, Ext.data.Store, {
	read: function(response) {
		try {
			var json = eval("(" + response.responseText + ")");
		} catch (e) {
			alert('error:' + e.message + '\n\n' + response.responseText)
			return
		}
		AWP.log(json);
		
		var rows = json.rows, total = json.total, records = [];
		for (var i = 0, len = rows.length; i < len; i++) {
			records[i] = new this.recordType(rows[i], rows[i].gid || rows[i].id);
		}
//		var geom, geometries = {};
//		if (json.geometries) {
//			for (var i = 0, len = json.geometries.length; i < len; i++) {
//				geom = json.geometries[i];
//				geometries[geom.gid] = geom;
//			}
//		}
	    return {total: total, records: records, geometries: json.geometries || {}};
	},
	loadRecords : function(o, options, success){
        if (!o || success === false) {
            if(success !== false){
                this.fireEvent("load", this, [], options);
            }
            if(options.callback){
                options.callback.call(options.scope || this, [], options, false);
            }
            return;
        }
        if (!options || options.add !== true) {
			if (this.pruneModifiedRecords) this.modified = [];
			for (var i = 0, len = o.records.length; i < len; i++) o.records[i].join(this);
			if (this.snapshot) {
				this.data = this.snapshot;
				delete this.snapshot;
			}
			this.data.clear();
			this.data.addAll(o.records);
			this.totalLength = o.total;
			this.applySort();
			this.fireEvent("datachanged", this);
        } else {
            this.totalLength = Math.max(o.total, this.data.length + o.records.length);
            this.add(o.records);
        }
        options.geometries = o.geometries;
        this.fireEvent("load", this, o.records, options);
        if (options.callback) {
            options.callback.call(options.scope || this, o.records, options, true);
        }
    },
	createProxy: function() {
		var proxy = new Ext.data.HttpProxy({
			url: AWP.cfg.serviceURL,
			timeout: 300000,
			success: function(response, options) {
				options.request.arg.dataLength = Math.round(response.responseText.length * 100 / 1024) / 100;
			}
		});
		
		proxy.addListener('load', function(proxy, o, arg) {
			var callback = o.request.callback;
			o.request.callback = function(result, options, success) {
				options.extraData = result.extraData;
				callback.call(this, result, options, success);
			}
		});
		
		return proxy;
	}
	/**
	 * for history 
	 *
	beforeLoad: function(store, options) {
		if (store.sortInfo) {
			options.params = options.params || {};
			if (store.sortInfo['direction'] == 'ASC') {
				options.params.order = store.sortInfo['field'] + '_up';
				store.sortInfo['direction'] == 'DESC'
			} else {
				options.params.order = store.sortInfo['field'] + '_down';
				store.sortInfo['direction'] == 'ASC'
			}
		}
	}*/
});

