var FATTY_WIDTH = 126;
var FATTY_HEIGHT = 140;
var ENEMY_WIDTH = 32;
var ENEMY_HEIGHT = 32;
var UPDATE_INTERVAL = 30;
var width = 800;
var height = 600;

function resize() {
	var w = window.innerWidth;
	var h = window.innerHeight;
	var arena = document.getElementById('arena');
	arena.style.left = (w - width) / 2 + 'px';
	arena.style.top = (h - height) / 2 + 'px';
}

window.onresize = resize;

function start() {
	resize();
	name = prompt("Oh no!\n\nWhile out climbing, FatClimber encounters a deadly butterfly!\n"
			+ "You must help FatClimber to escape it for as long time as possible.\n"
			+ "\n"
			+ "Move FatClimber with the arrow keys.\n"
			+ "\n"
			+ "Enter your name so that FatClimber knows who to thank:", "");

	var arena = document.getElementById('arena');

	fatty = document.createElement('img');
	fatty.setAttribute('src', 'fatty.png');
	fatty.setAttribute('width', FATTY_WIDTH);
	fatty.setAttribute('height', FATTY_HEIGHT);

	butterfly = document.createElement('img');
	butterfly.setAttribute('src', 'butterfly.png');
	butterfly.setAttribute('width', ENEMY_WIDTH);
	butterfly.setAttribute('height', ENEMY_HEIGHT);

	counter = 0;

	xButterfly = 350;
	yButterfly = 550;
	xFatty = 110;
	yFatty = 310;

	xVelocityButterfly = 1.0;
	yVelocityButterfly = 0.0;
	xVelocityFatty = 1.0;
	yVelocityFatty = 0.0;

	butterfly.style.top  = yButterfly + "px";
	butterfly.style.left = xButterfly + "px";
	fatty.style.top  = yFatty + "px";
	fatty.style.left = xFatty + "px";

	arena.appendChild(fatty);
	arena.appendChild(butterfly);

	document.addEventListener('keydown', keyPressed, false);
	gameLoop = setInterval("gameCycle()", UPDATE_INTERVAL);
}

function gameCycle() {
	if (++counter % 40 == 0) {
		var newSpeed = Math.min(counter / 189, 10.0);
		var rand = Math.random();
		if (rand < 0.15) {
			xVelocityButterfly = newSpeed;
		} else if (rand < 0.30) {
			xVelocityButterfly = -newSpeed;
		} else if (rand < 0.45) {
			yVelocityButterfly = newSpeed;
		} else if (rand < 0.60) {
			yVelocityButterfly = -newSpeed;
		}
	}

	if ((xButterfly + ENEMY_WIDTH >= width && xVelocityButterfly > 0) || (xButterfly < 0 && xVelocityButterfly < 0))
		xVelocityButterfly = -xVelocityButterfly;
	if ((yButterfly + ENEMY_HEIGHT >= height && yVelocityButterfly > 0) || (yButterfly < 0 && yVelocityButterfly < 0))
		yVelocityButterfly = -yVelocityButterfly;
	if ((xFatty + FATTY_WIDTH >= width && xVelocityFatty > 0) || (xFatty < 0 && xVelocityFatty < 0))
		xVelocityFatty = -xVelocityFatty;
	if ((yFatty + FATTY_HEIGHT >= height && yVelocityFatty > 0) || (yFatty < 0 && yVelocityFatty < 0))
		yVelocityFatty = -yVelocityFatty;

	xButterfly += xVelocityButterfly;
	yButterfly += yVelocityButterfly;
	xFatty += xVelocityFatty;
	yFatty += yVelocityFatty;

	butterfly.style.top  = yButterfly + "px";
	butterfly.style.left = xButterfly + "px";
	fatty.style.top  = yFatty + "px";
	fatty.style.left = xFatty + "px";

	if ((xFatty - xButterfly < ENEMY_WIDTH && xFatty - xButterfly > -FATTY_WIDTH) && (yFatty - yButterfly < ENEMY_HEIGHT && yFatty - yButterfly > - FATTY_HEIGHT)) {
		clearInterval(gameLoop);
		gameOver();
	}
}


function keyPressed(e) {
	switch (e.keyCode) {
		case 87: case 119: case 38: yVelocityFatty--; break;
		case 68: case 100: case 39: xVelocityFatty++; break;
		case 83: case 115: case 40: yVelocityFatty++; break;
		case 65: case 97: case 37:  xVelocityFatty--; break;
		default: break;
	}
	return true;
}

function gameOver() {
	var body = document.getElementById("body");

	var form = document.createElement("form");
	form.setAttribute("method", "post");
	form.setAttribute("action", "/games/highscore/?game=fatty");
	form.setAttribute("style", "display: none;");
	body.appendChild(form);

	var input = document.createElement("input");
	input.setAttribute("type", "text");
	input.setAttribute("name", "name");
	input.setAttribute("value", name);
	form.appendChild(input);

	var scoreInput = document.createElement("input");
	scoreInput.setAttribute("type", "text");
	scoreInput.setAttribute("name", "points");
	scoreInput.setAttribute("value", counter * UPDATE_INTERVAL);
	form.appendChild(scoreInput);

	form.submit();
}

