var hotKeyWaiting = false;
var hotKeyLinks = new Array;
var hotKeyTimeout = -1;
var hotKeyTimeOut;

function hotKeyClear() {
	hotKeyWaiting = false;
}

document.addEventListener('keypress', function (e) {
	target = e.target.nodeName.toUpperCase();
	if (target == 'TEXTAREA' || target == 'SELECT' || target == 'INPUT')
		return;
	key = (e.which) ? e.which : e.keyCode;
	if (key < 97) key += 32;
	if (hotKeyWaiting) {
		if (hotKeyLinks[key]) {
			document.location = hotKeyLinks[key];
		}
		if (hotKeyTimeOut) {
			clearTimeout(hotKeyTimeOut);
		}
		hotKeyWaiting = false;
	} else if(key == 103) {
		// Got our g
		hotKeyWaiting = true;
		if(hotKeyTimeout > 0) {
			hotKeyTimeOut = setTimeout(hotKeyClear, hotKeyTimeout * 1000);
		}
	} else if(key == 101) {
		// Got our e
		if(hotKeyPageID > 0) {
			// Stolen from SilverStripeNavigator.js
			window.open('admin/show/' + hotKeyPageID, windowName('cms')).focus();
		}
	}
}, true)

if(typeof windowName == 'undefined') {
	function windowName(suffix) {
	    var base = document.getElementsByTagName('base')[0].href.replace('http://','').replace(/\//g,'_').replace(/\./g,'_');
	    return base + suffix;
	}
}

