/*!\brief	Keypress class that allows registration of CustomEvents to
 *		specified key codes.  This class can be overloaded specifying
 *		the _keypress() method for a non key code specific handler
 */
BS.com.Keypress = function ()
{
	_bsGlobalEvt.addCB(this.E_TYPE, this._keypressCB, this);

	this.keyA = [];
};

BS.com.Keypress.prototype = {
	// because of the safari 3.1 change that made keypress no longer work
	// we're going with keyup to prevent the continuous firing of keydown
	E_TYPE:			'keyup',

	_delete: function ()
	{
		_bsGlobalEvt.delCB(this.E_TYPE, this._keypressCB, this);
		this.ce = this.keyA = null;
	},

	_keypress: function (e)
	{
		var		code = _bsEvt.getKeyCode(e);
		var		r = true;

		if (this.ce) {
			this.ce.exec(e);
			return false;
		}

		for (var i=0, l=this.keyA.length; i<l; i++) {
			if (this.keyA[i][0] != code) continue;
			this.keyA[i][1].exec(e);
			r = false;
		};
		
		return r;
	},

	_keypressCB: function (type, data, args)
	{
		var		e = args[0];
		var		obj = _bsEvt.getTarget(e);
		
		// return if any modifier keys present
		if (e.metaKey || e.altKey || e.ctrlKey)
			return;

		// don't fire if target is a form element
		switch (obj.nodeName) {
		case 'INPUT':
			if (obj.getAttribute('type') == 'button')
				break;
		case 'TEXTAREA':
		case 'SELECT':
			return true;
		}

		/*
		if (obj.nodeName != 'BODY' && obj.nodeName != 'HTML')
			return true;
		*/
		return this._keypress(e);
	},

	/*!\brief	Add key code and CustomEvent to handler array.
	 * \param	code		key code
	 * \param	ce		CustomEvent
	 */
	add: function (code, ce) { this.keyA.push([code, ce]); },

	addGlobal: function (ce) { this.ce = ce; }
};

