Ext.define("Ext.ux.form.field.ComboObjectType", {

	extend: "Ext.form.field.ComboBox",

	alias: "widget.comboobjecttype",

	isLoaded: false,

	queryParam: "search",

	config: {
		keyField: "key",
		valueField: "value",
		url: "",
		options: null
	},

	triggers: {
		clear: {
			cls: Ext.baseCSSPrefix + "form-clear-trigger",
			weight: 1,
			handler: "onClearClick",
			hidden: true,
			scope: "this"
		},
		picker: {
			weight: 3,
			handler: "onTriggerClick",
			scope: "this"
		}
	},

	requires: [
		"Ext.ux.buffer.Proxy"
	],

	listeners: {
		afterrender: {
			fn: function(i_field) {

				var value = i_field.getValue();
				i_field.fireEvent("change", i_field, value);
			}
		},
		change: {
			fn: function(i_field, value) {

				var i_self = this;

				if (value) {
					i_self.getTrigger("clear").show();
				}
				else {
					i_self.getTrigger("clear").hide();
				}
			}
		}
	},

	initComponent: function() {

		var i_self = this;

		Ext.apply(i_self, {
			queryMode: "remote",
			displayField: i_self.getKeyField(),
			valueField: i_self.getValueField(),
			triggerAction: "all",
			minChars: 1,
			typeAhead: false,
			maxLength: 1000,
			pageSize: 0,
			displayTpl: i_self.getDisplayTemplate(),
			tpl: i_self.getItemTemplate()
		});

		var i_store = Ext.create("Ext.data.Store", {
			fields: [
				{ name: i_self.getKeyField(), type: "auto" },
				{ name: i_self.getValueField(), type: "auto" }
			],
			proxy: {
				url: i_self.getUrl(),
				type: "ux.buffer"
			},
			remoteSort: true,
			autoLoad: false,
			autoSync: false,
			pageSize: 0
		});

		i_store.on({
			load: {
				fn: function(i_store, a_records) {

					if (!i_self.isLoaded) {

						i_self.isLoaded = true;

						if (i_self.value && a_records.length > 0) {
							i_self.setValue(i_self.value);
						}
						else {
							i_self.setValue("");
						}
						i_self.resetOriginalValue();
					}
				}
			},
			beforeload: {
				fn: function(i_store) {

					var i_proxy = i_store.getProxy();
					var o_extraParams = i_proxy.getExtraParams();

					Ext.apply(o_extraParams, i_self.getOptions());

					Ext.apply(o_extraParams, {
						start: 0,
						limit: 100
					});
				}
			}
		});

		i_self.store = i_store;
		i_self.callParent();

		i_self.on({
			afterrender: {
				fn: function(i_field) {

					var i_store = i_field.getStore();
					i_store.load();
				}
			}
		});

		return i_self;
	},

	getDisplayTemplate: function() {

		var i_self = this;

		var i_template = Ext.create("Ext.XTemplate",
			"<tpl for=\".\">",
				"{[this.renderer(values." + i_self.getKeyField() + ")]}",
			"</tpl>",
			{
				renderer: function(s_key) {
					return ip2.i18n(s_key);
				}
			}
		);
		return i_template;
	},

	getItemTemplate: function() {

		var i_self = this;

		var i_template = Ext.create("Ext.XTemplate",
			"<tpl for=\".\">",
				"<li role=\"option\" class=\"x-boundlist-item\">",
					"{[this.renderer(values." + i_self.getKeyField() + ")]}",
				"</li>",
			"</tpl>",
			{
				renderer: function(s_key) {
					return ip2.i18n(s_key);
				}
			}
		);
		return i_template;
	},

	onClearClick: function() {

		var i_self = this;

		i_self.clearValue();

		return i_self;
	}
});
