
//alert('I can haz loading');



// initialize the variables we need as global

// these define the position of the magic the
// player is seeking
var magicRow;
var magicColumn;

// how many turns does the player have left?
var movesRemaining; 

// what player clicked before now
pastClicks = new Array; 

var magicIsMoving = 1; // game starts upon load

function StartGame() {
// Okay, let's start a new game.  

// Lose all old data in cells
// and empty the array pastClicks

for (var i = 0; i < pastClicks.length; pastClicks.length > 0) {
  var j = pastClicks.shift();
  if (typeof(j) != 'undefined') {
    // if player won the last game quickly, this array will
    // have some holes, so check to make sure it's defined

    document.getElementById(j).style.color = "#ffffff";
    document.getElementById(j).innerHTML = "99";
  }
}

// Let's figure out where our 
// target magic starts out.

magicRow = Math.floor(Math.random()*20) + 1;
magicColumn = Math.floor(Math.random()*20) + 1;

//alert('Magic is row ' + magicRow + ' col ' + magicColumn);

magicIsMoving = 1; // turn on moving explicitly, just in case

movesRemaining = 40;
document.getElementById('Status').innerHTML = 'Moves remaining: ' + movesRemaining;

}


function Move(currentClick) {

if (magicIsMoving == 0) {
  // game is over
  return 0;
}

// split the id of the chosen square into row and column
var bits_array = currentClick.split('-');
var clickedRow = bits_array[1];
var clickedColumn = bits_array[2];


//otherwise, calculate new table and display
// adjusted data

//let's see if we have so much data that
// we need to lose some


// shift old one off
var gone = pastClicks.shift();
if (typeof(gone) != 'undefined') {
//	document.getElementById(gone).setAttribute("style","color: #ffffff");
	document.getElementById(gone).style.color = "#ffffff";
	document.getElementById(gone).setAttribute("innerHTML","99");
}


// for each previous datum which isn't gone,
// fade it
if (typeof(pastClicks[0]) != 'undefined') {
//	document.getElementById(pastClicks[0]).setAttribute("style","color: #dddddd");
	document.getElementById(pastClicks[0]).style.color = "#dddddd";
}
if (typeof(pastClicks[1]) != 'undefined') {
//	document.getElementById(pastClicks[1]).setAttribute("style","color: #bbbbbb");
	document.getElementById(pastClicks[1]).style.color = "#bbbbbb";
}
if (typeof(pastClicks[2]) != 'undefined') {
//	document.getElementById(pastClicks[2]).setAttribute("style","color: #999999");
	document.getElementById(pastClicks[2]).style.color = "#999999";
}
if (typeof(pastClicks[3]) != 'undefined') {
//	document.getElementById(pastClicks[3]).setAttribute("style","color: #777777");
	document.getElementById(pastClicks[3]).style.color = "#999999";
}
if (typeof(pastClicks[4]) != 'undefined') {
//	document.getElementById(pastClicks[4]).setAttribute("style","color: #555555");
	document.getElementById(pastClicks[4]).style.color = "#555555";
}
if (typeof(pastClicks[5]) != 'undefined') {
//	document.getElementById(pastClicks[5]).setAttribute("style","color: #333333");
	document.getElementById(pastClicks[5]).style.color = "#333333";
}
if (typeof(pastClicks[6]) != 'undefined') {
//	document.getElementById(pastClicks[6]).setAttribute("style","color: #111111");
	document.getElementById(pastClicks[6]).style.color = "#111111";
}


// do the new one, all nice and black
// figure out what number to display
var n = 0;
if ((clickedRow - magicRow) > n) {
  n = clickedRow - magicRow;
}
if ((clickedColumn - magicColumn) > n) {
  n = clickedColumn - magicColumn;
}
if ((magicRow - clickedRow) > n) {
  n = magicRow - clickedRow;
}
if ((magicColumn - clickedColumn) > n) {
  n = magicColumn - clickedColumn;
}

//document.getElementById(currentClick).setAttribute("style","color: #000000");
document.getElementById(currentClick).style.color = "#000000";
document.getElementById(currentClick).innerHTML = n;

// add our new data to the table
//pastClicks[pastClicks.length] = currentClick;
pastClicks[7] = currentClick;


// decrement movesRemaining.  If it is
// now zero, lose game.

movesRemaining = (movesRemaining -1);
document.getElementById('Status').innerHTML = 'Moves remaining: ' + movesRemaining;

// Now that we've done our figuring and made sure
// currentClick is in the array (whether we win
// or lose or keep playing, it needs to be
// erasable), we'll check our winning and losing
// cases

// if that's where magic is, win game
if (clickedRow == magicRow){
  if (clickedColumn == magicColumn) {
//    alert('you saved the world');
    WinGame();
//    StartGame();
  }
}

if (movesRemaining < 1) {
LoseGame();
//StartGame();
}



// now it's the magic's turn.  it moves.
// that is, if the game is active.

if (magicIsMoving == 1) {
// for rows--increment, leave alone, decrement
var a = Math.floor(Math.random()* 4); // random number 0-3
if (a == 0) {
  //increment row unless it's already maxed
  if (magicRow < 21) {
    magicRow++;
  }
} else if (a == 1){
  // decrement row unless it's already at minimum
  if (magicRow > 1) {
    magicRow--;
  }
} else {
  // do nothing; it's not changing rows

}

// for columns--increment, leave alone, decrement
var b = Math.floor(Math.random()* 4); // random number 0-3
if (b == 0) {
  //increment row unless it's already maxed
  if (magicColumn < 21) {
    magicColumn++;
  }
} else if (b == 1){
  // decrement row unless it's already at minimum
  if (magicColumn > 1) {
    magicColumn--;
  }
} else {
  // do nothing; it's not changing rows
}

} // closes if magicIsMoving == 1


} // ends Move function

function WinGame() {

var newStr = '<p class="Freebies">Congratulations.  You did it!  You found the target magic in time, in ' + (40 - movesRemaining) + ' moves, and prevented disaster.</p><br><p class="Freebies"><a href="javascript:location.reload(true)">Play again?</a></p><br><p class="Freebies">If not, head on back to <a href="index.html">the party</a>, or just close this window if you let the game open in a new window by default.</p><br>';

document.getElementById('WinOrLose').innerHTML = newStr;

magicIsMoving = 0;

}

function LoseGame() {
var newStr = '<p class="Freebies">It\'s over.  You used up 40 moves without finding the target.  You did your best, but it just wasn\'t good enough.</p><br><p class="Freebies"><a href="javascript:location.reload(true)">Play again?</a></p><br><p class="Freebies">If not, head on back to <a href="index.html">the party</a>, or just close this window if you let the game open in a new window by default.</p><br>';
document.getElementById('WinOrLose').innerHTML = newStr;
magicIsMoving = 0;
}



