/***********************************************************************************************************
 * su.bot.checkers.javascript.VisualGamePanel
 * Location: http://www.bot.su/library/su/bot/checkers/javascript/VisualGamePanel.js
 * Index Version Location: http://www.bot.su/library/su/bot/checkers/javascript/visualgamepanel/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.VisualGamePanel == "undefined") {
 su.bot.checkers.javascript.VisualGamePanel = function() {}; 
 su.bot.checkers.javascript.VisualGamePanel.prototype = new com.oclib.javascript.lang.Root();

 su.bot.checkers.javascript.VisualGamePanel.prototype.className = "VisualGamePanel"; 
 su.bot.checkers.javascript.VisualGamePanel.prototype.classNamespace = "su.bot.checkers.javascript.VisualGamePanel"; 
 su.bot.checkers.javascript.VisualGamePanel.prototype.classCreated = "20070403"; 
 su.bot.checkers.javascript.VisualGamePanel.prototype.classCreator = "http://www.bot.su/"; 
 su.bot.checkers.javascript.VisualGamePanel.prototype.classLocation = "http://www.bot.su/library/su/bot/checkers/javascript/VisualGamePanel.js"; 
 su.bot.checkers.javascript.VisualGamePanel.prototype.classIndexLocation = "http://www.bot.su/library/su/bot/checkers/javascript/visualgamepanel/index.html"; 
 su.bot.checkers.javascript.VisualGamePanel.prototype.classVersion = "20070528"; 
 su.bot.checkers.javascript.VisualGamePanel.prototype.classVersionLocation = "http://www.bot.su/library/su/bot/checkers/javascript/visualgamepanel/builds/20070528/VisualGamePanel.js"; 
 su.bot.checkers.javascript.VisualGamePanel.prototype.classVersionIndexLocation = "http://www.bot.su/library/su/bot/checkers/javascript/visualgamepanel/index.html"; 
 
 su.bot.checkers.javascript.VisualGamePanel.prototype.environment;
  su.bot.checkers.javascript.VisualGamePanel.prototype.getEnvironment = function () {return this.environment;};
  su.bot.checkers.javascript.VisualGamePanel.prototype.setEnvironment = function (environment) {this.environment = environment;}; 

 su.bot.checkers.javascript.VisualGamePanel.prototype.changeScore = function() {
  if(this.mustTrace()) {this.getLog().println("su.bot.checkers.javascript.VisualGamePanel.changeScore is running...");}; 
  this.changeTextContent("maxScore", this.getEnvironment().getReferee().getMaxScore());
  this.changeTextContent("minScore", this.getEnvironment().getReferee().getMinScore());
 }; 

 su.bot.checkers.javascript.VisualGamePanel.prototype.changeTextContent = function(eId, c) {
  //if(this.mustTrace()) {this.getLog().println("su.bot.checkers.javascript.VisualGamePanel.changeTextContent is running...");}; 
  var e = document.getElementById(eId);
  while(e.hasChildNodes() && e.firstChild) {e.removeChild(e.firstChild);};
  e.appendChild(document.createTextNode(c));
 }; 

 su.bot.checkers.javascript.VisualGamePanel.prototype.doMove = function(e) {
  if(this.mustTrace()) {this.getLog().println("su.bot.checkers.javascript.VisualGamePanel.doMove is runing...");};
  this.getEnvironment().getReferee().inviteDoMove()();
  this.getEnvironment().getDom().doHidden("lastButton");
 };

 su.bot.checkers.javascript.VisualGamePanel.prototype.doSmartCycle = function(e) {
  if(this.mustTrace()) {this.getLog().println("su.bot.checkers.javascript.VisualGamePanel.doSmartCycle is runing...");};
  this.getEnvironment().getMaxGamer().manager();
 };

 su.bot.checkers.javascript.VisualGamePanel.prototype.doStart = function(e) {
  if(this.mustTrace()) {this.getLog().println(".");};
  if(this.mustTrace()) {this.getLog().println(".");};
  if(this.mustTrace()) {this.getLog().println(".");};
  if(this.mustTrace()) {this.getLog().println("su.bot.checkers.javascript.VisualGamePanel.toStart is runing...");};
  if(this.getEnvironment().getReferee().getGameDoRun()) {return -1;};
  this.getEnvironment().getReferee().init();
  this.removeChilds("legalMove");
  this.removeChilds("lastMove");
  if(this.getEnvironment().getProperties().getAutoplay()) {
   this.toStartAutoplay();
  } else {
   this.toStart();
  };
  this.getEnvironment().getReferee().run();
 };

 su.bot.checkers.javascript.VisualGamePanel.prototype.doStop = function(e) {
  if(this.mustTrace()) {this.getLog().println("su.bot.checkers.javascript.VisualGamePanel.toStop is runing...");};
  this.getEnvironment().getReferee().end();
  this.toStop();
 };

 su.bot.checkers.javascript.VisualGamePanel.prototype.doStartAutoplay = function(e) {
  if(this.mustTrace()) {this.getLog().println("su.bot.checkers.javascript.VisualGamePanel.toStartAutoplay is runing...");};
  this.getEnvironment().getProperties().setAutoplay(true);
  this.doStart();
 };

 su.bot.checkers.javascript.VisualGamePanel.prototype.doStopAutoplay = function(e) {
  if(this.mustTrace()) {this.getLog().println("su.bot.checkers.javascript.VisualGamePanel.toStopAutoplay is runing...");};
  this.getEnvironment().getProperties().setAutoplay(false);
  this.toStopAutoplay();
  this.getEnvironment().getReferee().end();
 };
  
 su.bot.checkers.javascript.VisualGamePanel.prototype.init = function() {
  if(this.mustTrace()) {this.getLog().println("su.bot.checkers.javascript.VisualGamePanel.init is runing...");};
  this.removeChilds("legalMove");
  this.removeChilds("lastMove");
  var the = this;
  this.getEnvironment().getEvents().addEventListener("doMove", "click", function(e) {the.doMove(e);}, false);
  this.getEnvironment().getEvents().addEventListener("toGame", "click", function(e) {the.toGame(e);}, false);
  this.getEnvironment().getEvents().addEventListener("toProperties", "click", function(e) {the.toProperties(e);}, false);
  this.getEnvironment().getEvents().addEventListener("doSmartCycle", "click", function(e) {the.doSmartCycle(e);}, false);
  this.getEnvironment().getEvents().addEventListener("doStart", "click", function(e) {the.doStart(e);}, false);
  this.getEnvironment().getEvents().addEventListener("doStop", "click", function(e) {the.doStop(e);}, false);
  this.getEnvironment().getEvents().addEventListener("doStartAutoplay", "click", function(e) {the.doStartAutoplay(e);}, false);
  this.getEnvironment().getEvents().addEventListener("doStopAutoplay", "click", function(e) {the.doStopAutoplay(e);}, false);
  this.update();
 };

 su.bot.checkers.javascript.VisualGamePanel.prototype.move = function(move) {
  if(this.mustTrace()) {this.getLog().println("su.bot.checkers.javascript.VisualGamePanel.move is runing...");};
  var s = move.getChecker().getOwner() + ": " + move.getFrom().getId() + " - " + move.getTo().getId();
  if(move.getTaked()) {s += " (" + move.getTake().getId() + ")";};
  var divElement = document.createElement("div");
  divElement.appendChild(document.createTextNode(s));
  document.getElementById("lastMove").appendChild(divElement);
 };

 su.bot.checkers.javascript.VisualGamePanel.prototype.removeChilds = function(eId) {
  //if(this.mustTrace()) {this.getLog().println("su.bot.checkers.javascript.VisualGamePanel.removeChilds is running...");}; 
  var e = document.getElementById(eId);
  while(e.hasChildNodes() && e.firstChild) {e.removeChild(e.firstChild);};
 }; 

 su.bot.checkers.javascript.VisualGamePanel.prototype.showNodes = function(node) {
  if(this.mustTrace()) {this.getLog().println("su.bot.checkers.javascript.VisualGamePanel.showNodes is runing...");};
  this.removeChilds("legalMove");
  var nodes = node.getNodes();
  for(var i = 0; i < nodes.length; i++) {
   var move = nodes[i].getMove();
   var s = "" + (i+1) + ": " + move.getFrom().getId() + " - " + move.getTo().getId();
   if(move.getTaked()) {s += " (" + move.getTake().getId() + ")";};
   var divElement = document.createElement("div");
   divElement.appendChild(document.createTextNode(s));
   document.getElementById("legalMove").appendChild(divElement);
  };
 };

 su.bot.checkers.javascript.VisualGamePanel.prototype.showAutoplay = function(show) {
  if(this.mustTrace()) {this.getLog().println("su.bot.checkers.javascript.VisualGamePanel.showAutoplay is runing for show="+show);};
  var dom = environment.getDom();
  if(show) {
   dom.doHidden("startButton");
   dom.doHidden("stopButton");
   dom.doHidden("stopAutoplayButton");
   dom.doVisible("autoplayDiv");
   dom.doVisible("startAutoplayButton");
  } else {
   dom.doHidden("stopButton");
   dom.doHidden("stopAutoplayButton");
   dom.doHidden("autoplayDiv");
   dom.doHidden("startAutoplayButton");
   dom.doVisible("startButton");
  };
  this.changeScore();
 };

 su.bot.checkers.javascript.VisualGamePanel.prototype.toGame = function(e) {
  if(this.mustTrace()) {this.getLog().println("su.bot.checkers.javascript.VisualGamePanel.toGame is runing...");};
  var environment = this.getEnvironment();
  var maxGamer = environment.getMaxGamer();
  var minGamer = environment.getMinGamer();
  var opponents = maxGamer.getClassName()+"."+maxGamer.getClassVersion()+" ("+maxGamer.getName()+") vs "+minGamer.getClassName()+"."+minGamer.getClassVersion()+" ("+minGamer.getName()+")";
  var dom = environment.getDom();
  dom.changeTextContent("opponents", opponents);
  dom.doHidden("propertiesPanel");
  dom.doVisible("gamePanel");
  environment.getReferee().init();
 };

 su.bot.checkers.javascript.VisualGamePanel.prototype.toPause = function() {
  if(this.mustTrace()) {this.getLog().println("su.bot.checkers.javascript.VisualGamePanel.toPause is runing...");};
  if(this.getEnvironment().getReferee().getGameDoRun()) {
   this.getEnvironment().getDom().doVisible("lastButton");
  };
 };

 su.bot.checkers.javascript.VisualGamePanel.prototype.toProperties = function(e) {
  if(this.mustTrace()) {this.getLog().println("su.bot.checkers.javascript.VisualGamePanel.toProperties is runing...");};
  if(!this.getEnvironment().getReferee().getGameDoRun()) {
   this.getEnvironment().getDom().doHidden("gamePanel");
   this.getEnvironment().getDom().doVisible("propertiesPanel");
  };
 };

 su.bot.checkers.javascript.VisualGamePanel.prototype.toStart = function(e) {
  if(this.mustTrace()) {this.getLog().println("su.bot.checkers.javascript.VisualGamePanel.toStart is runing...");};
  this.getEnvironment().getDom().doHidden("startAutoplayButton");
  this.getEnvironment().getDom().doHidden("startButton");
  this.getEnvironment().getDom().doHidden("stopAutoplayButton");
  this.getEnvironment().getDom().doVisible("stopButton");
 };

 su.bot.checkers.javascript.VisualGamePanel.prototype.toStartAutoplay = function() {
  if(this.mustTrace()) {this.getLog().println("su.bot.checkers.javascript.VisualGamePanel.toStartAutoplay is runing...");};
  this.getEnvironment().getDom().doHidden("lastButton");
  this.getEnvironment().getDom().doHidden("startAutoplayButton");
  this.getEnvironment().getDom().doHidden("startButton");
  this.getEnvironment().getDom().doHidden("stopButton");
  this.getEnvironment().getDom().doVisible("stopAutoplayButton");
 };

 su.bot.checkers.javascript.VisualGamePanel.prototype.toStop = function(e) {
  if(this.mustTrace()) {this.getLog().println("su.bot.checkers.javascript.VisualGamePanel.toStop is runing...");};
  this.getEnvironment().getDom().doHidden("lastButton");
  this.getEnvironment().getDom().doHidden("startAutoplayButton");
  this.getEnvironment().getDom().doHidden("stopAutoplayButton");
  this.getEnvironment().getDom().doHidden("stopButton");
  this.getEnvironment().getDom().doVisible("startButton");
 };
 
 su.bot.checkers.javascript.VisualGamePanel.prototype.toStopAutoplay = function(e) {
  if(this.mustTrace()) {this.getLog().println("su.bot.checkers.javascript.VisualGamePanel.toStopAutoplay is runing...");};
  this.getEnvironment().getDom().doHidden("lastButton");
  this.getEnvironment().getDom().doHidden("stopAutoplayButton");
  this.getEnvironment().getDom().doHidden("startButton");
  this.getEnvironment().getDom().doHidden("stopButton");
  this.getEnvironment().getDom().doVisible("startAutoplayButton");
 };
 
 su.bot.checkers.javascript.VisualGamePanel.prototype.update = function() {
  // if(this.mustTrace()) {this.getLog().println("su.bot.checkers.javascript.VisualGamePanel.update is runing...");};
  var environment = this.getEnvironment();
  var referee = environment.getReferee();
  var properties = environment.getProperties();
  this.changeTextContent(properties.numberMoveId, referee.numberMove);
  this.changeTextContent(properties.whoseMoveId, referee.whoseMove);
  var the = this;
  setTimeout(function(){the.update();}, 1015);
 };
};
