$(function(){
	var aw = $('#accessibility').get(0).offsetWidth;
	var accesskeySecuence = [];
	var accesskeyTimer    = null;
	var acccesskeys = {
		48  : "0",
		49  : "1",
		50  : "2",
		51  : "3",
		52  : "4",
		53  : "5",
		54  : "6",
		55  : "7",
		56  : "8",
		57  : "9",
		//37  : "n",
		78  : "n",
		//39  : "p",
		80  : "p",
		38  : "H",
		72  : "H"
	};
	
	var especialAccesskeys = {
		96  : 48,
		97  : 49,
		98  : 50,
		99  : 51,
		100 : 52,
		101 : 53,
		102 : 54,
		103 : 55,
		104 : 56,
		105 : 57
	};
	
	$('#accessibility ul').css("width","95px");
	$('#accessibility kbd span').remove();
	$('#accessibility .swlist').css("display", "inline");
	$('#accessibility').data("aw",aw);
	$('#accessibility a.switch')
		.css("outline",0)
		.click(function(){
			var t = this;
			if ( $(this).hasClass("close") ) {
				$('#accessibility .swlist').animate({ right:147+'px' },500,"easeInCubic",function(){
					$(t).text(">");	
				});
				setTimeout(function(){
					$('#accessibility').animate({ right:'0px' },400,"easeInCubic");
				},200);
				
				$(this).removeClass("close");
			}
			else {
				$('#accessibility').animate({ right:(-aw)+'px' },500,"easeInCubic");
				setTimeout(function(){
					$('#accessibility .swlist').animate({ right:(1)+'px' },400,"easeOutCubic");
					$(t).text("<");
				},400);
				
				$(this).addClass("close");
			}
			return false;
		});
	
	$(document.documentElement).keyup(function (e) {
		var k = e.keyCode;
		accesskeySecuence.push(k);
		clearInterval(accesskeyTimer);
		accesskeyTimer = setTimeout(function(){
			goTo(k);
		},300);
	});
	function goTo() {
		var chr = "";
		var href = null;
		
		$.each(accesskeySecuence, function(i) {
			if ( especialAccesskeys[accesskeySecuence[i]] ) {
				chr += String.fromCharCode(especialAccesskeys[accesskeySecuence[i]]);
			}
			else if ( acccesskeys[accesskeySecuence[i]] ){
				chr += acccesskeys[accesskeySecuence[i]];
			}
		});
		
		if (chr !== "") {
			href = $('a[accesskey=' + chr + ']').attr("href");
			if (typeof href == "string") {
				if (href.indexOf("#") > -1) {
					$('a[accesskey=' + chr + ']').click();
				}
				else {
					document.location.href = href;
				}
			}
		}
		accesskeySecuence = [];
	}
});
