<!--

function onPageLoaded(){
	initialize();
}

var sbContext;
var urlroot;
var boardDim = 7;

var imagePathHead = baseprefix + "images/";
var pegPresent;
var pegEmpty;
var pegBlank;
var pegDecision;
var pegBranch;

var blank;
var empty;
var peg;

    // ------ GLOBAL VARIABLES --------
var taskId;

var pegboard;
var jumps;
var mode;    // 0 = normal; 1 = branch decision pending

var dec_x;
var dec_y;
var branch_ctr;
var branch_x;  // array
var branch_y;  // array

var winnerMessage;      // global variable for constructing winner messages to user

var nummoves = 0;       // current number of moves in the game (puzzle attempt)
var curscore = 0;       // current number of correct tile positions in the game
var playing = false;    // play is currently ongoing iff true
var totalPossibleScore = 65;
var bonus = 23;

var primaryParent;
var statisticsdiv;

var peg_count = 32;

var dbgmsg;

    // ------ METHODS --------
    
function initialize()
{ 
  mode = 0;
       // entry 0 -> cannot move; entry 1 -> can move:
  jumps = new Array(4);  // organized: n e s w
  branch_x = new Array(2);
  branch_y = new Array(2);
  blank = -1;
  empty = 0;
  peg = 1;
  pegPresent  = imagePathHead + "blue_peg32.gif";
  pegEmpty    = imagePathHead + "empty_peg32.gif";
  pegBlank    = imagePathHead + "blank32.gif";
  pegDecision = imagePathHead + "yellow_peg32.gif";
  pegBranch   = imagePathHead + "red_peg32.gif";

  pegboard = new Array(boardDim);
  for (y=0; y<boardDim; y++){
    pegboard[y] = new Array(boardDim);
    for (x=0; x<2; x++){
      if (y < 2 || y > 4){ 
	  setSq(x, y, blank);
      } else {
	  setSq(x, y, peg);
      } 
    }
    for (x=2; x<5; x++){    
	  setSq(x, y, peg);
    }
    for (x=5; x<7; x++){
      if (y < 2 || y > 4){ 
	  setSq(x, y, blank);
      } else {
	  setSq(x, y, peg);
      } 
    }
  }
  setSq(3, 3, empty);
}

function initialStartGame()
{
  var textdiv = document.getElementById('intro_text');
   textdiv.style.display='none';
   
  var statisticsdiv = document.getElementById('tried_correct_statistics');
  statisticsdiv.style.display='block';

  setupStartStop();
  startGame();
}

function startGame()
{
     var msgsdiv = document.getElementById('peg_sol_user_messages');  
    msgsdiv.style.display='none';
    
    initialize();
    init_current();
    playing = true;
    peg_count = 32;
    setStopStartBtns(false, true);
    start_timer();
}

function stopGame()
{
    stop_play();
}

function init_current()
{
    nummoves = 0;
    curscore = 0;
    nummovesdiv = document.getElementById('numtried_div');
    nummovesdiv.innerHTML = nummoves;
    scorediv = document.getElementById('score_div');
    scorediv.innerHTML = curscore;
}

function stop_play()
{
    var cur_time = new Date();
    elapsed_time = cur_time.getTime() - start_time.getTime();
    stop_timer();
    playing = false; 
      
    var finalMessage;
    if (peg_count == 1){
      finalMessage='Puzzle solved! Bonus = 23 points! Final score = ' + curscore;
    } else {
      finalMessage='Game ended. Final score = ' + curscore;
    }   
    var msgsdiv = document.getElementById('peg_sol_user_messages');  
    msgsdiv.innerHTML=finalMessage;
    msgsdiv.style.display="block";

    setStopStartBtns(true, false);
    var userNum = sbCookieData.usernum;
    Record.recordNumCorrectPlusMoves(taskId, nummoves,curscore, elapsed_time, userNum);
}

function updateStats()
{
    nummoves++;
    var nummovesdiv = document.getElementById('numtried_div');
    nummovesdiv.innerHTML = nummoves;

    peg_count--;
    if (peg_count == 1){
      curscore += bonus;

      var scorediv = document.getElementById('score_div');
      scorediv.innerHTML = curscore;

      stop_play();
    } else {
      setCurScore();
      var scorediv = document.getElementById('score_div');
      scorediv.innerHTML = curscore;
    }
}

function setCurScore()
{
  var initscore = 32 - peg_count;
  if (initscore <= 22){
    curscore = initscore;
  } else {
    curscore = 22 + (initscore - 22) * 2;
  }
}

function clicked(sx, sy) 
{
  if (!playing) {
        alert("Press the Start button to begin the puzzle.");
  } else {
    var x = parseInt(sx);
    var y = parseInt(sy);
    if (mode == 0) {
      processClick(x,y);
    } else {
      processBranchMove(x, y);
    }
  }
}

function processClick(x,y)
{
  var nj = checkCanMove(x,y);
  var sq;
  if (pegboard[y][x] == -1){ sq = blank; }
  else if (pegboard[y][x] == 0){ sq = empty; }
  else if (pegboard[y][x] == 1){ sq = peg; }
  else { sq = -2; }  // "error"
  if (sq == peg  && nj == 1) {
    executeImmediateMove(x,y);
    updateStats();
  } else if (nj >= 2) {
    mode = 1;
    branchMove(x,y);
  }
}

function checkWest(x,y)
{
  if (x < 2) return 0;
  if (x < 4 && (y < 2 || y > 5)) return 0;
  if (pegboard[y][x-1] != peg) return 0;
  if (pegboard[y][x-2] != empty) return 0;
  return 1;
}

function checkEast(x,y)
{
  if (x > 4) return 0;
  if (x > 2  && (y < 2 || y > 5)) return 0;
  if (pegboard[y][x+1] != peg) return 0;
  if (pegboard[y][x+2] != empty) return 0;
  return 1;
}

function checkNorth(x,y)
{
  if (y < 2) return 0;
  if (y < 4 && (x < 2 || x > 5)) return 0;
  if (pegboard[y-1][x] != peg) return 0;
  if (pegboard[y-2][x] != empty) return 0;
  return 1;
}

function checkSouth(x,y)
{
  if (y > 4) return 0;
  if (y > 2  && (x < 2 || x > 5)) return 0;
  if (pegboard[y+1][x] != peg) return 0;
  if (pegboard[y+2][x] != empty) return 0;
  return 1;
}

function checkCanMove(x,y)
{
  jumps[0] = checkNorth(x,y);
  jumps[1] = checkEast(x,y);
  jumps[2] = checkSouth(x,y);
  jumps[3] = checkWest(x,y);
  var nj = jumps[0] + jumps[1] + jumps[2] + jumps[3];
  return nj;
  
}

function setSq(x, y, value)
{
  var imagePath;
  if (value == blank){
    imagePath=pegBlank;
  } else if (value == empty) {
    imagePath = pegEmpty;
  } else {
    imagePath = pegPresent;
  }
  document.images["r0"+x+"c0"+y].src=imagePath;
  pegboard[y][x] = value;
}

function setDecisionPeg(x, y)
{
  dec_x = x;
  dec_y = y;
  document.images["r0"+x+"c0"+y].src=pegDecision;
  branch_ctr = 0;
}

function setBranchPeg(x, y)
{
  branch_x[branch_ctr] = x;
  branch_y[branch_ctr] = y;
  branch_ctr++;
  document.images["r0"+x+"c0"+y].src=pegBranch;
}

function executeImmediateMove(x,y)
{
  setSq(x, y, empty);
  if (jumps[0] == 1){
    jumpNorth(x, y);
  } else if (jumps[1] == 1){
    jumpEast(x, y);
  } else if (jumps[2] == 1){
    jumpSouth(x, y);
  } else if (jumps[3] == 1){
    jumpWest(x, y);
  }
}

function jumpNorth(x, y)
{
  setSq(x, y-1, empty);
  setSq(x, y-2, peg);
}
function jumpEast(x, y)
{
  setSq(x+1, y, empty);
  setSq(x+2, y, peg);
}
function jumpSouth(x, y)
{
  setSq(x, y+1, empty);
  setSq(x, y+2, peg);
}
function jumpWest(x, y)
{
  setSq(x-1, y, empty);
  setSq(x-2, y, peg);
}


function branchMove(x,y)
{
  setDecisionPeg(x, y);
  if (jumps[0] == 1){
    setBranchPeg(x, y-2);
  } 
  if (jumps[1] == 1){
    setBranchPeg(x+2, y);
  } 
  if (jumps[2] == 1){
    setBranchPeg(x, y+2);
  } 
  if (jumps[3] == 1){
    setBranchPeg(x-2, y);
  }
  updateStats();
}

/* (x,y) = square clicked to which to move */
function processBranchMove(x, y)
{
    for (var j=0; j<branch_ctr; j++)
    {
         if ( x==branch_x[j] && y==branch_y[j] ) 
         {
             setSq(dec_x, dec_y, empty);
             directedJump(dec_x, dec_y, x, y);
         } else {
             setSq(branch_x[j], branch_y[j], empty);
         }
    }
    mode = 0;
}

function directedJump(dec_x, dec_y, x, y)
{
  if (dec_x == x)
  {
    if (dec_y > y) {
      jumpNorth(dec_x, dec_y);
    } else {
      jumpSouth(dec_x, dec_y);
    }
  } else if (dec_x < x) {
      jumpEast(dec_x, dec_y);
  } else {
      jumpWest(dec_x, dec_y);
  }
}


function showInstructions()
{
    var ipath= "peg-solitaire-instructions";
    var entry_win=window.open(
                     ipath,
                     'Peg_Solitaire_Instructions',
                     'height=320,width=650');
    return false;
}

	//-------- Performance Display code -------------

function displayPerfData(response)
{
    genericDisplayPerfData('Peg Solitaire', response);
    return false;
}

// -->

