var HumanGame=new Class({Extends:Game,initialize:function(a){this.opponent=null;this.lastOpponentFrom=-1;this.lastOpponentDest=-1;this.lastSelectedPieceIE=-1;a.piecesContainerId="humanGamePieces";this.parent(a);this.OPPONENT=0;this.SELF=1;this.NOGAME=2;this.gameState=this.NOGAME;this.selfTimer=null;this.opponentTimer=null;this.DEFAULT_MAX_TIME=900;this.selfTime=this.DEFAULT_MAX_TIME;this.opponentTime=this.DEFAULT_MAX_TIME},setTurnSelf:function(){this.gameState=this.SELF;pragmaticLogic.Board.hideComputerThinking()},setTurnOpponent:function(){this.gameState=this.OPPONENT;pragmaticLogic.Board.drawComputerThinking()},setTurnNoGame:function(){this.gameState=this.NOGAME;this.opponent=null;pragmaticLogic.Board.clearAllLegalMoves();pragmaticLogic.Board.hideComputerThinking()},whoseTurn:function(){return this.gameState},getOpponentContact:function(){return this.opponent},getPiecesContainerId:function(){return"humanGamePieces"},opponentMakeMove:function(c,a,b){this.showOpponentMove(c,a);this.setTurnSelf();this.updateOpponentTime(b);this.stopOpponentTimer();if(this.opponent!=null){this.startSelfTimer()}},selfMakeMove:function(b,a){if(this.gameState!=this.NOGAME){pragmaticLogic.StropheService.sendGameMove(b,a,this.selfTime);this.setTurnOpponent();this.stopSelfTimer();this.startOpponentTimer()}else{}},showOpponentMove:function(c,a){var d=89-Number(c);var b=89-Number(a);if(this.lastOpponentFrom!=d&&this.lastOpponentDest!=b){this.lastOpponentFrom=d;this.lastOpponentDest=b;this.onMovePiece(d,b)}else{this.onMovePiece(d,b)}},verifyMove:function(n,k,i,j,h,f){var e=function(q,r,p){if(!q){var o=this.generalId[this.SELF];if(this.isChecked(this.SELF)){pragmaticLogic.Board.drawCheck(this.piece[o].position)}}else{pragmaticLogic.Board.drawTrail(r,p)}}.bind(this);var m=pragmaticLogic.Board.getBoardPosFromMouse(j,h);var l=pragmaticLogic.Board.getBoardPosFromMouse(k,i);var d=false;var b=new Fx.Morph(n,{duration:500,link:"chain",onComplete:function(){e(d,m,l)}});var g=pragmaticLogic.Board.getBoardCoord(m);if(this.opponent&&this.gameState==this.SELF){if(!pragmaticLogic.Board.isPosInBoard(k,i)||this.gameState!=this.SELF){b.options.transition=Fx.Transitions.Cubic;d=false}else{if(pragmaticLogic.Board.getBoardPosFromMouse(k,i)==-1){b.options.transition=Fx.Transitions.Cubic}else{if(pragmaticLogic.Board.getBoardPosFromMouse(k,i)==pragmaticLogic.Board.getBoardPos(g.x,g.y)){b.options.transition=Fx.Transitions.Cubic}else{d=this.checkLegalMove(m,l,f);if(d){b.options.transition=Fx.Transitions.linear;var a=pragmaticLogic.Board.getBoardPosFromMouse(k,i);g=pragmaticLogic.Board.getBoardCoord(a)}else{b.options.transition=Fx.Transitions.Cubic}}}}b.start({left:[k-25,g.x+"px"],top:[i-25,g.y+"px"]})}else{if(this.opponent==null){var c="You are currently not in a game. You must be in a game in order to make a move.";b.start({left:[k,j+"px"],top:[i,h+"px"]})}}return d},checkForEndGame:function(c,d,b){pragmaticLogic.Board.clearCheck();this.makeMove(c,d,b);var a=this.generalId[this.OPPONENT];if(this.isChecked(this.OPPONENT)){pragmaticLogic.Board.drawCheck(this.piece[a].position)}if(this.isGameOver(this.OPPONENT)){this.gameState=this.NOGAME;pragmaticLogic.StropheService.sendCheckMateMove(d,b)}else{this.selfMakeMove(d,b)}},handleLegalMoveClicked:function(a,e){var d=function(g,h,f){this.checkForEndGame(g,h,f)}.bind(this);var c=function(k,h,i,j,f){pragmaticLogic.Board.drawTrail(k,h);if(this.opponent==null&&this.gameState==this.NOGAME){var g=new Fx.Morph(i.pieceView.eDiv,{duration:500,link:"chain",transition:Fx.Transitions.Cubic});g.start({left:[f.x,j.x+"px"],top:[f.y,j.y+"px"]}).chain(function(){pragmaticLogic.Board.clearTrail()})}}.bind(this);var b="legalMove_"+a.dest;$(b).addEvent("click",function(j){this.lastSelectedPieceIE=-1;var l=a.from;var g=a.dest;var h=this.piece[e];var i=new Fx.Morph(h.pieceView.eDiv,{duration:500,link:"chain",transition:Fx.Transitions.Cubic,onComplete:function(){d(e,l,g)}});var k=pragmaticLogic.Board.getBoardCoord(l);var f=pragmaticLogic.Board.getBoardCoord(g);i.start({left:[k.x+"px",f.x+"px"],top:[k.y+"px",f.y+"px"]}).chain(function(){c(l,g,h,k,f)});this.clearAllLegalMoves($(b))}.bind(this))},clearAllLegalMoves:function(a){a.setStyle("z-index","100");pragmaticLogic.Board.clearAllLegalMoves();this.lastSelectedPieceIE=-1;pragmaticLogic.Board.clearCheck();pragmaticLogic.Board.clearSelectedIE()},setupMovePreference:function(){var a=Cookie.read("moveStyle");var b=(a!=null)?a:this.POINTCLICK;this.setMoveStyle(b)},onMovePiece:function(c,a){var b=this.vertex[c];this.animateMove(Number(b.id),b.pieceView.eDiv,c,a)},animateMove:function(f,d,h,b){var e=function(j){pragmaticLogic.Board.clearCheck();this.makeMove(j,h,b);var i=this.generalId[this.SELF];if(this.isChecked(this.SELF)){pragmaticLogic.Board.drawCheck(this.piece[i].position)}if(this.isGameOver(this.SELF)){this.gameState=this.NOGAME}else{$j("#lobby_greetings").text("It's your turn to move...").css("display","block").fadeOut(7000)}}.bind(this);var c=new Fx.Morph(d,{duration:500,link:"chain",transition:Fx.Transitions.Cubic,onComplete:function(){e(f)}});var g=pragmaticLogic.Board.getBoardCoord(h);var a=pragmaticLogic.Board.getBoardCoord(b);c.start({left:[g.x+"px",a.x+"px"],top:[g.y+"px",a.y+"px"]}).chain(function(){pragmaticLogic.Board.drawTrail(h,b)})},startGame:function(b,a){this.opponent=b;if(a==null){this.selfTime=this.DEFAULT_MAX_TIME;this.opponentTime=this.DEFAULT_MAX_TIME}else{this.selfTime=a;this.opponentTime=a}$("selfTime").set("text",$j.secToFriendlyDisplayTime(this.selfTime));$("opponentTime").set("text",$j.secToFriendlyDisplayTime(this.opponentTime))},stopGame:function(){this.opponent=null;this.setTurnNoGame();this.stopSelfTimer();this.stopOpponentTimer()},updateOpponentTime:function(a){this.opponentTime=a},startSelfTimer:function(a,c,b){this.selfTimer=setInterval(function(){this.selfTime-=1;var f=this.selfTime%60;var e=(this.selfTime-f)/60;if(e>=0){e=Math.floor(e)}else{e=Math.ceil(e)}var d=e+":"+(f>9?f:"0"+f);$("selfTime").set("text",d);if(this.selfTime<=0){pragmaticLogic.StropheService.sendNoTimeLeft()}}.bind(this),1000)},stopSelfTimer:function(){clearInterval(this.selfTimer);this.selfTimer=null},startOpponentTimer:function(){this.opponentTimer=setInterval(function(){this.opponentTime-=1;var c=this.opponentTime%60;var b=(this.opponentTime-c)/60;if(b>=0){b=Math.floor(b)}else{b=Math.ceil(b)}if(this.opponentTime>=0){var a=b+":"+(c>9?c:"0"+c);$("opponentTime").set("text",a)}}.bind(this),1000)},stopOpponentTimer:function(){clearInterval(this.opponentTimer);this.opponentTimer=null},getMoveStyle:function(){return this.moveStyle},setMoveStyle:function(d){this.moveStyle=d;Cookie.write("moveStyle",d,{duration:735});if(d==this.DRAGDROP){pragmaticLogic.Board.clearSelectedIE();pragmaticLogic.Board.clearAllLegalMoves();this.lastSelectedPieceIE=-1;var a=this;var c={};var b={};$j(".humanGamePieces.piece").unbind("click");$j(".humanGamePieces.piece").bind("mouseover",{thisContext:this},function(f,g){if(f.data.thisContext.gameState==f.data.thisContext.SELF||f.data.thisContext.gameState==f.data.thisContext.NOGAME){var e=f.data.thisContext.findLegalMoves(Number($j(this).attr("id")));$j(this).css({"z-index":102});pragmaticLogic.Board.showLegalMoves(e,true)}}).bind("mouseout",{thisContext:this},function(e,f){$j(this).css({"z-index":100});pragmaticLogic.Board.clearAllLegalMoves()}).draggable({start:function(e,f){c.x=$j(this).position().left;c.y=$j(this).position().top},stop:function(g,i){b.x=$j(this).position().left;b.y=$j(this).position().top;pragmaticLogic.Board.clearAllLegalMoves();var j=Number($j(this).attr("id"));var e=a.findLegalMoves(j);var h=a.verifyMove(this,b.x,b.y,c.x,c.y,e);var k=pragmaticLogic.Board.getBoardPosFromMouse(c.x,c.y);var f=pragmaticLogic.Board.getBoardPosFromMouse(b.x,b.y);if(h){a.checkForEndGame(j,k,f)}}})}else{$j(".humanGamePieces.piece").unbind("mouseover").unbind("mouseout").draggable("destroy");$j(".humanGamePieces.piece").bind("click",{thisContext:this},function(f,g){if(f.data.thisContext.gameState==f.data.thisContext.SELF||f.data.thisContext.gameState==f.data.thisContext.NOGAME){pragmaticLogic.Board.clearSelectedIE();var h=Number($j(this).attr("id"));if(h!=f.data.thisContext.lastSelectedPieceIE){pragmaticLogic.Board.drawSelectedIE(f.data.thisContext.piece[h].position);pragmaticLogic.Board.clearAllLegalMoves();var e=f.data.thisContext.findLegalMoves(h);$j(this).css({"z-index":102});pragmaticLogic.Board.showLegalMoves(e,true);f.data.thisContext.lastSelectedPieceIE=h;if(e!=null){e.each(function(i){$("legalMove_"+i.dest).removeEvents();f.data.thisContext.handleLegalMoveClicked(i,h)})}}else{pragmaticLogic.Board.clearCheck();clearAllLegalMoves(item)}}})}}});