/*
   copyright (c) 2009, keith drakard
   this program is distributed under the terms of the creative commons
   license at http://creativecommons.org/licenses/by-nc-sa/2.0/
*/

  // dynamic globals
  var running= 1; var ox= -1; var oy= -1;
  var player= 1; var score= new Array(3);
  score[1]= 24; score[2]= 12;

  function preload() {
    if (document.images) {
      counter= new makeArray(7);
         counter[0].src= "/wp-content/scripts/images/common/blank.gif";
         counter[1].src= "/wp-content/scripts/images/hnefat/count1.gif";  // white
         counter[2].src= "/wp-content/scripts/images/hnefat/count2.gif";  // black
         counter[3].src= "/wp-content/scripts/images/hnefat/count3.gif";  // black king
         counter[4].src= "/wp-content/scripts/images/hnefat/count1b.gif"; // white      selected
         counter[5].src= "/wp-content/scripts/images/hnefat/count2b.gif"; // black      selected
         counter[6].src= "/wp-content/scripts/images/hnefat/count3b.gif"; // black king selected

    } else {
      alert("Sorry, this game needs to run on a browser\nwhich supports the image object.");
    }
  }

  function play(x,y) {
    if (running) {
      var piece_played= select(x,y);

      if (piece_played== 3) {
        // check if reached edge
        if (x==0 || x==10 || y==0 || y==10) {
          alert("Congratulations, player 2");
          running= 0;
        }
      }

      if (piece_played && running) {
        swap_player();
      }
    }
  }

  function select(x,y) {
    var c= 0; var what= document.images["x"+x+"y"+y].src;
    if (ox!= -1) var oldc= match(document.images["x"+ox+"y"+oy].src)- 3;

    if (ox!= -1 && what== counter[0].src) {
      // check move and return 1 if legal
      if (check_move(x,y,oldc)) {

        // move the selected piece
        document.images["x"+ox+"y"+oy].src= counter[0].src;
        ox= -1; oy= -1;
        document.images["x"+x+"y"+y].src= counter[oldc].src;

        // check if capture made (if piece isn't the king)
        if (oldc!= 3) check_capture(x,y);

        return(oldc);
      }

    } else {
      // select piece
      c= match(what);

      if ((player==1 && (c==1 || c==4)) || (player==2 && !(c==1 || c==4))) {
        if (c) {
          if (c> 3) {
            // deselect the already selected piece
            c-= 3;
            document.images["x"+x+"y"+y].src= counter[c].src;
            ox= -1; oy= -1;

          } else {
            if (ox!= -1) {
              // deselect the previously selected piece
              document.images["x"+ox+"y"+oy].src= counter[oldc].src;
            }  

            // select the new piece
            c+= 3;
            document.images["x"+x+"y"+y].src= counter[c].src;
            ox= x; oy= y;
          }
        }
      }
    }

    return(0);
  }

  function match(what) {
    for (i=1; i<=6; i++) {
      if (what== counter[i].src) return(i);
    }
    return(0);
  }

  function check_move(nx,ny,piece) {
    var dx= 0; var dy= 0;
    if (ox> nx) dx= -1; else if (ox< nx) dx= 1;
    if (oy> ny) dy= -1; else if (oy< ny) dy= 1;

    // check no diagonals (horizontal and vertical moves only):
    if (dx!=0 && dy!=0) return(0);

    // check nothing between old square and target square:
    var xx= dx; var yy= dy;
    var what_now= document.images["x"+(ox+xx)+"y"+(oy+yy)].src;
    while (what_now== counter[0].src) {

      // only the king allowed on or through the centre:
      if ((ox+xx== 5) && (oy+yy== 5) && piece!= 3) {
        alert("Only the king is allowed\nthrough the centre."); return(0);
      }

      // but the king can't travel more than three squares:
      if (piece== 3 && (xx<-3 || xx>3 || yy<-3 || yy> 3)) {
        alert("The king cannot travel\nmore than 3 squares."); return(0);
      }

      // otherwise, see if we're at the target:
      if ((dx && (ox+xx)== nx) || (dy && (oy+yy)== ny)) return(1);

      // or carry on:
      xx+= dx; yy+= dy;
      what_now= document.images["x"+(ox+xx)+"y"+(oy+yy)].src
    }

    return(0);
  }

  function check_capture(x,y) {
    // left/right & up/down
    check_c(-2,0,x,y); check_c(2,0,x,y);
    check_c(0,-2,x,y); check_c(0,2,x,y);
  }


  function check_c(dx,dy,x,y) {
    // within board area?
    if (x+dx>=0 && x+dx<11 && y+dy>=0 && y+dy<11) {

      if (dx) middle= document.images["x"+(x+dx/2)+"y"+y].src;
        else middle= document.images["x"+x+"y"+(y+dy/2)].src;

      if (middle== counter[3].src && player== 1) {
        // king capture?
        if (dx && (y>0 && y<10)) {
          t1= document.images["x"+(x+dx)+"y"+y].src;
              if (x+dx==5 && y==5) t1= counter[1].src;
          t2= document.images["x"+(x+dx/2)+"y"+(y+1)].src;
              if (x+dx/2==5 && y+1==5) t2= counter[1].src;
          t3= document.images["x"+(x+dx/2)+"y"+(y-1)].src;
              if (x+dx/2==5 && y-1==5) t3= counter[1].src;
          if ((t1== t2) && (t2== t3) && (t3== counter[1].src)) {
            document.images["x"+(x+dx/2)+"y"+y].src= counter[0].src;
            alert("Congratulations, player 1"); running= 0;
          }

        } else if (x>0 && x<10) {
          t1= document.images["x"+x+"y"+(y+dy)].src;
              if (x==5 && y+dy==5) t1= counter[1].src;
          t2= document.images["x"+(x+1)+"y"+(y+dy/2)].src;
              if (x+1==5 && y+dy/2==5) t2= counter[1].src;
          t3= document.images["x"+(x-1)+"y"+(y+dy/2)].src;
              if (x-1==5 && y+dy/2==5) t3= counter[1].src;
          if ((t1== t2) && (t2== t3) && (t3== counter[1].src)) {
            document.images["x"+x+"y"+(y+dy/2)].src= counter[0].src;
            alert("Congratulations, player 1"); running= 0;
          }
        }


      } else if (middle== counter[3-player].src) {
        // ordinary capture?
        target= document.images["x"+(x+dx)+"y"+(y+dy)].src;
        if (target== counter[player].src) {
          if (dx) document.images["x"+(x+dx/2)+"y"+y].src= counter[0].src;
            else document.images["x"+x+"y"+(y+dy/2)].src= counter[0].src;

          score[3-player]--;
          // white needs at least 3 pieces to capture the king...
          if (score[1]< 3) {
            alert("Congratulations, player 2"); running= 0;
          }
        }
      }
    }
  }


  function setup_board() {
    // top & bottom white
    for (x=3; x<8; x++) {
      document.images["x"+x+"y0"].src= counter[1].src;
      document.images["x"+x+"y10"].src= counter[1].src;
    }
    document.images["x5y1"].src= counter[1].src;
    document.images["x5y9"].src= counter[1].src;
    // left & right white
    for (y=3; y<8; y++) {
      document.images["x0y"+y].src= counter[1].src;
      document.images["x10y"+y].src= counter[1].src;
    }
    document.images["x1y5"].src= counter[1].src;
    document.images["x9y5"].src= counter[1].src;

    // top & bottom black
    for (y=2; y<9; y++) {
      if (y!=5) document.images["x5y"+y].src= counter[2].src;
    }
    // left & right black
    for (x=2; x<9; x++) {
      if (x!=5) document.images["x"+x+"y5"].src= counter[2].src;
       else document.images["x5y5"].src= counter[3].src;
    }
  }

  function newgame() {
    // same options so just reset the board
    running= 1; ox= -1; oy= -1;
    score[1]= 24; score[2]= 12;
    if (player!= 1) swap_player();
    for (y=0; y<11; y++) {
      for (x=0; x<11; x++) {
        document.images["x"+x+"y"+y].src= counter[0].src;
      }
    }
    setup_board();
  }

  function help() {
    alert("\nBlack has to get their king to any perimeter square.\nWhite has to capture the king before this happens.\n\nPieces move horizontally or vertically over any number\nof empty squares. The king can move up to 3 squares.\nNo piece can move diagonally, and only the king can\nmove onto or through the centre square.\n\nCaptures are done by flanking the enemy. However,\nyou may safely move your own piece between two\nenemy pieces.\n\nTo capture the king, he must be surrounded on all four\nsides by the enemy or when his only move is to the centre\nsquare. The king cannot assist in any captures.\n\n");
  }

  function swap_player() {
    // just a "decorative" function really...
    document.images[player+"a"].src= counter[0].src;
    document.images[player+"b"].src= counter[0].src;
    player= 3- player;
    document.images[player+"a"].src= "/wp-content/scripts/images/common/notblank.gif";
    document.images[player+"b"].src= "/wp-content/scripts/images/common/notblank.gif";
  }


  // The following function was written by Martin Webb at http://www.irt.org/
  function makeArray(n) {
    this.length= n; for (i=0; i<n; i++) { this[i] = new Image(); }
    return this;
  }


function initialise() {
  var output= '';

  output+= '<table align=center cellspacing=12 cellpadding=0 border=0><tr>\n';

  // create the board
  output+= '<td align=center valign=top><table cellspacing=0 cellpadding=0 border=1>\n';
  for (y=0; y<11; y++) {
    output+= '<tr bgcolor="#7f3f00">';
    for (x=0; x<11; x++) {
      output+= '<td'; if (x==5 && y==5) output+= ' bgcolor="#662200"';
      output+= '><a href="javascript:play('+x+','+y+')" onFocus="blur();"><img src="/wp-content/scripts/images/common/blank.gif"';
      output+= ' name="x'+x+'y'+y+'" width=35 height=35 alt="" border=0></a></td>';
    }
    output+= '</tr>\n';
  }
  output+= '</table></td>\n';

  // and create the whose-go-is-it indicator + score etc
  output+= '<td valign=top><form name="user"><table cellspacing=0 cellpadding=4 border=0>\n';
  output+= '<tr bgcolor="#882200"><td align=center><b>Player</b></td></tr>\n';
  output+= '<tr bgcolor="#882200"><td align=center><img src="/wp-content/scripts/images/common/notblank.gif" name="1a" width=4 height=35 alt="">&nbsp;';
  output+= '<img src="/wp-content/scripts/images/hnefat/count1.gif" width=35 height=35 alt="">&nbsp;';
  output+= '<img src="/wp-content/scripts/images/common/notblank.gif" name="1b" width=4 height=35 alt=""></td></tr>\n';
  output+= '<tr bgcolor="#882200"><td align=center><img src="/wp-content/scripts/images/common/blank.gif" name="2a" width=4 height=35 alt="">&nbsp;';
  output+= '<img src="/wp-content/scripts/images/hnefat/count2.gif" width=35 height=35 alt="">&nbsp;';
  output+= '<img src="/wp-content/scripts/images/common/blank.gif" name="2b" width=4 height=35 alt=""></td></tr>\n';

  output+= '<tr><td>&nbsp;</td></tr>';
  output+= '<tr bgcolor="#882200"><td align=center>';
  output+= '<input type="button" value="New" onClick="newgame()"></td></tr>\n';

  output+= '<tr><td>&nbsp;</td></tr>';
  output+= '</table></form></td>\n';

  output+= '</tr></table>\n';

  document.write(output);
  preload(); setup_board();
}
