
var	arrayOfOneMove=null
var	arrayOfFromLocation=null
var	arrayOfToLocation=null


function PieceObject(location, color, piece, alg_notation ) {
	this.location='Piece'+location
	this.color=color
	this.piece=piece
	this.alg_notation=alg_notation
}

var fromPiece = null
var toPiece = null
var finalPiece = null

var squareArray = new Array(
"w","b","w","b","w","b","w","b",
"b","w","b","w","b","w","b","w",
"w","b","w","b","w","b","w","b",
"b","w","b","w","b","w","b","w",
"w","b","w","b","w","b","w","b",
"b","w","b","w","b","w","b","w",
"w","b","w","b","w","b","w","b",
"b","w","b","w","b","w","b","w"
)



var nextMoveNumber = 0

arrayOfMoves=moves.split('|')

function nextMove(){
	size = parseInt(arrayOfMoves.length)

	if(nextMoveNumber != size){

	n=nextMoveNumber++;
	n_move=arrayOfMoves[n]

	if (n>=0 && n < size && n_move.toString() != "") return n_move
	else {
	nextMoveNumber=size-1
	return "end"
	}
	}else return "end"


}


function beforeMove(){

	if(nextMoveNumber > 0){

	n=nextMoveNumber--
	n=n-1
	n_move=arrayOfMoves[n]
	size = parseInt(arrayOfMoves.length)
	//document.testform.area.value = 'move number : ' + n
	if (n >= 0 && n < size && n_move.toString() != "") return n_move
	else {
	 return "start"
	}

	} else return "start"
}



function setMoveArrays(move){

	arrayOfOneMove=move.split(':')
	//alert(arrayOfOneMove[0])
	arrayOfFromLocation  =  arrayOfOneMove[0]
	arrayOfToLocation    =  arrayOfOneMove[1]
	arrayOfFinalLocation =  arrayOfOneMove[2]
	_from = arrayOfFromLocation.split('-')
	_to = arrayOfToLocation.split('-')
	_final = arrayOfFinalLocation.split('-')
	//alert (_from[0] + _from[1] + _from[2] + _from[3])
	fromPiece = new PieceObject(_from[0],_from[1],_from[2],_from[3])
	toPiece = new PieceObject(_to[0],_to[1],_to[2],_to[3])
	finalPiece = new PieceObject(_final[0],_final[1],_final[2],_final[3])


}

function moveForward(){

n=nextMove();
x=Math.floor((nextMoveNumber+1)/2)


if(n!="end"){
	if(n=="w-o-o-o") {
		document.Piece58.src = '/chess/images/pieces/wblackK.gif'
		document.Piece59.src = '/chess/images/pieces/wwhiteR.gif'
	    document.Piece60.src = '/chess/images/pieces/black.gif'
		document.Piece56.src = '/chess/images/pieces/black.gif'  
		document.testform.area.value = x + ") White : o-o-o "
	} else if (n=="w-o-o") {
	  	document.Piece62.src = '/chess/images/pieces/wblackK.gif'
		document.Piece61.src = '/chess/images/pieces/wwhiteR.gif'
	    document.Piece60.src = '/chess/images/pieces/black.gif'
		document.Piece63.src = '/chess/images/pieces/white.gif'
		document.testform.area.value = x + ") White : o-o-o"
	} else if (n=="b-o-o-o") {
		document.Piece2.src = '/chess/images/pieces/bwhiteK.gif'
		document.Piece3.src = '/chess/images/pieces/bblackR.gif'
	    document.Piece4.src = '/chess/images/pieces/white.gif'
		document.Piece0.src = '/chess/images/pieces/white.gif'
		document.testform.area.value = x + ") Black : o-o-o"
	} else if (n=="b-o-o") {
		document.Piece6.src = '/chess/images/pieces/bwhiteK.gif'
		document.Piece5.src = '/chess/images/pieces/bblackR.gif'
	    document.Piece4.src = '/chess/images/pieces/white.gif'
		document.Piece7.src = '/chess/images/pieces/black.gif'
		document.testform.area.value = x + ") Black : o-o"
	} else {
		setMoveArrays(n)
		if( squareArray[fromPiece.location.substring(5)]=="b" ) squareColor="black"
		else squareColor="white"
		if( squareArray[toPiece.location.substring(5)]=="b" ) squareColor_to="black"
		else squareColor_to="white"
		document.images[fromPiece.location].src = '/chess/images/pieces/'+squareColor+'.gif'
		document.images[toPiece.location].src = '/chess/images/pieces/'+fromPiece.color+squareColor_to+fromPiece.piece+'.gif'
			writeToConsole('forward')
	}
}else alert('The End')

}

function moveBackward(){

n=beforeMove();
x=Math.floor((nextMoveNumber+1)/2)+1
if(n!="start"){

if(n=="w-o-o-o") {
		document.Piece58.src = '/chess/images/pieces/black.gif'
		document.Piece59.src = '/chess/images/pieces/white.gif'
	    document.Piece60.src = '/chess/images/pieces/wblackK.gif'
		document.Piece56.src = '/chess/images/pieces/wblackR.gif'
		document.testform.area.value = 'white move [ '+x + ' ]'

	} else if (n=="w-o-o") {
	  	document.Piece62.src = '/chess/images/pieces/black.gif'
		document.Piece61.src = '/chess/images/pieces/white.gif'
	    document.Piece60.src = '/chess/images/pieces/wblackK.gif'
		document.Piece63.src = '/chess/images/pieces/wwhiteR.gif'
		document.testform.area.value = 'white move [ '+x + ' ]'

	} else if (n=="b-o-o-o") {
		document.Piece2.src = '/chess/images/pieces/white.gif'
		document.Piece3.src = '/chess/images/pieces/black.gif'
	    document.Piece4.src = '/chess/images/pieces/bwhiteK.gif'
		document.Piece0.src = '/chess/images/pieces/bwhiteR.gif'
		document.testform.area.value = 'black move [ '+x + ' ]'

	} else if (n=="b-o-o") {
		document.Piece6.src = '/chess/images/pieces/white.gif'
		document.Piece5.src = '/chess/images/pieces/black.gif'
	    document.Piece4.src = '/chess/images/pieces/bwhiteK.gif'
		document.Piece7.src = '/chess/images/pieces/bblackR.gif'
		document.testform.area.value = 'black move [ '+x + ' ]'

	} else {
		setMoveArrays(n)

		//document.testform.area.value = fromPiece.location + ': /chess/images/pieces/'+fromPiece.color+squareColor+fromPiece.piece+'.gif\n'+toPiece.location + ' /chess/images/pieces/'+toPiece.color+squareColor_to+toPiece.piece+'.gif'


		if( squareArray[fromPiece.location.substring(5)]=="b" ) squareColor="black"
		else squareColor="white"
		if( squareArray[toPiece.location.substring(5)]=="b" ) squareColor_to="black"
		else squareColor_to="white"

		document.images[fromPiece.location].src = '/chess/images/pieces/'+fromPiece.color+squareColor+fromPiece.piece+'.gif'
		if(toPiece.piece=="0"){
		document.images[toPiece.location].src = '/chess/images/pieces/'+squareColor_to+'.gif'
		} else {
		document.images[toPiece.location].src = '/chess/images/pieces/'+toPiece.color+squareColor_to+toPiece.piece+'.gif'
		}

		writeToConsole('back')
	}
}else alert('You reached the beginning..')

}


function writeToConsole(_back){

x=Math.floor((nextMoveNumber+1)/2)

if(_back!="back"){


if (nextMoveNumber>0) {
			if (finalPiece.color=="w"){
			  document.testform.area.value = x + ") White : " + alg_Notation_writer()

			} else {
			document.testform.area.value = x + ") Black : "  + alg_Notation_writer()

			}
		} else document.testform.area.value = ''
}else {

if (finalPiece.color=="w") 
document.testform.area.value = 'white move [ '+x + ' ]'
else document.testform.area.value = 'black move [ '+x + ' ]'

}
}

function alg_Notation_writer(){
//finalPiece.piece + finalPiece.alg_notation
	if(toPiece.piece=='0') {
		if(fromPiece.piece=='P') str = finalPiece.alg_notation
		else str =finalPiece.piece + finalPiece.alg_notation
		return  str
	} else {
		if(fromPiece.piece=='P') str = fromPiece.alg_notation.substring(0,1) + 'x' + finalPiece.alg_notation
		else str= finalPiece.piece + 'x' + finalPiece.alg_notation
		return  str

	}
}


function historyWriter(){

y=1;
z=1;
document.write('<table cellpadding=3 cellspacing=0 border=0>\n');
for (x=0; x < arrayOfMoves.length-1; x++){
	if(arrayOfMoves[x]=="w-o-o-o" || arrayOfMoves[x]=="w-o-o" || arrayOfMoves[x]=="b-o-o-o" || arrayOfMoves[x]=="b-o-o") {
		//document.write('other..');
	  if(z==1)  document.write('<tr><td>'+y+'</td><td>'+arrayOfMoves[x].substring(2)+
	  '</td><td>')
	 else document.write(arrayOfMoves[x].substring(2)+'</td></tr>')
	} else {
	setMoveArrays(arrayOfMoves[x])
	//document.write('<b>'+ arrayOfMoves[x]+'</b><br>')
	 if(z==1)  document.write('<tr><td>'+y+'</td><td>'+alg_Notation_writer()+
	  '</td><td>')
	 else document.write(' '+alg_Notation_writer()+'</td></tr>\n')
	}

	z=x % 2
	if(z==1) y++

}
	if(z==0){
	document.write('&nbsp;</td></tr></table>')
	} else {
	document.write('</table> ')
	}
}
