function animateMove(xmove, smove) {
// alert('animateMove ' + xmove + ', ' + smove + ' curAnimateMove=' + curAnimateMove + ' animateCount=' + animateCount);
 	if(xmove == curAnimateMove && xmove >= 0) {
//	 	 	if(animateCount == 0) { alert("ANIMATE "+ smove); }
		if(xmove > 1 && xmove < 100) { animateCount+=5; } else { animateCount+=15; }
		if(animateCount > 100) { animateCount = 100; }
 	 	var mImg;
 	 	var dTop = -10;
 	 	if(smove.substring(1,2) == "B") {
			mImg = document.getElementById("ball");
			dTop = -4;
			mImg.style.width = 25;
			mImg.style.height = 25;
		} else {
			mImg = document.getElementById("player" + smove.substring(1,2));
			mImg.style.width = 23;
			mImg.style.height = 37;
		}
		var topStart = dTop + getPosX(smove.substring(2,4),iam) * sheight;
		var leftStart = 2 + getPosY(smove.substring(2,4),iam) * swidth;
		var topEnd = dTop + getPosX(smove.substring(4,6),iam) * sheight;
		var leftEnd = 2 + getPosY(smove.substring(4,6),iam) * swidth;
		
		var topx = ((100 - animateCount) * topStart + animateCount * topEnd)/100;
		var leftx = ((100 - animateCount) * leftStart + animateCount * leftEnd)/100;
		mImg.style.top = topx;
		mImg.style.left = leftx;
		
		if(smove.substring(1,2) != "B" && smove.substring(1,2) != "0" && smove.substring(1,2) != "5" && numbersonback > 0) {
		 	var i = smove.substring(1,2);
			var numberImg = document.getElementById('backnum' + i);
			numberImg.style.width = 6;
			numberImg.style.height = 9;
			var di, dj;
			if((i < 5 && iam != 1) || (i > 5 && iam == 1)) {
				topx += 25 - 12 * numbersonback;
				leftx += 9 * numbersonback;
			} else {
				topx += 3 + 12 * numbersonback;
				leftx += 19 - 10 * numbersonback;
			}
			numberImg.style.top = topx;
			numberImg.style.left = leftx; 	 	
		}
	
		if(animateCount < 100) {
			setTimeout("animateMove(" + xmove + ", '" + smove + "')", 5);
		} else {
	 	 	if(smove.length > 6) {
				if(smove.substring(6,7) == ":") {
					smove = smove.substring(6);
				} else {
					smove = smove.substring(0,2) + smove.substring(4);
				}
				animateCount = 0;
				setTimeout("animateMove(" + xmove + ", '" + smove + "')", 5);
			} else {
			 	stopAnimation(false);
			}
		}
	}
}

function startAnimateMove(xmove, smove) {
// 	if(xmove < move.length) {
// alert("startAnimateMove " + xmove + " " + smove);
	 	animateCount = 0;
	 	curAnimateMove = xmove;
	 	var apath = "";
	 	var postaken = "";
	 	if(xmove == 0) {
			for(var i=0; i<5; i++) {
				apath = apath + ":" + i + "G0" +
				getAnimationPathPlayerX("G0", "G0", smove.substring(i*2+1,i*2+3), 99, true, postaken);
				postaken = postaken + smove.substring(i*2+1,i*2+3);
			}
		}
	 	if(xmove == 1) {
			for(var i=5; i<10; i++) {
				apath = apath + ":" + i + "F0" + 
				getAnimationPathPlayerX("F0", "F0", smove.substring(i*2+1,i*2+3), 99, true, postaken);
				postaken = postaken + smove.substring(i*2+1,i*2+3);
			}
		}
	 	if(xmove > 1 && xmove < 100) { 
	 	 	if(xmove == 2) { ballpos = smove.substring(0,2); }
			apath = getAnimationPath(smove, (xmove >= 90));
		}
		if(xmove >= 100) {
			var iP = (smove.length-3) / 2 + (xmove - 100) * 5;
			var sPos;
			postaken = smove.substring(1, smove.length-2);
			if(xmove == 100) { sPos = "F0";} else { sPos = "G0"; }
			apath = ":" + iP + sPos + getAnimationPathPlayerX(sPos, sPos, smove.substring(smove.length-2,smove.length), 99, true, postaken);
//			alert(iP + ":" + sPos + "-" + smove.substring(smove.length-2,smove.length) + "  " + postaken + " >> " + apath);
		}
	 	clearMoveInd();	
//alert("Animation path=" + apath);
//	window.status = xmove + ":" + smove + " ball at " + ballpos + " >>> Animation path=" + apath;
		if(apath != "") {
			startAnimation();
	 		animateMove(xmove, apath);	
	 	} else {
			stopAnimation(false);
		}
// 	}
}

function getAnimationPath(xmove, unfinishedmove) {
	xmove = xmove.replace("+", "");
	xmove = xmove.replace("!", "");
	var startPos = xmove.substring(0,2);
	var endPos = xmove.substring(2,4);
	var ballMoves = 0;
	var apath = "";
	var kickout = false;
	var goalscored = false;
	if(ballpos.substring(0,1) == "A" || ballpos.substring(0,1) == "L") { 
	 	ballpos = endPos; 
	 	kickout = true;
	}
	var iP = 0;
	while(positions[iP] != startPos) { iP++; }
 	var ballStart = ballpos;
	if(xmove.length > 4) {
	 	positions[iP] = ballpos;
	 	apath = ":B" + ballStart;
		for(var i=4; i<xmove.length; i=i+2) {
			var ballEnd = xmove.substring(i,i+2);
			apath = apath + getAnimationPathBall(ballStart, ballEnd);
			ballMoves += Math.max(Math.abs(ballStart.charCodeAt(0) - ballEnd.charCodeAt(0)), Math.abs(ballStart.charCodeAt(1) - ballEnd.charCodeAt(1))) - 1;
			ballStart = ballEnd;
		}
	 	positions[iP] = startPos;
	}
	var numMoves = lastroll - ballMoves;
	if(xmove.length > 4 || ballStart.substring(0,1) == "A" || ballStart.substring(0,1) == "L") {
		if(ballStart.substring(0,1) == "A" || ballStart.substring(0,1) == "L") {
			goalscored = true;
		}
		if(endPos != ballpos) {
			apath = apath + ":" + iP + ballpos + endPos;
			endPos = ballpos;
		}
	}
//	alert("apath:"+ apath + " ballpos:" + ballpos + " endPos:" + endPos );
//	alert(lastroll + "-" + ballMoves + "=>" + numMoves);
	if(kickout) {
	 	if(startPos != endPos) {
			apath = ":" + iP + startPos + getAnimationPathPlayer(startPos, endPos, 99, true) + apath;
		}
	} else {
	 	if(startPos != ballpos) {
	 	 	if(unfinishedmove) {
				apath = ":" + iP + startPos + getAnimationPathPlayer(startPos, endPos, numMoves, true) + apath;
			} else {
				apath = ":" + iP + startPos + getAnimationPathPlayer(startPos, endPos, numMoves, goalscored) + apath;
			}
		}
	}
	return apath;
}

function getAnimationPathBall(ballStart, ballEnd) {
	var xdiv = ballEnd.charCodeAt(0) - ballStart.charCodeAt(0);
	var ydiv = ballEnd.charCodeAt(1) - ballStart.charCodeAt(1);
	var dx = 0;
	var dy = 0;
	if(xdiv != 0) { dx = xdiv / Math.abs(xdiv); }
	if(ydiv != 0) { dy = ydiv / Math.abs(ydiv); }
	var apath = "";
	if(dx * xdiv > dy * ydiv) {
		var i = 0;
		var ok = true;
		var newpos = ballStart;
		// try hooked, then straight
		while(ok && i < dy * ydiv) {
			newpos = String.fromCharCode(newpos.charCodeAt(0) + dx, newpos.charCodeAt(1) + dy);
		 	apath = apath + newpos;
			ok = (newpos == ballEnd || squareIsEmpty(newpos));
			if(ok && (newpos.substring(0,1) == "A" || newpos.substring(0,1) == "L")) {
				ok = (apath.substring(apath.length-3,apath.length-2) == "3" || apath.substring(apath.length-3,apath.length-2) == "4");
			}
			i++;
		}
		while(ok && i < dx * xdiv) {
			newpos = String.fromCharCode(newpos.charCodeAt(0) + dx, newpos.charCodeAt(1));
		 	apath = apath + newpos;
			ok = (newpos == ballEnd || squareIsEmpty(newpos));
			if(ok && (newpos.substring(0,1) == "A" || newpos.substring(0,1) == "L")) {
				ok = (apath.substring(apath.length-3,apath.length-2) == "3" || apath.substring(apath.length-3,apath.length-2) == "4");
			}
			i++;
		}
		if(!ok) {
			i = 0;
			ok = true;
			apath = "";
			var newpos = ballStart;
			// try straight, then hooked
			while(ok && i < dx * xdiv - dy * ydiv) {
				newpos = String.fromCharCode(newpos.charCodeAt(0) + dx, newpos.charCodeAt(1));
			 	apath = apath + newpos;
				ok = (newpos == ballEnd || squareIsEmpty(newpos));
				if(ok && (newpos.substring(0,1) == "A" || newpos.substring(0,1) == "L")) {
					ok = (apath.substring(apath.length-3,apath.length-2) == "3" || apath.substring(apath.length-3,apath.length-2) == "4");
				}
				i++;
			}
			while(ok && i < dx * xdiv) {
				newpos = String.fromCharCode(newpos.charCodeAt(0) + dx, newpos.charCodeAt(1) + dy);
			 	apath = apath + newpos;
				ok = (newpos == ballEnd || squareIsEmpty(newpos));
				i++;
			}
		}		
	} else {
		var i = 0;
		var ok = true;
		var newpos = ballStart;
		// try hooked, then straight
		while(ok && i < dx * xdiv) {
			newpos = String.fromCharCode(newpos.charCodeAt(0) + dx, newpos.charCodeAt(1) + dy);
		 	apath = apath + newpos;
			ok = (newpos == ballEnd || squareIsEmpty(newpos));
			if(ok && (newpos.substring(0,1) == "A" || newpos.substring(0,1) == "L")) {
				ok = (apath.substring(apath.length-3,apath.length-2) == "3" || apath.substring(apath.length-3,apath.length-2) == "4");
			}
			i++;
		}
		while(ok && i < dy * ydiv) {
			newpos = String.fromCharCode(newpos.charCodeAt(0), newpos.charCodeAt(1) + dy);
		 	apath = apath + newpos;
			ok = (newpos == ballEnd || squareIsEmpty(newpos));
			if(ok && (newpos.substring(0,1) == "A" || newpos.substring(0,1) == "L")) {
				ok = (apath.substring(apath.length-3,apath.length-2) == "3" || apath.substring(apath.length-3,apath.length-2) == "4");
			}
			i++;
		}
		if(!ok) {
			i = 0;
			ok = true;
			apath = "";
			var newpos = ballStart;
			// try straight, then hooked
			while(ok && i < dy * ydiv - dx * xdiv) {
				newpos = String.fromCharCode(newpos.charCodeAt(0), newpos.charCodeAt(1) + dy);
			 	apath = apath + newpos;
				ok = (newpos == ballEnd || squareIsEmpty(newpos));
				if(ok && (newpos.substring(0,1) == "A" || newpos.substring(0,1) == "L")) {
					ok = (apath.substring(apath.length-3,apath.length-2) == "3" || apath.substring(apath.length-3,apath.length-2) == "4");
				}
				i++;
			}
			while(ok && i < dy * ydiv) {
				newpos = String.fromCharCode(newpos.charCodeAt(0) + dx, newpos.charCodeAt(1) + dy);
			 	apath = apath + newpos;
				ok = (newpos == ballEnd || squareIsEmpty(newpos));
				i++;
			}
		}				
	}
//	alert("BALLPATH " + ballStart + "-" + ballEnd + "= " + apath);
	return apath;
}

function getAnimationPathPlayer(startPos, endPos, numMoves, shortestpath) {
 	return getAnimationPathPlayerX(startPos, startPos, endPos, numMoves, shortestpath, "");
}

function getAnimationPathPlayerX(orgStartPos, startPos, endPos, numMoves, shortestpath, pathsofar) {
// 	alert("ANIMATE PLAYER " + startPos + "-" + endPos + " in " + numMoves + "  :: " + xdiv + "," + ydiv + " " + dx + "," + dy + " :: " + shortestpath + " :: " + pathsofar);
	var xdiv = endPos.charCodeAt(0) - startPos.charCodeAt(0);
	var ydiv = endPos.charCodeAt(1) - startPos.charCodeAt(1);
	var dx = 0;
	var dy = 1;
	if(xdiv != 0) { dx = xdiv / Math.abs(xdiv); }
	if(ydiv != 0) { dy = ydiv / Math.abs(ydiv); }
	var apath = "";
// 	alert("ANIMATE PLAYER " + startPos + "-" + endPos + " in " + numMoves + "  :: " + xdiv + "," + ydiv + " " + dx + "," + dy + " :: " + shortestpath + " :: " + pathsofar);
 	if(numMoves == 0) {
		apath = endPos;
	} else {
		if(dx * xdiv + dy * ydiv <= numMoves) {
		 	if(dx != 0) { 
				apath = getPartialPath(orgStartPos, startPos, dx, 0, endPos, numMoves, shortestpath, pathsofar);
			}
			if(apath == "") { 
				apath = getPartialPath(orgStartPos, startPos, 0, dy, endPos, numMoves, shortestpath, pathsofar);
			}
		 	if(apath == "" && dx == 0) { 
				apath = getPartialPath(orgStartPos, startPos, 1, 0, endPos, numMoves, shortestpath, pathsofar);
			}
			if(apath == "" && dx != 0) { 
				apath = getPartialPath(orgStartPos, startPos, -dx, 0, endPos, numMoves, shortestpath, pathsofar);
			}
			if(apath == "") { 
				apath = getPartialPath(orgStartPos, startPos, 0, -dy, endPos, numMoves, shortestpath, pathsofar);
			}
			if(apath == "" && dx == 0) { 
				apath = getPartialPath(orgStartPos, startPos, -1, 0, endPos, numMoves, shortestpath, pathsofar);
			}
		}
	}
	return apath;
}

function getPartialPath(orgStartPos, startPos, dx, dy, endPos, numMoves, shortestpath, pathsofar) {
 	var apath = "";
 	var validpos = (startPos.charCodeAt(0) + dx >= 65 && startPos.charCodeAt(0) + dx <= 76 && startPos.charCodeAt(1) + dy >= 48 && startPos.charCodeAt(1) + dy <= 55);
	var newpos = String.fromCharCode(startPos.charCodeAt(0) + dx, startPos.charCodeAt(1) + dy); 
 	if(validpos && shortestpath) {
		if(pathsofar.indexOf(newpos) >= 0) { validpos = false; }
	}
	if(validpos) {
		if(newpos == endPos && (numMoves == 1 || shortestpath)) { 
		 	apath = newpos; 
		} else if(numMoves > 1) {	
// alert("getPartialPath("+ orgStartPos + ", " + startPos + ", " + dx + ", " + dy + ", " + endPos + ", " + numMoves + ", " + shortestpath + ", " + pathsofar + ") = " + newpos + ":" + squareIsEmpty(newpos) + "-" + (newpos == startPos));
			if(squareIsEmpty(newpos) || (newpos == orgStartPos && !shortestpath)) { 
				var x = getAnimationPathPlayerX(orgStartPos, newpos, endPos, numMoves - 1, shortestpath, pathsofar + newpos);
				if(x != "") {
					apath = newpos + x;
				}
			}
		}
	}
	return apath;
}
