// *******************************************************************************
// This is the main Minesweeper script.
// *******************************************************************************

dir = "";
// Global Variables
var cf = parent.cfg.document.cfg;
var cx=cf.x, cy=cf.y, cc=cf.c; cm=cf.m;
var mines = [], shown = [];
var gridx = cx.value 
var gridy = cy.value
var maxmines = cm.value




// Levels of difficulty
function lvlB() {
cx.value = "8",cy.value = "8",cm.value = "10";
parent.field.location.reload();
}

function lvlI() {
cx.value = "16",cy.value = "16",cm.value = "40";
parent.field.location.reload();
}

function lvlE() {
cx.value = "30",cy.value = "16",cm.value = "99";
parent.field.location.reload();
}

function accepted(){
var sqL, fL, sfL;
var eT;
var playing;
var placeflag;
var clicked;
}

// Measurements for different elements of the
// Minesweeper board.
var gridSq = gridx * 16;
var grid8 = gridSq - 128;
var grid16 = gridx - 8
var grid32 = grid16 * 8
var grid64 = grid16 * 16
var topBarWidth = 8 + grid64;
var menuBarWidth = 86 + grid64;
var wideWidth = gridx * 16;
var highHeight = gridy * 16;
var cplLeft = 6 + grid32;
var cplRight = 4 + grid32;
var totalWidth = gridSq + 32;
var tW6 = totalWidth - 6;
var ww2 = wideWidth + 2;

// Assigning names to number images
num=new Array(10);

for (i=0;i<10;i++) {
  num[i]=new Image()
  num[i].src = (parent.cfg.gcolor) ? "m"+i+".gif" : i+".gif"
  }

function keyDown(e) {
if(document.layers)
placeflag = (e.modifiers & Event.CONTROL_MASK) > 0;
else
placeflag = window.event.ctrlKey;
setStatus();
}
function keyUp(e) {
placeflag = false;
setStatus();
}


function newgame() {
// reset state arrays. mines holds the position of each mine. shown keeps
// track of the image shown at each grid location
var y;
for(y = 0; y < gridy; ++y)
{
mines[y] = [false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false];
shown[y] = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0];
}

// Place the mines, making sure positions are unique
var m;
for(m = 0; m < maxmines; ++m) {
var x,y;
do {
x = Math.floor(Math.random() * gridx);
y = Math.floor(Math.random() * gridy);
} while(mines[y][x]);
mines[y][x] = true;
}

// initialize game variables
sqL = gridy * gridx;
fL = maxmines;
eT = 0;
playing = true;
clicked = false
placeflag = false;
sfL = maxmines - fL;

// insert the grid into the document
buildgrid();

// Set up keypress handlers
if (document.layers)
window.captureEvents(Event.KEYDOWN | Event.KEYUP);
window.onKeyDown = keyDown;
window.onKeyUp = keyUp;

// start the clock
setInterval("ticker()", 1000);
}

// clock tick handler
function ticker() {
if (playing) {
if (clicked) {
eT++;
setStatus();
   }
  }
}

// Refreshing control panel
function setStatus() {
document.images.elapse3.src=num[eT-(Math.floor(eT/10))*10].src;
document.images.elapse2.src=num[Math.floor((eT-(Math.floor(eT/100))*100)/10)].src;
document.images.elapse1.src=num[Math.floor(eT/100)].src;
document.images.flag3.src=num[fL-(Math.floor(fL/10))*10].src;
document.images.flag2.src=num[Math.floor((fL-(Math.floor(fL/100))*100)/10)].src;
document.images.flag1.src=num[Math.floor(fL/100)].src;

}



// *******************************************************************************
// These are functions that run during the game.
// *******************************************************************************
// Function to calculate the number of mines adjacent to a grid location
function surrounding(y,x) {
var count = 0;
if (y > 0 && x > 0 && mines[y-1][x-1]) count++;
if (y > 0 && mines[y-1][x]) count++;
if (y > 0 && x < gridx-1 && mines[y-1][x+1]) count++;
if (x > 0 && mines[y][x-1]) count++;
if (x < gridx-1 && mines[y][x+1]) count++;
if (y < gridy-1 && x > 0 && mines[y+1][x-1]) count++;
if (y < gridy-1 && mines[y+1][x]) count++;
if (y < gridy-1 && x < gridx-1 && mines[y+1][x+1]) count++;
return count;
}

// Recursive function to 'roll back' the grid when user clicks on a tile
// with no surrounding mines
function rollback(y,x) {
if (y >= 0 && y < gridy && x >=0 && x < gridx) {
if (shown[y][x] != 3) {
var c = surrounding(y,x);
shown[y][x] = 3;
sqL--;
document.images["grd"+y+"_"+x].src = (parent.cfg.gcolor) ? "msq"+c+".gif" : "sq"+c+".gif";
if (c == 0) {
rollback(y-1,x-1);
rollback(y-1,x);
rollback(y-1,x+1);
rollback(y,x-1);
rollback(y,x+1);
rollback(y+1,x-1);
rollback(y+1,x);
rollback(y+1,x+1);
         }
      }
   }
}

// Function called when player steps on a mine. All mine locations are uncovered
function dead() {
var y, x;
for(y = 0; y < gridy; ++y) {
for(x = 0; x < gridx; ++x) {
if (mines[y][x]) {
if (shown[y][x] != 1) {
document.images["grd"+y+"_"+x].src = (parent.cfg.gcolor) ? "mmine.gif" : "mine.gif"
   }
}
else if (shown[y][x] == 1) {
document.images["grd"+y+"_"+x].src = (parent.cfg.gcolor) ? "mnomine.gif" : "nomine.gif"
      }
   }
}
document.images.condition.src = (parent.cfg.gcolor) ? "mbdead.gif" : "btndead.gif";
playing = false;
clicked = false;
}

// handler called whenever the grid is clicked
function gridclick(y, x) {
if (playing) {
clicked = true;
if (placeflag) {
if (shown[y][x] < 3) {
var s = shown[y][x];
var change = true;
if (s == 1) {
fL++;
sqL++;
}
if (fL == 0 && s == 0) {
change = false;
}
else {
if (s == 2) s = 0;
else s++;
if (s == 1) {
fL--;
sqL--;
   }
}
if (change) {
shown[y][x] = s;
document.images["grd"+y+"_"+x].src = (parent.cfg.gcolor) ? "msqt"+s+".gif" : "sqt"+s+".gif";
setStatus();
}

   }
}

// check not flagged as a mine
else if (shown[y][x] != 1) {
if (mines[y][x]) {
document.images["grd"+y+"_"+x].src = (parent.cfg.gcolor) ? "mminered.gif" : "minered.gif";
dead();
}
else  {
rollback(y,x);
         }
      }
   }
if (sqL == fL) {
document.images.condition.src = (parent.cfg.gcolor) ? "mbcool.gif" : "btncool.gif";
playing = false;
clicked = false;
showmines();

      }

}

function chng() {
if (playing) {document.images.condition.src=(parent.cfg.gcolor) ? "mbscare.gif" : "btnscare.gif"}
}
function chng2() {
if (playing) {document.images.condition.src=(parent.cfg.gcolor) ? "mbsmile.gif" : "btnsmile.gif"}
}

function showmines() {
var y, x;
for(y = 0; y < gridy; ++y) {
for(x = 0; x < gridx; ++x) {
if (mines[y][x]) {
if (shown[y][x] != 1) {
document.images["grd"+y+"_"+x].src = (parent.cfg.gcolor) ? "msqt1.gif" : "sqt1.gif"
   }
}
}
}
}
// Start the game
newgame();


