/***********************************************************************************************************
 * su.bot.checkers.javascript.VisualBoard
 * Location: http://www.bot.su/library/su/bot/checkers/javascript/VisualBoard.js
 * Index Version Location: http://www.bot.su/library/su/bot/checkers/javascript/visualboard/index.html
 * © Bot.Su (http://www.bot.su/)
***********************************************************************************************************/

if (typeof su == "undefined") {su = {};};
if (typeof su.bot == "undefined") {su.bot = {};};
if (typeof su.bot.checkers == "undefined") {su.bot.checkers = {};};
if (typeof su.bot.checkers.javascript == "undefined") {su.bot.checkers.javascript = {};};

if (typeof su.bot.checkers.javascript.VisualBoard == "undefined") {
 su.bot.checkers.javascript.VisualBoard = function() {}; 
 su.bot.checkers.javascript.VisualBoard.prototype = new com.oclib.javascript.lang.Root();

 su.bot.checkers.javascript.VisualBoard.prototype.className = "VisualBoard"; 
 su.bot.checkers.javascript.VisualBoard.prototype.classNamespace = "su.bot.checkers.javascript.VisualBoard"; 
 su.bot.checkers.javascript.VisualBoard.prototype.classCreated = "20070403"; 
 su.bot.checkers.javascript.VisualBoard.prototype.classCreator = "http://www.bot.su/"; 
 su.bot.checkers.javascript.VisualBoard.prototype.classLocation = "http://www.bot.su/library/su/bot/checkers/javascript/VisualBoard.js"; 
 su.bot.checkers.javascript.VisualBoard.prototype.classIndexLocation = "http://www.bot.su/library/su/bot/checkers/javascript/visualboard/index.html"; 
 su.bot.checkers.javascript.VisualBoard.prototype.classVersion = "20070528"; 
 su.bot.checkers.javascript.VisualBoard.prototype.classVersionLocation = "http://www.bot.su/library/su/bot/checkers/javascript/visualboard/builds/20070528/VisualBoard.js"; 
 su.bot.checkers.javascript.VisualBoard.prototype.classVersionIndexLocation = "http://www.bot.su/library/su/bot/checkers/javascript/visualboard/index.html"; 
 
 su.bot.checkers.javascript.VisualBoard.prototype.environment;
  su.bot.checkers.javascript.VisualBoard.prototype.getEnvironment = function () {return this.environment;};
  su.bot.checkers.javascript.VisualBoard.prototype.setEnvironment = function (environment) {this.environment = environment;}; 
 su.bot.checkers.javascript.VisualBoard.prototype.moveTimer;
  su.bot.checkers.javascript.VisualBoard.prototype.getMoveTimer = function () {return this.moveTimer;};
  su.bot.checkers.javascript.VisualBoard.prototype.setMoveTimer = function (moveTimer) {this.moveTimer = moveTimer;}; 
 
 su.bot.checkers.javascript.VisualBoard.prototype.display = function() {
  if(this.mustTrace()) {this.getLog().println("su.bot.checkers.javascript.VisualBoard.display is runing...");};
  var environment = this.getEnvironment();
  var properties = environment.getProperties();
  var board = environment.getMainBoard();
  var numberSquaresHorizontal = properties.getNumberSquaresHorizontal();
  var numberSquaresVertical = properties.getNumberSquaresVertical();
  var boardTable = document.createElement("table");
  boardTable.id = properties.getBoardTableElementId(); 
  for(var i = 0; i < numberSquaresHorizontal; i++) {
   var boardTableTr = document.createElement("tr");
   for(var j = 0; j < numberSquaresVertical; j++) {
    var boardTableTd = document.createElement("td");
    var boardTableTdImg = document.createElement("img");
	var checker = board.getChecker(i, j);
	var square = board.getSquare(i, j);
    boardTableTdImg.id = square.getId();
	if(square.getStatus()) {
     boardTableTd.className = properties.getTrueSquareClassName();
     if(checker.getOwner() == "max") {
      if(checker.getType() == "checker") {
       boardTableTdImg.src = properties.getMaxCheckerLocation();
	  } else if(checker.getType() == "king") {
       boardTableTdImg.src = properties.getMaxKingLocation();
	  } else {
       boardTableTdImg.src = properties.getTrueSquareLocation();
	  };
	 } else if(checker.getOwner() == "min") {
      if(checker.getType() == "checker") {
       boardTableTdImg.src = properties.getMinCheckerLocation();
	  } else if(checker.getType() == "king") {
       boardTableTdImg.src = properties.getMinKingLocation();
	  } else {
       boardTableTdImg.src = properties.getTrueSquareLocation();
	  };
	 } else {
      boardTableTdImg.src = properties.getTrueSquareLocation();
	 };
	} else {
     boardTableTd.className = properties.getFalseSquareClassName();
     boardTableTdImg.src = properties.getFalseSquareLocation();
	};
    boardTableTd.appendChild(boardTableTdImg);
    boardTableTr.appendChild(boardTableTd);
   };
   boardTable.appendChild(boardTableTr);
  };
  this.getEnvironment().getDom().changeChilds(this.getEnvironment().getProperties().getBoardElementId(), boardTable);
 };  
 
 su.bot.checkers.javascript.VisualBoard.prototype.move = function(move) {
  if(this.mustTrace()) {this.getLog().println("su.bot.checkers.javascript.VisualBoard.move is runing for move="+move);};
  //if(this.mustDebug()) {this.getLog().println("su.bot.checkers.javascript.VisualBoard.move is runing move: "+move.from.getId()+"-"+move.from.getId()+"; move.comeKing="+move.comeKing+"; move.checker.owner="+move.checker.owner);};
  var from = move.getFrom();
  var to = move.getTo();
  document.getElementById(from.getId()).style.borderColor = "yellow";
  var the = this; this.moveTimer = setTimeout(function(){the.moveLast(move);}, 100);
 }; 
 
 su.bot.checkers.javascript.VisualBoard.prototype.moveLast = function(move) {
  if(this.mustTrace()) {this.getLog().println("su.bot.checkers.javascript.VisualBoard.moveLast is runing for move="+move);};
  //if(this.mustDebug()) {this.getLog().println("su.bot.checkers.javascript.VisualBoard.moveLast is runing move: "+move.from.getId()+"-"+move.from.getId()+"; move.comeKing="+move.comeKing+"; move.checker.owner="+move.checker.owner);};
  var from = move.getFrom();
  var to = move.getTo();
  var environment = this.getEnvironment();
  var properties = environment.getProperties();
  var board = environment.getMainBoard();
  var checker = move.getChecker();
  document.getElementById(to.getId()).style.borderColor = "yellow";
  document.getElementById(from.getId()).src = properties.getTrueSquareLocation();
  if(checker.getOwner() == "max") {
   if(checker.isChecker() && !move.getComeKing()) {
    document.getElementById(to.getId()).src = properties.getMaxCheckerLocation();
   } else {
    document.getElementById(to.getId()).src = properties.getMaxKingLocation();
   };
  } else {
   if(checker.isChecker() && !move.getComeKing()) {
    document.getElementById(to.getId()).src = properties.getMinCheckerLocation();
   } else {
    document.getElementById(to.getId()).src = properties.getMinKingLocation();
   };
  };
  if(move.isTaked()) {
   var take = move.getTake();
   document.getElementById(take.getId()).style.borderColor = "yellow";
   document.getElementById(take.getId()).src = properties.getTrueSquareLocation();
   document.getElementById(take.getId()).style.borderColor = "#000000";
  };
  document.getElementById(from.getId()).style.borderColor = "#000000";
  document.getElementById(to.getId()).style.borderColor = "#000000";
 }; 
};
