var mineArray = new Array();
	var elementChecked = new Array();
	var flaggedCells = new Array();
	
	var gameOver=false;
	var totalMines = 60;
	var restMines = totalMines;
	var gameStarted = false;
	var timeStarted = false;
	function initMineSweeper(){
		
		var cells = document.getElementsByTagName('DIV');
		for(var no=0;no<cells.length;no++){
			if(cells[no].className=='game_cell'){
				cells[no].onclick = clickCell;	
				cells[no].onmousedown = flagCell;			
			}	
		}		
		document.body.oncontextmenu = cancelEvent;
		document.getElementById('theGame_button').onclick = initMines;
		document.getElementById('theGame_button').onmousedown = buttonDown;
		document.getElementById('theGame_button').onmouseup = buttonUp;
		document.getElementById('theGame_button').onmouseout = buttonUp;
		initMines();
	}
	
	function buttonDown()
	{
		this.style.borderLeft = '2px solid #808080';
		this.style.borderTop = '2px solid #808080';
		this.style.borderRight = '1px solid #808080';
		this.style.borderBottom = '1px solid #808080';
	}
	function buttonUp(){
		this.style.borderLeft = '2px solid #FFFFFF';
		this.style.borderTop = '2px solid #FFFFFF';		
		this.style.borderRight = '2px solid #808080';
		this.style.borderBottom = '2px solid #808080';		
	}
	
	function showRestMines(prefix,number){
		number = number + '';
		while(number.length<3){
			number = '0' + number;
		}
		for(var no=0;no<3;no++){
			document.getElementById(prefix+(no+1)).src = 'img/number_'+number.charAt(no) + '.gif';
		}
	}
	
	function initMines()
	{
		document.getElementById('theGame_button').style.backgroundImage = 'url(\'img/face1.gif\')';
		var countAssigned = 0;
		for(var prop in mineArray){
			mineArray[prop] = false;
		}
		for(var prop in elementChecked){
			elementChecked[prop] = false;
		}
		for(var prop in flaggedCells){
			flaggedCells[prop] = false;
		}
		while(countAssigned<totalMines){
			var no1 = Math.floor(Math.random()*30);
			var no2 = Math.floor(Math.random()*16);
			if(!mineArray[no1+','+no2]){
				mineArray[no1+','+no2] = true;
				countAssigned = countAssigned + 1;				
			}
		}	
		restMines = totalMines
		showRestMines('pic_remainder',totalMines);
		var parent = document.getElementById('game_container');
		var sub = parent.getElementsByTagName('DIV');
		for(var no=0;no<sub.length;no++){
			if(sub[no].className=='game_cell_clicked'){
				sub[no].innerHTML = '<span></span>';
				sub[no].className='game_cell';
				sub[no].style.backgroundImage='';	
			}
			if(sub[no].className=='game_cell'){
				sub[no].style.backgroundImage='';
			}			
		}
		gameOver = false;
		gameStarted = false;
		

	}

	function cancelEvent(){
		return false;
	}
	
	function flagCell(e){
		if(gameOver)return;
		if(this.className=='game_cell_clicked')return;
		if(document.all)e = event;
		var flagExists = false;
		if(e.button==2){
			if(this.style.backgroundImage.indexOf('img/flag.gif')>=0)flagExists=true;
			
			var id = this.id;
			var numbers = id.split('_');
			numbers[0] = numbers[0].replace(/[^\d]/g,'');
			if(flagExists){
				restMines = restMines/1 + 1;
				this.style.backgroundImage = '';
				flaggedCells[numbers[0] + ',' +numbers[1]] = false;
			}else{
				if(restMines==0)return; // Number of flags equals number of mines
				this.style.backgroundImage = 'url(\'img/flag.gif\')';	
				var id = this.id;
				flaggedCells[numbers[0] + ',' +numbers[1]] = true;
				restMines = restMines/1 - 1;
			}	
			showRestMines('pic_remainder',restMines);
			if(restMines==0)checkScore();
			return false;	
		}			
		if(e.button==1){	// Left mouse button clicked -> Show smiling face at the button
			document.getElementById('theGame_button').style.backgroundImage = 'url(\'img/face3.gif\')';
		}	
	}
	
	/*
	Number of flags equals number of mines -> Check if they are checked correctly
	*/
	
	function checkScore()
	{
		var d = new Date();
		var sec = Math.floor(d.getTime()/1000);
		var diff = sec - timeStarted;
		document.forms[0].seconds.value = diff;
		var errorsFound = false;
		for(var prop in flaggedCells){
			if(!mineArray[prop] && flaggedCells[prop]){
				errorsFound = true;
				break;
			}
		}
		
		if(!errorsFound){ // Wrong flags
			document.getElementById('theGame').style.display='none';
			document.getElementById('theGame_score').style.display='block';
		
		}
		
	}
	
	
	/*
	Start game - reset timer
	*/
	function startTimer()
	{
		var d = new Date();
		timeStarted = Math.floor(d.getTime()/1000);
		showTimer();
	}
	/*
	Display seconds at the top right corner 
	*/
	function showTimer()
	{
		var d = new Date();
		var sec = Math.floor(d.getTime()/1000);
		var diff = sec - timeStarted;
		showRestMines('pic_time',diff);
		if(gameStarted)setTimeout('showTimer()',800);else{
			showRestMines('pic_time',0);
		}
	}
	
	function clickAllCells(){
		for(var no=0;no<30;no++){
			for(var no2=0;no2<16;no2++){
				currentCell = document.getElementById('cell' + no + '_'+no2);
				var theID = currentCell.id;	
				var numbers = theID.split('_');	// Getting array of the x and y coordinage
				numbers[0] = numbers[0].replace(/[^\d]/g,'');
				var x = numbers[0]/1;
				var y = numbers[1]/1;	
				var neighbours = getNeighbours(numbers[0],numbers[1]);	// Retreve number of mines in the "neihgbourhood"
				currentCell.className='game_cell_clicked';	
				if(neighbours==99){
					currentCell.style.backgroundImage = 'url(\'img/mine.gif\')';
				}else if(neighbours>0){
					currentCell.innerHTML = neighbours;
					currentCell.style.backgroundImage = '';
					switch(neighbours){
						case 1: 
							currentCell.style.color = '#0000FF';
							break;
						case 2:	
							currentCell.style.color = '#008000';
							break;
						case 3:
							currentCell.style.color = '#FF0000';
							break;
						case 4:
							currentCell.style.color = '#000080';
							break;
						case 5:
							currentCell.style.color = 'brown';
							break;
								
					}					
				}	
							
			}			
		}		
	}
	
	function clickCell(e,currentCell,skipNeighbors)
	{
		
		if(gameOver && skipNeighbors!='1')return;
		if(!gameStarted){
			gameStarted=true;
			startTimer();	
		}
		if(document.all)e = event;
		if(this && this!="undefined" && !currentCell){
			currentCell=this;
			document.getElementById('theGame_button').style.backgroundImage = 'url(\'img/face1.gif\')';
			if(currentCell.style.backgroundImage.indexOf('flag')>=0)return;	
		}
		if(currentCell.className=='game_cell_clicked')return;
		currentCell.className='game_cell_clicked';	
		currentCell.style.backgroundImage='';	
		var theID = currentCell.id;		
		var numbers = theID.split('_');	// Getting array of the x and y coordinage
		numbers[0] = numbers[0].replace(/[^\d]/g,'');
		var x = numbers[0]/1;
		var y = numbers[1]/1;
		elementChecked[x+','+y] = true;	// Flag this element as checked
		var neighbours = getNeighbours(numbers[0],numbers[1]);	// Retreve number of mines in the "neihgbourhood"
		if(neighbours==99){
			currentCell.style.backgroundImage = 'url(\'img/mine.gif\')';
			clickAllCells();
			gameOver = true;
			gameStarted = false;
			document.getElementById('theGame_button').style.backgroundImage = 'url(\'img/face2.gif\')';
			
			return;	
		}
		if(neighbours>0){
			switch(neighbours){
				case 1: 
					currentCell.style.color = '#0000FF';
					break;
				case 2:	
					currentCell.style.color = '#008000';
					break;
				case 3:
					currentCell.style.color = '#FF0000';
					break;
				case 4:
					currentCell.style.color = '#000080';
					break;
				case 5:
					currentCell.style.color = 'brown';
					break;
				case 6:
					currentCell.style.color = 'orange';
					break;
						
			}
			currentCell.innerHTML = neighbours;
		}else{
			if(skipNeighbors!='1'){
				if(x<29){
					if(!elementChecked[(x+1)+','+y])callClickFunction((x+1),y);
					if(!elementChecked[(x+1)+','+(y-1)] && y>0)callClickFunction((x+1),(y-1));
					if(!elementChecked[(x+1)+','+(y+1)] && y<15)callClickFunction((x+1),(y+1));				
				}
				if(x>0){
					if(!elementChecked[(x-1)+','+y])callClickFunction((x-1),y);
				}
				if(y<15){
					if(!elementChecked[(x)+','+(y+1)])callClickFunction(x,(y+1));
					if(!elementChecked[(x-1) + ',' + (y+1)] && x>0)callClickFunction( (x-1),(y+1));
	
				}
				if(y>0){
					if(!elementChecked[(x)+','+(y-1)])callClickFunction(x,(y-1));
					if(x>0 && !elementChecked[(x-1)+','+(y-1)])callClickFunction((x-1),(y-1));
				}
			}
		}
	}
	
	
	/*
	To get a smooth auto click
	*/
	function callClickFunction(x,y)
	{		
		setTimeout('clickCell(false,document.getElementById("cell' + x + '_' + y + '"))',1);
	}
	
	/*
	Count number of mines in the neighbourhood
	*/
	function getNeighbours(x,y)
	{
		if(mineArray[x+','+y])return 99;
		var countNeighbours = 0;
		if(x>0){
			if(y>0)if(mineArray[(x-1)+','+(y-1)])countNeighbours++;	
			if(mineArray[(x-1)+','+y])countNeighbours++;
			if(y<15)if(mineArray[(x-1)+','+(y/1+1)])countNeighbours++;
		}
		if(y>0){
			if(mineArray[x+','+(y-1)])countNeighbours++;	
			if(x<29)if(mineArray[(x/1+1)+','+(y-1)])countNeighbours++;
		}
		if(y<15){
			if(mineArray[x+','+(y/1+1)])countNeighbours++;
			if(x<29)if(mineArray[(x/1+1)+','+(y/1+1)])countNeighbours++;
		}
		if(x<29){
			if(mineArray[(x/1+1)+','+y])countNeighbours++;	
		}
		
		return countNeighbours;
		
	}
	
	function showHideScore(buttonObj)
	{

		if(buttonObj.value=='Show highscore'){
			document.getElementById('theGame').style.display='none';	
			document.getElementById('theGame_scoreList').style.display='block';	
			buttonObj.value = 'Show game';
		}else if(buttonObj.value=='Show game'){
			document.getElementById('theGame').style.display='block';	
			document.getElementById('theGame_scoreList').style.display='none';	
			buttonObj.value = 'Show highscore';
		}	
		
	}
	