var Shortcut = new Class
(
	{
		aList: new Array(),

		initialize: function()
		{
			oElements = $$('[accesskey]');
			oElements.each(this.indexAccessKey, this);

			if (this.aList.length > 0)
			{
				window.addEvent('keydown', this.fireKeyEvent.create({'bind' : this}));
			}
		},

		indexAccessKey: function(oElement)
		{
			sAccessKey = oElement.get('accesskey').toLowerCase();
			aKeys = sAccessKey.split('|');

			for (i = 0; i < aKeys.length; i++)
			{
				iOperatorKey	= this.getOperatorKey(aKeys[i]);
				sKey			= this.getKey(aKeys[i]);
				this.addKeyEvent(iOperatorKey, sKey, oElement);
			}
		},

		getOperatorKey: function(mInput)
		{
			var iOperatorKey = 0;
			if (mInput instanceof Event)
			{
				oEvent = mInput;
				if (oEvent.shift)	iOperatorKey += Math.pow(2, 0);
				if (oEvent.control)	iOperatorKey += Math.pow(2, 1);
				if (oEvent.alt) 	iOperatorKey += Math.pow(2, 2);
				if (oEvent.meta)	iOperatorKey += Math.pow(2, 3);
			}
			else
			{
				sAccessKey = mInput;
				sAccessKey = sAccessKey.replace('ctrl', 'control');

				if(sAccessKey.contains('shift'))	iOperatorKey += Math.pow(2, 0);
				if(sAccessKey.contains('control'))	iOperatorKey += Math.pow(2, 1);
				if(sAccessKey.contains('alt'))		iOperatorKey += Math.pow(2, 2);
				if(sAccessKey.contains('meta'))		iOperatorKey += Math.pow(2, 3);
			}
			return iOperatorKey;
		},

		getKey: function(mInput)
		{
			var sKey = '';
			if (mInput instanceof Event)
			{
				oEvent = mInput;
				sKey = oEvent.key;
			}
			else
			{
				sAccessKey = mInput;
				if(sAccessKey == '+' || sAccessKey.contains('++'))
					sKey = '+';
				else
					sKey = sAccessKey.split('+').getLast();
			}
			return sKey;
		},

		addKeyEvent :function(iOperatorKey, sKey, oEvent)
		{
			if (this.aList[iOperatorKey] == undefined) this.aList[iOperatorKey] = new Array();
			this.aList[iOperatorKey][sKey] = oEvent;
		},

		fireKeyEvent: function(oEvent)
		{
			iOperatorKey = this.getOperatorKey(oEvent);
			sKey = this.getKey(oEvent);

			if (this.aList[iOperatorKey] == undefined) return true;
			if (this.aList[iOperatorKey][sKey] == undefined) return true;

			oEvent.stop();

			oElement = this.aList[iOperatorKey][sKey];

			oEvent.target = oElement;

			oElement.fireEvent('click', oEvent);

			sLink = oElement.get('href');

			if (sLink != undefined && sLink != '#')
			{
				document.location = sLink;
			}

			return false;

		}
	}
);

shortcutInit = function()
{
	shortcutObj = new Shortcut();
}

var shortcutInit;

window.addEvent('domready', shortcutInit);
