/**
 *
 * KeyControl functionality wrapped in a function
 * to allow private variables and the creation of a singleton class
 *
 * TODO display shortcuts
 *
 */
(function() {

// private vars


// singleton instance
var instance 		= null;

this.blKeyControl = new Class({

	Extends: Keyboard,

	Implements: [Options, blInterfaceEvents],

	events: {
	},

	options: {
	},

	initialize: function(options, events) {
		if (instance)
			return instance;

		this.parent(options);
		//this.enableLog();

		this.setEvents(events);
		this.setOptions(options);

		this.getEventManager().addEvent(this.getEvents().FOCUS_SET, this.parseElements.bind(this));
		this.getEventManager().addEvent(this.getEvents().FOCUS_RELEASED, this.resetShortcuts.bind(this));

		alert('keyboard : ');
		alert(this);

		instance = this;
		return instance;

	},

	parseElements: function(el) {
        alert('> parseShortcuts: ');
        alert(el);
        alert(typeOf(el));

		if (typeOf(el) != 'elements' && (!(el = $(el)) || !(el = Array.from(el))))
			return;

		el.each(function(el) {
			alert('  - ' + el.get('tag') + ' #' + el.get('id') + ' ' + el.get('class'));

			el.getChildren('a[accesskey]').each(this.parseShortcut, this);
			this.parseElements(el.getChildren('div:not([class*="level"])'));

		}, this);

	},

	parseShortcut: function(el) {
		alert('   - shortcuts: ');
		alert(el);
		var key = el.get('accesskey');
		if (!key || key == "")
			return false;
		
		// get method
		var handler = function (event) {
			event.stop();
			this.onclick();
		};

		if (typeOf(handler) != "function")
			return false;

		var title = el.getProperty('title');
		handler = handler.bind(el);

		this.addShortcut(title, {
			'keys' : key,
			'description' : title,
			'handler' : handler
		});
		return true;
	},

	resetShortcuts : function () {
		alert('> resetShortcuts');

		if (!this.shortcuts && !this.shortcutIndex)
			return;

		this.shortcuts.empty();
		this.shortcutIndex = {};
		this.$events = {};
	}

});


})();
