var NUMBER_OF_MINES = 20;
var POSITIONS = 110;

function MineHunt()
{
	/*
	 * VARIABLES
	 */
	this.spaceKeyDown = false;		// keep track of the space bar
	this.flagCount = NUMBER_OF_MINES;	// number of mines on the field
	this.countText = document.getElementById('count'); // keep track of the flag count
	this.field = new Array(POSITIONS);
	this.minePositions = new Array(NUMBER_OF_MINES);
	this.totalClick = 0;			// keep track of how many blocks have been clicked


	/*
	 * FUNCTIONS
	 */
	this.Init = Init;
	this.Click = Click;
	this.KeyDown = KeyDown;
	this.KeyUp = KeyUp;
	this.CreateMines = CreateMines;
	this.Random = Random;
	this.CheckPosition = CheckPosition;
	this.HasMine = HasMine;
	this.BlowUp = BlowUp;
	this.Win = Win;
}

// initialize variables
function Init()
{
	// keep track of the space bar
	this.spaceKeyDown = false;

	// number of mines on the field
	this.flagCount = NUMBER_OF_MINES;

	// reset the total count
	this.totalCount = 0;

	// keep track of the flag count
	this.countText.firstChild.nodeValue = this.flagCount;

	// lay the mines on the field
	this.CreateMines();

	for(i = 1; i <= POSITIONS; ++i)
	{
		//block = document.getElementById(i);
		block = eval("document.getElementById('"+i+"')");
		block.src = "block_blank.gif";
	}
}


// places the mines randomly on the field
function CreateMines()
{
	selections = NUMBER_OF_MINES;
	range = POSITIONS;
	var i = 1;
	var ChosenNumber;
	var Duplicates = new Array(range);

	while (i <= selections)
	{
		ChosenNumber = Math.floor(Math.random() * range) + 1;

		if (Duplicates[ChosenNumber] != "selected")
		{
			Duplicates[ChosenNumber] = "selected";
			this.minePositions[i-1] = ChosenNumber;
			++i;
		}
	}
}


// a position has been clicked on, lets check
// it for a mine, and mark it accordingly if
// one is not there
function CheckPosition(num)
{
	// keep track of how many mines are around
	// according to Internet Explorer the word
	// 'count' is reserved for something, so
	// we use another name
	_count = 0;
	block = eval("document.getElementById('"+num+"')");

	if(this.HasMine(num))
	{
		this.BlowUp();
		return;
	}

	if(num%11 != 0)
	{
		// check to the right
		if(this.HasMine(num+1))
			++_count;

		// check top right diagnol
		if(this.HasMine(num-10))
			++_count;

		// check bottom right diagnol
		if(this.HasMine(num+12))
			++_count;
	}


	if((num-1)%11 != 0)
	{
		// check to the left
		if(this.HasMine(num-1))
			++_count;

		// check the top left diagnol
		if(this.HasMine(num-12))
			++_count;

   		// check the bottom left diagnol
		if(this.HasMine(num+10))
			++_count;
	}

	// check above
	if(this.HasMine(num-11))
		++_count;

	// check below
	if(this.HasMine(num+11))
		++_count;

	// now set the image accordingly
	if(_count == 0)
		block.src = "blank.gif";
	else
		block.src = _count + ".gif";

	if(this.totalCount == (POSITIONS - NUMBER_OF_MINES))
		this.Win();
}


// checks a position to see if it contains a mine
// return true if it does, else false
function HasMine(num)
{
	for(i=0; i<this.minePositions.length; ++i)
	{
		if(this.minePositions[i] == num)
			return true;
	}

	return false;
}


// game over, a mine was hit
function BlowUp()
{
	for(i=0; i<this.minePositions.length; ++i)
	{
		//block = document.getElementById(this.minePositions[i]);
		block = eval("document.getElementById('"+this.minePositions[i]+"')");
		block.src = "mine.gif";
	}

	if(confirm("You Blew Up! Want to play again?"))
		this.Init();
}


// the user has won. do something cool
function Win()
{
	if(confirm("Congratulations, you've won. Play again?"))
		this.Init();hey
}


// create a random number between 1 and number
function Random(number)
{
	today = new Date();
	seed = today.getTime();
	seed = (seed*9301+49297) % 233280;
	rand = seed/(233280.0);
	return Math.ceil(rand*number);
}


// handle click events on the images
function Click(num)
{
	//block = document.getElementById(num);
	block = eval("document.getElementById('"+num+"')");

	if(this.spaceKeyDown && block.src.match("block_blank.gif"))
	{
		// we are marking a block with a flag
		block.src = "block_flag.gif";
		--this.flagCount;
		this.countText.firstChild.nodeValue = this.flagCount;
  	}
	else if(this.spaceKeyDown && block.src.match("block_flag.gif"))
	{
		// we are unmarking a block with a flag
		block.src = "block_blank.gif";
		++this.flagCount;
		this.countText.firstChild.nodeValue = this.flagCount;
  	}
  	else if(block.src.match("block_blank.gif"))
	{
		++this.totalCount;

		// lets see whats under the block
		this.CheckPosition(num);
	}
}

// handle a key down event
function KeyDown(e)
{
	// if the space bar is being pressed
	if(e.keyCode == 32)
		this.spaceKeyDown = true;
}


// handle a key up event
function KeyUp(e)
{
	// if the space bar is being unpressed
	if(e.keyCode == 32)
		this.spaceKeyDown = false;
}