var names = new Array();
var base = -1;
var pieces = "KQNBR ";
var nMoves = 0;
var EmptyWhitePath = "w.gif";
var EmptyBlackPath = "b.gif";
// Modified from version Copyright 98,99 by ChessBase GmbH, Germany

function SyncPicture( )
{	if ( base > -1 )
		 return base;

	for ( i=0; i < document.images.length; i++ )
	{	if ( document.images[i].name != "" )
			if ( document.images[i].name == basename )
				return i;
			else
				i+= 63;
	}
	return -1;
}

function GoStart( )
{	
	while ( nMoves > 0 )
		MoveBack( );

	return "";
}

function GoEnd( )
{	
	while ( nMoves < moves.length / 2 )
		MoveForward( );

	return GetMoveString( );
}

function MoveForward( )
{	base = SyncPicture( );
	if ( nMoves < moves.length / 2 )
	{	var n = nMoves*2;
		var from = moves[ n ] & 0x3f;
		var to = moves[ n+1 ] & 0x3f;
		names[ n   ] = document.images[ base + from ].src;
		names[ n+1 ] = document.images[ base + to ].src;
		var len = names[n].length;
		var nn = (Math.floor(( to / 8 )) + ( to % 8 ));
		var dest = ( nn % 2 ) == 1 ? "b" : "w";
		var piece = names[n].substring(len-6,len-5);
		if (( moves[ n ] & 0x380 ) == 0x80 )
			piece = "q";
		else if (( moves[ n ] & 0x380 ) == 0x100 )
			piece = "n";
		else if (( moves[ n ] & 0x380 ) == 0x180 )
			piece = "b";
		else if (( moves[ n ] & 0x380 ) == 0x200 )
			piece = "r";
		document.images[ base + to ].src = names[n].substring(0,len-6) + piece + dest + 				names[n].substring(len-4,len);
		nn = (Math.floor(( from / 8 )) + ( from % 8 ));
		if (( nn % 2 ) == 1 )
			document.images[ base + from ].src = EmptyBlackPath;
		else
			document.images[ base + from ].src = EmptyWhitePath;
		nMoves++;

		// Now handle double moves (castling)
		if ( nMoves < moves.length / 2 && ( moves[ nMoves*2 ] & 0x40 ) == 0x40 )
			MoveForward( );
	}
	return GetMoveString( );
}

function MoveBack( )
{	
	if ( nMoves > 0 )
	{	nMoves--;
		var from = moves[ nMoves*2 ] & 0x3f;
		var to = moves[ nMoves*2+1 ] & 0x3f;
		document.images[ base + from ].src = names[ nMoves*2 ];
		document.images[ base + to ].src = names[ nMoves*2+1 ];
		if (( moves[ nMoves*2 ] & 0x40 ) == 0x40 )
			MoveBack( );

	}
	return GetMoveString( );
}

function GetMoveString( )
{	
	if ( nMoves > 0 && nMoves <= moves.length / 2 )
	{	var n = (nMoves-1)*2;
		var from = moves[ n ] & 0x3f;
		var to = moves[ n+1 ] & 0x3f;
		var len = document.images[ base + to ].src.length;
		var piece = document.images[ base + to ].src.substring(len-6,len-5);
		if ( piece == "q" )
			piece = pieces.substring(1,2);
		else if ( piece == "r" )
			piece = pieces.substring(4,5);
		else if ( piece == "b" )
			piece = pieces.substring(3,4);
		else if ( piece == "n" )
			piece = pieces.substring(2,3);
		else if ( piece == "k" )
			piece = pieces.substring(0,1);
		else piece = pieces.substring(5,6);
		var piece2 = "";
		if (( moves[ n ] & 0x380 ) == 0x80 )
			piece2 = pieces.substring(1,2);
		else if (( moves[ n ] & 0x380 ) == 0x100 )
			piece2 = pieces.substring(2,3);
		else if (( moves[ n ] & 0x380 ) == 0x180 )
			piece2 = pieces.substring(3,4);
		else if (( moves[ n ] & 0x380 ) == 0x200 )
			piece2 = pieces.substring(4,5);
		var lines = "abcdefgh";
		var rows = "87654321";
		var fromLine = from%8;
		var fromRow = Math.floor(from/8);
		var toLine = to%8;
		var toRow = Math.floor(to/8);
		var off=1;
		for ( i=0; i <= n; i += 2 )
			if (( moves[i] & 0x40 ) == 0x40 )
				off = off+1;
		var result = (Math.floor((nMoves-off)/2)+1).toString() + ". ";
		if ( document.images[ base + to ].src.substring(len-7,len-6) == "b" )
			result = result + "... ";
		if ( n >= 2 && (( moves[n] & 0x40 ) == 0x40 ))
		{	if (( moves[n-1] == 62 ) || ( moves[n-1] == 6 ))
				result = result + "0-0"
			else if (( moves[n-1] == 2 + 7 * 8 ) || ( moves[n-1] == 2 + 0 * 8 ))
				result = result + "0-0-0"
			else
			{	var sep = "x";
				from = moves[ n-2 ] & 0x3f;
				to = moves[ n-1 ] & 0x3f;
				fromLine = from%8;
				fromRow = Math.floor(from/8);
				toLine = to%8;
				toRow = Math.floor(to/8);
				result = result	+ lines.substring( fromLine, fromLine+1 ) + rows.substring( fromRow, 						fromRow+1 )	+ sep + lines.substring( toLine, toLine+1 ) 
						+ rows.substring( toRow, toRow+1 ) + piece2 + " ep";
			}
		}
		else
		{	var len = names[ n+1 ].length;
			var substr = names[ n+1 ].substring( len-6, len );
			var sep = (( substr == "/w.gif" ) || ( substr == "\\w.gif" ) ||
						  ( substr == "/b.gif" ) || ( substr == "\\b.gif" )) ? "-" : "x";
			if ( piece2 != "" ) piece = "";
			result = result + piece.toUpperCase() + lines.substring( fromLine, fromLine+1 ) 
				+ rows.substring( fromRow, fromRow+1 ) + sep + lines.substring( toLine, toLine+1 )
				+ rows.substring( toRow, toRow+1 ) + piece2;
		}
		return result;
	}
	return "";
}

