KeyHandler = new Class.create();

KeyHandler.prototype = {
	handlers: [],
	initialize: function(element,displayElement) {
		this.element = $(element);
		this.displayElement = $(displayElement);
		Event.observe(this.element, "keydown", this.onKeyDown.bindAsEventListener(this));
	},
		
	addHandler: function(handler) {
		// actions = { keys: Array(), active: true, shiftKeyEnabled: false, ctrlKeyEnabled: false, stopEvent: false, action: someMethod, updateDisplay: true }
		if (!handler.shiftKey) handler.shiftKey = false;
		if (!handler.ctrlKey) handler.ctrlKey = false;
		if (!handler.altKey) handler.altKey = false;
		if (!handler.active) handler.active = false;
		this.handlers.push(handler);
		
		return this.handlers.length - 1;
	},
	
	activateHandler: function(index) {
		this.handlers[index].active = true;
	},
	
	deactivateHandler: function(index) {
		this.handlers[index].active = false;
	},
	
	onKeyDown: function(event) {
		// handle events
		for (i=0;i<this.handlers.length;i++) {
			handler = this.handlers[i];
			if (handler.active) {
				if (event.shiftKey == handler.shiftKey && event.ctrlKey == handler.ctrlKey && event.altKey == handler.altKey) {
					if (handler.keys.indexOf(event.keyCode) >= 0) {
						if (handler.stopEvent) Event.stop(event);
						handler.action(event,this);
					}
				}
			}
		}
	}

}

Array.prototype.createRange = function(min,max) {
	for (i=min; i<=max; i++) {
		this.push(i);
	}
	return this;
}

Number.prototype.msToMinutes = function() {
	var seconds = this/1000;
	var minutes = Math.floor(seconds/60);
	var extraZero = "";
	
	seconds = Math.round(seconds - minutes*60);
	if (seconds < 10) extraZero = "0";
	
	return minutes + ":" + extraZero + seconds;
}



	
	
	