function SGoBoard(){this.SAMECOLOR=0;this.DIFFERENTCOLOR=1;this.MARGIN=2;this.EMPTY=3;this.m_stones=new Array();this.boardSize=19;this.m_lastIndex=0;this.currentProp=Array()}SGoBoard.prototype.init=function(c){var b,a;this.boardSize=c;for(b=0;b<this.boardSize;b++){this.m_stones[b]=new Array(19);for(a=0;a<c;a++){this.m_stones[b][a]=null}}};SGoBoard.prototype.getLastIndex=function(){return this.m_lastIndex};SGoBoard.prototype.clearProp=function(a){this.currentProp.splice(0,this.currentProp.length)};SGoBoard.prototype.addProp=function(a){this.currentProp.push(a)};SGoBoard.prototype.addMove=function(c){var b,a;b=c.m_x;a=c.m_y;if((b>=this.boardSize)||(a>=this.boardSize)){alert("Stone out of bounds("+(b+1)+","+(a+1)+")");return}if(this.m_stones[b][a]==null){this.m_stones[b][a]=c}};SGoBoard.prototype.remMove=function(c){var b,a;b=c.m_x;a=c.m_y;if(this.m_stones[b][a]!=null){this.m_stones[b][a]=null}};SGoBoard.prototype.getGroup=function(f,d){var o,g,q,c,b,a,h,m,n,e;n=new Array();b=0;e=0;q=this.m_stones[f][d];q.m_visited=true;n.push(q);m=false;while(b<n.length){e++;if(e>100){alert("Failed to capture stones");return}for(c=0;c<4;c++){o=n[b].m_x;g=n[b].m_y;if(c==0){o--}if(c==1){g--}if(c==2){o++}if(c==3){g++}a=this.checkNeighbor(n[b],o,g);if(a==this.SAMECOLOR){q=this.m_stones[o][g];if(q.m_visited==false){q.m_visited=true;n.push(q)}}else{if(a==this.EMPTY){m=true}}}b++;if(m){break}}h="";for(f=0;f<n.length;f++){n[f].m_visited=false;if(m==false){this.remMove(n[f])}}if(m){n=null}return n};SGoBoard.prototype.checkNeighbor=function(c,b,a){if((b<0)||(b>=this.boardSize)||(a<0)||(a>=this.boardSize)){return this.MARGIN}if(this.m_stones[b]==undefined){alert(b+""+this.boardSize)}s=this.m_stones[b][a];if(s==null){return this.EMPTY}if(c.m_color==s.m_color){return this.SAMECOLOR}else{return this.DIFFERENTCOLOR}};SGoBoard.prototype.makeMove=function(f){var e,d,b,h,a,g,c;f.m_index=++this.m_lastIndex;this.addMove(f);g=f.m_parent;if(g.m_capture!=null){for(b=0;b<g.m_capture.length;b++){this.remMove(g.m_capture[b])}}else{for(b=0;b<4;b++){e=f.m_x;d=f.m_y;if(b==0){e--}if(b==1){d--}if(b==2){e++}if(b==3){d++}h=this.checkNeighbor(f,e,d);if(h==this.DIFFERENTCOLOR){a=this.getGroup(e,d);if((a!=null)&&(g!=null)){if(g.m_capture==null){g.m_capture=a.slice(0)}else{g.m_capture=g.m_capture.concat(a)}}}a=null}}};SGoBoard.prototype.takeMove=function(a){var b;b=a.m_parent;this.m_lastIndex--;a.m_index=0;if((b!=null)&&(b.m_capture!=null)){for(i=0;i<b.m_capture.length;i++){this.addMove(b.m_capture[i])}}this.remMove(a)};SGoBoard.prototype.clear=function(){var b,a;for(b=0;b<this.boardSize;b++){for(a=0;a<this.boardSize;a++){if(this.m_stones[b][a]!=null){this.remMove(this.m_stones[b][a])}}}this.m_lastIndex=0;this.clearProp()};SGoBoard.prototype.dump=function(){var a='<br><font color="blue">';for(j=0;j<19;j++){for(i=0;i<19;i++){if(this.m_stones[i][j]!=null){a+=" "+this.m_stones[i][j].m_color}else{a+=" ."}}a+="<br>"}return a+"<font>"};function SGoUI(b,a){this.m_sgoCore=null;this.m_size=null;this.m_template=null;this.m_size={width:380,height:380};this.m_index=b;this.m_drawarea=null;this.m_variationDiv=Array();this.m_sgoDiagram=null;this.m_imgPath=a;this.m_debugstr=""}SGoUI.prototype.createDiaFrame=function(e,b,d,f){var g=22,a;if(this.m_template.coordinates=="false"){g=0}a=b+g;this.m_sgoDiagram=e;mainDia=new Element("div");mainDia.inject(e);mainDia.setProperty("id","mainDia-"+f);mainDia.setProperty("class","mainDia");mainDia.setStyles({width:a});gameInfo=new Element("div");gameInfo.inject(mainDia);gameInfo.setProperty("id","gameInfo-"+f);gameInfo.setProperty("class","gameInfo");gameInfo.setStyles({width:a,position:"relative",textAlign:"center"});drawArea=new Element("div");drawArea.inject(mainDia);drawArea.setProperty("id","drawArea-"+f);drawArea.setProperty("class","drawArea");drawArea.setStyles({width:b+g,height:d+g,position:"relative"});var c=new Element("canvas");c.inject(drawArea);c.setProperty("id","canvas-"+f);c.setProperty("class","canvas");c.setStyles({width:b,height:d,position:"relative"});c.setProperty("width",b);c.setProperty("height",d);if(this.m_template.coordinates!="false"){bottomBar=new Element("div");bottomBar.inject(drawArea);bottomBar.setProperty("id","bottomBar-"+f);bottomBar.setProperty("class","bottomBar");bottomBar.setStyles({width:a,height:g,position:"absolute",left:0,top:d});rightBar=new Element("div");rightBar.inject(drawArea);rightBar.setProperty("id","rightBar-"+f);rightBar.setProperty("class","rightBar");rightBar.setStyles({width:g,height:d,position:"absolute",left:b,top:0})}statusBar=new Element("div");statusBar.inject(mainDia);statusBar.setProperty("id","statusBar-"+f);statusBar.setProperty("class","statusBar");statusBar.setStyles({width:a,height:g,textAlign:"center"});navBar=new Element("div");navBar.inject(mainDia);navBar.setProperty("id","navBar-"+f);navBar.setProperty("class","navBar");navBar.setStyles({width:a,height:30,textAlign:"center"})};SGoUI.prototype.init=function(e,a,g){var h,c,b,d,f;this.m_sgoCore=e;this.config(a);if(this.m_template.autocrop=="true"){e.setAutoCropBounds()}else{e.setCropBounds(this.m_template.bounds)}h=e.getCropBounds();this.m_size.width=(h.right-h.left)*this.m_template.stonesize;this.m_size.height=(h.bottom-h.top)*this.m_template.stonesize;this.createDiaFrame(g,this.m_size.width,this.m_size.height,this.m_index);c="canvas-"+this.m_index;this.m_drawarea=new SGoGoban($(c));this.m_drawarea.init(h,this.m_template.stonesize,e.m_boardSize);this.m_drawarea.setBoard(this.m_sgoCore.m_board);this.m_drawarea.repaint();c="navBar-"+this.m_index;this.addNavigation($(c));c="rightBar-"+this.m_index;d=$(c);c="bottomBar-"+this.m_index;f=$(c);if((d!==null)&&(f!=null)){this.addCoordinates(d,f)}c="statusBar-"+this.m_index;this.addMoveNumber($(c));c="gameInfo-"+this.m_index;gi=$(c);this.setGameInfo(gi);this.addVariations();c="mainDia-"+this.m_index;ma=$(c);this.addCommentArea(ma);e.addProperties(e.m_gametree.getCurrent());this.m_drawarea.repaint();this.addComment(e.getCurrentComment(),false)};SGoUI.prototype.handleEvent=function(e,b){var d,h,g,f,a;a=this.m_sgoCore;d=0;h=true;if(e=="variation"){switch(b){case 0:a.moveNext();a.moveNext();break;default:a.moveChild(b-1);a.moveNext();break}}else{switch(b){case"goNext":a.moveNext();break;case"goPrev":a.movePrev();break;case"goPrev10":case"goNext10":for(i=0;i<10;i++){if(b=="goNext10"){h=a.moveNext()}else{h=a.movePrev()}if(h==false){break}}break;case"goEnd":case"goStart":while(h){if(b=="goEnd"){h=a.moveNext()}else{h=a.movePrev()}d++;if(d>361){alert("Failed to go next");break}}break;case"restart":a.restart();break}}this.m_drawarea.setLastMove(a.m_gametree.getCurrent());a.addProperties(a.m_gametree.getCurrent());lastind=a.m_board.getLastIndex();this.setMoveNumber(lastind);this.addVariations();this.addComment(a.getCurrentComment(),false);this.m_drawarea.repaint()};SGoUI.prototype.addComment=function(f,a){var e,d,b;d="comment-"+this.m_index;e=$(d);if(e!=null){if(e.get("tag")=="div"){b=f.replace(/\n/g,"<br/>");if(a){e.innerHTML+=b}else{e.innerHTML=b}}else{if(e.get("tag")=="textarea"){}}if(a){e.value+=f+"\n"}else{e.value=f}}};SGoUI.prototype.config=function(a){var b;b=new SGoTemplate(null);if(a!=null){if(a.filename!=undefined){b.filename=a.filename}if(a.stonesize!=undefined){ss=parseInt(a.stonesize);if(!isNaN(ss)){b.stonesize=ss}}if(a.bounds!=undefined){b.bounds.left=parseInt(a.bounds.l);b.bounds.top=parseInt(a.bounds.t);b.bounds.right=parseInt(a.bounds.r);b.bounds.bottom=parseInt(a.bounds.b)}if((a.filetype=="problem")||(a.filetype=="game")){b.filetype=a.filetype}if((a.commentpos=="right")||(a.commentpos=="none")||(a.commentpos=="bottom")){b.commentpos=a.commentpos}if((a.coordinates=="false")||(a.coordinates=="true")){b.coordinates=a.coordinates}if((a.autocrop=="false")||(a.autocrop=="true")){b.autocrop=a.autocrop}}this.m_template=b};SGoUI.prototype.addCoordinates=function(c,m){var k,f,e,n,b,d,a,h,g,o;g=this.m_sgoCore;o=this.m_template.stonesize;h=65;cropb=g.getCropBounds();a=cropb.right-cropb.left;d=cropb.bottom-cropb.top;for(k=0;k<a;k++){b=new Element("div");e=k*o;b.setStyles({top:0,left:e,width:o,height:15,position:"absolute",textAlign:"center"});if(k==8){h++}b.innerHTML=String.fromCharCode(h+k);b.injectInside(m)}for(k=0;k<d;k++){b=new Element("div");e=k*o;b.setStyles({top:0,left:e,width:15,height:o,textAlign:"center"});b.innerHTML=""+(d-k);b.injectInside(c)}};SGoUI.prototype.addVariations=function(e){var f,a,c,d,b;f=this.m_sgoCore;nd=f.m_gametree.getCurrent();if(nd==null){this.m_debugstr+="Invalid current node\n";return}a=nd.getNext();c=nd.getNumberOfChilds();c+=1;ssize=this.m_template.stonesize;if(c>this.m_variationDiv.length){for(d=0;d<c;d++){dv=new Element("div");dv.inject($("drawArea-"+this.m_index));dv.setAttribute("id","variation-"+this.m_index+"-"+d);dv.addEvent("click",canvasEvent);this.m_variationDiv.push(dv);dv.setStyles({display:"none",position:"absolute",width:ssize,height:ssize})}}for(d=0;d<this.m_variationDiv.length;d++){b=this.m_variationDiv[d];if(b.getStyle("display")!="none"){b.setStyle("display","none")}}for(d=0;d<c;d++){if(d==0){cld=a}else{cld=nd.getChild(d-1)}if((cld!=null)&&cld.m_stone!=null){stone=cld.m_stone;p=this.m_drawarea.getPoint(stone.m_x,stone.m_y);t=p.y-ssize/2;l=p.x-ssize/2;dv=this.m_variationDiv[d];dv.setStyles({top:t,left:l,width:ssize,height:ssize,display:"inline"})}}};SGoUI.prototype.addNavigation=function(e){var c,b,d,f,h,a,g;g=this.m_sgoCore;d=["goStart","goPrev10","goPrev","goNext","goNext10","goEnd","info"];f=["restart","goPrev","goNext","goEnd","info"];if(this.m_template.filetype=="problem"){b=f}else{b=d}for(c=0;c<b.length;c++){a=new Element("img");a.setProperty("src",this.m_imgPath+b[c]+".png");a.setProperty("alt",b[c]);a.setStyle("margin","1px 5px");if(b[c]!="info"){a.addEvent("click",handleEvent)}else{a.addEvent("click",infoEvent)}a.setProperty("id",b[c]+"-"+this.m_index);a.injectInside(e)}};SGoUI.prototype.addMoveNumber=function(a){var c,b;c=new Element("div");c.setProperty("id","moveNumber-"+this.m_index);c.setStyle("display","inline");c.innerHTML="0";c.inject(a)};SGoUI.prototype.setMoveNumber=function(b){var a=$("moveNumber-"+this.m_index);if(a!=null){a.innerHTML=""+b}};SGoUI.prototype.setGameInfo=function(d){var f,g,e,a,c,b;f=this.m_sgoCore;g="";e=f.m_gametree.m_root;if(e==null){return}if(this.m_template.filetype=="problem"){c=e.getProp(SGFProp.ID_PL);if(c==""){e=e.m_next;if((e!=null)&&(e.m_stone!=null)){if(e.m_stone.m_color==SGFProp.ID_W){c="W"}else{c="B"}}}if(c.charAt(0)=="W"){g='<span style="color:#ffffff">White to play<span>'}else{g='<span style="color:#000000">Black to play<span>'}}else{g+='<span style="color:#ffffff">'+e.getProp(SGFProp.ID_PW)+" "+e.getProp(SGFProp.ID_WR)+"</span>";g+=" vs. ";g+='<span style="color:#000000">'+e.getProp(SGFProp.ID_PB)+" "+e.getProp(SGFProp.ID_BR)+"</span>";g+="<br>";g+=e.getProp(SGFProp.ID_DT)+", ";g+=e.getProp(SGFProp.ID_EV)+", ";g+=e.getProp(SGFProp.ID_RE)}d.innerHTML=g};SGoUI.prototype.addCommentArea=function(b){var a,c;cpos=this.m_template.commentpos;if(cpos!="none"){if(this.m_template.commentpos=="right"){c=new Element("div");c.inject(this.m_sgoDiagram)}else{c=new Element("div");c.setProperty("rows","6");c.setStyles({width:this.m_size.width+22,overflow:"scroll",height:"120px"});c.inject(b)}c.setProperty("id","comment-"+this.m_index)}};function SGoCore(){this.m_gametree=null;this.m_sgfparser=null;this.m_board=null;this.m_boardSize=19;this.m_bounds={left:0,top:0,right:19,bottom:19}}SGoCore.prototype.setCropBounds=function(a){if(a==undefined){return}if(!isNaN(a.left)){this.m_bounds.left=a.left}if(!isNaN(a.top)){this.m_bounds.top=a.top}if(!isNaN(a.right)){this.m_bounds.right=a.right}if(!isNaN(a.bottom)){this.m_bounds.bottom=a.bottom}};SGoCore.prototype.getCropBounds=function(){return this.m_bounds};SGoCore.prototype.setAutoCropBounds=function(){var a;a=this.m_gametree.getCropBounds();this.m_bounds.left=a.left;this.m_bounds.top=a.top;this.m_bounds.right=a.right;this.m_bounds.bottom=a.bottom};SGoCore.prototype.initEngineSGF=function(a){this.m_sgfparser=new SGoParser();this.m_gametree=this.m_sgfparser.decodeSGF(a);this.m_gametree.traverse(this.m_gametree.m_root,debug);this.m_gametree.goRoot();if(this.m_gametree.m_root!=null){sz=this.m_gametree.m_root.getProp(SGFProp.ID_SZ);if(sz!=null){this.m_boardSize=parseInt(sz)}if(isNaN(this.m_boardSize)){this.m_boardSize=19}}this.m_board=new SGoBoard();this.m_board.init(this.m_boardSize)};SGoCore.prototype.moveNext=function(){var b,c,a=false;b=this.m_gametree;if(b!=null){a=b.moveNext();if(a==true){c=b.getCurrent();if((c!=null)&&(c.m_stone!=null)){this.m_board.makeMove(c.m_stone)}}}return a};SGoCore.prototype.movePrev=function(){var b,c,a=false;b=this.m_gametree;if(b!=null){c=b.getCurrent();if((c!=null)&&(c.m_stone!=null)){this.m_board.takeMove(c.m_stone)}a=b.movePrev()}return a};SGoCore.prototype.moveChild=function(b){var a,c,d=false;a=this.m_gametree;if(a!=null){d=a.moveChild(b);if(d!=false){c=a.getCurrent();this.m_board.makeMove(c.m_stone)}}return d};SGoCore.prototype.restart=function(){var a,b,c=false;a=this.m_gametree;if(a!=null){b=a.goRoot();this.m_board.clear();this.addProperties(a.getCurrent());c=true}return c};SGoCore.prototype.addProperties=function(a){var b;this.m_board.clearProp();if((a!=null)&&(a.m_prop!=null)){for(i=0;i<a.m_prop.length;i++){b=a.m_prop[i];if((b.m_id==SGFProp.ID_AW)||(b.m_id==SGFProp.ID_AB)){this.m_board.addMove(b.m_stone)}else{if((b.m_id==SGFProp.ID_LB)||(b.m_id==SGFProp.ID_TR)||(b.m_id==SGFProp.ID_SQ)){this.m_board.addProp(b)}}}}};SGoCore.prototype.getCurrentComment=function(){var a;a=this.m_gametree.getCurrent();if(a!=null){return a.m_comment}return""};function Stone(){this.m_color;this.m_x;this.m_y;this.m_visited=false;this.m_parent=null;this.m_index=0}function Prop(){this.m_id="";this.m_lbl="";this.m_x=0;this.m_y=0;this.m_stone=null;this.m_value=""}function Node(){this.m_str="";this.m_stone=null;this.m_next=null;this.m_prev=null;this.m_childs=null;this.m_parent=null;this.m_comment="";this.m_prop=[];this.decoded=false;this.m_capture=null;this.m_crop=null}function SGoAutoCrop(){this.left=20;this.top=20;this.right=0;this.bottom=0;this.setCrop=function(a,b){if(this.left>a){this.left=a}if(this.top>b){this.top=b}if(this.right<a){this.right=a}if(this.bottom<b){this.bottom=b}}}function xy_toa(b,a){return String.fromCharCode(b+97)+String.fromCharCode(a+97)}Node.prototype.addChild=function(a){if(this.m_childs==null){this.m_childs=new Array()}a.m_parent=this;this.m_childs.push(a)};Node.prototype.encodeSGFList=function(d,a){var b,c="";c+=d;for(b=0;b<a.length;b++){p=a[b];if(d==SGFProp.ID_LB){c+="["+xy_toa(p.m_x,p.m_y)+":"+p.m_lbl+"]"}else{c+="["+xy_toa(p.m_x,p.m_y)+"]"}}return c};Node.prototype.encodeSGFNode=function(){var e,d,c,b,g,a=[],f="<br>;";c=this.m_stone;if(c!=null){b=this.m_stone.m_x;g=this.m_stone.m_y;f+=c.m_color+"["+xy_toa(b,g)+"]"}if(this.m_prop!=null){for(e=0;e<SGFProp.lp.length;e++){for(d=0;d<this.m_prop.length;d++){if(SGFProp.lp[e]==this.m_prop[d].m_id){a.push(this.m_prop[d])}}if(a.length>0){f+=this.encodeSGFList(SGFProp.lp[e],a)}a=[]}}if(this.m_comment!=""){f+="C["+this.m_comment+"]"}debug('<font color="green">'+f+"</font>")};Node.prototype.decodeSGFNode=function(){var a,d,c,b;if(this.m_str!=""){a=this.m_str.match(/[A-Z]+(\[(.|\s)*?\])+/g);if(a!=null){for(c=0;c<a.length;c++){d=a[c].split("[");this.addProp(d)}}}};Node.prototype.addProp=function(g){var f,b,d,c,a,e;f=g[0];a=e=0;switch(f){case SGFProp.ID_SZ:case SGFProp.ID_PW:case SGFProp.ID_PB:case SGFProp.ID_WR:case SGFProp.ID_BR:case SGFProp.ID_EV:case SGFProp.ID_DT:case SGFProp.ID_KM:case SGFProp.ID_RE:case SGFProp.ID_PL:p=new Prop();p.m_id=f;p.m_value=g[1].substring(0,g[1].length-1);this.m_prop.push(p);break;case SGFProp.ID_B:case SGFProp.ID_W:c=new Stone();c.m_color=f;c.m_x=g[1].charCodeAt(0)-97;c.m_y=g[1].charCodeAt(1)-97;this.gametree.m_crop.setCrop(c.m_x,c.m_y);c.m_parent=this;this.m_stone=c;break;case SGFProp.ID_AW:case SGFProp.ID_AB:case SGFProp.ID_LB:case SGFProp.ID_TR:case SGFProp.ID_SQ:for(d=1;d<g.length;d++){p=new Prop();a=g[d].charCodeAt(0)-97;e=g[d].charCodeAt(1)-97;if((f==SGFProp.ID_AW)||(f==SGFProp.ID_AB)){c=new Stone();if(f==SGFProp.ID_AW){c.m_color="W"}else{c.m_color="B"}c.m_x=a;c.m_y=e;this.gametree.m_crop.setCrop(c.m_x,c.m_y);p.m_stone=c;p.m_id=f}else{if((f==SGFProp.ID_LB)||(f==SGFProp.ID_TR)||(f==SGFProp.ID_SQ)){p.m_id=f;if(f==SGFProp.ID_LB){p.m_lbl=(g[d].toUpperCase()).charAt(3)}p.m_x=a;p.m_y=e}}if(this.m_prop==null){this.m_prop=new Array()}this.m_prop.push(p)}break;case SGFProp.ID_C:this.m_comment=g[1].substring(0,g[1].length-1);break}};Node.prototype.getProp=function(a){for(i=0;i<this.m_prop.length;i++){if(this.m_prop[i].m_id==a){return this.m_prop[i].m_value}}return""};Node.prototype.getNumberOfChilds=function(a){if(this.m_childs!=null){return this.m_childs.length}return 0};Node.prototype.getChild=function(a){if(this.m_childs!=null){if(a<this.m_childs.length){return this.m_childs[a]}}return null};Node.prototype.getNext=function(){return this.m_next};function SGoGameTree(){this.m_root=null;this.m_current=null;this.lstack=new Array();this.MAX_NODES=1000;this.m_crop=new SGoAutoCrop()}SGoGameTree.prototype.getCropBounds=function(){var c=3,b,a;this.m_crop.left-=c;this.m_crop.top-=c;this.m_crop.right+=c;this.m_crop.bottom+=c;b=this.m_crop.right-this.m_crop.left;a=this.m_crop.bottom-this.m_crop.top;if((this.m_crop.left<0)||(b<2)){this.m_crop.left=0}if((this.m_crop.top<0)||(a<2)){this.m_crop.top=0}if((this.m_crop.right>19)||(b<2)){this.m_crop.right=19}if((this.m_crop.bottom>19)||(a<2)){this.m_crop.bottom=19}return this.m_crop};SGoGameTree.prototype.addNode=function(a){a.gametree=this;if(this.m_root==null){this.m_root=a;this.m_current=this.m_root}else{if(this.m_current==null){alert("Invalid structure\n");return}if(this.m_current.m_next==null){this.m_current.m_next=a;a.m_prev=this.m_current;this.m_current=a}else{this.m_current.addChild(a);this.m_current=a}}};SGoGameTree.prototype.getCurrent=function(){return this.m_current};SGoGameTree.prototype.goRoot=function(){this.m_current=this.m_root};SGoGameTree.prototype.moveNext=function(){if((this.m_current!=null)&&(this.m_current.m_next!=null)){this.m_current=this.m_current.m_next;return true}return false};SGoGameTree.prototype.movePrev=function(){if(this.m_current!=null&&this.m_current.m_prev!=null){this.m_current=this.m_current.m_prev;return true}return false};SGoGameTree.prototype.moveParent=function(){if(this.m_current!=null&&this.m_current.m_parent!=null){this.m_current=this.m_current.m_parent;return true}return false};SGoGameTree.prototype.moveChild=function(a){if((this.m_current!=null)&&(this.m_current.m_childs!=null)){if(a<this.m_current.m_childs.length){this.m_current=this.m_current.m_childs[a];return true}}return false};SGoGameTree.prototype.findNextMove=function(a,c){var b;b=this.m_current.m_next;if((b!=null)&&(b.m_stone!=null)){if((b.m_stone.m_x==a)&&(b.m_stone.m_y==c)){return true}}return false};SGoGameTree.prototype.findChildMove=function(a,d){var c,b;b=this.m_current;if((b!=null)&&(b.m_childs!=null)){for(i=0;i<b.m_childs.length;i++){c=b.m_childs[i];if((c!=null)&&(c.m_stone!=null)){if((c.m_stone.m_x==a)&&(c.m_stone.m_y==d)){return i}}}}return -1};SGoGameTree.prototype.getNextMove=function(){cnode=this.m_current;if(this.m_current!=null){return this.m_current.m_next}return null};SGoGameTree.prototype.pushCurrent=function(){this.lstack.push(this.m_current)};SGoGameTree.prototype.popCurrent=function(){this.m_current=this.lstack.pop()};SGoGameTree.prototype.traverse=function(c,b){var f,a,e,d;e=0;a=new Array();a.push(c);f=c;if(f==null){b("SGoGameTree.traverse: current is null");return}while(a.length>0){f=a.pop();f.decodeSGFNode();if(f.m_childs!=null){for(d=f.m_childs.length-1;d>=0;d--){a.push(f.m_childs[d])}}if(f.m_next!=null){a.push(f.m_next)}e++;if(e>this.MAX_NODES){alert("Error in traversal");break}}};SGoGameTree.prototype.traverse_3=function(c,b){var g,a,e,f,d,h;f=0;a=new Array();e=new Array();if(c==null){if(b!=null){b("SGoGameTree.traverse: current is null")}return}a.push(c);h="";while(a.length>0){g=a.pop();if(g.m_parent!=null){h=e.pop();h=""}h+=";"+g.m_str;if(g.m_childs!=null){for(d=g.m_childs.length-1;d>=0;d--){a.push(g.m_childs[d]);e.push(h.slice(0))}}if(g.m_next!=null){a.push(g.m_next)}else{if(b!=null){b("<br/>"+h)}}f++;if(f>this.MAX_NODES){alert("Error in traversal");break}}for(d=0;d<e.length;d++){b("<br/>;"+e[d])}};SGoGameTree.prototype.traverse_1=function(b,a,d){var c;if(d<this.MAX_NODES&&b!=null){a(b.m_str);if(b.m_childs!=null){a("(")}this.traverse_1(b.m_next,a,d++);if(b.m_childs!=null){a(")")}if(b.m_childs!=null){for(c=0;c<b.m_childs.length;c++){a("(");this.traverse_1(b.m_childs[c],a,d++);a(")")}}}};var handi_19x19=[3,3,3,9,3,15,9,3,9,9,9,15,15,3,15,9,15,15];var handi_13x13=[3,3,3,6,3,9,6,3,6,6,6,9,9,3,9,6,9,9];var handi_9x9=[2,2,2,4,2,6,4,2,4,4,4,6,6,2,6,4,6,6];function SGoGoban(a){this.pointx=new Array();this.pointy=new Array();this.handiCrop=null;this.handiArr;this.m_size=19;this.m_canvas=a;this.m_board=null;this.m_ctx=null;this.m_bounds;this.m_stonesize;if(this.m_canvas!=null){if(typeof G_vmlCanvasManager!="undefined"){this.m_canvas=G_vmlCanvasManager.initElement(this.m_canvas)}if(this.m_canvas.getContext){this.m_ctx=this.m_canvas.getContext("2d")}else{alert("Your browser has no support for canvas")}}this.m_lastMove=null}SGoGoban.prototype.init=function(d,a,e){var c,b;this.m_bounds=d;this.m_dim=null;this.m_stonesize=a;for(c=0;c<(this.m_bounds.right-this.m_bounds.left);c++){this.pointx[c]=Math.round(this.m_stonesize/2+c*this.m_stonesize)}for(c=0;c<(this.m_bounds.bottom-this.m_bounds.top);c++){this.pointy[c]=Math.round(this.m_stonesize/2+c*this.m_stonesize)}this.setHandi(e)};SGoGoban.prototype.setHandi=function(a){this.m_size=a;if(this.m_size==9){this.handiArr=handi_9x9}else{if(this.m_size==13){this.handiArr=handi_13x13}else{this.handiArr=handi_19x19}}};SGoGoban.prototype.setBoard=function(a){this.m_board=a};SGoGoban.prototype.drawStones=function(a){var c,b,d;if(this.m_board==null){return}this.lastMoveMarkup();for(c=0;c<this.m_size;c++){for(b=0;b<this.m_size;b++){st=this.m_board.m_stones[c][b];if(st!=null){this.drawStone(a,st.m_color,st.m_x,st.m_y)}}}};SGoGoban.prototype.setLastMove=function(a){this.m_lastMove=a};SGoGoban.prototype.lastMoveMarkup=function(){var b,a,c;if((this.m_lastMove==null)||((b=this.m_lastMove.m_stone)==null)){return}a=b.m_x-this.m_bounds.left;c=b.m_y-this.m_bounds.top;this.m_ctx.fillStyle="rgba(255,0,0,0.7)";this.m_ctx.beginPath();this.m_ctx.moveTo(this.pointx[a],this.pointy[c]);this.m_ctx.arc(this.pointx[a],this.pointy[c],this.m_stonesize/2+1,0,Math.PI*2,true);this.m_ctx.fill()};SGoGoban.prototype.checkBounds=function(a,b){if((a>=0)&&(a<this.m_bounds.right)&&(b>=0)&&(b<this.m_bounds.bottom)){return true}else{return false}};SGoGoban.prototype.getPoint=function(a,e){var d,b,c;b=a-this.m_bounds.left;c=e-this.m_bounds.top;if(!this.checkBounds(b,c)){b=0;c=0}d={x:this.pointx[b],y:this.pointy[c]};return d};SGoGoban.prototype.drawStone=function(b,h,a,g){var c,d,e;if(h==SGFProp.ID_B){b.fillStyle="rgb(0,0,0)";if(this.m_blackImg){c=this.m_blackImg}}else{b.fillStyle="rgb(255,255,255)";if(this.m_whiteImg){c=this.m_whiteImg}}a-=this.m_bounds.left;g-=this.m_bounds.top;if(this.checkBounds(a,g)){if(false){b.beginPath();b.moveTo(this.pointx[a],this.pointy[g]);b.arc(this.pointx[a],this.pointy[g],this.m_stonesize/2,0,Math.PI*2,true);b.fill()}d=this.pointx[a]-this.m_stonesize/2;e=this.pointy[g]-this.m_stonesize/2;try{if(this.m_whiteImg&&this.m_blackImg){b.drawImage(c,d,e)}else{b.beginPath();b.moveTo(this.pointx[a],this.pointy[g]);b.arc(this.pointx[a],this.pointy[g],this.m_stonesize/2,0,Math.PI*2,true);b.fill()}}catch(f){debug("Error in drawing stones")}}};SGoGoban.prototype.drawHandi=function(b){var d,c,a,e;b.fillStyle="rgb(0,0,0)";b.beginPath();if(this.handiCrop==null){c=0;this.handiCrop=new Array();for(d=0;d<this.handiArr.length;d+=2){a=this.handiArr[d]-this.m_bounds.left;e=this.handiArr[d+1]-this.m_bounds.top;if(this.checkBounds(a,e)){this.handiCrop[c]=a;this.handiCrop[c+1]=e;c+=2}}}for(d=0;d<this.handiCrop.length;d+=2){a=this.handiCrop[d];e=this.handiCrop[d+1];b.moveTo(this.pointx[a],this.pointy[e]);b.arc(this.pointx[a],this.pointy[e],2.5,0,Math.PI*2,true)}b.fill()};SGoGoban.prototype.drawGrid=function(b){var d,c,a,e;a=this.m_bounds.right-this.m_bounds.left;e=this.m_bounds.bottom-this.m_bounds.top;b.fillStyle="rgb(197,179,88)";b.fillRect(0,0,a*this.m_stonesize,e*this.m_stonesize);a=a-1;e=e-1;b.strokeStyle="rgb(0,0,0)";b.beginPath();for(d=1;d<e;d++){b.moveTo(this.pointx[0],this.pointy[d]);b.lineTo(this.pointx[a],this.pointy[d])}for(d=1;d<a;d++){b.moveTo(this.pointx[d],this.pointy[0]);b.lineTo(this.pointx[d],this.pointy[e])}if(this.m_bounds.left==0){b.moveTo(this.pointx[0],this.pointy[0]);b.lineTo(this.pointx[0],this.pointy[e])}if(this.m_bounds.top==0){b.moveTo(this.pointx[0],this.pointy[0]);b.lineTo(this.pointx[a],this.pointy[0])}if(this.m_bounds.right==19){b.moveTo(this.pointx[a],this.pointy[0]);b.lineTo(this.pointx[a],this.pointy[e])}if(this.m_bounds.bottom==19){b.moveTo(this.pointx[0],this.pointy[e]);b.lineTo(this.pointx[a],this.pointy[e])}b.stroke()};SGoGoban.prototype.drawProperties=function(a){var b,c;b=this.m_board.currentProp;for(i=0;i<b.length;i++){c=b[i];this.drawProp(c)}};SGoGoban.prototype.drawProp=function(e){var b,a,f,c,d;a=e.m_x-this.m_bounds.left;f=e.m_y-this.m_bounds.top;c=this.m_stonesize/2;if(!this.checkBounds(a,f)){return}if((e!=null)){this.m_ctx.font="bold 12px sans-serif";b=this.m_board.m_stones[e.m_x][e.m_y];this.m_ctx.fillStyle="rgb(255,0,0)";if(b==null){this.m_ctx.fillStyle="rgb(197,179,88)";this.m_ctx.fillRect(this.pointx[a]-c,this.pointy[f]-c,2*c,2*c)}d=4;if(e.m_id==SGFProp.ID_LB){this.drawText(e.m_lbl,a,f)}else{if(e.m_id==SGFProp.ID_TR){this.m_ctx.fillStyle="rgb(255,0,0)";this.m_ctx.beginPath();this.m_ctx.moveTo(this.pointx[a],this.pointy[f]-d);this.m_ctx.lineTo(this.pointx[a]-d,this.pointy[f]+d);this.m_ctx.lineTo(this.pointx[a]+d,this.pointy[f]+d);this.m_ctx.closePath();this.m_ctx.fill()}else{if(e.m_id==SGFProp.ID_SQ){this.m_ctx.fillStyle="rgb(255,0,0)";this.m_ctx.fillRect(this.pointx[a]-d,this.pointy[f]-d,2*d,2*d)}}}}};SGoGoban.prototype.drawText=function(b,a,c){if(this.m_ctx.fillText){this.m_ctx.fillText(b,this.pointx[a]-5,this.pointy[c]+6)}else{if(this.m_ctx.mozDrawText){this.m_ctx.fillStyle="rgb(128,128,128)";this.m_ctx.translate(this.pointx[a]-5,this.pointy[c]+6);this.m_ctx.mozDrawText(b);this.m_ctx.translate(-this.pointx[a]+5,-this.pointy[c]-6)}}};SGoGoban.prototype.repaint=function(){this.drawGrid(this.m_ctx);this.drawHandi(this.m_ctx);this.drawStones(this.m_ctx);this.drawProperties(this.m_ctx)};var SGoObjArray=[];SGoTemplate_default={tname:"default",stonesize:20,filename:null,filetype:"game",commentpos:"bottom",guessnext:"false",navigation:"true",autocrop:"false",gameinfo:"true",coordinates:"true"};function SGoTemplate(a){if(a==null){a=SGoTemplate_default}else{a=a}this.tname=a.tname;this.stonesize=a.stonesize;this.filename=a.filename;this.filetype=a.filetype;this.commentpos=a.commentpos;this.guessnext=a.guessnext;this.navigation=a.navigation;this.autocrop=a.autocrop;this.gameinfo=a.gameinfo;this.coordinates=a.coordinates;this.bounds={left:0,top:0,right:19,bottom:19}}function sgoDebug(c,b){var a;a=$("sgodebugarea");if(a!=null){if(c==0){str_lvl='<span style="color:red">'}else{if(c==1){str_lvl='<span style="color:green">'}else{str_lvl='<span style="color:blue">'}}a.innerHTML+="*"+str_lvl+b+"</span><br/>"}}function SGoObj(a){this.m_ui=null;this.m_core=null;this.m_index=SGoObjArray.length;SGoObjArray.push(this);this.m_imagePath=a}SGoObj.prototype.init=function(a){sgoDebug(2,"Loading "+a.cfg.filename);this.m_parentDiv=a.parentDiv;if(a.cfg.filename!=undefined){this.getFile(a,this)}else{sgoCore=new SGoCore();this.m_core=sgoCore;sgoCore.initEngineSGF(a.rawData);sgoUI=new SGoUI(this.m_index,this.m_imagePath);this.m_ui=sgoUI;sgoUI.init(sgoCore,a.cfg,a.parentDiv)}};SGoObj.prototype.getFile=function(b,a){var c;imagePath=this.m_imagePath;ind=this.m_index;c=new Request({method:"get",url:b.cfg.filename,onComplete:function(d){sgoCore=new SGoCore();a.m_core=sgoCore;sgoCore.initEngineSGF(d);sgoUI=new SGoUI(a.m_index,a.m_imagePath);a.m_ui=sgoUI;sgoUI.init(sgoCore,b.cfg,b.parentDiv)},onFailure:function(d){sgoDebug(1,"Failed to get file")}}).send()};function sgoRender(b,c){var d,a;a=getConfig(c);d=new SGoObj();d.init({parentDiv:b,cfg:a,rawData:c})}function sgoRenderDOMReady(a){var f,d,g,b,e,c;b=$$("div.SGoDia");if((b!=null)&&(b.length>0)){for(f=0;f<b.length;f++){g=b[f].getElements("pre");if((g!=null)&&(g.length>0)){for(d=0;d<g.length;d++){e=b[f];c=getConfig(g[d].innerHTML);rd=g[d].innerHTML;if(a==undefined){obj=new SGoObj("/gvfiles/com_gvtools/gvlibs/sgo/img/")}else{obj=new SGoObj(imagepath)}obj.init({parentDiv:e,cfg:c,rawData:rd})}}}}}function getConfig(c){var b,a;a=null;b=c.match(/\{.*\}/);if(b!=null){sgoDebug(2,"Config: "+b[0]);a=JSON.decode(b[0])}return a}function create_sgo_diagram(f,b,e){var d,a,c;c=SGoObjArray.length;d=new SGoCore();d.initEngineSGF(f);a=new SGoUI(c);a.init(d,b,e);SGoObjArray.push(a)}function handleEvent(d){var b,e,b,c,a;e=this.id;b=e.split("-");c=b[0];a=parseInt(b[1]);obj=SGoObjArray[a];if(obj==undefined){alert("handleEvent:Undefined SGoObject");return}obj.m_ui.handleEvent(null,c)}function canvasEvent(d){var f,b,e,c,a;f=this.id;b=f.split("-");e=b[0];a=parseInt(b[1]);c=parseInt(b[2]);obj=SGoObjArray[a];if(obj==undefined){sgoDebug(0,"canvasEvent:Undefined SGoUI object");return}obj.m_ui.handleEvent(e,c)}function infoEvent(d){var b,c,a;c=this.id.split("-");a="SGoInfo-"+c[1];b=$(a);if(b!=null){disp=b.getStyle("display");if(disp!="none"){b.setStyle("display","none")}else{b.setStyle("display","block")}}}function SGoParser(){this.sgfStart=false;this.dbg_cb=null}SGoParser.prototype.debug=function(a){dbgdv=$("debugp");if(dbgdv!=null){dbgdv.innerHTML+=a+"<br/>"}};SGoParser.prototype.decodeSGF=function(d){var f,h,e,a,b,c,g;h=f=0;e="";a=0;g=new SGoGameTree();for(index=0;index<d.length;index++){ch=d.charAt(index);if((ch!="(")&&(this.sgfStart==false)){continue}switch(ch){case";":if(h==0){c=new Node();c.m_str=c.m_str;g.addNode(c)}a++;break;case"(":case")":if(h!=0){c.m_str+=ch}else{if(ch=="("){if(f==0){this.debug("SGF START:<br>");this.sgfStart=true}else{this.debug('<font color="magenta">Variation START:<br></font>')}f++;g.pushCurrent()}else{f--;if(f!=0){this.debug('<font color="magenta">Variation END:<br></font>')}else{this.debug("SGF END:<br>")}g.popCurrent()}}break;case"[":case"]":b=false;if(ch=="["){h++;if(h>1){c.m_str+="\\{";h=1}else{c.m_str+=ch}}else{if(ch=="]"){if(h==1){h--}c.m_str+=ch}}break;case"\\":c.m_str+=ch;index++;ch=d.charAt(index);if(ch=="]"){c.m_str+="}"}break;case"\r":case"\n":case" ":if(h==1){c.m_str+=ch}break;default:c.m_str+=ch;break}}if(c!=null){this.debug(c.m_str)}this.debug("done<br>");return g};var SGFProp={ID_SZ:"SZ",ID_PL:"PL",ID_PW:"PW",ID_WR:"WR",ID_PB:"PB",ID_BR:"BR",ID_EV:"EV",ID_DT:"DT",ID_KM:"KM",ID_RE:"RE",ID_B:"B",ID_W:"W",ID_C:"C",ID_AB:"AB",ID_AW:"AW",ID_LB:"LB",ID_TR:"TR",ID_SQ:"SQ",lp:["AB","AW","LB","TR","SQ"]};var SGoCanvasInfo={Name:"<b>SGoCanvas</b>",Version:"<b>Version 0.1</b>",Description:'<font size="-1">Small and lightweight javascript tool to show go games (sgf files) on webpages</font>',Copyright:'<font size="-1">Copyright &copy; 2010 Daniel Cioata',Homepage:'<a href="http://www.govortex.org/gocanvas">www.govortex.org/gocanvas</a>'};function getSGoCanvasInfo(){var a="";a+='<div style="padding:20px" align="center">';a+=SGoCanvasInfo.Name+"<br>";a+=SGoCanvasInfo.Version+"<br>";a+=SGoCanvasInfo.Description+"<br>";a+=SGoCanvasInfo.Copyright+"<br>";a+=SGoCanvasInfo.Homepage+"<br>";a+="</div>";return a}function debug(b){var a;a=document.getElementById("SGoDebug");if(a!=null){a.innerHTML+=b+"<br>"}}function debug_clear(b){var a;a=document.getElementById("SGoDebug");if(a!=null){a.innerHTML=""}}function SLparser(h){var e,d,k,f,g,m,c,b,a;k=document.getElementById(h);f=k.innerHTML;g=new SGoBoard();g.init(19);m=c=0;for(a=0;a<f.length;a++){ch=f.charAt(a);switch(ch){case"|":m=0;c++;break;case"X":case"O":b=new Stone();b.m_color=ch;b.m_x=m;b.m_y=c;g.addMove(b);m++;break;case".":case",":m++}}st={m_color:"X",m_x:2,m_y:5};g.makeMove(st);k.innerHTML+=g.board_dump()};
