// moosesquirrel.js Copyright (c) 2000 by P. Martin Beck

function squirrel(theid, thedirection, theposition){
	this.id = theid;
	this.direction = thedirection;
	this.position = theposition;
}	//squirrel

function movesquirrel(theposition){
	var thedirection;
	if (squirrelarray[theposition] == null){
		alert("There's no squirrel there, silly!");
		return;
	}
	thedirection = Number(squirrelarray[theposition].direction);
	if (theposition + thedirection < 0 ||
		theposition + thedirection > 7){
		alert("Don't make the poor squirrel jump off the \
antler!");
		return;
	} //if
	if (squirrelarray[theposition + thedirection] == null)
		switcheroo (theposition, (theposition + thedirection));
	else if (theposition + (2*thedirection) >= 0 &&
		theposition + (2*thedirection) <= 7 &&
		squirrelarray[theposition + (2*thedirection)] == null)
		switcheroo (theposition, (theposition + (2*thedirection)));
	else
		alert("Alas, there is no legal move for that squirrel to make.");
}	// movesquirrel

function switcheroo(index1, index2){
	var temp = squirrelarray[index1];
	squirrelarray[index1] = squirrelarray[index2];
	squirrelarray[index2] = temp;
	redraw(index1);
	redraw(index2);
	wincheck(squirrelarray);
	document.undoform.undo1.value = index1;
	document.undoform.undo2.value = index2;
}

function redraw(index){
	if(squirrelarray[index] == null)
		document.images[index].src = "blank.gif";
	else if(squirrelarray[index].direction == 1)
		document.images[index].src = "greysquirrel.gif";
	else if(squirrelarray[index].direction == -1)
		document.images[index].src = "brownsquirrel.gif";
	else
		alert("There's been a screw-up somewhere.");
}

function wincheck(thearray){
	for(var i=0; i<3; ++i)
		if (thearray[i] == null || thearray[i].direction == 1)
			return;
	for(i=3; i<5; ++i)
		if (thearray[i] != null)
			return;
	for(i=5; i<8; ++i)
		if (thearray[i] == null || thearray[i].direction == -1)
			return;
	alert("You did it! Congratulations!");
}	// wincheck
