  // 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/
  
  var IMAGESBEFORE= 1; // no of images in html page before the game
  
//================================================================================
//
//              JavaScript Solitaire v0.10
//
// Author:      Keith Drakard 
// Date:        12th December 1997
// Abstract:    Remove marbles by jumping over them until only one is left
//              in the center.
//
//
// THE SOLITAIRE BOARD:
// |-01-|-02-| 03 | 04 | 05 |-06-|-07-|
// |-08-|-09-| 10 | 11 | 12 |-13-|-14-|
// | 15 | 16 | 17 | 18 | 19 | 20 | 21 |
// | 22 | 23 | 24 | 25 | 26 | 27 | 28 |
// | 29 | 30 | 31 | 32 | 33 | 34 | 35 |
// |-36-|-37-| 38 | 39 | 40 |-41-|-42-|
// |-43-|-44-| 45 | 46 | 47 |-48-|-49-|
// 

	 // Global variables
	 var selected= 0;                // currently highlighted ball
	 var solving= 0;                 // state of solving function (0=off, 1=on)
	 // SOLUTION 1:
	 var solution= new Array(11, 25, 20, 18, 5, 19, 26, 12, 40, 26, 35, 33, 32,
							 34, 21, 35, 33, 17, 19, 15, 17, 30, 32, 34, 20, 18,
							 16, 3, 5, 19, 33, 45, 31, 24, 38, 47, 45, 31, 29,
							 15, 17, 10, 24, 38, 40, 26, 24, 23, 25, 25);
							 
	 // preload images and check browser
	 if (document.images) {
	   // all four images used are just 678 bytes all together..
	   // I really think pre-loading is a bit OTT here :)
	 } else { alert("Image property not supported... this game will not work on your browser."); }


	 // check to make sure the move is a legal one
	 function check_move(from, to) {
	   var middle= "";       // the board number of the ball in the middle of
							 // "from" and "to" ELSE "" if illegal move

	   from= from* 1; to= to* 1; // convert from string to number
								 // could also do: to= (to- 0);
	   var diff= from- to; var xpos= (from% 7);

	   // first we check to see if "diff" equals either 2, -2, 14 or -14
	   // because that holds true for all valid moves only (ie: it is two spaces
	   // away in a direct line - due to the setup of the board)
	   if (diff== 2) {
		 // now we check to see if a wraparound of "from" and "to" has occured
		 // on the left hand side of the board
		 if (!(xpos==1 || xpos==2)) { middle= "ball"+ (from- 1); }

	   } else if (diff== -2) {
		 // or alternatively, on the right hand side
		 if (!(xpos==6 || xpos==0)) { middle= "ball"+ (from+ 1); }

	   } else if (diff== 14) {
		 // there's a no need to check this case
		 // because of the setup of the board (numbers: left to right, top to bottom)
		 middle= "ball"+ (from- 7);

	   } else if (diff== -14) {
		 // and no need to check this case either
		 middle= "ball"+ (from+ 7);

	   } else { // illegal move, but we flag it later
			  }

	   // okay, so it's a valid move, but lets just make sure that there _is_
	   // a marble to be jumped over...
	   if (middle!= "") {
		 var len= document.images[middle].src.length;
		 var img= document.images[middle].src.substring(len-10, len);
		 if (img== "ball_0.gif") { middle=""; } // doh! no marble, no move
	   }

	   // we've done our checks and now we return the board number of the marble in
	   // the middle or, if the move is illegal, we return a default ""

	   return (middle);
	 }


	 // deal with user input
	 function play(where, whatis) {
	  if ((whatis && !solving) || (!whatis && solving)) {
	   var len= document.images[where].src.length;
	   var img= document.images[where].src.substring(len-10, len);

	   // click on a hollow
	   if (img== "ball_0.gif") {
		 if (selected) {
		   var sel= selected.substring(4, selected.length);
		   var wh= where.substring(4, where.length);

		   var middle= check_move(sel, wh);
		   if (middle!= "") {
			 document.images[selected].src= "/wp-content/scripts/images/sol/ball_0.gif";
			 document.images[middle].src= "/wp-content/scripts/images/sol/ball_0.gif";
			 document.images[where].src= "/wp-content/scripts/images/sol/ball_2.gif"; selected= where;
		   } else { alert("Only horizontal and vertical jumps over one ball are allowed."); }
		 } else { alert("Nothing to move to here.. Select a ball first!"); }

	   } // click on a red ball
		 else if (img== "ball_1.gif") {
		 if (selected) {
		   document.images[selected].src= "/wp-content/scripts/images/sol/ball_1.gif";
		 }
		 document.images[where].src= "/wp-content/scripts/images/sol/ball_2.gif"; selected= where;

	   } // click on a selected (yellow) ball
		 else if (img== "ball_2.gif") {
		 document.images[selected].src= "/wp-content/scripts/images/sol/ball_1.gif"; selected= "";

	   } else { alert("uh-oh... program error"); }
	  }
	 }


	 // step through the solution array, and simulate a mouse click on the
	 // board position represented by the array
	 function solve() {
	   if (document.images && !solving) {
		 set_up(); solving= 1; // show that we're currently involved in solving
		 for (demo=demo_state; demo<(solution.length); demo++) {
		   setTimeout("play('ball" + solution[demo] + "', 0)", demo* 1000);
		 }
		 setTimeout("solving= 0", demo* 1000); // and now we've finished
	   }
	 }


	 // reset the board
	 // NB: adding images before the solitaire board on the same HTML page
	 //     will mess things up. Add the noof images to [i] & [24] or use
	 //     the image NAME.. that's what it's there for..
	 function set_up() {
	  if (document.images && !solving) {
	   demo_state= 0;
	   for (var i=2; i<47; i++) {
		 if (!(i>4 && i<9) && (i!=12 && i!=13 && i!=24 && i!=35 && i!=36) && !(i>39 && i<44)) {
		   document.images[(i+IMAGESBEFORE)].src= "/wp-content/scripts/images/sol/ball_1.gif";
		 }
	   }
	   document.images[(24+IMAGESBEFORE)].src= "/wp-content/scripts/images/sol/ball_0.gif";
	  }
	 }


function initialise() {
	var output = '';
    output+= '<table align="center" border="0" width="280" cellspacing="0" cellpadding="0" bgcolor="#00ff00">\n';
    output+= '<tr><td>&nbsp;&nbsp;&nbsp;</td><td colspan="7">&nbsp;</td><td>&nbsp;&nbsp;&nbsp;</td></tr>\n';

    var count= 1;

    // build up the board
    for (var i=0; i<7; i++) {
      if (i<2 || i>4) {
        // we're doing a row with 3 on
        output+= '<tr><td>&nbsp;</td><td align="center"><img src="/wp-content/scripts/images/sol/green.gif" width="40" height="40" border="0"></td><td align="center"><img src="/wp-content/scripts/images/sol/green.gif" width="40" height="40" border="0"></td>'; count+= 2;
        for (var j=0; j<3; j++) {
           output+= '<td align="center"><a href="javascript:play(\'ball'+ count +'\', 1);" onfocus="blur();"><img src="/wp-content/scripts/images/sol/ball_1.gif" width="40" height="40" border="0" name="ball'+ count +'"></a></td>'; count++;
        }
        output+= '<td align="center"><img src="/wp-content/scripts/images/sol/green.gif" width="40" height="40" border="0"></td><td align="center"><img src="/wp-content/scripts/images/sol/green.gif" width="40" height="40" border="0"></td><td>&nbsp;</td></tr>\n'; count+= 2;
      } else {
        // we're doing a full row of 7
        output+= '<tr><td>&nbsp;</td>';
        for (var j=0; j<7; j++) {
           if (count == 25) { output+= '<td align="center"><a href="javascript:play(\'ball'+ count +'\', 1);" onfocus="blur();"><img src="/wp-content/scripts/images/sol/ball_0.gif" width="40" height="40" border="0" name="ball'+ count +'"></a></td>'; count++;
           } else { output+= '<td align="center"><a href="javascript:play(\'ball'+ count +'\', 1);" onfocus="blur();"><img src="/wp-content/scripts/images/sol/ball_1.gif" width="40" height="40" border="0" name="ball'+ count +'"></a></td>'; count++;
           }
        }
        output+= '<td>&nbsp;</td></tr>\n';
      }
    }
	
	output+= '<tr><td colspan="9">&nbsp;</td></tr>\n</table>\n';
    output+= '<br><form><center><input type="button" onClick="set_up();" value="reset board" style="width:30%"><br><br><input type="button" onClick="solve();" value="demo solution" style="width:30%"></center></form>';

	document.write(output);	
}
