// ---
var currentItem = false;
var customOnFocus;
var customOnBlur;

// --- shortcut config
	
var globalKeys = new Hash({});

var itemKeys = new Hash ({
	
	previousResult : new Hash ({
		name: lang['previous'],
		code: 37,
		method: browseResults.pass(-1)
	}),
	
	nextResult : new Hash ({
		name: lang['next'],
		code: 39,
		method: browseResults.pass(1)
	}),
	
	selectAttachments : new Hash ({
		name: lang['down'],
		code: 40,
		method: selectLevel.pass('attachments')
	}),
	
	previousAttachment : new Hash ({
		name: lang['previous'],
		code: 37,
		method: browseAttachments.pass(-1)
	}),
	
	selectResults : new Hash ({
		name: lang['up'],
		code: 38,
		method: selectLevel.pass('results')
	}),
	
	nextAttachment : new Hash ({
		name: lang['next'],
		code: 39,
		method: browseAttachments.pass(1)
	})
	
});

var classShortcuts = new Hash ({
    'attachment' : new Array(
		'previousAttachment',
    	'nextAttachment',
    	'selectResults'
    )    
});

//---


var currentShortcuts = new Hash();

function keyInput(e) {
	
	var performMethods = new Array;
	
	// --- check global keys
	if (!currentShortcuts)
		return;

	currentShortcuts.each(function(keyProps, name){
		var c = String.fromCharCode(e.code).toLowerCase();
		if (((keyProps.key && keyProps.key.toLowerCase() == c) || (keyProps.code && keyProps.code == e.code)) && $chk(keyProps.shift) === e.shift) {
			performMethods.include(currentShortcuts[name].method);
		}		
	});

	if (performMethods.length > 0) {
		performMethods.each(function(item){item();});
		e.stop();
	}
	
}


Element.implement({
    setFocus: function(){
    	trace('> setFocus');

		if (currentItem)
			currentItem.releaseFocus();
		
    	
        this.addClass('focus');
        
        currentItem = this;
               
        if(!currentItem.retrieve('itemKeys')) {
        	 	
        	var currentItemKeys = new Hash();
        	
	        // - get item shortcuts
	        var c = currentItem.get('class').split(' ');
			c.each(function(className, index){
				
				if (className.contains('key:')) {
					//  custom shortcut
					var name = className.split(':')[1];
					trace('  - item key: ' + name);

					if (itemKeys.has(name))
						currentItemKeys.include(name, itemKeys.get(name));
					else
						trace('--- ERROR ' + name + ' not found in itemKeys');

				} else if ($type(classShortcuts.get(className)) == 'array') {
					// class shortcut
					classShortcuts.get(className).each(function(name) {
						currentItemKeys.include(name, itemKeys.get(name));
						trace('  - class item key: ' + name);
					});
					
				}
				
				
			});
	        
			currentItem.store('itemKeys', currentItemKeys);
			
        }
        
        setShortcuts();

		this.fireEvent('focus');
        if (typeof this.onfocus == 'function')
			this.onfocus();

		if (typeof customOnFocus == 'function') {
			customOnFocus();
			customOnFocus = null;
		}
    },
    
    releaseFocus: function () {

		this.fireEvent('blur');
		if (typeof this.onblur == 'function')
			this.onblur();

		if (typeof customOnBlur == 'function') {
			customOnBlur();
			customOnBlur = null;
		}

		this.removeClass('focus');
		currentItem = false;	
		
		resetShortcuts();
		
	}
	
});


var shorcutsEnabled = false;
function setShortcuts() {
	
	if (!shorcutsEnabled)
		return;
	
	resetShortcuts();
	
	if (!currentItem)
		return;
		
	var currentItemKeys = currentItem.retrieve('itemKeys');
	if (currentItemKeys)
		currentShortcuts.combine(currentItemKeys);

	displayShortcuts();
}


function resetShortcuts() {
	
	currentShortcuts.empty();
	currentShortcuts.combine(globalKeys);
	
	displayShortcuts();	
}

function loadShortcuts() {

	if (shorcutsEnabled)
		return;

	shorcutsEnabled = true;
	document.addEvent('keydown', keyInput);

}

function unloadShortcuts() {

	shorcutsEnabled = false;
	document.removeEvent('keydown', keyInput);

}

function addItemShortcut(key) {
	if (!key || !itemKeys.has(key))
		return;

	currentShortcuts.include(key, itemKeys.get(key));
	displayShortcuts();
}

function removeItemShortcut(key) {
	if (!key || !currentShortcuts.has(key))
		return;

	currentShortcuts.erase(key);
	displayShortcuts();
}

function displayShortcuts() {

	trace()

	if (!currentItem)
		return;
	
	var currentItemKeys = currentItem.retrieve('itemKeys');
	if (!currentItemKeys) return;
	
	var shortcutList = $('shortcuts');
	shortcutList.empty();
		
	//var s = "";
	currentItemKeys.each(function(keyProps, name){
		//s += name + "\ncode: " + keyProps.code  + "\nkey: " + keyProps.key + "\n____________\n";
		
		var keyChar;
		
		if (keyProps.key)
			keyChar = keyProps.key;
		else if (keyProps.code)
			keyChar = getCodeChar(keyProps.code);
		else
			return;

		var buttonClass = (keyChar.length > 1 && keyChar.substr(0, 2) != '&#') ? 'wide button' : 'button';
		var h = '<div class="' + buttonClass + '">' + keyChar + '</div>' + keyProps.name;
		var listItem = new Element('li', {'html': h});
		//var keyImg = new Element('div', {'html': keyChar, 'class': 'button'})
		//listItem.adopt(keyImg);
		
		shortcutList.adopt(listItem);
		
	});
		
}

function getCodeChar(code) {
	
	switch (code) {

		case 13:
			return "enter";

		case 37:
			return "&#8592;";
			
		case 38:
			return "&#8593;";
			
		case 39:
			return "&#8594;";
		
		case 40:
			return "&#8595;";
		
	}
	
	return "";
}

