Ext.define("Ext.ux.buffer.Grid", {
	extend: "Ext.grid.Panel",
	alias: [
		"widget.ux.buffer.grid",
		"widget.legacy.subfile"
	],
	requires: [
		"Ext.ux.buffer.Proxy"
	],

	config: {
		toolbar: null,
		pageSize: 100
	},

	initComponent: function() {

		var i_self = this;
		var o_config = i_self.initialConfig;

		var i_store = i_self.createStore(o_config);

		var a_columns = i_self.columns || [];

		var a_newColumns = Ext.Array.filter(a_columns, function(o_config) {
			return !o_config.hidden;
		});

		i_self.store = i_store;
		i_self.columns = a_newColumns;

		i_self.callParent(arguments);
	},

	createStore: function(o_config) {

		var i_self = this;

		var a_fields = [];
		var a_columns = o_config.columns || [];

		Ext.Array.each(a_columns, function(o_column) {
			var o_field = Ext.apply({
				name: o_column.dataIndex
			});

			o_field.type = o_column.dataType || "auto";
			a_fields.push(o_field);
		});

		var o_storeConfig = Ext.applyIf({}, {
			remoteSort: true,
			pageSize: i_self.getPageSize(),
			autoLoad: false,
			proxy: {
				url: o_config.url || "",
				type: "ux.buffer"
			},
			fields: a_fields
		}, o_config.store);

		var i_store = Ext.create("Ext.data.BufferedStore", o_storeConfig);

		return i_store;
	},

	load: function(o_options, f_callback) {

		var i_self = this;
		var i_store = i_self.getStore();
		var i_proxy = i_store.getProxy();

		f_callback = f_callback || Ext.emptyFn;

		if (o_options) {
			i_proxy.setExtraParams(o_options);
		}
		
		i_store.load({
			callback: f_callback
		});
	}
});
