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

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

	alias: [
		"widget.enumcombobox",
		"widget.enumpicker"
	],

	config: {
		file: "adm.ns",
		"enum": "",
		regexp: "webr~{0}~",
		search: null
	},

	queryMode: "local",

	forceSelection: true,

	valueField: "key",

	displayField: "text",

	initComponent: function() {

		var i_self = this;
		var i_store = i_self.createStore();

		i_self.store = i_store;
		i_self.callParent(arguments);
	},

	createStore: function() {

		var i_self = this;
		var s_regexp = i_self.getRegexp();
		var s_search = i_self.getSearch();

		s_regexp = s_regexp.replace("{0}", i_self.getEnum());

		var s_valueKey = i_self.valueField || 'key';
		var s_displayKey = i_self.displayField || 'text';

		var a_data = [];

		var o_enum = ip2.resource.get(i_self.getFile());
		if (o_enum) {
			var i_dynamicNs = new ip2.DynamicNS(o_enum);

			var a_data = [];
			var a_keys = i_dynamicNs.getEnumLayoutKeys(i_self.getEnum());

			Ext.Array.each(a_keys, function(s_key) {

				var o_value = i_dynamicNs.getEnumValue(s_key);

				if (s_regexp) {
					s_key = s_key.replace(new RegExp(s_regexp, 'g'), "" || "");
				}
				else if (s_search) {
					s_key = s_key.replace("", "" || "");
				}

				var o_dataValue = {};
				o_dataValue[s_valueKey] = s_key;
				o_dataValue[s_displayKey] = ip2.i18n(o_value);
				a_data.push(o_dataValue);
			});

			a_data.sort(function (a, b) {

				var s_keyA = a[s_valueKey] || 0;
				var s_keyB = b[s_valueKey] || 0;

				if (Ext.isNumber(parseFloat(s_keyA)) && Ext.isNumber(parseFloat(s_keyA))) {
					return parseFloat(s_keyA) > parseFloat(s_keyB);
				}
				else {

					s_keyA = a[s_displayKey];
					s_keyB = b[s_displayKey];

					return s_keyA.toLowerCase() > s_keyB.toLowerCase();
				}
			});
		}

		var i_store = Ext.create("Ext.data.Store", {
			fields: [
				{ name: s_valueKey, type: 'auto' },
				{ name: s_displayKey, type: 'auto' }
			],
			proxy: {
				type: 'memory'
			}
		});

		i_store.loadData(a_data);

		return i_store;
	}
});
