/* JavaScript Chess by Heiner Sextro - www.sextro.de */
/*  */
/* */
var chessgame = null;
var picked_x = -1;
var picked_y = -1;
var last_chess_move = 0;
var last_player = 1;
var moves = new Array;
var figures = new Array;
figures[ 0 ] = "";
figures[ 1 ] = "pawn";
figures[ 2 ] = "rook";
figures[ 3 ] = "knight";
figures[ 4 ] = "bishop";
figures[ 5 ] = "queen";
figures[ 6 ] = "king";
var j_Names = new Array('a','b','c','d','e','f','g','h' );
var i_Names = new Array(' 8 ',' 7 ',' 6 ',' 5 ',' 4 ',' 3 ',' 2 ',' 1 ');
b = new Array('a','b','c');



/*******/
function initChessGame( id ) {
last_chess_move = 0;
chessgame = new initChessBoard( 7 , 7 , id );

document.getElementById('chessboard').style.visibility = "hidden";
document.getElementById('chessboard').innerHTML = chessgame.html;
document.getElementById('chessboard').style.visibility = "visible";
doAjaxRequest('index.php?ajax=1&com=start_chat&command=refresh_game&game_id=' + id,'content',true);
}
function initChessBoard( a , b , id ) {
this.id = id;
this.matrix = new Array();
this.html = '';
var c = 0;
for ( i = 0; i <= b ;i++ )
   {
   this.matrix[i] = new Array;
   this.html = this.html + '<div class="rank">';
   for ( j = 0; j <= a ;j++ )
      {
	  var classnumber = (c+i)%2;
      this.matrix[i][j] = new box( c ,  i , j , a , b , classnumber ) ;
	  this.html = this.html + '<div class="p_' + classnumber + '" id="s_' + i + '_' + j + '"><img onclick="movecast(' + i + ',' + j + ')" onmouseover="tellcast(' + i + ',' + j + ')" src="system/grafx/chess/chess_0.png"  id="chess_' + c + '" width="40" height="40" /></div>';
	  c++;
      }
   this.html = this.html + '</div>';
   }
return this;
}
function box( c , i , j , a , b , classnumber ) {
this.classnumber = classnumber;
this.id = 'chess_' + c;
this.cast = new cast( i , j );
this.row = i;
this.col = j;
this.updateCast = updateCast;
   this.repaint = function()
     {
     var id = 's_' + this.row + '_' + this.col;
	 if ( this.classnumber == 0 ) {
	    document.getElementById( id ).style.backgroundColor = "#ffffff";
	    }
	 else {
	    document.getElementById( id ).style.backgroundColor = "#000000";
		}
     }
return this;
}
function updateCast() {
   document.getElementById( chessgame.matrix[ this.row ][ this.col ].id ).src = chessgame.matrix[ this.row ][ this.col ].cast.picture;
   }
function cast( i , j ) {
   this.i = i;
   this.j = j;
   this.picture = "system/grafx/chess/chess_0.png";
   this.cast_id = 0;
   this.cast_number = 0;
   this.moves = 0;
   this.cast_color = -1;
   this.freeMoves = new Array();

   this.countMove = function()
     {
     this.moves++;
     }
   this.tellCast = function()
     {
     this.getFreeMoves();
     }
   this.markMoves = function()
     {
     for ( i = 0; i < this.freeMoves.length ;i++ )
       {
	   var id = 's_' + this.freeMoves[ i ][ 0 ] + '_' + this.freeMoves[ i ][ 1 ];
	   document.getElementById( id ).style.backgroundColor = "#FF9933";
	   /* alert(this.freeMoves[ i ][ 0 ]); */
	   }
     }
   return this;
   }
   

function king() {
this.chess = 23;
this.tellCast = function()
  {
  this.getFreeMoves();
    writeHtml('points', figures[ this.cast_id ] + this.chess );
  }
this.getFreeMoves = function()
  {
  this.freeMoves = new Array();
  var test_i = this.i;
  var test_j = this.j;
  /*****/
  test_i = this.i;
  test_j = this.j;
  test_j--;
     if ( test_i >= 0 &&  test_j >= 0 && test_i <= 7 &&  test_j <= 7 ) {
        if ( chessgame.matrix[ test_i ][ test_j ].cast.cast_id == 0  ) {
           this.freeMoves[ this.freeMoves.length ] = new Array( test_i , test_j );
		   }
		else {
		  if ( chessgame.matrix[ this.i ][ this.j ].cast.cast_color != chessgame.matrix[ test_i ][ test_j ].cast.cast_color) {
		     this.freeMoves[ this.freeMoves.length ] = new Array( test_i , test_j );
			 }
		  }
      }
  /*****/
  /*****/
  test_i = this.i;
  test_j = this.j;
  test_j++;
     if ( test_i >= 0 &&  test_j >= 0 && test_i <= 7 &&  test_j <= 7 ) {
        if ( chessgame.matrix[ test_i ][ test_j ].cast.cast_id == 0  ) {
           this.freeMoves[ this.freeMoves.length ] = new Array( test_i , test_j );
		   }
		else {
		  if ( chessgame.matrix[ this.i ][ this.j ].cast.cast_color != chessgame.matrix[ test_i ][ test_j ].cast.cast_color) {
		     this.freeMoves[ this.freeMoves.length ] = new Array( test_i , test_j );
			 }
		  }
      }
  /*****/
  /*****/
  test_i = this.i;
  test_j = this.j;
  test_i++;
     if ( test_i >= 0 &&  test_j >= 0 && test_i <= 7 &&  test_j <= 7 ) {
        if ( chessgame.matrix[ test_i ][ test_j ].cast.cast_id == 0  ) {
           this.freeMoves[ this.freeMoves.length ] = new Array( test_i , test_j );
		   }
		else {
		  if ( chessgame.matrix[ this.i ][ this.j ].cast.cast_color != chessgame.matrix[ test_i ][ test_j ].cast.cast_color) {
		     this.freeMoves[ this.freeMoves.length ] = new Array( test_i , test_j );
			 }
		  }
      }
  /*****/
  /*****/
  test_i = this.i;
  test_j = this.j;
  test_i--;
     if ( test_i >= 0 &&  test_j >= 0 && test_i <= 7 &&  test_j <= 7 ) {
        if ( chessgame.matrix[ test_i ][ test_j ].cast.cast_id == 0  ) {
           this.freeMoves[ this.freeMoves.length ] = new Array( test_i , test_j );
		   }
		else {
		  if ( chessgame.matrix[ this.i ][ this.j ].cast.cast_color != chessgame.matrix[ test_i ][ test_j ].cast.cast_color) {
		     this.freeMoves[ this.freeMoves.length ] = new Array( test_i , test_j );
			 }
		  }
      }
  /*****/
  /*****/
  test_i = this.i;
  test_j = this.j;
  test_i--;
  test_j--;
     if ( test_i >= 0 &&  test_j >= 0 && test_i <= 7 &&  test_j <= 7 ) {
        if ( chessgame.matrix[ test_i ][ test_j ].cast.cast_id == 0  ) {
           this.freeMoves[ this.freeMoves.length ] = new Array( test_i , test_j );
		   }
		else {
		  if ( chessgame.matrix[ this.i ][ this.j ].cast.cast_color != chessgame.matrix[ test_i ][ test_j ].cast.cast_color) {
		     this.freeMoves[ this.freeMoves.length ] = new Array( test_i , test_j );
			 }
		  }
      }
  /*****/
  /*****/
  test_i = this.i;
  test_j = this.j;
  test_i++;
  test_j++;
     if ( test_i >= 0 &&  test_j >= 0 && test_i <= 7 &&  test_j <= 7 ) {
        if ( chessgame.matrix[ test_i ][ test_j ].cast.cast_id == 0  ) {
           this.freeMoves[ this.freeMoves.length ] = new Array( test_i , test_j );
		   }
		else {
		  if ( chessgame.matrix[ this.i ][ this.j ].cast.cast_color != chessgame.matrix[ test_i ][ test_j ].cast.cast_color) {
		     this.freeMoves[ this.freeMoves.length ] = new Array( test_i , test_j );
			 }
		  }
      }
  /*****/
  /*****/
  test_i = this.i;
  test_j = this.j;
  test_i++;
  test_j--;
     if ( test_i >= 0 &&  test_j >= 0 && test_i <= 7 &&  test_j <= 7 ) {
        if ( chessgame.matrix[ test_i ][ test_j ].cast.cast_id == 0  ) {
           this.freeMoves[ this.freeMoves.length ] = new Array( test_i , test_j );
		   }
		else {
		  if ( chessgame.matrix[ this.i ][ this.j ].cast.cast_color != chessgame.matrix[ test_i ][ test_j ].cast.cast_color) {
		     this.freeMoves[ this.freeMoves.length ] = new Array( test_i , test_j );
			 }
		  }
      }
  /*****/
  /*****/
  test_i = this.i;
  test_j = this.j;
  test_i--;
  test_j++;
     if ( test_i >= 0 &&  test_j >= 0 && test_i <= 7 &&  test_j <= 7 ) {
        if ( chessgame.matrix[ test_i ][ test_j ].cast.cast_id == 0  ) {
           this.freeMoves[ this.freeMoves.length ] = new Array( test_i , test_j );
		   }
		else {
		  if ( chessgame.matrix[ this.i ][ this.j ].cast.cast_color != chessgame.matrix[ test_i ][ test_j ].cast.cast_color) {
		     this.freeMoves[ this.freeMoves.length ] = new Array( test_i , test_j );
			 }
		  }
      }
  /*****/
    writeHtml('points', figures[ this.cast_id ] + ' at ' +  j_Names[ this.j ] + ' ' + i_Names[ this.i ]  );
	
  }
}

function queen() {
this.chess = 23;
this.getFreeMoves = function()
  {
  this.freeMoves = new Array();
  var test_i = this.i;
  var test_j = this.i;
  /* test -- */
  exitloop = 0;
  test_i = this.i;
  test_j = this.j;
  while( exitloop == 0 ) {
     if ( test_i >= 1 && test_j >= 1 ) {
        test_i--;
		test_j--;
        if ( chessgame.matrix[ test_i ][ test_j ].cast.cast_id == 0  ) {
           this.freeMoves[ this.freeMoves.length ] = new Array( test_i , test_j );
		   }
		else {
		  if ( chessgame.matrix[ this.i ][ this.j ].cast.cast_color != chessgame.matrix[ test_i ][ test_j ].cast.cast_color) {
		     this.freeMoves[ this.freeMoves.length ] = new Array( test_i , test_j );
			 exitloop = 1;
			 }
	       else {
	         exitloop = 1;
	         }
		  }
      }
	else {
	   exitloop = 1;
	   }
	}
  /* test ++ */
  exitloop = 0;
  test_i = this.i;
  test_j = this.j;
  while( exitloop == 0 ) {
     if ( test_i < 7 &&  test_j < 7 ) {
        test_i++;
		test_j++;
        if ( chessgame.matrix[ test_i ][ test_j ].cast.cast_id == 0  ) {
           this.freeMoves[ this.freeMoves.length ] = new Array( test_i , test_j );
		   }
		else {
		  if ( chessgame.matrix[ this.i ][ this.j ].cast.cast_color != chessgame.matrix[ test_i ][ test_j ].cast.cast_color) {
		     this.freeMoves[ this.freeMoves.length ] = new Array( test_i , test_j );
			 exitloop = 1;
			 }
	       else {
	         exitloop = 1;
	         }
		  }
      }
	else {
	   exitloop = 1;
	   }
	}
  /* test -+ */
  exitloop = 0;
  test_i = this.i;
  test_j = this.j;
  while( exitloop == 0 ) {
     if ( test_i >= 1 &&  test_j < 7 ) {
        test_i--;
		test_j++;
        if ( chessgame.matrix[ test_i ][ test_j ].cast.cast_id == 0  ) {
           this.freeMoves[ this.freeMoves.length ] = new Array( test_i , test_j );
		   }
		else {
		  if ( chessgame.matrix[ this.i ][ this.j ].cast.cast_color != chessgame.matrix[ test_i ][ test_j ].cast.cast_color) {
		     this.freeMoves[ this.freeMoves.length ] = new Array( test_i , test_j );
			 exitloop = 1;
			 }
	       else {
	         exitloop = 1;
	         }
		  }
      }
	else {
	   exitloop = 1;
	   }
	}
  /* test +- */
  exitloop = 0;
  test_i = this.i;
  test_j = this.j;
  while( exitloop == 0 ) {
     if ( test_i < 7 && test_j >= 1 ) {
        test_i++;
		test_j--;
        if ( chessgame.matrix[ test_i ][ test_j ].cast.cast_id == 0  ) {
           this.freeMoves[ this.freeMoves.length ] = new Array( test_i , test_j );
		   }
		else {
		  if ( chessgame.matrix[ this.i ][ this.j ].cast.cast_color != chessgame.matrix[ test_i ][ test_j ].cast.cast_color) {
		     this.freeMoves[ this.freeMoves.length ] = new Array( test_i , test_j );
			 exitloop = 1;
			 }
	       else {
	         exitloop = 1;
	         }
		  }
      }
	else {
	   exitloop = 1;
	   }
	}

  /* test top */
  exitloop = 0;
  test_i = this.i;
  test_j = this.j;
  while( exitloop == 0 ) {
     if ( test_i >= 1 ) {
        test_i--;
        if ( chessgame.matrix[ test_i ][ test_j ].cast.cast_id == 0  ) {
           this.freeMoves[ this.freeMoves.length ] = new Array( test_i , test_j );
		   }
		else {
		  if ( chessgame.matrix[ this.i ][ this.j ].cast.cast_color != chessgame.matrix[ test_i ][ test_j ].cast.cast_color) {
		     this.freeMoves[ this.freeMoves.length ] = new Array( test_i , test_j );
			 exitloop = 1;
			 }
	       else {
	         exitloop = 1;
	         }
		  }
      }
	else {
	   exitloop = 1;
	   }
	}
  /* test bottom */
  exitloop = 0;
  test_i = this.i;
  test_j = this.j;
  while( exitloop == 0 ) {
     if ( test_i < 7 ) {
        test_i++;
        if ( chessgame.matrix[ test_i ][ test_j ].cast.cast_id == 0  ) {
           this.freeMoves[ this.freeMoves.length ] = new Array( test_i , test_j );
		   }
		else {
		  if ( chessgame.matrix[ this.i ][ this.j ].cast.cast_color != chessgame.matrix[ test_i ][ test_j ].cast.cast_color) {
		     this.freeMoves[ this.freeMoves.length ] = new Array( test_i , test_j );
			 exitloop = 1;
			 }
	       else {
	         exitloop = 1;
	         }
		  }
      }
	else {
	   exitloop = 1;
	   }
	}
  /* test left */
  exitloop = 0;
  test_i = this.i;
  test_j = this.j;
  while( exitloop == 0 ) {
     if ( test_j >= 1 ) {
        test_j--;
        if ( chessgame.matrix[ test_i ][ test_j ].cast.cast_id == 0  ) {
           this.freeMoves[ this.freeMoves.length ] = new Array( test_i , test_j );
		   }
		else {
		  if ( chessgame.matrix[ this.i ][ this.j ].cast.cast_color != chessgame.matrix[ test_i ][ test_j ].cast.cast_color) {
		     this.freeMoves[ this.freeMoves.length ] = new Array( test_i , test_j );
			 exitloop = 1;
			 }
	       else {
	         exitloop = 1;
	         }
		  }
      }
	else {
	   exitloop = 1;
	   }
	}
  /* test right */
  exitloop = 0;
  test_i = this.i;
  test_j = this.j;
  while( exitloop == 0 ) {
     if ( test_j < 7 ) {
        test_j++;
        if ( chessgame.matrix[ test_i ][ test_j ].cast.cast_id == 0  ) {
           this.freeMoves[ this.freeMoves.length ] = new Array( test_i , test_j );
		   }
		else {
		  if ( chessgame.matrix[ this.i ][ this.j ].cast.cast_color != chessgame.matrix[ test_i ][ test_j ].cast.cast_color) {
		     this.freeMoves[ this.freeMoves.length ] = new Array( test_i , test_j );
			 exitloop = 1;
			 }
	       else {
	         exitloop = 1;
	         }
		  }
      }
	else {
	   exitloop = 1;
	   }
	}
  /*****/
    writeHtml('points', figures[ this.cast_id ] + ' at ' +  j_Names[ this.j ] + ' ' + i_Names[ this.i ]  );
  }
}

function bishop() {
this.chess = 23;
this.getFreeMoves = function()
  {
  this.freeMoves = new Array();
  var test_i = this.i;
  var test_j = this.i;
  var exitloop = 0;
  /* test -- */
  exitloop = 0;
  test_i = this.i;
  test_j = this.j;
  while( exitloop == 0 ) {
     if ( test_i >= 1 && test_j >= 1 ) {
        test_i--;
		test_j--;
        if ( chessgame.matrix[ test_i ][ test_j ].cast.cast_id == 0  ) {
           this.freeMoves[ this.freeMoves.length ] = new Array( test_i , test_j );
		   }
		else {
		  if ( chessgame.matrix[ this.i ][ this.j ].cast.cast_color != chessgame.matrix[ test_i ][ test_j ].cast.cast_color) {
		     this.freeMoves[ this.freeMoves.length ] = new Array( test_i , test_j );
			 exitloop = 1;
			 }
	       else {
	         exitloop = 1;
	         }
		  }
      }
	else {
	   exitloop = 1;
	   }
	}
  /* test ++ */
  exitloop = 0;
  test_i = this.i;
  test_j = this.j;
  while( exitloop == 0 ) {
     if ( test_i < 7 &&  test_j < 7 ) {
        test_i++;
		test_j++;
        if ( chessgame.matrix[ test_i ][ test_j ].cast.cast_id == 0  ) {
           this.freeMoves[ this.freeMoves.length ] = new Array( test_i , test_j );
		   }
		else {
		  if ( chessgame.matrix[ this.i ][ this.j ].cast.cast_color != chessgame.matrix[ test_i ][ test_j ].cast.cast_color) {
		     this.freeMoves[ this.freeMoves.length ] = new Array( test_i , test_j );
			 exitloop = 1;
			 }
	       else {
	         exitloop = 1;
	         }
		  }
      }
	else {
	   exitloop = 1;
	   }
	}
  /* test -+ */
  exitloop = 0;
  test_i = this.i;
  test_j = this.j;
  while( exitloop == 0 ) {
     if ( test_i >= 1 &&  test_j < 7 ) {
        test_i--;
		test_j++;
        if ( chessgame.matrix[ test_i ][ test_j ].cast.cast_id == 0  ) {
           this.freeMoves[ this.freeMoves.length ] = new Array( test_i , test_j );
		   }
		else {
		  if ( chessgame.matrix[ this.i ][ this.j ].cast.cast_color != chessgame.matrix[ test_i ][ test_j ].cast.cast_color) {
		     this.freeMoves[ this.freeMoves.length ] = new Array( test_i , test_j );
			 exitloop = 1;
			 }
	       else {
	         exitloop = 1;
	         }
		  }
      }
	else {
	   exitloop = 1;
	   }
	}
  /* test +- */
  exitloop = 0;
  test_i = this.i;
  test_j = this.j;
  while( exitloop == 0 ) {
     if ( test_i < 7 && test_j >= 1 ) {
        test_i++;
		test_j--;
        if ( chessgame.matrix[ test_i ][ test_j ].cast.cast_id == 0  ) {
           this.freeMoves[ this.freeMoves.length ] = new Array( test_i , test_j );
		   }
		else {
		  if ( chessgame.matrix[ this.i ][ this.j ].cast.cast_color != chessgame.matrix[ test_i ][ test_j ].cast.cast_color) {
		     this.freeMoves[ this.freeMoves.length ] = new Array( test_i , test_j );
			 exitloop = 1;
			 }
	       else {
	         exitloop = 1;
	         }
		  }
      }
	else {
	   exitloop = 1;
	   }
	}
      /*****/
    writeHtml('points', figures[ this.cast_id ] + ' at ' +  j_Names[ this.j ] + ' ' + i_Names[ this.i ]  );
  }

}

function knight() {
this.chess = 23;
this.getFreeMoves = function()
  {
  this.freeMoves = new Array();
  var test_i = this.i;
  var test_j = this.i;
  
  /*****/
  test_i = this.i;
  test_j = this.j;
  test_i = test_i - 1;
  test_j = test_j - 2;
     if ( test_i >= 0 &&  test_j >= 0 && test_i <= 7 &&  test_j <= 7 ) {
        if ( chessgame.matrix[ test_i ][ test_j ].cast.cast_id == 0  ) {
           this.freeMoves[ this.freeMoves.length ] = new Array( test_i , test_j );
		   }
		else {
		  if ( chessgame.matrix[ this.i ][ this.j ].cast.cast_color != chessgame.matrix[ test_i ][ test_j ].cast.cast_color) {
		     this.freeMoves[ this.freeMoves.length ] = new Array( test_i , test_j );
			 }
		  }
      }
  /*****/
  /*****/
  test_i = this.i;
  test_j = this.j;
  test_i = test_i - 2;
  test_j = test_j - 1;
     if ( test_i >= 0 &&  test_j >= 0 && test_i <= 7 &&  test_j <= 7 ) {
        if ( chessgame.matrix[ test_i ][ test_j ].cast.cast_id == 0  ) {
           this.freeMoves[ this.freeMoves.length ] = new Array( test_i , test_j );
		   }
		else {
		  if ( chessgame.matrix[ this.i ][ this.j ].cast.cast_color != chessgame.matrix[ test_i ][ test_j ].cast.cast_color) {
		     this.freeMoves[ this.freeMoves.length ] = new Array( test_i , test_j );
			 }
		  }
      }
  /*****/
  /*****/
  test_i = this.i;
  test_j = this.j;
  test_i = test_i + 1;
  test_j = test_j + 2;
     if ( test_i >= 0 &&  test_j >= 0 && test_i <= 7 &&  test_j <= 7 ) {
        if ( chessgame.matrix[ test_i ][ test_j ].cast.cast_id == 0  ) {
           this.freeMoves[ this.freeMoves.length ] = new Array( test_i , test_j );
		   }
		else {
		  if ( chessgame.matrix[ this.i ][ this.j ].cast.cast_color != chessgame.matrix[ test_i ][ test_j ].cast.cast_color) {
		     this.freeMoves[ this.freeMoves.length ] = new Array( test_i , test_j );
			 }
		  }
      }
  /*****/
  /*****/
  test_i = this.i;
  test_j = this.j;
  test_i = test_i + 2;
  test_j = test_j + 1;
     if ( test_i >= 0 &&  test_j >= 0 && test_i <= 7 &&  test_j <= 7 ) {
        if ( chessgame.matrix[ test_i ][ test_j ].cast.cast_id == 0  ) {
           this.freeMoves[ this.freeMoves.length ] = new Array( test_i , test_j );
		   }
		else {
		  if ( chessgame.matrix[ this.i ][ this.j ].cast.cast_color != chessgame.matrix[ test_i ][ test_j ].cast.cast_color) {
		     this.freeMoves[ this.freeMoves.length ] = new Array( test_i , test_j );
			 }
		  }
      }
  /*****/
/*****/
  /*****/
  test_i = this.i;
  test_j = this.j;
  test_i = test_i + 1;
  test_j = test_j - 2;
     if ( test_i >= 0 &&  test_j >= 0 && test_i <= 7 &&  test_j <= 7 ) {
        if ( chessgame.matrix[ test_i ][ test_j ].cast.cast_id == 0  ) {
           this.freeMoves[ this.freeMoves.length ] = new Array( test_i , test_j );
		   }
		else {
		  if ( chessgame.matrix[ this.i ][ this.j ].cast.cast_color != chessgame.matrix[ test_i ][ test_j ].cast.cast_color) {
		     this.freeMoves[ this.freeMoves.length ] = new Array( test_i , test_j );
			 }
		  }
      }
  /*****/
  /*****/
  test_i = this.i;
  test_j = this.j;
  test_i = test_i + 2;
  test_j = test_j - 1;
     if ( test_i >= 0 &&  test_j >= 0 && test_i <= 7 &&  test_j <= 7 ) {
        if ( chessgame.matrix[ test_i ][ test_j ].cast.cast_id == 0  ) {
           this.freeMoves[ this.freeMoves.length ] = new Array( test_i , test_j );
		   }
		else {
		  if ( chessgame.matrix[ this.i ][ this.j ].cast.cast_color != chessgame.matrix[ test_i ][ test_j ].cast.cast_color) {
		     this.freeMoves[ this.freeMoves.length ] = new Array( test_i , test_j );
			 }
		  }
      }
  /*****/
  /*****/
  test_i = this.i;
  test_j = this.j;
  test_i = test_i - 1;
  test_j = test_j + 2;
     if ( test_i >= 0 &&  test_j >= 0 && test_i <= 7 &&  test_j <= 7 ) {
        if ( chessgame.matrix[ test_i ][ test_j ].cast.cast_id == 0  ) {
           this.freeMoves[ this.freeMoves.length ] = new Array( test_i , test_j );
		   }
		else {
		  if ( chessgame.matrix[ this.i ][ this.j ].cast.cast_color != chessgame.matrix[ test_i ][ test_j ].cast.cast_color) {
		     this.freeMoves[ this.freeMoves.length ] = new Array( test_i , test_j );
			 }
		  }
      }
  /*****/
  /*****/
  test_i = this.i;
  test_j = this.j;
  test_i = test_i - 2;
  test_j = test_j + 1;
     if ( test_i >= 0 &&  test_j >= 0 && test_i <= 7 &&  test_j <= 7 ) {
        if ( chessgame.matrix[ test_i ][ test_j ].cast.cast_id == 0  ) {
           this.freeMoves[ this.freeMoves.length ] = new Array( test_i , test_j );
		   }
		else {
		  if ( chessgame.matrix[ this.i ][ this.j ].cast.cast_color != chessgame.matrix[ test_i ][ test_j ].cast.cast_color) {
		     this.freeMoves[ this.freeMoves.length ] = new Array( test_i , test_j );
			 }
		  }
      }
  /*****/
    writeHtml('points', figures[ this.cast_id ] + ' at ' +  j_Names[ this.j ] + ' ' + i_Names[ this.i ]  );
  }
}

function rook() {
this.chess = 23;
this.getFreeMoves = function()
  {
  this.freeMoves = new Array();
  var test_i = this.i;
  var test_j = this.i;
  var exitloop = 0;
  /* test top */
  exitloop = 0;
  test_i = this.i;
  test_j = this.j;
  while( exitloop == 0 ) {
     if ( test_i >= 1 ) {
        test_i--;
        if ( chessgame.matrix[ test_i ][ test_j ].cast.cast_id == 0  ) {
           this.freeMoves[ this.freeMoves.length ] = new Array( test_i , test_j );
		   }
		else {
		  if ( chessgame.matrix[ this.i ][ this.j ].cast.cast_color != chessgame.matrix[ test_i ][ test_j ].cast.cast_color) {
		     this.freeMoves[ this.freeMoves.length ] = new Array( test_i , test_j );
			 exitloop = 1;
			 }
	       else {
	         exitloop = 1;
	         }
		  }
      }
	else {
	   exitloop = 1;
	   }
	}
  /* test bottom */
  exitloop = 0;
  test_i = this.i;
  test_j = this.j;
  while( exitloop == 0 ) {
     if ( test_i < 7 ) {
        test_i++;
        if ( chessgame.matrix[ test_i ][ test_j ].cast.cast_id == 0  ) {
           this.freeMoves[ this.freeMoves.length ] = new Array( test_i , test_j );
		   }
		else {
		  if ( chessgame.matrix[ this.i ][ this.j ].cast.cast_color != chessgame.matrix[ test_i ][ test_j ].cast.cast_color) {
		     this.freeMoves[ this.freeMoves.length ] = new Array( test_i , test_j );
			 exitloop = 1;
			 }
	       else {
	         exitloop = 1;
	         }
		  }
      }
	else {
	   exitloop = 1;
	   }
	}
  /* test left */
  exitloop = 0;
  test_i = this.i;
  test_j = this.j;
  while( exitloop == 0 ) {
     if ( test_j >= 1 ) {
        test_j--;
        if ( chessgame.matrix[ test_i ][ test_j ].cast.cast_id == 0  ) {
           this.freeMoves[ this.freeMoves.length ] = new Array( test_i , test_j );
		   }
		else {
		  if ( chessgame.matrix[ this.i ][ this.j ].cast.cast_color != chessgame.matrix[ test_i ][ test_j ].cast.cast_color) {
		     this.freeMoves[ this.freeMoves.length ] = new Array( test_i , test_j );
			 exitloop = 1;
			 }
	       else {
	         exitloop = 1;
	         }
		  }
      }
	else {
	   exitloop = 1;
	   }
	}
  /* test right */
  exitloop = 0;
  test_i = this.i;
  test_j = this.j;
  while( exitloop == 0 ) {
     if ( test_j < 7 ) {
        test_j++;
        if ( chessgame.matrix[ test_i ][ test_j ].cast.cast_id == 0  ) {
           this.freeMoves[ this.freeMoves.length ] = new Array( test_i , test_j );
		   }
		else {
		  if ( chessgame.matrix[ this.i ][ this.j ].cast.cast_color != chessgame.matrix[ test_i ][ test_j ].cast.cast_color) {
		     this.freeMoves[ this.freeMoves.length ] = new Array( test_i , test_j );
			 exitloop = 1;
			 }
	       else {
	         exitloop = 1;
	         }
		  }
      }
	else {
	   exitloop = 1;
	   }
	}
      /*****/
    writeHtml('points', figures[ this.cast_id ] + ' at ' +  j_Names[ this.j ] + ' ' + i_Names[ this.i ]  );
  }
}

function pawnwhite() {
this.chess = 23;
this.getFreeMoves = function()
  {
  this.freeMoves = new Array();
  var test_i = 0;
  var test_j = 0;
  if ( this.i > 1 ) {
     test_i = this.i - 1;
     test_j = this.j;
     if ( chessgame.matrix[ test_i ][ test_j ].cast.cast_id == 0 ) {
        this.freeMoves[ this.freeMoves.length ] = new Array( test_i , test_j );
	    }
     }
  if ( this.moves == 0 ) {
     test_i = this.i - 2;
     test_j = this.j;
     if ( chessgame.matrix[ test_i + 1 ][ test_j ].cast.cast_id == 0 && chessgame.matrix[ test_i ][ test_j ].cast.cast_id == 0 ) {
        this.freeMoves[ this.freeMoves.length ] = new Array( test_i , test_j );
	    }
	 }

   if ( this.j > 0 ) {
      test_i = this.i - 1;
      test_j = this.j - 1;
     if ( chessgame.matrix[ test_i ][ test_j ].cast.cast_id > 0 && chessgame.matrix[ test_i ][ test_j ].cast.cast_color == 0 ) {
        this.freeMoves[ this.freeMoves.length ] = new Array( test_i , test_j );
	    }
	  }
   if ( this.j < 7 ) {
      test_i = this.i - 1;
      test_j = this.j + 1;
     if ( chessgame.matrix[ test_i ][ test_j ].cast.cast_id > 0 && chessgame.matrix[ test_i ][ test_j ].cast.cast_color == 0 ) {
        this.freeMoves[ this.freeMoves.length ] = new Array( test_i , test_j );
	    }
	  }

    /*****/
    writeHtml('points', figures[ this.cast_id ] + ' at ' +  j_Names[ this.j ] + ' ' + i_Names[ this.i ]  );
  }
}
function pawnblack() {
this.chess = 23;
this.getFreeMoves = function()
  {
  this.freeMoves = new Array();
  var test_i = 0;
  var test_j = 0;
  if ( this.i < 7 ) {
     test_i = this.i + 1;
     test_j = this.j;
     if ( chessgame.matrix[ test_i ][ test_j ].cast.cast_id == 0 ) {
        this.freeMoves[ this.freeMoves.length ] = new Array( test_i , test_j );
	    }
     }
  if ( this.moves == 0 ) {
     test_i = this.i + 2;
     test_j = this.j;
     if ( chessgame.matrix[ test_i - 1][ test_j ].cast.cast_id == 0 && chessgame.matrix[ test_i ][ test_j ].cast.cast_id == 0 ) {
        this.freeMoves[ this.freeMoves.length ] = new Array( test_i , test_j );
	    }
	 }

   if ( this.j > 0 ) {
      test_i = this.i + 1;
      test_j = this.j - 1;
     if ( chessgame.matrix[ test_i ][ test_j ].cast.cast_id > 0 && chessgame.matrix[ test_i ][ test_j ].cast.cast_color == 1 ) {
        this.freeMoves[ this.freeMoves.length ] = new Array( test_i , test_j );
	    }
	  }
   if ( this.j < 7 ) {
      test_i = this.i + 1;
      test_j = this.j + 1;
     if ( chessgame.matrix[ test_i ][ test_j ].cast.cast_id > 0 && chessgame.matrix[ test_i ][ test_j ].cast.cast_color == 1 ) {
        this.freeMoves[ this.freeMoves.length ] = new Array( test_i , test_j );
	    }
	  }

    /*****/
    writeHtml('points', figures[ this.cast_id ] + ' at ' +  j_Names[ this.j ] + ' ' + i_Names[ this.i ]  );
  }
}
function tellcast( i , j ) {
   if ( chessgame.matrix[i][j].cast.cast_id > 0 ) {
      /* alert( chessgame.matrix[i][j].cast.chess ); */
	  chessgame.matrix[i][j].cast.tellCast();
      }
   
   writeHtml('menue', i + 'x' + j + 'y' + chessgame.matrix[i][j].cast.picture + '<br (>cast_id:' + chessgame.matrix[i][j].cast.cast_id );
   }
function copyMoves(  temp ) {
   /*alert( temp['move_number'] );*/
   moves[ temp['move_number'] ] = temp;
   }
function movecast( i , j ) {
repaintChessBoard();  
   if ( picked_x < 0 ) {
      if ( chessgame.matrix[i][j].cast.cast_id > 0 ) {
         picked_x = i;
	     picked_y = j;
		 chessgame.matrix[i][j].cast.getFreeMoves();
		 chessgame.matrix[i][j].cast.markMoves();
		 }
	  }
   else {
	  if ( i != picked_x || j != picked_y ) {
	     if ( chessgame.matrix[i][j].cast.cast_color != chessgame.matrix[ picked_x ][ picked_y ].cast.cast_color ) {
		    var execute = 0;
			for ( id = 0; id < chessgame.matrix[picked_x][picked_y].cast.freeMoves.length ;id++ )
               {
			   if ( chessgame.matrix[picked_x][picked_y].cast.freeMoves[id][0] == i && chessgame.matrix[picked_x][picked_y].cast.freeMoves[id][1] == j ) {
			     execute = 1;
				 }
			   }
			if ( execute == 1 ) {
			   last_chess_move++;
               chessgame.matrix[i][j].cast = chessgame.matrix[ picked_x ][ picked_y ].cast;
			   chessgame.matrix[i][j].cast.i = i;
			   chessgame.matrix[i][j].cast.j = j;
			   chessgame.matrix[i][j].cast.countMove();
	           chessgame.matrix[i][j].updateCast();
	           chessgame.matrix[ picked_x ][ picked_y ].cast = new cast( picked_x , picked_y );
	           chessgame.matrix[ picked_x ][ picked_y ].updateCast();
	           doAjaxRequest('index.php?ajax=1&com=start_chat&command=move_cast&game_id=' + chessgame.id + '&cast_id=' + chessgame.matrix[i][j].cast.cast_id + '&cast_number=' + chessgame.matrix[i][j].cast.cast_number + '&cast_color=' + chessgame.matrix[i][j].cast.cast_color + '&x1=' + i + '&y1=' + j + '&x2=' + picked_x + '&y2=' + picked_y ,'content',true);
               }
			}
	  }
	  picked_x = -1;
	  picked_y = -1;
	  }

   writeHtml('menue', i + 'x' + j + 'y' + chessgame.matrix[i][j].cast.picture + 'number:' + chessgame.matrix[i][j].cast.cast_number );
   }
function showMoves()
{
firstChessPosition( chessgame.id);
for ( i = 1; i < moves.length ;i++ )
   {
   /*alert(moves[i]['pos_1_x']);*/
   pos_2_x = parseInt( moves[i]['pos_2_x'] );
   pos_2_y = parseInt( moves[i]['pos_2_y'] );
   pos_1_x = parseInt( moves[i]['pos_1_x'] );
   pos_1_y = parseInt( moves[i]['pos_1_y'] );
   
   chessgame.matrix[ pos_1_x ] [ pos_1_y ].cast = chessgame.matrix[ pos_2_x ][ pos_2_y ].cast;
   chessgame.matrix[ pos_1_x ] [ pos_1_y ].cast.i = pos_1_x;
   chessgame.matrix[ pos_1_x ] [ pos_1_y ].cast.j = pos_1_y;
   chessgame.matrix[ pos_1_x ][ pos_1_y ].updateCast();
   chessgame.matrix[ pos_1_x ][ pos_1_y ].cast.countMove();
   
   chessgame.matrix[ pos_2_x ][ pos_2_y ].cast = new cast( pos_2_x , pos_2_y );
   chessgame.matrix[ pos_2_x ] [ pos_2_y ].cast.i = pos_2_x;
   chessgame.matrix[ pos_2_x ] [ pos_2_y ].cast.j = pos_2_y;
   chessgame.matrix[ pos_2_x ][ pos_2_y ].updateCast();
   }
last_player = ( moves[ moves.length - 1 ][ 'color_number' ] == 1 ) ? 0 : 1 ;
}










/**/
function firstChessPosition( id  )
{
/* make two kings */
king.prototype = new cast;

chessgame.matrix[0][4].cast = new king( 0 , 4 );
   chessgame.matrix[0][4].cast.i = 0;
   chessgame.matrix[0][4].cast.j = 4;
   chessgame.matrix[0][4].cast.picture = "system/grafx/chess/king_0.png";
   chessgame.matrix[0][4].cast.cast_id = 6;
   chessgame.matrix[0][4].cast.cast_number = 1;
   chessgame.matrix[0][4].cast.cast_color = 0;
   document.getElementById( chessgame.matrix[0][4].id ).src = "system/grafx/chess/king_0.png";
chessgame.matrix[7][4].cast = new king( 7 , 4 );
   chessgame.matrix[7][4].cast.i = 7;
   chessgame.matrix[7][4].cast.j = 4;
   chessgame.matrix[7][4].cast.picture = "system/grafx/chess/king_1.png";
   chessgame.matrix[7][4].cast.cast_id = 6;
   chessgame.matrix[7][4].cast.cast_number = 1;
   chessgame.matrix[7][4].cast.cast_color = 1;
   document.getElementById( chessgame.matrix[7][4].id ).src = "system/grafx/chess/king_1.png";

/* make two queens */
queen.prototype = new cast;
chessgame.matrix[0][3].cast = new queen( 0 , 3 );
   chessgame.matrix[0][3].cast.i = 0;
   chessgame.matrix[0][3].cast.j = 3;
   chessgame.matrix[0][3].cast.picture = "system/grafx/chess/queen_0.png";
   chessgame.matrix[0][3].cast.cast_id = 5;
   chessgame.matrix[0][3].cast.cast_number = 1;
   chessgame.matrix[0][3].cast.cast_color = 0;
   document.getElementById( chessgame.matrix[0][3].id ).src = "system/grafx/chess/queen_0.png";
chessgame.matrix[7][3].cast = new queen( 7 , 3 );
   chessgame.matrix[7][3].cast.i = 7;
   chessgame.matrix[7][3].cast.j = 3;
   chessgame.matrix[7][3].cast.picture = "system/grafx/chess/queen_1.png";
   chessgame.matrix[7][3].cast.cast_id = 5;
   chessgame.matrix[7][3].cast.cast_number = 1;
   chessgame.matrix[7][3].cast.cast_color = 1;
   document.getElementById( chessgame.matrix[7][3].id ).src = "system/grafx/chess/queen_1.png";

/* make 4 bishops */
bishop.prototype = new cast;
chessgame.matrix[0][2].cast = new bishop( 0 , 2 );
   chessgame.matrix[0][2].cast.i = 0;
   chessgame.matrix[0][2].cast.j = 2;
   chessgame.matrix[0][2].cast.picture = "system/grafx/chess/bishop_0.png";
   chessgame.matrix[0][2].cast.cast_id = 4;
   chessgame.matrix[0][2].cast.cast_number = 1;
   chessgame.matrix[0][2].cast.cast_color = 0;
   document.getElementById( chessgame.matrix[0][2].id ).src = "system/grafx/chess/bishop_0.png";
chessgame.matrix[7][2].cast = new bishop( 7 , 2 );
   chessgame.matrix[7][2].cast.i = 7;
   chessgame.matrix[7][2].cast.j = 2;
   chessgame.matrix[7][2].cast.picture = "system/grafx/chess/bishop_1.png";
   chessgame.matrix[7][2].cast.cast_id = 4;
   chessgame.matrix[7][2].cast.cast_number = 1;
   chessgame.matrix[7][2].cast.cast_color = 1;
   document.getElementById( chessgame.matrix[7][2].id ).src = "system/grafx/chess/bishop_1.png";
chessgame.matrix[0][5].cast = new bishop( 0 , 5 );
   chessgame.matrix[0][5].cast.i = 0;
   chessgame.matrix[0][5].cast.j = 5;
   chessgame.matrix[0][5].cast.picture = "system/grafx/chess/bishop_0.png";
   chessgame.matrix[0][5].cast.cast_id = 4;
   chessgame.matrix[0][5].cast.cast_number = 2;
   chessgame.matrix[0][5].cast.cast_color = 0;
   document.getElementById( chessgame.matrix[0][5].id ).src = "system/grafx/chess/bishop_0.png";
chessgame.matrix[7][5].cast = new bishop( 7 , 5 );
   chessgame.matrix[7][5].cast.i = 7;
   chessgame.matrix[7][5].cast.j = 5;
   chessgame.matrix[7][5].cast.picture = "system/grafx/chess/bishop_1.png";
   chessgame.matrix[7][5].cast.cast_id = 4;
   chessgame.matrix[7][5].cast.cast_number = 2;
   chessgame.matrix[7][5].cast.cast_color = 1;
   document.getElementById( chessgame.matrix[7][5].id ).src = "system/grafx/chess/bishop_1.png";

/* make 4 knights */
knight.prototype = new cast;
chessgame.matrix[0][1].cast = new knight( 0 , 1 );
   chessgame.matrix[0][1].cast.i = 0;
   chessgame.matrix[0][1].cast.j = 1;
   chessgame.matrix[0][1].cast.picture = "system/grafx/chess/knight_0.png";
   chessgame.matrix[0][1].cast.cast_id = 3;
   chessgame.matrix[0][1].cast.cast_number = 1;
   chessgame.matrix[0][1].cast.cast_color = 0;
   document.getElementById( chessgame.matrix[0][1].id ).src = "system/grafx/chess/knight_0.png";
chessgame.matrix[7][1].cast = new knight( 7 , 1 );
   chessgame.matrix[7][1].cast.i = 7;
   chessgame.matrix[7][1].cast.j = 1;
   chessgame.matrix[7][1].cast.picture = "system/grafx/chess/knight_1.png";
   chessgame.matrix[7][1].cast.cast_id = 3;
   chessgame.matrix[7][1].cast.cast_number = 1;
   chessgame.matrix[7][1].cast.cast_color = 1;
   document.getElementById( chessgame.matrix[7][1].id ).src = "system/grafx/chess/knight_1.png";
chessgame.matrix[0][6].cast = new knight( 0 , 6 );
   chessgame.matrix[0][6].cast.i = 0;
   chessgame.matrix[0][6].cast.j = 6;
   chessgame.matrix[0][6].cast.picture = "system/grafx/chess/knight_0.png";
   chessgame.matrix[0][6].cast.cast_id = 3;
   chessgame.matrix[0][6].cast.cast_number = 2;
   chessgame.matrix[0][6].cast.cast_color = 0;
   document.getElementById( chessgame.matrix[0][6].id ).src = "system/grafx/chess/knight_0.png";
chessgame.matrix[7][6].cast = new knight( 7 , 6 );
   chessgame.matrix[7][6].cast.i = 7;
   chessgame.matrix[7][6].cast.j = 6;
   chessgame.matrix[7][6].cast.picture = "system/grafx/chess/knight_1.png";
   chessgame.matrix[7][6].cast.cast_id = 3;
   chessgame.matrix[7][6].cast.cast_number = 2;
   chessgame.matrix[7][6].cast.cast_color = 1;
   document.getElementById( chessgame.matrix[7][6].id ).src = "system/grafx/chess/knight_1.png"; 

/* make 4 rooks */
rook.prototype = new cast;
chessgame.matrix[0][0].cast = new rook( 0 , 0 );
   chessgame.matrix[0][0].cast.i = 0;
   chessgame.matrix[0][0].cast.j = 0;
   chessgame.matrix[0][0].cast.picture = "system/grafx/chess/rook_0.png";
   chessgame.matrix[0][0].cast.cast_id = 2;
   chessgame.matrix[0][0].cast.cast_number = 1;
   chessgame.matrix[0][0].cast.cast_color = 0;
   document.getElementById( chessgame.matrix[0][0].id ).src = "system/grafx/chess/rook_0.png";
chessgame.matrix[7][0].cast = new rook( 7 , 0 );
   chessgame.matrix[7][0].cast.i = 7;
   chessgame.matrix[7][0].cast.j = 0;
   chessgame.matrix[7][0].cast.picture = "system/grafx/chess/rook_1.png";
   chessgame.matrix[7][0].cast.cast_id = 2;
   chessgame.matrix[7][0].cast.cast_number = 1;
   chessgame.matrix[7][0].cast.cast_color = 1;
   document.getElementById( chessgame.matrix[7][0].id ).src = "system/grafx/chess/rook_1.png";
chessgame.matrix[0][7].cast = new rook( 0 , 7 );
   chessgame.matrix[0][7].cast.i = 0;
   chessgame.matrix[0][7].cast.j = 7;
   chessgame.matrix[0][7].cast.picture = "system/grafx/chess/rook_0.png";
   chessgame.matrix[0][7].cast.cast_id = 2;
   chessgame.matrix[0][7].cast.cast_number = 2;
   chessgame.matrix[0][7].cast.cast_color = 0;
   document.getElementById( chessgame.matrix[0][7].id ).src = "system/grafx/chess/rook_0.png";
chessgame.matrix[7][7].cast = new rook( 7 , 7 );
   chessgame.matrix[7][7].cast.i = 7;
   chessgame.matrix[7][7].cast.j = 7;
   chessgame.matrix[7][7].cast.picture = "system/grafx/chess/rook_1.png";
   chessgame.matrix[7][7].cast.cast_id = 2;
   chessgame.matrix[7][7].cast.cast_number = 2;
   chessgame.matrix[7][7].cast.cast_color = 1;
   document.getElementById( chessgame.matrix[7][7].id ).src = "system/grafx/chess/rook_1.png"; 
   
/* make 16 lousy pawns */
pawnblack.prototype = new cast;
x = 1;
for ( i = 8; i <= 15 ;i++ )
   {
   y = i - 8;
   chessgame.matrix[x][y].cast = new pawnblack( x , y );
   chessgame.matrix[x][y].cast.i = x;
   chessgame.matrix[x][y].cast.j = y;
   chessgame.matrix[x][y].cast.picture = "system/grafx/chess/pawn_0.png";
   chessgame.matrix[x][y].cast.cast_id = 1;
   chessgame.matrix[x][y].cast.cast_number = y + 1;
   chessgame.matrix[x][y].cast.cast_color = 0;
   document.getElementById( chessgame.matrix[x][y].id ).src = "system/grafx/chess/pawn_0.png";
   }
pawnwhite.prototype = new cast;
x = 6;
for ( i = 48; i <= 55 ;i++ )
   {
   y = i - 48;
   chessgame.matrix[x][y].cast = new pawnwhite( x , y );
   chessgame.matrix[x][y].cast.i = x;
   chessgame.matrix[x][y].cast.j = y;
   chessgame.matrix[x][y].cast.picture = "system/grafx/chess/pawn_1.png";
   chessgame.matrix[x][y].cast.cast_id = 1;
   chessgame.matrix[x][y].cast.cast_number = y + 1;
   chessgame.matrix[x][y].cast.cast_color = 1;
   document.getElementById( chessgame.matrix[x][y].id ).src = "system/grafx/chess/pawn_1.png";
   }

/*alert(id);*/
}
/**/
function repaintChessBoard() {
for ( i = 0; i <= 7 ;i++ )
   {
   for ( j = 0; j <= 7 ;j++ )
      {
      chessgame.matrix[i][j].repaint();
      }
   }
}
/**/
function writeHtml( id , temp )
{
document.getElementById( id ).style.visibility = "hidden";
document.getElementById( id ).innerHTML = temp;
document.getElementById( id ).style.visibility = "visible";
}

