var boardSize;
var mineLocations;
var mineFlags;
var xsize;
var ysize;
var gameOver = false;
var seconds = 0;
var timer;
var mineCountGlobal = 0;

function submitForm()
{
	var formHeight = $("#height").attr("value");
	var formWidth = $("#width").attr("value");
	var formDifficulty = $("#difficulty").attr("value");
	
	$("#boardGreeting").hide();
	drawBoard(formHeight, formWidth);
	setMines(formDifficulty);
}

function timerStart()
{
		seconds++;
		$("#timer").html(seconds);
		timer = setTimeout("timerStart()",1000)
}
function drawBoard(x, y)
{
	xsize = x;
	ysize = y;
	var width = 19*ysize;
	var height = 19*xsize;
	
	timerStart();
	$("#board").css("width", width);
	$("#board").css("height", height);
	$("#board").fadeIn(2000);
	$("#toolbar").slideDown(2000);
	
	
	boardSize = xsize;
	for(var i = 0; i < xsize; i++)
	{
		var row = "<div id='"+i+"' class='row'></div>";
		$("#board").append(row);
		for(var k = 0; k < ysize; k++)
		{
			$("#" + i).append("<div id='"+i+"-"+k+"' class='mine'></div>");
		}
	}
	
	$(".row").width(width);
	
	$(".mine").hover(
		function(){		
			var coordsN = $(this).attr("id").split('-');
			if(mineState(coordsN[0], coordsN[1]) != 2)
			{
				$(this).css("background-color", "#0090FF");
			}
		},
		function(){
			var coordsN = $(this).attr("id").split('-');
			if(mineState(coordsN[0], coordsN[1]) != 2)
			{
				$(this).css("background-color", "#A4A4A4");
			}
		}
	);
	
	$(".mine").click(function(){
		var coords = $(this).attr("id").split('-');
		if(mineState(coords[0], coords[1]) == 0 && mineFlags[coords[0]][coords[1]] == 0 && !gameOver)
		{
			countMines(coords[0], coords[1], 0)
			
		}
		else if(mineState(coords[0], coords[1]) == 1 && mineFlags[coords[0]][coords[1]] == 0 && !gameOver)
		{
			$(this).append("<span class='mineCount'>*</span>");
			gameOver = true;
			Boxy.alert("You have stepped on a mine, game over.", loseGame());
		}
	});
	
	$(".mine").rightClick(function(e){
		var coords = $(this).attr("id").split('-');
		if(mineState(coords[0], coords[1]) != 2 && !gameOver)
		{
			if(mineFlags[coords[0]][coords[1]] == 1)
			{
				$(this).css("background-image", "");
				$(this).html("<span class='mineCount'>?</span>");
				mineFlags[coords[0]][coords[1]] = 2;
				mineCountGlobal++;
				$("#minesDisplay").html(mineCountGlobal);
			}
			else if(mineFlags[coords[0]][coords[1]] == 2)
			{
				$(this).empty();
				mineFlags[coords[0]][coords[1]] = 0;
			}
			else
			{
				$(this).css("background-image", "url(images/flag.gif)");
				mineFlags[coords[0]][coords[1]] = 1;
				mineCountGlobal--;
				$("#minesDisplay").html(mineCountGlobal);
				
				if(mineCountGlobal == 0)
				{
					if(checkIfWin())
					{
						Boxy.alert("You have successfully found all mines in "+seconds+" seconds", winGame());
					}
				}
			}
			
		}
	});
}

function checkIfWin()
{
	var win = true;
	
	for(var i = 0; i < xsize; i++)
	{
		for(var j = 0; j < ysize; j++)
		{
			if(mineLocations[i][j] == "*")
			{
				if(mineFlags[i][j] != 1)
				{
					win = false;
				}
			}
		}
	}

	return win;
}

function winGame()
{
	$("#board").empty();
	$("#board").hide();
	$("#boardGreeting").show();
	$("#container").css("width", "600px");
	$("#toolbar").hide();
	gameOver = false;
	xsize = null;
	ysize = null;
	boardSize = null;
	clearTimeout(timer);
	seconds = 0;
	mineCountGlobal = 0;
}

function loseGame()
{
	$("#board").empty();
	$("#board").hide();
	$("#boardGreeting").show();
	$("#container").css("width", "600px");
	$("#toolbar").hide();
	gameOver = false;
	xsize = null;
	ysize = null;
	boardSize = null;
	clearTimeout(timer);
	seconds = 0;
	mineCountGlobal = 0;
}
function mineState(x, y)
{
	if(mineLocations[x][y] == "*")
	{
		return 1;
	}
	else if(mineLocations[x][y] == 2)
	{
		return 2;
	}
	else
	{
		return 0;
	}
}

function countMines(xbox, ybox, iter)
{

	if(mineFlags[xbox][ybox] != 1)
	{
		mineLocations[xbox][ybox] = 2;
		$("#"+xbox+"-"+ybox).html("");
		var startx;
		var starty;
		var xcheck = xbox;
		var ycheck = ybox;
		
		if(xbox > 0)
		{
			startx = xbox-1;
		}
		else
		{
			startx = xbox;
		}
		
		if(ybox > 0)
		{
			starty = ybox-1;
		}
		else
		{
			starty = ybox;
		}
		
		if(xbox == xsize-1)
		{
			xbox--;
		}
		
		if(ybox == ysize-1)
		{
			ybox--;
		}
		
		xbox++;
		ybox++;
		
		var count = 0;
		

		for(var i = startx; i <= xbox; i++)
		{		
			for(var k = starty; k <= ybox; k++)
			{
				if(mineState(i, k) == 1)
				{
					count++;
				}
			}
		}
		
		if(count == 0)
		{
			for(var n = startx; n <= xbox; n++)
			{
				for(var o = starty; o <= ybox; o++)
				{
					if(mineState(n, o) != 2)
					{
						countMines(n, o, iter+1);
						
					}
				}
			}
		}
		
		if(count == 0)
		{
			count = "";
		}
		
		$("#"+xcheck+"-"+ycheck).html("<span class='mineCount'>"+count+"</span>");
		$("#"+xcheck+"-"+ycheck).css("background-color", "#838383");
	}
}

function setMines(mineProb)
{
	mineLocations = new Array(xsize);
	mineFlags = new Array(xsize);
	for(var i = 0; i < xsize; i++)
	{
		mineLocations[i] = new Array(boardSize);
		mineFlags[i] = new Array(boardSize);
	}
	
	for(var j = 0; j < xsize; j++)
	{
		for(var k = 0; k < ysize; k++)
		{
			var chance = Math.floor(Math.random()*101);
			if(chance < mineProb)
			{
				mineLocations[j][k] = "*";
				mineFlags[j][k] = 0;
				mineCountGlobal++;
			}
			else
			{
				mineLocations[j][k] = 0;
				mineFlags[j][k] = 0;
			}
		}
	}			
	
	$("#minesDisplay").html(mineCountGlobal);
}


