// This is where the hole is
var col;
var row;
var state=0;
var moves=0;

var girl;

// Init the array
var pieces = new Array;

var newgameTimer;

function newgame() {
	if(newgameTimer) {
		return;
	}

	if(state == 1) {
		// shuffle in progress
		return;
	}

	girl = girls[randint(girls.length)];

	i = 1;
	for(r = 0; r < ysize; r++) {
		for(c = 0; c < xsize; c++) {
			pieces[c + (ysize*r)] = i;
			eval("window.document.playfield.x"+r+"x"+c+
				"x.src='images/"+girl+"/"+i+".gif'");
			i++;
		}
	}

	newgameTimer = setTimeout("newgame2();", 1000);
}

function newgame2() {
	newgameTimer = 0;
	col=xsize-1;
	row=ysize-1;
	moves=0;
	updateDisplay();
	startshuffle();
}

function solvit() {
	i = 1;
	for(r = 0; r < ysize; r++) {
		for(c = 0; c < xsize; c++) {
			pieces[c + (ysize*r)] = i;
			eval("window.document.playfield.x"+r+"x"+c+
				"x.src='images/"+girl+"/"+i+".gif'");
			i++;
		}
	}
	state = 0;
	setstatus("");
	moves=0;
	updateDisplay();
}

function movePiece(r, c, newr, newc) {
	// we swap the two elements
	newidx=(newc + (ysize*newr));
	idx=(c + (ysize*r));

	tmp = pieces[idx];
	pieces[idx] = pieces[newidx];
	pieces[newidx] = tmp;

	eval("window.document.playfield.x"+newr+"x"+newc+
		"x.src='images/"+girl+"/"+tmp+".gif'");
	eval("window.document.playfield.x"+r+"x"+c+
		"x.src='images/"+girl+"/"+pieces[idx]+".gif'");
}

function move(r, c) {
	if(state != 2) {
		return;
	}

	setstatus("");

	if(r != row && c != col) {
		setstatus("That's not legal");
		return;
	}

	if(r == row && c == col) {
		// clicked on the hole. weird.
		return;
	}

	moves++;

	doMove(r, c);

	updateDisplay();
}

function shufflemove(r, c) {
	if(state != 1) {
		setstatus("Internal error");
		state = 0;
		return;
	}
	doMove(r, c);
}

function doMove(r, c) {
	if(r >= ysize || r < 0) {
		alert("row out of range");
		return;
	}
	if(c >= xsize || c < 0) {
		alert("col out of range");
		return;
	}
	
	if(r == row) {
		// we know c != col
		if(c > col) {
			// we're moving stuff left
			for(i = col; i < c; i++) {
				movePiece(r, i+1, r, i);
			}
		} else {
			// we're moving stuff right
			for(i = col; i > c; i--) {
				movePiece(r, i-1, r, i);
			}
		}
	}
	if(c == col) {
		// we know r != row
		if(r > row) {
			// we're moving stuff up 
			for(i = row; i < r; i++) {
				movePiece(i+1, c, i, c);
			}
		} else {
			// we're moving stuff down
			for(i = row; i > r; i--) {
				movePiece(i-1, c, i, c);
			}
		}
	}
	row=r;
	col=c;

	// Check for a win
	i = 1;
	won=1;
	for(c = 0; c < xsize; c++) {
		for(r = 0; r < ysize; r++) {
			if(pieces[c + (ysize*r)] != i) {
				won=0;
			}
			i++;
		}
	}

	if(state == 2 && won) {
		setstatus("You won!");
		state=0;
	}
}
// return an int between 0 and max-1
function randint(max) {
	return(Math.floor(Math.random() * (max - 0.5)));
}

function shuffle(count) {
	// do one move
	choose=randint(2);

	if(choose == 1) {
		// row
		tmp=randint(ysize);
		if(tmp == row) {
			tmp++;
			if(tmp == ysize) {
				tmp = 0;
			}
		}
		// But take the longest push if we can
		if(row == 0 && (count % 2) == 1) {
			tmp = ysize-1;
		}
		shufflemove(tmp, col);
	} else {
		// col
		tmp=randint(xsize);
		if(tmp == col) {
			tmp++;
			if(tmp == xsize) {
				tmp = 0;
			}
		}
		// But take the longest push if we can
		if(col == 0 && (count % 2) == 1) {
			tmp = xsize-1;
		}
		shufflemove(row, tmp);
	}
	if(count <= 1) {
		setstatus("Running");
		state=2; // running
		return;
	}
	count--;
	setTimeout("shuffle("+count+")", 35);
}

function startshuffle() {
	setstatus("Shuffling");
	state=1; // shuffling
	count=75+randint(30);
	shuffle(count);
}

function updateDisplay() {
	document.status.moves.value = moves;
}

function setstatus(s) {

	document.status.status.value = s;
	return;

	if(s.length == 0) {
		document.status.status.value = "";
		return;
	}
	fill = (32 - s.length) / 2;

	tmp = "";
	for(i = 0; i < fill; i++) {
		if((i % 2) == 0) {
			tmp += "-";
		} else {
			tmp += " ";
		}
	}
	tmp += s;

	for(i = tmp.length; i < 32; i++) {
		if((i % 2) == 0) {
			tmp += "-";
		} else {
			tmp += " ";
		}
	}
	document.status.status.value = tmp;
}
