var badThings = new Array("You kicked a puppy.",
"You paid for $1.79 item with a $20 bill.\nYou're not the only person they have to serve today, you know!",
"You laughed at a raunchy joke.\n\nAnd then you retold the joke as your own.",
"You used American spelling.",
"You were late to church.",
"You yelled at a small child.",
"You forgot the thank-you card.",
"You committed genocide.",
"Remember the trash? That you said you'd take out?",
"You ate the whole thing.\n The WHOLE thing.",
"I know, I know. It was only a little white one. \n\nLiar.",
"Sarcastic? You? You don't know the meaning of the word!\n\n!",
"You ogled. She has a personality, you know.",
"You didn't call your mother. Your mother!",
"You didn't do a good enough job.",
"You gave in to the world today.",
"You worked on a Sunday.",
"You built a Doomsday machine.",
"You recommended 'Deliverance' to a friend.\n\nAn EX-friend.",
"You coughed up the puck in the last minute of the season opener, allowing your bitter divisonal rivals to score.",
"You firebombed the embassy.",
"You cheated on your taxes.",
"You taught your niece to be afraid of spoons.",
"You said it looked good on her.",
"You said she could stand to lose a few pounds.",
"You huffed a kitten.",
"You killed a man.",
"You made her cry.",
"You didn't say please.",
"You said something stupid.",
"You ran a red light.",
"You got caught speeding.",
"I know, I know. It felt so good...\n\nRight until you realized...",
"You farted.",
"You trimmed the corners of your beard.",
"You didn't take up your cross daily.",
"You had little faith.",
"You dug a hole to China and now the neighbor's cat is missing.",
"The gift you gave was half the price of theirs.",
"You never got back to them.",
"I know, everyone else was doing it. \nOf course, we can't judge *them*, because only God sees the heart. \nBut you, you now...",
"You said you knew you shouldn't say it... and then you said it.",
"You swore. D***!",
"You're not racist.\n You have nothing against those people.\n You just understand the differences. \nYeah.\n The differences.\n They're not like us, okay?",
"You ate a baby."); 


var GRID_SIZE = 10;

var CELL_HEIGHT = 30;
var CELL_WIDTH = 30;

var EXPLORED_COLOUR = "#F0F0F0";
var UNEXPLORED_COLOUR = "#606060";
var WINNING_COLOUR = "gold";
var PLAYER_COLOUR = "red";
var DEAD_COLOUR = "black";


function addEvent( obj, type, fn )
{
	if (obj.addEventListener)
		obj.addEventListener( type, fn, false );
	else if (obj.attachEvent)
	{
		obj["e"+type+fn] = fn;
		obj[type+fn] = function() { obj["e"+type+fn]( window.event ); }
		obj.attachEvent( "on"+type, obj[type+fn] );
	}
}

function removeEvent( obj, type, fn )
{
	if (obj.removeEventListener)
		obj.removeEventListener( type, fn, false );
	else if (obj.detachEvent)
	{
		obj.detachEvent( "on"+type, obj[type+fn] );
		obj[type+fn] = null;
		obj["e"+type+fn] = null;
	}
}



function getCell(row, column) {
	var grid_cells = document.getElementsByTagName("td");
	for(var i =0 ; i < grid_cells.length; i++){
		var cell = grid_cells[i];
		if(cell.rowNum == row && cell.colNum == column){
			return cell;
		}
	}
}

function getPlayer(){
	var grid_cells = document.getElementsByTagName("td");
	for(var i =0 ; i < grid_cells.length; i++){
		var cell = grid_cells[i];
		if(cell.style.backgroundColor == PLAYER_COLOUR){
			return cell;
		}
	}
}

function canMove(row, column){
	var player = getPlayer();
	if((Math.abs(row - player.rowNum ) == 0 && Math.abs(column - player.colNum) <= 1)
	 || (Math.abs(row - player.rowNum ) <=1  && Math.abs(column - player.colNum) == 0)){
		return true;
	}
	return false;
}
function handleKeyPress(evt) {
	var myKey = evt.keyCode;
	var player = getPlayer();
	if(myKey == 37 || myKey == 65) {
		var row = player.rowNum - 1;
		var col = player.colNum;
	} else if (myKey == 38 || myKey == 87) {
		var row = player.rowNum;
		var col = player.colNum - 1;
	} else if (myKey == 39 || myKey == 68) {
		var row = player.rowNum + 1;
		var col = player.colNum;
	} else if (myKey == 40 || myKey == 83) {
		var row = player.rowNum;
		var col = player.colNum + 1;
	} else if (myKey == 13) {
		reset(true);
		return;
	}
	if(row >= 0 && row < GRID_SIZE && col >= 0 && col < GRID_SIZE){
		var cell = getCell(row, col);
		setExplored.call(cell);
	}
}

function getBadThing(){
	var randIndex = Math.floor(Math.random() * (badThings.length));
	return badThings[randIndex];
}

function setExplored(){
	if(!dead && canMove(this.rowNum, this.colNum)){
		movePlayer(this.rowNum,this.colNum);
		if(this.isTrap){
			dead = true;
			this.style.backgroundColor = DEAD_COLOUR;
			alert(getBadThing() + "\nYou are a Bad Person!");
		}
	}
	document.getElementById("game_area").focus();
}

function movePlayer(row, column){
	var grid_cells = document.getElementsByTagName("td");
	for(var i =0 ; i < grid_cells.length; i++){
		var cell = grid_cells[i];
		if(cell.style.backgroundColor == PLAYER_COLOUR){
			cell.style.backgroundColor = EXPLORED_COLOUR;
		}
		if(cell.rowNum == row && cell.colNum == column){
			cell.style.backgroundColor = PLAYER_COLOUR;
		}
	}
	
}
function pickStrategy() {
	var strategy = Math.floor(Math.random() * 4);
	if(strategy == 0){
		return function(row,column,killLine){
			if(row == killLine){
				return true;
			} else {
				return false;
			}
		};
	} else if(strategy == 1) {
		return function(row,column,killLine){
			if(column == killLine){
				return true;
			} else {
				return false;
			}
		};
	
	} else if(strategy == 2) {
		return function(row,column,killLine){
			if((row + column) == killLine){
				return true;
			} else {
				return false;
			}
		};
	
	} else if(strategy == 3) {
		return function(row,column,killLine){
			if(row == killLine || column == killLine){
				return true;
			} else {
				return false;
			}
		};
	}
	
}

function reset(focus){
	dead = false;
	if(focus){
		document.getElementById("game_area").focus();
	}
	var killLine = Math.floor(Math.random() * (GRID_SIZE - 2)) + 1;

	var grid_cells = document.getElementsByTagName("td");
	var row = 0;
	var col = 0;
	var isTrap = pickStrategy();
	
	for(var i = 0; i < grid_cells.length; i++){
		if(grid_cells[i].className == "grid_cell"){
			grid_cells[i].style.backgroundColor = UNEXPLORED_COLOUR;
			grid_cells[i].isTrap = isTrap(row,col,killLine);
		}
		if( i != 0 && i % 10 == 9){
			row++;
			col = 0;
		} else {
			col++;
		}
		if(i == 0){
			grid_cells[i].style.backgroundColor = PLAYER_COLOUR;
		}
		if(i == grid_cells.length - 1){
			grid_cells[i].style.backgroundColor = WINNING_COLOUR;
		}
	}
	
}

function setup(){
        var gameArea = document.getElementById("game_area");
	addEvent(gameArea,"keydown",handleKeyPress);
	var table = document.createElement('table');
	var button = document.getElementById("shrive_button");
	var container = button.parentNode;
	container.appendChild(table);
	for(var row = 0; row < GRID_SIZE; row++){
		var currentRow = document.createElement('tr');
		for(var col=0; col < GRID_SIZE; col++){
			var elem = document.createElement('td');
			elem.className = "grid_cell";
			elem.maxHeight = CELL_HEIGHT;
			elem.maxWidth = CELL_WIDTH;
			elem.padding = "0px";
			elem.height = CELL_HEIGHT;
			elem.width = CELL_WIDTH;
			elem.colNum = row;
			elem.rowNum = col;
			addEvent(elem,"click",setExplored);
			
			currentRow.appendChild(elem);
		}
		table.appendChild(currentRow);
	}
	reset(false);
}
