// Global variables
var selected= 0;        // currently highlighted ball
var solving= 0;         // state of solving function (0=off, 1=on)
var firstmove = 1;
var pinsleft = 15;
var start = new Date();
// 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; 

// 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
		middle= "ball"+ (from- 1);
    }
	else if (diff== -2) {
    // or alternatively, on the right hand side
		middle= "ball"+ (from+ 1);
    }
	else if (diff== 9) {
    // 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- 4.5);
    }
	else if (diff== -9) {
    // and no need to check this case either
		middle= "ball"+ (from+ 4.5);
	}
	else if (diff== 11) {
    // 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- 5.5);
    }
	else if (diff== -11) {
    // and no need to check this case either
		middle= "ball"+ (from+ 5.5);
	}

// 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 user_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= "images/ball_0.gif";
					document.images[middle].src= "images/ball_0.gif";
					document.images[where].src= "images/ball_2.gif";
					selected= where;
					pinsleft--;
					if(!moremoves())	{
						diff=new Date()-start;
						elapsed=new String(diff/1000);
						if(pinsleft==1)	{
							var msg=("You're a winner - great job! "+elapsed+" seconds");
							}
							else	{
								var msg=(pinsleft+" Balls left - No move moves... "+elapsed+" seconds");
						}
						//var xname = prompt("This is a test","");
//						var tname = prompt(msg+"\n\r"+"Can we get your name for the Triangle Hall of Fame?", "");
//						if (! tname)
//						{
//                            my_form = '<form method="post" action="some_action">'; 
//                            my_form +='<input type="text" name="tname">'; 
//                            my_form += '<\/form>'; 
//                            document.write(my_form); 
//                        } 
//						if(tname!="")
//						{
                            var tname = msg;
							var wk = document.all("hiddenparams");
							wk.value = tname+"`"+pinsleft+"`"+elapsed;
							//window.location.reload();
							__doPostBack('PSender1','');
							//document.Form1.Submit();
//						}
					}
				} 
				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 and firstmove
		}
		else if (img== "ball_1.gif" && firstmove) {
			document.images[where].src= "images/ball_0.gif";
			firstmove=0;
			pinsleft--;
			document.all['Instructions'].innerHTML="Now select a ball (by clicking), then 'jump' (horizontically or diagonally) to an empty spot to remove the (jumped) ball.";
			start=new Date();
		}
		// click on a red ball
		else if (img== "ball_1.gif") {
			if (selected) {
				document.images[selected].src= "images/ball_1.gif";
			}
			document.images[where].src= "images/ball_2.gif";
			selected= where;
		} // click on a selected (yellow) ball
		else if (img== "ball_2.gif") {
			document.images[selected].src= "images/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) {
		solving= 1; // show that we're currently involved in solving
		for (var i=0; i<(solution.length); i++) {
			setTimeout("user_play('ball" + solution[i] + "', 0)", i* 300);
		}
		setTimeout("solving= 0", i* 300); // 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) {
		for (var i=0; i<document.images.length; i++) {
			if (document.images[i].src.match("images/ball")) {
				document.images[i].src= "images/ball_1.gif";
			}
		}
		firstmove=1;
		pinsleft=15;
		document.all['Instructions'].innerHTML="Start by removing (clicking on) one ball - any ball.";
	}
}
function moremoves()
{
	var emptyCount=0;
	for (var i=0; i<document.images.length; i++) {
		if (document.images[i].src.match("images/ball_0")) {
			emptyCount++;
			for (var k=1;k<4;k++)	{
				if(k==1){
					var diff=2;
				}
				if(k==2){
					var diff=9;
				}
				if(k==3){
					var diff=11;
				}
				for (var j=1;j<3;j++)	{
					if(j==1){
						var sign=1;
					}
					if(j==2){
						var sign=-1;
					}
					var cur=document.images[i].name;
					wk=document.images[i].name.substring(4,document.images[i].name.length);
					wk=wk*1;
					if(document.images["ball"+(wk+(diff*sign))] && document.images["ball"+(wk+(diff*sign/2))])	{
						if(!document.images["ball"+(wk+(diff*sign))].src.match("images/ball_0") && !document.images["ball"+(wk+(diff*sign/2))].src.match("images/ball_0"))	{
							return emptyCount;
						}
					}
				}
			}					
		}
	}
	return 0;
}

