Ext.define('Ext.ux.util.KeyMapping', {

	initKeyMapping: function () {

		var i_cmp = this;

		i_cmp.on('afterrender', function () {

			var i_view = i_cmp.getView && i_cmp.getView() ? i_cmp.getView() : null;

			if (i_view) {
				i_view.on('itemkeydown', function itemkeydown(i_self, i_record, o_item, n_index, e, eOpts) {
					
					var s_key = e.browserEvent.keyIdentifier,
						s_method;

					s_key = i_cmp._normalizeKeyMapping(s_key);
					
					s_method = ['on', s_key, 'Key'].join(''); // Deprecate this!
					i_cmp.fireEvent(s_method, i_cmp, i_record, o_item, n_index, e, eOpts); // Deprecate this!
					i_cmp[s_method] && i_cmp[s_method].apply(i_cmp, arguments); // Deprecate this!

					s_method = [s_key, 'Key'].join('');
					i_cmp.fireEvent(s_method, i_cmp, i_record, o_item, n_index, e, eOpts);
					i_cmp[s_method] && i_cmp[s_method].apply(i_cmp, arguments);
				});
			}
			else {
				i_cmp.getEl().on('keydown', function keydown(e, i_htmlNode, eOpts) {
					
					var s_key = e.browserEvent.keyIdentifier,
						s_method;

					s_key = i_cmp._normalizeKeyMapping(s_key);
					
					s_method = ['on', s_key, 'Key'].join(''); // Deprecate this!
					i_cmp[s_method] && i_cmp[s_method].apply(i_cmp, arguments); // Deprecate this!
					// Deprecate this!
					if (i_cmp.fireEvent(s_method, i_cmp, e, i_htmlNode, eOpts) === false) {
						return false;
					}

					s_method = [s_key, 'Key'].join('');
					i_cmp[s_method] && i_cmp[s_method].apply(i_cmp, arguments);
					if (i_cmp.fireEvent(s_method, i_cmp, e, i_htmlNode, eOpts) === false) {
						return false;
					}
				});
			}
		}, i_cmp, { single: true });

		return i_cmp;
	},
	_normalizeKeyMapping: function (s_key) {

		switch (s_key) {
			case 'U+007F':
				s_key = 'Delete';
			break;

			case 'U+004B':
				s_key = 'Plus';
			break;
			
			case 'U+004D':
				s_key = 'Minus';
			break;

			case 'U+0009':
				s_key = 'Tab';
			break;

			default:
			break;
		}

		return s_key;
	}

});
