var THE_CODE = "UUDDLRLRBA"; // The splendiferous Konami Code

var codeString = "";
var countingDown = false;

function pressKey(e) {
	var keyCode;
	keyCode = e ? e.which : window.event.keyCode;
	
	if(codeString.length == 0) {
		setTimeout('codeString = "";', 2000);
	}
	
	switch(keyCode) {
	case 38: // Up
		codeString += "U";
		break;
	case 40: // Down
		codeString += "D";
		break;
	case 37: // Left
		codeString += "L";
		break;
	case 39: // Right
		codeString += "R";
		break;
	case 66: // B
		codeString += "B";
		break;
	case 65: // A
		codeString += "A";
		break;
	}
	if(codeString == THE_CODE) {
		makeDonkey();
		codeString = "";
	}
}

function getSize() {
	 var viewportwidth;
	 var viewportheight;
	 
	 // the more standards compliant browsers (mozilla/netscape/opera/IE7) use window.innerWidth and window.innerHeight
	 
	 if (typeof window.innerWidth != 'undefined')
	 {
	      viewportwidth = window.innerWidth,
	      viewportheight = window.innerHeight
	 }
	 
	// IE6 in standards compliant mode (i.e. with a valid doctype as the first line in the document)

	 else if (typeof document.documentElement != 'undefined'
	     && typeof document.documentElement.clientWidth !=
	     'undefined' && document.documentElement.clientWidth != 0)
	 {
	       viewportwidth = document.documentElement.clientWidth,
	       viewportheight = document.documentElement.clientHeight
	 }
	 
	 // older versions of IE
	 
	 else
	 {
	       viewportwidth = document.getElementsByTagName('body')[0].clientWidth,
	       viewportheight = document.getElementsByTagName('body')[0].clientHeight
	 }
	 
	 return [viewportwidth, viewportheight];
}

function makeDonkey() {
	// Make a donkey and place it randomly
	var windowSize = getSize();
	var donkey = document.createElement("img");
	donkey.src = "/sys/anes-11.gif";
	donkey.style.position = "absolute";
	donkey.style.top = Math.floor(Math.random() * (windowSize[1])) + "px";
	donkey.style.left = Math.floor(Math.random() * (windowSize[0])) + "px";
	document.body.appendChild(donkey);
	
	// Make another donkey within the next second
	setTimeout("makeDonkey()", 500 +  Math.floor(Math.random() * 500));
}

document.onkeydown = pressKey;
