//------------------------------------------------------------------------------------------
// (c) iBoard/netAdapt Ltd.
//------------------------------------------------------------------------------------------
function CPgnControl(tileSize,setStyle,boardStyle){this.m_instanceId=Math.floor(Math.random()*999999);this.m_moveIndex=0;this.m_headersArray=new Array;this.m_fensArray=new Array;this.m_algebraicMovesArray=new Array;this.m_rawMovesArray=new Array;this.m_tileSize=tileSize;this.m_setStyle=setStyle;this.m_boardStyle=boardStyle;this.m_isInverted=false;this.m_isPgnVisibleAtRender=false;this.m_isHeaderVisibleAtRender=false;this.m_isStartGameLink=false;this.m_pgnArray=new Array;this.m_isMaterialCapturedShown=true;this.RenderControl=RenderControl;this.RenderBoardContent=RenderBoardContent;this.SetHeadersArray=SetHeadersArray;this.SetFensArray=SetFensArray;this.SetMoveIndex=SetMoveIndex;this.GetLastMoveIndex=GetLastMoveIndex;this.SetAlgebraicMovesArray=SetAlgebraicMovesArray;this.SetRawMovesArray=SetRawMovesArray;this.GetInstanceId=GetInstanceId;this.GoToFirstMove=GoToFirstMove;this.GoToLastMove=GoToLastMove;this.GoToPreviousMove=GoToPreviousMove;this.GoToNextMove=GoToNextMove;this.InvertBoard=InvertBoard;this.StartGame=StartGame;this.SetIsPgnVisibleAtRender=SetIsPgnVisibleAtRender;this.SetIsHeadersVisibleAtRender=SetIsHeadersVisibleAtRender;this.SetPgnArray=SetPgnArray;this.TogglePgn=TogglePgn;this.ToggleHeaders=ToggleHeaders;this.RenderNavControl=RenderNavControl;this.RenderSpecialControl=RenderSpecialControl;this.SetIsStartGameLink=SetIsStartGameLink;this.SetIsMaterialCapturedShown=function(isMaterialCapturedShown){this.m_isMaterialCapturedShown=isMaterialCapturedShown;};this.GetCapturedContentDiv=GetCapturedContentDiv;this.m_capturedBarPcSizePx=22;this.m_capturedBarXOffset=0;this.m_capturedBarYOffset=0;this.m_isPlayOnRender=false;this.AddCapturedPieces=AddCapturedPieces;this.NextAnimation=NextAnimation;this.AnimatePieceMove=AnimatePieceMove;this.IsAnimationFinished=IsAnimationFinished;this.StartPlayAnimation=StartPlayAnimation;this.m_currentAnimatedPcEl=null;this.StopAnimation=StopAnimation;this.IsMicroBoard=function(){return this.m_tileSize<44;};}function GetInstanceId(){return this.m_instanceId;}function SetHeadersArray(headersArray){this.m_headersArray=headersArray;}function SetFensArray(fensArray){this.m_fensArray=fensArray;}function SetMoveIndex(moveIndex){this.m_moveIndex=moveIndex;}function SetAlgebraicMovesArray(algebraicMovesArray){this.m_algebraicMovesArray=algebraicMovesArray;}function SetRawMovesArray(rawMovesArray){this.m_rawMovesArray=rawMovesArray;}function SetIsPgnVisibleAtRender(isPgnVisibleAtRender){this.m_isPgnVisibleAtRender=isPgnVisibleAtRender;}function SetPgnArray(pgnArray){this.m_pgnArray=pgnArray;}function RenderControl(instanceName){var isPgn=this.m_pgnArray.length>0;if(this.IsMicroBoard()){this.m_isMaterialCapturedShown=false;}InitMiniBoard(this.m_tileSize,this.m_setStyle,this.m_boardStyle);document.write("<div id=\"pgnBrowserDivId"+this.GetInstanceId()+"\" style=\"text-align:left;width:"+(this.m_tileSize*8+14)+"px;background:#FFF;padding:4px;margin:4px;border:solid 1px #CCC\" align=\"center;\">");document.write("<div id=\"headersDivId"+this.GetInstanceId()+"\" style=\"display:"+(this.m_isHeaderVisibleAtRender?"block":"none")+";\">");document.write("<table width=\"100%\">");var n=0;for(key in this.m_headersArray){document.write("<tr><td style=\"border-bottom:dashed 1px #CCC;border-right:solid 1px #CCC;font-family:helvetica;font-size:11px;\" valign=\"top\">"+key+"</td><td style=\"font-size:11px;border-bottom:dashed 1px #CCC\">"+this.m_headersArray[key]+"&nbsp;</td></tr>");}document.write("</table>");document.write("</div>");document.write("<div id=\"boardDivId"+this.GetInstanceId()+"\"></div>");document.getElementById("boardDivId"+this.GetInstanceId()).innerHTML=GetVacantMiniBoardCode(this.GetInstanceId());document.write("<div id=\"moveDivId"+this.GetInstanceId()+"\"  align=\"center\" style=\"margin-top:2px;border:solid 1px #888;padding:4px;font-size:13px;font-weight:bold\">");document.write("</div>");if(this.m_isMaterialCapturedShown){var captureElementsStr="";captureElementsStr+="<div style=\"margin-top:2px;padding:4px;position:relative;height:56px;background:#DEDEDE;border:solid 1px #888;\">";captureElementsStr+="<div id=\"materialDiffScoreId"+this.GetInstanceId()+"\" style=\"float:right;height:"+ValInPx(this.m_capturedBarPcSizePx*2)+";padding-top:"+ValInPx(this.m_capturedBarPcSizePx-3)+";\"></div>";captureElementsStr+="<div id=\"capturedMaterial"+this.GetInstanceId()+"\" style=\"position:absolute;margin-top:2px;padding:2px;height:50px;\">&nbsp;</div>";captureElementsStr+="</div>";document.write(captureElementsStr);}document.write("<div id=\"navControlsDivId"+this.GetInstanceId()+"\" style=\"margin-top:2px;padding-top:8px;padding-bottom:8px;background:#DEDEDE;border:solid 1px #888;\" align=\"center\">");this.RenderNavControl(instanceName,this.IsMicroBoard()?"&laquo;":"&laquo; First","GoToFirstMove( )");this.RenderNavControl(instanceName,"&lt; Prev","GoToPreviousMove( )");this.RenderNavControl(instanceName,"Play","StartPlayAnimation( )");this.RenderNavControl(instanceName,"Next &gt;","GoToNextMove( )");this.RenderNavControl(instanceName,this.IsMicroBoard()?"&raquo;":"Last &raquo;","GoToLastMove( )");document.write("</div>");document.write("<div id=\"specialControlsDivId"+this.GetInstanceId()+"\" style=\"padding:8px;\" align=\"center\">");this.RenderSpecialControl(instanceName,"Invert Board","InvertBoard( )");if(isPgn){this.RenderSpecialControl(instanceName,"PGN","TogglePgn( )");}this.RenderSpecialControl(instanceName,"Headers","ToggleHeaders( )");if(this.m_isStartGameLink){this.RenderSpecialControl(instanceName,"Start Game","StartGame( )");}document.write("</div>");if(isPgn){document.write("<div id=\"pgnDivId"+this.GetInstanceId()+"\" style=\"display:"+(this.m_isPgnVisibleAtRender?"block":"none")+";padding:4px;border:solid 1px #FFD735;background:#FFF1BB;font-family:Courier New,Courier;font-size:11px;height:80px;overflow-x: hidden; overflow-y: scroll;\">");var n=0;for(var n=0;n<this.m_pgnArray.length;n++){document.write(this.m_pgnArray[n]+"<br>");}document.write("</div>");}document.write("</div>");this.RenderBoardContent();}function RenderNavControl(instanceName,label,action){document.write("<input type=\"button\" value=\""+label+"\"   onclick=\""+instanceName+"."+action+";return false;\" style=\"background:#FDFDFD;border:solid 1px #AAA;margin:1px;\" >");}function RenderSpecialControl(instanceName,label,action){document.write("<span style=\"margin-top:2px;padding:2px;\"><a style=\"color:#000\" href=\"#\" onclick=\""+instanceName+"."+action+";return false;\">"+label+"</a></span>");}function SetIsHeadersVisibleAtRender(isHeaderVisibleAtRender){this.m_isHeaderVisibleAtRender=isHeaderVisibleAtRender;}function SetIsStartGameLink(isStartGameLink){this.m_isStartGameLink=isStartGameLink;}function IsAnimationFinished(){return this.m_moveIndex>=this.GetLastMoveIndex();}function StartPlayAnimation(){this.StopAnimation();if(this.m_moveIndex==this.GetLastMoveIndex()){this.GoToFirstMove();}this.NextAnimation(true);}function NextAnimation(isFirstAnimationP){var isFirstAnimation=isFirstAnimationP||false;if(!this.IsAnimationFinished()){this.RenderBoardContent();this.m_moveIndex++;var rawMove=this.m_rawMovesArray[this.m_moveIndex];var srcX=rawMove.charAt(0);var srcY=rawMove.charAt(1);var tgtX=rawMove.charAt(2);var tgtY=rawMove.charAt(3);var pcEl=GetPcEl(this.GetInstanceId(),srcX,srcY);pcEl.m_owner=this;if(pcEl){pcEl.m_numPauseIterations=isFirstAnimation?0:30;pcEl.m_pauseIterationsIndex=0;this.AnimatePieceMove(pcEl,srcX,srcY,tgtX,tgtY);}}}function AnimatePieceMove(pcEl,srcX,srcY,tgtX,tgtY){srcX=CorrectBoardPos(srcX,this.m_isInverted);srcY=CorrectBoardPos(srcY,this.m_isInverted);tgtX=CorrectBoardPos(tgtX,this.m_isInverted);tgtY=CorrectBoardPos(tgtY,this.m_isInverted);pcEl.m_animStepsMax=25;pcEl.m_animStepIndex=0;var xDiff=tgtX-srcX;var yDiff=tgtY-srcY;var xDiffPx=this.m_tileSize*xDiff;var yDiffPx=this.m_tileSize*yDiff;pcEl.m_xSrcPx=this.m_tileSize*srcX;pcEl.m_ySrcPx=this.m_tileSize*srcY;pcEl.m_xStepPx=xDiffPx/(pcEl.m_animStepsMax+1);pcEl.m_yStepPx=yDiffPx/(pcEl.m_animStepsMax+1);pcEl.style.zIndex=250;pcEl.m_previousZindex=pcEl.style.zIndex;if(pcEl.m_isLMovement){}this.m_currentAnimatedPcEl=pcEl;g_animationManager.AddAnimatedEl(pcEl.id,pcEl);}function StopAnimation(){if(this.m_currentAnimatedPcEl){g_animationManager.RemoveAnimatedEl(this.m_currentAnimatedPcEl.id);this.m_currentAnimatedPcEl=null;this.RenderBoardContent();}}function GetLastMoveIndex(){return this.m_fensArray.length-1;}function GoToFirstMove(){this.StopAnimation();this.m_moveIndex=0;this.RenderBoardContent();}function GoToLastMove(){this.StopAnimation();this.m_moveIndex=this.GetLastMoveIndex();this.RenderBoardContent();}function GoToPreviousMove(){this.StopAnimation();if(this.m_moveIndex>0){this.m_moveIndex--;}this.RenderBoardContent();}function GoToNextMove(){this.StopAnimation();if(this.m_moveIndex<this.m_fensArray.length-1){this.m_moveIndex++;}this.RenderBoardContent();}function InvertBoard(){this.m_isInverted=!this.m_isInverted;this.RenderBoardContent();}function TogglePgn(){ToggleBlock("pgnDivId"+this.GetInstanceId());}function ToggleHeaders(){ToggleBlock("headersDivId"+this.GetInstanceId());}function RenderBoardContent(){var rawMove=null;if(this.m_rawMovesArray.length>0){rawMove=this.m_rawMovesArray[this.m_moveIndex];}g_pcSize=this.m_tileSize;g_pcSet=this.m_setStyle;g_board=this.m_boardStyle;PopulateMiniBoard(this.GetInstanceId(),this.m_fensArray[this.m_moveIndex],this.m_isInverted,rawMove);document.getElementById("moveDivId"+this.GetInstanceId()).innerHTML=this.m_algebraicMovesArray[this.m_moveIndex];if(this.m_isMaterialCapturedShown){var fenReport=new CFenReport(this.m_fensArray[this.m_moveIndex]);var capturedMaterialDiv=document.getElementById("capturedMaterial"+this.GetInstanceId());capturedMaterialDiv.innerHTML="";capturedMaterialDiv.appendChild(this.GetCapturedContentDiv(fenReport));var diffScore=fenReport.GetMaterialDifferenceScore();document.getElementById("materialDiffScoreId"+this.GetInstanceId()).innerHTML=diffScore>0?"+"+diffScore:diffScore;}}function GetCapturedContentDiv(fenReport){var capturedContentDiv=CreateElement("div");this.m_capturedBarXOffset=0;this.m_capturedBarYOffset=0;this.AddCapturedPieces(capturedContentDiv,"Q",1-fenReport.GetTotalWhiteQueens());this.AddCapturedPieces(capturedContentDiv,"R",2-fenReport.GetTotalWhiteRooks());this.AddCapturedPieces(capturedContentDiv,"B",2-fenReport.GetTotalWhiteBishops());this.AddCapturedPieces(capturedContentDiv,"N",2-fenReport.GetTotalWhiteKnights());this.AddCapturedPieces(capturedContentDiv,"P",8-fenReport.GetTotalWhitePawns());this.m_capturedBarXOffset=0;this.m_capturedBarYOffset=1;this.AddCapturedPieces(capturedContentDiv,"q",1-fenReport.GetTotalBlackQueens());this.AddCapturedPieces(capturedContentDiv,"r",2-fenReport.GetTotalBlackRooks());this.AddCapturedPieces(capturedContentDiv,"b",2-fenReport.GetTotalBlackBishops());this.AddCapturedPieces(capturedContentDiv,"n",2-fenReport.GetTotalBlackKnights());this.AddCapturedPieces(capturedContentDiv,"p",8-fenReport.GetTotalBlackPawns());return capturedContentDiv;}function AddCapturedPieces(parentEl,pcChar,num){var i;for(i=0;i<num;i++){parentEl.appendChild(CreatePcEl(this.GetInstanceId(),g_pcSet,this.m_capturedBarPcSizePx,pcChar,this.m_capturedBarXOffset++,this.m_capturedBarYOffset),false);}}function StartGame(){window.location="/core/newgame.php?setpiecefen="+escape(this.m_fensArray[this.m_moveIndex]);}function CFenReport(fen){this.m_fen=fen;this.m_fenCursor=0;this.m_totalWhite=0;this.m_totalBlack=0;this.m_totalWhitePawns=0;this.m_totalBlackPawns=0;this.m_totalWhiteKnights=0;this.m_totalBlackKnights=0;this.m_totalWhiteBishops=0;this.m_totalBlackBishops=0;this.m_totalWhiteRooks=0;this.m_totalBlackRooks=0;this.m_totalWhiteQueens=0;this.m_totalBlackQueens=0;this.m_totalWhiteKings=0;this.m_totalBlackKings=0;this.m_totalUnknown=0;this.AnalyseFen=AnalyseFen;this.GetFenBoardToken=GetFenBoardToken;this.GetTotalWhitePawns=function(){return this.m_totalWhitePawns;};this.GetTotalWhiteKnights=function(){return this.m_totalWhiteKnights;};this.GetTotalWhiteBishops=function(){return this.m_totalWhiteBishops;};this.GetTotalWhiteRooks=function(){return this.m_totalWhiteRooks;};this.GetTotalWhiteQueens=function(){return this.m_totalWhiteQueens;};this.GetTotalWhiteKings=function(){return this.m_totalWhiteKings;};this.GetTotalWhite=function(){return this.m_totalWhite;};this.GetTotalBlackPawns=function(){return this.m_totalBlackPawns;};this.GetTotalBlackKnights=function(){return this.m_totalBlackKnights;};this.GetTotalBlackBishops=function(){return this.m_totalBlackBishops;};this.GetTotalBlackRooks=function(){return this.m_totalBlackRooks;};this.GetTotalBlackQueens=function(){return this.m_totalBlackQueens;};this.GetTotalBlackKings=function(){return this.m_totalBlackKings;};this.GetTotalBlack=function(){return this.m_totalBlack;};this.GetTotalPieces=function(){return this.GetTotalWhite()+this.GetTotalBlack();};this.GetTotalKings=function(){return this.GetTotalBlackKings()+this.GetTotalWhiteKings();};this.GetScoreWhiteCapturedMaterial=GetScoreWhiteCapturedMaterial;this.GetScoreBlackCapturedMaterial=GetScoreBlackCapturedMaterial;this.GetScoreFromPiecesOnBoard=GetScoreFromPiecesOnBoard;this.GetMaterialDifferenceScore=GetMaterialDifferenceScore;this.AnalyseFen();}function SetFen(fen){this.m_fen=fen;}function AnalyseFen(){while((fenChar=this.GetFenBoardToken())){switch(fenChar){case"P":this.m_totalWhitePawns++;break;case"p":this.m_totalBlackPawns++;break;case"N":this.m_totalWhiteKnights++;break;case"n":this.m_totalBlackKnights++;break;case"B":this.m_totalWhiteBishops++;break;case"b":this.m_totalBlackBishops++;break;case"R":this.m_totalWhiteRooks++;break;case"r":this.m_totalBlackRooks++;break;case"Q":this.m_totalWhiteQueens++;break;case"q":this.m_totalBlackQueens++;break;case"K":this.m_totalWhiteKings++;break;case"k":this.m_totalBlackKings++;break;default:this.m_totalUnknown++;}}this.m_totalWhite=this.m_totalWhitePawns+this.m_totalWhiteKnights+this.m_totalWhiteBishops+this.m_totalWhiteRooks+this.m_totalWhiteQueens+this.m_totalWhiteKings;this.m_totalBlack=this.m_totalBlackPawns+this.m_totalBlackKnights+this.m_totalBlackBishops+this.m_totalBlackRooks+this.m_totalBlackQueens+this.m_totalBlackKings;}function GetScoreWhiteCapturedMaterial(){return this.GetScoreFromPiecesOnBoard(this.m_totalWhitePawns,this.m_totalWhiteKnights,this.m_totalWhiteBishops,this.m_totalWhiteRooks,this.m_totalWhiteQueens);}function GetScoreBlackCapturedMaterial(){return this.GetScoreFromPiecesOnBoard(this.m_totalBlackPawns,this.m_totalBlackKnights,this.m_totalBlackBishops,this.m_totalBlackRooks,this.m_totalBlackQueens);}function GetScoreFromPiecesOnBoard(pawns,knights,bishops,rooks,queens){return(8-pawns)*1+(2-knights)*3+(2-bishops)*3+(2-rooks)*5+(1-queens)*9;}function GetMaterialDifferenceScore(){return this.GetScoreBlackCapturedMaterial()-this.GetScoreWhiteCapturedMaterial();}function IsErrorInFen(){return this.m_totalUnknown>0;}function GetFenBoardToken(){if(this.m_fenCursor>this.m_fen.length||this.m_fen.charAt(this.m_fenCursor)==" "){return false;}return this.m_fen.charAt(this.m_fenCursor++);}function ProcessAnimationQueue(){g_animationManager.AnimateAll();}function CAnimationManager(intervalTime){this.m_intervalTime=intervalTime||25;this.m_animatedElArray=new Array;this.IsAnimatedEl=IsAnimatedEl;this.AddAnimatedEl=AddAnimatedEl;this.GetAnimatedEl=GetAnimatedEl;this.m_timerId=0;this.SetTimerId=function(timerId){this.m_timerId=timerId;};this.GetTimerId=function(){return this.m_timerId;};this.RemoveAnimatedEl=RemoveAnimatedEl;this.AnimateAll=AnimateAll;this.SetTimerId(setInterval("ProcessAnimationQueue()",this.m_intervalTime));}function AddAnimatedEl(elId,el){this.m_animatedElArray[elId]=el;}function IsAnimatedEl(elId){if(this.m_animatedElArray[elId]){return true;}return false;}function RemoveAnimatedEl(elId){if(this.IsAnimatedEl(elId)){delete this.m_animatedElArray[elId];}}function GetAnimatedEl(elId){return this.m_animatedElArray[elId];}function AnimateAll(){for(key in this.m_animatedElArray){if(this.IsAnimatedEl(key)){var el=this.GetAnimatedEl(key);if(el.m_pauseIterationsIndex>el.m_numPauseIterations){var xTransPx=el.m_xSrcPx+el.m_animStepIndex*el.m_xStepPx;var yTransPx=el.m_ySrcPx+el.m_animStepIndex*el.m_yStepPx;SetElPos(el,parseInt(xTransPx),parseInt(yTransPx));if(el.m_animStepIndex>el.m_animStepsMax){el.style.zIndex=el.m_previousZindex;this.RemoveAnimatedEl(key);if(!el.m_owner.IsAnimationFinished()){el.m_owner.NextAnimation();}else{el.m_owner.StopAnimation();}}el.m_animStepIndex++;}else{el.m_pauseIterationsIndex++;}}}}var g_animationManager=new CAnimationManager;