function ProbeQueries(){function t(t,i,r){var u=location.pathname,o=u.indexOf("probe"),e=u.indexOf("/",o),f=e>=0?u.substring(0,e):u;t&&i&&(f+="/"+t+"/"+i);r.fen||(r.fen=n.fenInput.val());!history.state||!history.state.probe||history.state&&history.state.probe&&history.state.probe.fen!=r.fen?n.board.currentFEN(!0)!="8/8/8/8/8/8/8/8 w - -"&&history.state&&history.state.probe&&history.state.currentFen!=n.board.currentFEN(!0)?history.pushState({probe:r,tree:null,currentFen:null},null,f):history.replaceState({probe:r,tree:null,currentFen:null},null,f):r.fen==history.state.probe.fen&&history.state.probe.authorId&&!r.authorId&&history.pushState({probe:r,tree:null,currentFen:null},null,"/probe")}var n=this;n.metric=null;n.fen=null;n.fenInput=null;n.fenFail=null;n.fenCurrent=null;n.fenSubmitBtn=null;n.fenSubmitLoader=null;n.cleanBoardBtn=null;n.switchTurnBtn=null;n.switchEnPassantBtn=null;n.fenEvaluateBtn=null;n.fenEvaluateLoader=null;n.shareAuthorForm=null;n.shareAuthorLink=null;n.board=null;n.treeFail=null;n.treeSubmitBtn=null;n.tableTreeController=null;n.init=function(t){n.board=t.notationController.game.board;n.fenInput=$("#fen-value");n.fenCurrent=$("#current-fen");n.fenFail=$("#fen-fail");n.fenSubmitBtn=$("#fen-submit-btn");n.fenSubmitLoader=$("#fen-submit-loader");n.cleanBoardBtn=$("#clean-board-btn");n.switchTurnBtn=$("#switch-turn-btn");n.switchEnPassantBtn=$("#switch-en-passant-btn");n.fenEvaluateBtn=$("#fen-evaluate-btn");n.fenEvaluateLoader=$("#fen-evaluate-loader");n.shareAuthorForm=$("#share-author-form");n.shareAuthorLink=$("#share-author-link");n.treeFail=$("#tree-fail");n.treeLoader=$("#tree-loader");n.treeSubmitBtn=$("#tree-submit-btn");n.treeFail.hide();n.fenFail.hide();n.metric=0;n.tableTreeController=new TableTree(n.board);n.initRequestControlButtons()};n.OnGetAuthor=function(t){n.shareAuthorLink.attr("href","#");n.shareAuthorLink.text("");n.shareAuthorForm.hide();t.canEdit!="yes"&&t.authorLogin&&(n.shareAuthorLink.attr("href","/user/"+t.authorId),n.shareAuthorLink.text(t.authorLogin),n.shareAuthorForm.show())};n.OnCleanBoard=function(){n.fenFail.hide();PubSub.publish("clean_board")};n.SwitchTurn=function(){PubSub.publish("switch-turn")};n.SwitchEnPassant=function(n){PubSub.publish("switch-en-passant",n)};n.CustomFen=function(t){n.fen=t;n.fenEvaluateLoader.show();n.OnBranchFind()};n.Evaluate=function(){n.fen=n.board.currentFEN(!0);n.fenEvaluateLoader.show();n.OnBranchFind()};n.LoadFen=function(){if(n.fen=n.fenInput.val(),!n.fen){n.fenFail.show();return}n.fenSubmitLoader.show();n.OnBranchFind()};n.OnBranchFind=function(){var t=n.fen;n.fenFail.hide();n.fenInput.attr("disabled",!0);n.fenEvaluateBtn.attr("disabled",!0);n.fenSubmitBtn.attr("disabled",!0);PubSub.publish("hide_board");PubSub.publish("switch-board-and-buttons",!0);n.tableTreeController.CleanTree();n.tableTreeController.InitCurrentTree();n.GetBranch(t,n.metric,1,20)};n.OnTreeFind=function(){var t=n.board.currentFEN(!0);if(!t){n.fenFail.show();return}n.treeFail.hide();n.treeSubmitBtn.attr("disabled",!0);n.treeLoader.show();n.GetTree(t,n.metric,1,20)};n.GetBranch=function(t,i,r,u){i=document.getElementById("metric-dtm").checked?$("#metric-dtm").val():$("#metric-dtz50").val();$.ajax({url:"/probe/branch",type:"POST",data:{Fen:t,CurrentMetric:i},dataType:"json",traditional:!0}).done(function(f){f.data.state=="inQueue"&&r<=u?setTimeout(function(){n.GetBranch(t,i,++r,u)},2e3):n.AfterGetBranch(f.data)})};n.GetTree=function(t,i,r,u){i=document.getElementById("metric-dtm").checked?$("#metric-dtm").val():$("#metric-dtz50").val();$.ajax({url:"/probe/tree",type:"POST",data:{Fen:t,CurrentMetric:i},dataType:"json",traditional:!0}).done(function(f){f.data.state=="inQueue"&&r<=u?setTimeout(function(){n.GetTree(t,i,++r,u)},2e3):n.AfterGetTree(f.data)})};n.AfterGetBranch=function(i){PubSub.publish("update_probe",i);i.state=="complete"?(PubSub.publish("init_probe"),PubSub.publish("update-statistics-mask",{x:!0}),document.cookie="lastFEN="+i.fen+"; path=/"):(PubSub.publish("init_fail_probe"),PubSub.publish("init_branch_error",i),PubSub.publish("update-statistics-mask",{x:!1}));PubSub.publish("switch-board-and-buttons",!1);n.fenInput.attr("disabled",!1);n.fenEvaluateBtn.attr("disabled",!1);n.fenSubmitBtn.attr("disabled",!1);n.fenSubmitLoader.hide();n.fenEvaluateLoader.hide();t(i.authorId,i.positionId,i)};n.AfterGetTree=function(t){n.tableTreeController.AddTree(t);n.tableTreeController.InitCurrentTree();n.treeSubmitBtn.attr("disabled",!1);n.treeLoader.hide();PubSub.publish("init-tree-notation")};n.initRequestControlButtons=function(){n.fenEvaluateBtn.click(function(){n.Evaluate()});n.switchTurnBtn.click(function(){n.SwitchTurn()});n.switchEnPassantBtn.on("change",function(t){var i=t.target.value;n.SwitchEnPassant(i)});n.cleanBoardBtn.click(function(){n.OnCleanBoard()});n.fenSubmitBtn.click(function(){n.LoadFen()});n.fenInput.keydown(function(t){(t.keyCode==10||t.keyCode==13)&&n.LoadFen()});n.treeSubmitBtn.click(function(){n.OnTreeFind()})}}function TableTree(n){var t=this;t.treeWrapper=null;t.board=null;t.positionsArray=[];t.init=function(){t.treeWrapper=$("#table-tree");t.board=n};t.InitCurrentTree=function(){var n=t.board.currentFEN(!0);t.positionsArray[n]?(t.createNewTableTree(t.positionsArray[n]),t.treeWrapper.show()):t.treeWrapper.hide()};t.AddTree=function(n){var i=MovesParser(n.pgn);n.pgn=i;t.positionsArray[n.fen]=n;history.replaceState({probe:history.state.probe,tree:t.positionsArray,currentFen:history.state.currentFen},null,location.pathname)};t.CleanTree=function(){t.positionsArray=[]};t.createNewTableTree=function(n){var r,i;if(n.state==="complete")if(n.pgn.length>0){for(r='<table class="table table-bordered" style="font-size:13px">',r+="<tr style='opacity: 0.5;'><th style='padding-bottom: 10px;'>№<\/th><th>"+Msg.t("Move")+"<\/th><th>"+(n.currentMetric=="dtm"?Msg.t("DTM"):Msg.t("DTZ50"))+"<\/th><th class='right-col'>№<\/th><th>"+Msg.t("Move")+"<\/th><th>"+(n.currentMetric=="dtm"?Msg.t("DTM"):Msg.t("DTZ50"))+"<\/th><\/tr>",i=0;i<5;i++)r+=i==1||i==3?"<tr style='background-color: white'>":"<tr>",r+="<td>"+(i+1)+"<\/td><td class='table-tree-move'>"+(n.pgn[i]?n.pgn[i].move:"")+"<\/td><td>"+(n.pgn[i]?n.pgn[i].dtm:"")+"<\/td><td class='right-col'>"+(i+6)+"<\/td><td class='table-tree-move'>"+(n.pgn[i+5]?n.pgn[i+5].move:"")+"<\/td><td>"+(n.pgn[i+5]?n.pgn[i+5].dtm:"")+"<\/td><\/tr>";r+="<\/table>";t.treeWrapper.html(r)}else t.treeWrapper.html('<div id="tree-fail" class="fail-container" style="width: 100%; margin: 10px 0 0 -10px;">'+Msg.t("No moves")+"<\/div>");else t.treeWrapper.text(Errors(n.state,n.numberError))};t.init()}function MovesParser(n){var i,r,t,u;if(!n||n.indexOf("No moves")>=0)return[];for(i=[],r=n.split(";"),t=0;t<r.length;++t)u=r[t].split(" "),i[t]={move:u[0],dtm:u[1]};return i}function Favorites(){var n=this;n.probe=null;n.can7TbAccess=null;n.addToFavoritesBtn=null;n.addToFavoritesText=null;n.saveToFavoritesBtn=null;n.addToFavoritesForm=null;n.favoritesThemeInput=null;n.favoritesThemeInputFail=null;n.favoritesDescriptionInput=null;n.favoritesStatus=null;n.favoritesStatusForm=null;n.descriptionForm=null;n.descriptionFavoritesForm=null;n.alreadyHavePositionForm=null;n.init=function(t,i){n.probe=t;n.can7TbAccess=i;n.addToFavoritesBtn=$("#add-to-favorites-btn");n.delFavoritesBtn=$("#del-favorites-btn");n.saveToFavoritesBtn=$("#save-to-favorites-btn");n.editFavouriteBtn=$("#edit-favourite-btn");n.addToFavoritesForm=$(".favorite-edit-module");n.favoritesThemeInput=document.getElementById("theme-text-input");n.favoritesThemeInputFail=$("#probe-theme-fail");n.favoritesDescriptionInput=document.getElementById("description-text-input");n.favoritesStatus=$("#share-favorite-position");n.favoritesStatusForm=$("#favorite-share-status");n.cancelBtn=$("#cancel-favorites-btn");n.descriptionFavoritesForm=$("#description-favorites-from");n.alreadyHavePositionForm=$("#already-have-position");n.descriptionForm=$(".theme-description-module");$("#restore-btn").on("click",function(){$("#description-favorites-from").show();$("#restore-favourite").hide();n.probe.canEdit="no";n.addToFavorites()});n.editFavouriteBtn.on("click",function(){n.addToFavoritesForm.show();n.delFavoritesBtn.show();n.addToFavoritesBtn.hide();n.descriptionForm.hide();$("#share-author-form").hide();$(this).hide()});n.delFavoritesBtn.on("click",function(){n.probe.positionId&&$.ajax({url:"/users/positions/delete",type:"POST",dataType:"json",traditional:!0,data:{positionIds:n.probe.positionId}}).done(function(){$("#description-favorites-from > *:not(#restore-favourite)").hide();$("#restore-favourite").show();n.makeProbeInitial(history.state.probe);history.replaceState({probe:history.state.probe,tree:history.state.tree,currentFen:history.state.currentFen},null,location.pathname.substring(0,location.pathname.indexOf("probe")+5))}).fail(function(){})});n.cancelBtn.on("click",function(){n.addToFavoritesForm.hide();n.descriptionForm.show();document.getElementById("description-text").innerHTML.length<=120&&$(".theme-expander").hide();n.reloadButtons(n.probe);n.probe.canEdit=="no"&&n.probe.authorId&&$("#share-author-form").show()});n.addToFavoritesBtn.click(function(){n.addToFavoritesForm.show();n.delFavoritesBtn.show();n.addToFavoritesBtn.hide();n.descriptionForm.hide();n.probe.canEdit=="no"&&n.probe.authorId&&$("#share-author-form").hide()});n.saveToFavoritesBtn.click(function(){n.addToFavorites(n.probe)});n.reloadAllFavoritesForm(n.probe)};n.reloadAllFavoritesForm=function(t){n.probe=t;n.favoritesThemeInputFail.hide();n.favoritesThemeInput.value=t.theme?t.theme:"";n.favoritesDescriptionInput.value=t.description?t.description:"";t.fen&&!n.can7TbAccess&&n.getFenFigureCount(t.fen)==7?(n.favoritesStatusForm.hide(),n.favoritesStatus.prop("checked",!1)):t.favorShareStatus=="shareAndFavor"?(n.favoritesStatusForm.show(),n.favoritesStatus.prop("checked",!0)):(n.favoritesStatusForm.show(),n.favoritesStatus.prop("checked",!1));n.reloadButtons(t);n.addToFavoritesForm.hide()};n.reloadButtons=function(t){$("#restore-favourite").hide();t.canEdit=="yes"?(n.delFavoritesBtn.show(),n.addToFavoritesBtn.hide(),n.editFavouriteBtn.show(),n.alreadyHavePositionForm.hide()):t.canEdit=="no"?(n.delFavoritesBtn.hide(),n.addToFavoritesBtn.show(),n.editFavouriteBtn.hide(),n.alreadyHavePositionForm.hide()):(n.delFavoritesBtn.hide(),n.addToFavoritesBtn.hide(),n.editFavouriteBtn.hide(),n.alreadyHavePositionForm.show())};n.getFenFigureCount=function(n){return n.substring(0,n.indexOf(" ")).toLowerCase().match(/[a-z]/g).length};n.changePath=function(n,t){var i=location.pathname,f=i.indexOf("probe"),r=i.indexOf("/",f),u=r>=0?i.substring(0,r):i;return n&&t&&(u+="/"+n+"/"+t),u};n.makeProbeInitial=function(n){n.authorId=null;n.canEdit="no";n.countDislikes=null;n.countLikes=null;n.description=null;n.lastViewDate="/Date(-62135596800000)/";n.positionId=null;n.saveDate="/Date(-62135596800000)/";n.theme=null;n.views=null};n.addToFavorites=function(){if(n.favoritesThemeInput.value=="")return n.favoritesThemeInputFail.show(),!1;n.favoritesThemeInputFail.hide();var t={Theme:n.favoritesThemeInput.value,Description:n.favoritesDescriptionInput.value,Status:n.favoritesStatus.prop("checked")?3:1,Fen:n.probe.fen,CurrentMetric:n.probe.currentMetric,PositionId:n.probe.positionId};n.probe.canEdit=="yes"?$.ajax({url:"/user/positions/edit",type:"POST",data:t}).done(function(t){$("#add-to-favorites-body").hide();n.probe=t.data;n.probe.theme==""&&(n.probe.theme=n.favoritesThemeInput.value);n.probe.description==""&&(n.probe.description=n.favoritesDescriptionInput.value);PubSub.publish("update-favorites",t.data);t.data.state!="complete"?PubSub.publish("update-favorites-error",t.data):(document.cookie="lastFEN="+t.data.fen+"; path=/",history.replaceState({probe:t.data,tree:history.state.tree,currentFen:history.state.currentFen},null,n.changePath(t.data.authorId,t.data.positionId)),Notifications.success.show(Msg.t("Changes saved")),n.editFavouriteBtn.show())}):n.probe.canEdit=="no"&&$.ajax({url:"/user/positions/add",type:"POST",data:t}).done(function(t){$("#add-to-favorites-body").hide();n.probe=t.data;n.probe.theme==""&&(n.probe.theme=n.favoritesThemeInput.value);n.probe.description==""&&(n.probe.description=n.favoritesDescriptionInput.value);PubSub.publish("update-favorites",t.data);t.data.state!="complete"?PubSub.publish("update-favorites-error",t.data):(document.cookie="lastFEN="+t.data.fen+"; path=/",history.replaceState({probe:t.data,tree:history.state.tree,currentFen:history.state.currentFen},null,n.changePath(t.data.authorId,t.data.positionId)),Notifications.success.show(Msg.t("Position saved. Free slots left:")+t.emptyCell),n.editFavouriteBtn.show())})}}function Likedislike(){var n=this;n.probe=null;n.likesAndViewsContainer=null;n.likeBtn=null;n.likeBtnText=null;n.dislikeBtn=null;n.dislikeBtnText=null;n.viewsFormText=null;n.init=function(){n.likesAndViewsContainer=$(".likes-and-views-container");n.likeBtn=$(".btn-like");n.likeBtnText=$(".btn-like").children("span");n.dislikeBtn=$(".btn-dislike");n.dislikeBtnText=$(".btn-dislike").children("span");n.viewsFormText=$(".views").children("span");n.likeBtn.on("click",function(){n.probe.positionId&&$.ajax({url:"/users/positions/like",type:"POST",dataType:"json",traditional:!0,data:{positionId:n.probe.positionId,isLike:!0}}).done(function(t){t==="success"&&(n.probe.countLikes++,PubSub.publish("update_probe",n.probe),history.replaceState({probe:n.probe,tree:history.state.tree,currentFen:history.state.currentFen},null,location.pathname));n.probe.canAddLike!="manageFavorites"&&(n.probe.canAddLike="no");n.reloadLikedislikeButtons(null)})});n.dislikeBtn.on("click",function(){n.probe.positionId&&$.ajax({url:"/users/positions/like",type:"POST",dataType:"json",traditional:!0,data:{positionId:n.probe.positionId,isLike:!1}}).done(function(t){t==="success"&&(n.probe.countDislikes++,PubSub.publish("update_probe",n.probe),history.replaceState({probe:n.probe,tree:history.state.tree,currentFen:history.state.currentFen},null,location.pathname));n.probe.canAddLike!="manageFavorites"&&(n.probe.canAddLike="no");n.reloadLikedislikeButtons(null)})})};n.reloadLikedislikeButtons=function(t){t!=null&&(n.probe=t);n.probe.positionId==null?n.likesAndViewsContainer.hide():(n.viewsFormText.text(n.probe.views?n.probe.views:0),n.probe.canAddLike=="yes"?(n.likeBtn.attr("disabled",!1),n.dislikeBtn.attr("disabled",!1),n.likeBtnText.text(""),n.dislikeBtnText.text("")):n.probe.canAddLike=="manageFavorites"?(n.likeBtn.attr("disabled",!1),n.dislikeBtn.attr("disabled",!1),n.likeBtnText.text(n.probe.countLikes?n.probe.countLikes:0),n.dislikeBtnText.text(n.probe.countDislikes?n.probe.countDislikes:0)):(n.likeBtn.attr("disabled",!0),n.dislikeBtn.attr("disabled",!0),n.likeBtnText.text(n.probe.countLikes?n.probe.countLikes:0),n.dislikeBtnText.text(n.probe.countDislikes?n.probe.countDislikes:0)),n.likesAndViewsContainer.show())};n.init()}function Statistics(){var n=this;n.probe=null;n.statisticsMask=null;n.statisticsForm=null;n.init=function(){n.statisticsMask=$("#statistics-mask");n.statisticsForm=$("#statistics-form")};n.UpdateStatisticsMask=function(t){for(var i=t.substr(0,t.indexOf(" ")),r,u;i.indexOf("/")!=-1;)i=i.replace("/","");while(i.match(/[0-9]/))i=i.replace(/[0-9]/,"");return r=i.split("").sort(n.SortMaskCase).filter(function(n){return n.match(/[a-z]/)}).join("").toLowerCase().split("").sort(n.SortMaskHierarchy).join(""),u=i.split("").sort(n.SortMaskCase).filter(function(n){return n.match(/[A-Z]/)}).join("").toLowerCase().split("").sort(n.SortMaskHierarchy).join(""),i=u.length>r.length?u.concat(r):r.concat(u),i=="kk"||i.match(/k[a-z][a-z][a-z][a-z][a-z]k/)?!1:(n.statisticsMask.attr("href","/statistic/"+i),!0)};n.SortMaskCase=function(n,t){return n.match(/[a-z]/)&&t.match(/[A-Z]/)||n=="k"&&t.match(/[a-z]/)||n=="K"&&t.match(/[A-Z]/)?-1:n.match(/[A-Z]/)&&t.match(/[a-z]/)||n.match(/[A-Z]/)&&t=="K"||n.match(/[a-z]/)&&t=="k"?1:0};n.SortMaskHierarchy=function(n,t){return n=="q"&&t.match(/[rbnp]/)||n=="r"&&t.match(/[bnp]/)||n=="b"&&t.match(/[np]/)||n=="n"&&t=="p"?-1:t=="q"&&n.match(/[rbnp]/)||t=="r"&&n.match(/[bnp]/)||t=="b"&&n.match(/[np]/)||t=="n"&&n=="p"?1:0};n.init()}