/**
 * @version		1.0.0 - 2 februari 2010
 * 
 * @copyright	1.0.0,	2 februari 2010,	Jan Niemantsverdriet,	Gemaakt
 */

var oKeys_ParentFunction = null;
var oKeys_PreviousBindings = new Object;

/**
 * Verbind een toets met een functie
 * 
 * @param string a_sKey				de code van de toets
 * @param string a_hFunction		de functie
 * @since 1.0.0 - 2 februari 2010
 * @author Jan Niemantsverdriet
 */
function keys_vBind(a_sKey, a_hFunction) {
	if (bIsNull(oKeys_ParentFunction) && document.hasOwnProperty("onkeyup") && document.onkeyup != keys_vProcessKey) {
		oKeys_ParentFunction = document.onkeyup;
	}
	if (!oKeys_PreviousBindings.hasOwnProperty(a_sKey)) oKeys_PreviousBindings[a_sKey] = new Array();
	oKeys_PreviousBindings[a_sKey].push(a_hFunction);
	document.onkeypress = new function(a) { return false; };
	document.onkeydown = new function(a) { return false; };
	document.onkeyup = keys_vProcessKey;
}

/**
 * Verwerkt de aanslag van een toets
 * 
 * @param Object a_oEvent			het event van het aanslaan van een toets
 * @since 1.0.0 - 2 februari 2010
 * @author Jan Niemantsverdriet
 */
function keys_vProcessKey(a_oEvent) {
	var sCode = a_oEvent.keyCode;
	if (oKeys_PreviousBindings.hasOwnProperty(sCode)) {
		if (oKeys_PreviousBindings[sCode].length > 0) {
			var hFunction = oKeys_PreviousBindings[sCode][oKeys_PreviousBindings[sCode].length - 1];
			return hFunction(a_oEvent);
		}
	}
	if (bIsFunction(oKeys_ParentFunction)) {
		return oKeys_ParentFunction(a_oEvent);
	}
}

/**
 * Haal de laatst ingestelde binding van de toets met de functie weg
 * 
 * @param string a_sKey				de code van de toets
 * @since 1.0.0 - 2 februari 2010
 * @author Jan Niemantsverdriet
 */
function keys_vUnbind(a_sKey) {
	if (!bIsDefined(oKeys_PreviousBindings[a_sKey])) return;
	oKeys_PreviousBindings[a_sKey].pop();
}