$(document).ready(function(){ setup();
	$("#gameoflife").click(function(e) {
		var offsetX = $(this).offset().left;
		var offsetY = $(this).offset().top;
		var x = Math.round(e.pageX - offsetX);
		var y = Math.round(e.pageY - offsetY);
		clicked(x,y);
	});

	$("#gameoflife").mouseover(function(e) {
		paused = true;
		$(this).addClass('paused');
		$("#pause").addClass('active');
	});

	$("#gameoflife").mouseout(function(e) {
		if (button_paused === false) {
			paused = false;
			$(this).removeClass('paused');
			$("#pause").removeClass('active');
		}
	});
	
	$("#pause").click(function(e) {
		if (paused === false) {
			paused = true;
			button_paused = true;
			$(this).parent().addClass('active');
			$("#gameoflife").addClass('paused');
		} else {
			paused = false;
			button_paused = false;
			$(this).parent().removeClass('active');
			$("#gameoflife").removeClass('paused');
		}
	});

	$("#clear").click(function(){ reset(); draw(); });
});

var canvas;
var ctx;

var button_paused = false;

map = new Object;

var cell_width = 8;
var cell_padding = 1;

loopInterval = 1000;

paused = false;
wrap = true;

cell_colours = new Array();
cell_colours[0] = '#f7f7f7';
cell_colours[1] = '#000';

function draw() {
	canvas.width = canvas.width;//clear canvas
	ctx.fillStyle = '#b6ca5b';

	for (x = 0; x < map.width; x++) {
		for (y = 0; y < map.height; y++) {
			if (map.map[x][y] == 1) {
				ctx.fillStyle = cell_colours[1];
			} else {
				ctx.fillStyle = cell_colours[0];
			}
			draw_cell(x, y);
		}
	}
}

function draw_cell(x, y) {
	//ctx.fillRect(x * cell_width + x * cell_padding, y * cell_width + y * cell_padding, cell_width, cell_width);
	ctx.beginPath();
	ctx.arc(x * cell_width + x * cell_padding + (cell_width / 2), y * cell_width + y * cell_padding + (cell_width / 2),  cell_width / 2, 0, Math.PI*2, true);
	ctx.closePath();
	ctx.fill();
}

function clicked(screen_x, screen_y) {
	var x = Math.floor(screen_x / (cell_width + cell_padding));
	var y = Math.floor(screen_y / (cell_width + cell_padding));

	ctx.fillStyle = cell_colours[step_cell(x, y)];
	draw_cell(x, y);
}

function step_cell(x, y) {
	if (map.map[x][y] == 1) {
		map.map[x][y] = 0;
		return 0;
	} else {
		map.map[x][y] = 1;
		return 1;
	}
}

function setup() {
	canvas = document.getElementById('gameoflife');

	map.width = Math.floor(canvas.width / (cell_width + cell_padding) + cell_padding);
	map.height = Math.floor(canvas.height / (cell_width + cell_padding) + cell_padding);

	if (!canvas.getContext) return;
	ctx = canvas.getContext("2d");

	reset();
	place_random();

	draw();

	setInterval (function(){step();}, loopInterval);

}

function step() {
	if (paused == false) {
		update();
		draw();
	}
}

function reset() {
	map.map = new Array();
	map.nextmap = new Array();
	for (x = 0; x < map.width; x++) {
		map.map[x] = new Array();
		map.nextmap[x] = new Array();
		for (y = 0; y < map.height; y++) {
			map.map[x][y] = 0;
			map.nextmap[x][y] = 0;
		}
	}
}

function place_random() {
//I lied this isn't going to be random
var sweet_shape = new Array();
sweet_shape.push(new Array(-3, 0));
sweet_shape.push(new Array(-2, 0));
sweet_shape.push(new Array(-1, 0));
sweet_shape.push(new Array(0, 0));
sweet_shape.push(new Array(0, +1));
sweet_shape.push(new Array(0, -2));
sweet_shape.push(new Array(3, 0));
sweet_shape.push(new Array(2, 0));
sweet_shape.push(new Array(1, 0));
sweet_shape.push(new Array(-3, -1));
sweet_shape.push(new Array(3, -1));
sweet_shape.push(new Array(-2, -2));
sweet_shape.push(new Array(2, -2));
sweet_shape.push(new Array(0, 0));

var center_x = Math.floor(map.width / 2);
var center_y = Math.floor(map.height / 2);

for (i = 0; i < sweet_shape.length; i++) {
	try {
		map.map[center_x + sweet_shape[i][0]][center_y + sweet_shape[i][1]] = 1
	} catch(err){/* don't care */}
}
}

function neighbours(x, y) {
	var n = 0;

	var neighbour_locations = new Array();
	neighbour_locations[0] = new Array(-1, -1);
	neighbour_locations[1] = new Array(0, -1);
	neighbour_locations[2] = new Array(1, -1);
	neighbour_locations[3] = new Array(-1, 0);
	neighbour_locations[4] = new Array(1, 0);
	neighbour_locations[5] = new Array(-1, 1);
	neighbour_locations[6] = new Array(0, 1);
	neighbour_locations[7] = new Array(1, 1);

	for (i = 0; i < neighbour_locations.length; i++) {
		try {
			var cur_x = x + neighbour_locations[i][0];
			var cur_y = y + neighbour_locations[i][1];
			if (wrap = true) {
				if (cur_x < 0) {
					cur_x = map.width;
				} else if (cur_x > map.width) {
					cur_x = 0;
				}
				if (cur_y < 0) {
					cur_y = map.height;
				} else if (cur_y > map.height) {
					cur_y = 0;
				}
			}
			if (map.map[cur_x][cur_y] == 1) {
				n++;
			}
		} catch(err){/* don't care */}
	}
	return n;
}

function update() {
	for (x = 0; x < map.width; x++) {
		for (y = 0; y < map.height; y++) {
			n = neighbours(x, y);

			if (map.map[x][y] == 0) {
				if (n == 3) {
					map.nextmap[x][y] = 1;
				} else {
					map.nextmap[x][y] = 0;
				}
			} else if (map.map[x][y] == 1) {
				if (n < 2) {
					map.nextmap[x][y] = 0;
				} else if (n > 3) {
					map.nextmap[x][y] = 0;
				} else {
					map.nextmap[x][y] = 1;
				}
			}
		}
	}
	for (x = 0; x < map.width; x++) {
		for (y = 0; y < map.height; y++) {
			map.map[x][y] = map.nextmap[x][y];
		}
	}
}