var nRows = 4;
var nCols = 5;
var nCells = nRows * nCols;
var nPairs = nCells / 2

var imageMap = new Array(nCells);
var imageNames = new Array(nPairs);

var nFound = 0
var nMoves = 0
var first = -1
var second
var idTimer = 0
var idClock = 0
var nTicks = 0

function initGame() {
   // The images are number 0.gif, 1.gif, ...
   // The image 0.gif is the back of each image (the hidden image)
   // This loop populates the list card faces (the exposed images)
   // Also populate the imageMap (will be shuffled later)
   for (i = 0; i < nPairs; i++) {
      imageNames[i] = i + 1;
      imageMap[2 * i] = imageNames[i]
      imageMap[2 * i + 1] = imageNames[i]
   }

   shuffleGame()
}

function shuffleGame() {
   // Randomize the puzzle.
   // This is a simple shuffling algorithm - each cell is swapped with another.
   for (i = 0; i < nCells; i++) {
      hideCell(i);
      swapCell = Math.floor(Math.random() * nCells)
      tmp = imageMap[swapCell]
      imageMap[swapCell] = imageMap[i]
      imageMap[i] = tmp
   }
}

function newGame() {
   // A new games starts the clock right away.
   if (idClock == 0)
      idClock = setTimeout(clockTick, 1000)

   displayMsg("")

   if (idTimer != 0)
      clearTimeout(idTimer)

   first = -1
   nMoves = 0
   nFound = 0
   idTimer = 0
   nTicks = 0

   shuffleGame()
}

function displayMsg(m) {
   document.getElementById('textArea').innerHTML = m
}

function getCellImgSrc(n) {
   eval("fn = document.game.p" + n + ".src")
   afn = fn.split("/")
   return afn[afn.length-1]
}

function showCell(n) {
   eval("document.game.p" + n + ".src = '/jg-bp2/images/memory/" + imageMap[n] + ".gif'")
}

function hideCell(n) {
   eval("document.game.p" + n + ".src = '/jg-bp2/images/memory/0.gif'")
}

function hideMistakes() {
   hideCell(first)
   hideCell(second)
   first = -1
   idTimer = 0
}

function displayStatus() {
   displayMsg(nMoves + " guesses, " + nTicks + " seconds.")   
}

function clockTick() {
   ++nTicks
   displayStatus()
   idClock = setTimeout(clockTick, 1000)
}

function selectCell(n) {
   // The first guess starts the closk
   if (idClock == 0)
      idClock = setTimeout(clockTick, 1000)

   // A new guess before the hide timeout will preempt the timer.
   if (idTimer != 0) {
      clearTimeout(idTimer)
      hideMistakes()
   }
   
   // If the image is already displayed then don't do anything.
   if (getCellImgSrc(n) != "0.gif")
      return;

   // For a new guess, show the cell.
   // If it's the 2nd guess then we check to see if it's a match.
   showCell(n)
   if (first == -1 || n == first) {
      first = n;
      return;
   }

   second = n 
   nMoves++
   displayStatus()
   if(imageMap[first] != imageMap[second]) {
      idTimer = setTimeout("hideMistakes()", 1500)
      return;
   }

   // If the guess is correct then check to see if the puzzle is solved.
   first = -1
   nFound++
   if (nFound == nPairs) {
      if (idClock != 0)
         clearTimeout(idClock)

      idClock = 0;

      if (nMoves <= 15)
	 m = "You are the King or Queen!"
      else if (nMoves <= 20)
         m = "You are a Prince or possibly a Princess!"
      else if (nMoves >= 30)
         m = "You are a peasant!"
      else
	 m = "You could become royalty if you keep practicing!"

      displayMsg(m + " You have solved the puzzle in " + nMoves + " tries and " + nTicks + " seconds!")
   }
}

function generateGame() {
   document.write("<table><tr><td width=10></td> <td> <form name='game'> <table border='1'> <tbody>")
   for (r = 0; r < nRows; r++) {
      document.write("<tr>")

      for (c = 0; c < nCols; c++) {
	 cell = c + r * nCols;
	 document.write("<td><img src='/jg-bp2/images/memory/0.gif' onclick='selectCell(" + cell + ")' name='p" + cell + "'></td>")
      }
      document.write("</tr>")
   }
   document.write("</tbody></table> <input value='Play Again' onclick='newGame()' type='button'> <br><br> <p id=textArea>&nbsp;</p> </form> </table>")
}
