function Board(a){this.pointArr=new Array();this.stoneArr=new Array(19);this.m_stoneList=new Array();this.m_currentIndex=0;this.boardSize=19;this.stoneSize=20;this.boardWidth=0;this.boardHeight=0;this.diaBoard=a}Board.prototype.initGoban=function(d,c){var b,a;this.boardSize=c;this.stoneSize=d;for(b=0;b<this.boardSize;b++){this.pointArr[b]=d/2+b*d;this.stoneArr[b]=new Array(19);for(a=0;a<c;a++){this.stoneArr[b][a]=null}}};Board.prototype.addHandi=function(a,e){var b,d,c;b=this.pointArr[a];d=this.pointArr[e];c=this.createDiv(null,"handi",b-2,d-2,4,4);this.diagram.appendChild(c)};Board.prototype.renderDia=function(g,f,b,l){var k,e,d,c,a;k=(b-g)*this.stoneSize;e=(l-f)*this.stoneSize;this.diagram=this.createDiv(null,"diagram",0,0,k+this.stoneSize,e+this.stoneSize);this.diagram.style.backgroundColor="#C5B358";this.diaBoard.appendChild(this.diagram);for(d=g;d<l;d++){for(c=f;c<b;c++){if(((d==3)||(d==9)||(d==15))&&((c==3)||(c==9)||(c==15))){this.addHandi(c-f+1,d-g+1)}}}for(d=1;d<l-f;d++){a=this.createDiv(null,"line",this.pointArr[0],this.pointArr[d],k,1);this.diagram.appendChild(a)}if(f==1){a=this.createDiv(null,"line",this.pointArr[0],this.pointArr[0],k,2);this.diagram.appendChild(a)}if(l==19){a=this.createDiv(null,"line",this.pointArr[0],this.pointArr[l-f],k+2,2);this.diagram.appendChild(a)}for(d=1;d<b-g;d++){a=this.createDiv(null,"line",this.pointArr[d],this.pointArr[0],1,e);this.diagram.appendChild(a)}if(g==1){a=this.createDiv(null,"line",this.pointArr[0],this.pointArr[0],2,e);this.diagram.appendChild(a)}if(b==19){a=this.createDiv(null,"line",this.pointArr[b-g],this.pointArr[0],2,e+2);this.diagram.appendChild(a)}};Board.prototype.addMove=function(c){var b,a;b=c.m_x;a=c.m_y;if(this.stoneArr[b][a]==null){this.diagram.appendChild(c.m_div);this.stoneArr[b][a]=c}};Board.prototype.remMove=function(c){var b,a;b=c.m_x;a=c.m_y;if(this.stoneArr[b][a]!=null){this.diagram.removeChild(c.m_div);this.stoneArr[b][a]=null}};Board.prototype.makeMove=function(g){var d,f,a,b,c,e;b="";d=this.pointArr[g.m_x]-this.stoneSize/2;f=this.pointArr[g.m_y]-this.stoneSize/2;switch(g.m_id){case"B":case"AB":a="blackStone";if(g.m_id=="B"){b=++this.m_currentIndex;this.m_stoneList.push(g)}break;case"W":case"AW":a="whiteStone";if(g.m_id=="W"){b=++this.m_currentIndex;this.m_stoneList.push(g)}break;case"TR":c=this.stoneArr[g.m_x][g.m_y];if(c==null){a="emptyTR"}else{if((c.m_id=="AB")){a="blackTR"}else{if((c.m_id=="AW")){a="whiteTR"}}}break;case"LB":a="labelStone";b=g.m_lbl;break;default:break}this.stoneArr[g.m_x][g.m_y]=g;if(g.m_div==null){e=this.createDiv(null,a,d,f,this.stoneSize,this.stoneSize);if(b!=""){e.innerHTML=b}this.divColorStyle(e);g.m_div=e}this.diagram.appendChild(g.m_div)};Board.prototype.makeMoveSL=function(g){var d,f,a,b,c,e;b="";d=this.pointArr[g.m_x]-this.stoneSize/2;f=this.pointArr[g.m_y]-this.stoneSize/2;switch(g.m_id){case"X":case"B":a="blackStone";if(g.m_id=="B"){b=++this.m_currentIndex;this.m_stoneList.push(g)}break;case"O":case"W":a="whiteStone";if(g.m_id=="W"){b=++this.m_currentIndex;this.m_stoneList.push(g)}break;case"TR":c=this.stoneArr[g.m_x][g.m_y];if(c==null){a="emptyTR"}else{if((c.m_id=="B")||(c.m_id=="AB")){a="blackTR"}else{if((c.m_id=="W")||(c.m_id=="AW")){a="whiteTR"}}}break;case"Q":a="whiteTR";break;case"LB":a="labelStone";b=g.m_lbl;break;default:break}this.stoneArr[g.m_x][g.m_y]=g;if(g.m_div==null){e=this.createDiv(null,a,d,f,this.stoneSize,this.stoneSize);if(b!=""){e.innerHTML=b}this.divColorStyle(e);g.m_div=e}this.diagram.appendChild(g.m_div)};Board.prototype.moveTo=function(b){var a,c;if(b=="start"){for(a=0;a<this.m_stoneList.length;a++){c=this.m_stoneList[a];this.remMove(c)}this.m_currentIndex=0}else{if(b=="end"){for(a=0;a<this.m_stoneList.length;a++){c=this.m_stoneList[a];this.addMove(c)}this.m_currentIndex=this.m_stoneList.length-1}else{if(b=="next"){if(this.m_currentIndex<this.m_stoneList.length){c=this.m_stoneList[this.m_currentIndex];this.addMove(c);this.m_currentIndex++}}else{if(b=="prev"){if(this.m_currentIndex>0){this.m_currentIndex--;c=this.m_stoneList[this.m_currentIndex];this.remMove(c)}}}}}};Board.prototype.createDiv=function(g,c,e,d,b,a){var f=document.createElement("div");f.position="absolute";if(g!=null){f.id=g}f.className=c;f.style.left=String(e)+"px";f.style.top=String(d)+"px";f.style.width=String(b)+"px";f.style.height=String(a)+"px";return(f)};Board.prototype.divColorStyle=function(a){a.style.position="absolute";a.style.overflow="hidden";a.style.textAlign="center";a.style.fontSize="13px";a.style.fontFamily="Verdana, Arial, Helvetica, sans-serif";a.style.backgroundRepeat="no-repeat"};Board.prototype.clear=function(){var b,a;for(b=0;b<19;b++){for(a=0;a<19;a++){if(this.stoneArr[b][a]!=null){this.diagram.remMove(this.stoneArr[b][a])}}}};Board.prototype.getWidth=function(){return(this.diagram.style.width)};function traverse(a,h){var c;var d=new Array();var b=new Array();var k=new Array();var g;var f,e;var m;m="";a.m_nodelist.goHead();c=a.m_nodelist.getCurrent();if(h=="brief"){g=new List();g.m_str=c.m_str.slice(0);k.push(g);return k}e=0;while(c!=null){e++;m+=c.m_str;if(c.m_childs!=null){for(f=c.m_childs.length-1;f>=0;f--){d.push(c.m_childs[f]);b.push(m.slice(0))}}c=c.m_next;if(c==null){g=new List();g.m_str=m.slice(0);k.push(g);c=d.pop();m=b.pop()}if(c==null){break}if(e>10){alert("Only 10 variations supported");break}}return k}function renderSGFDia(f,a,c){var g;var d=0;var k,e,m,l;var h=new SGoBounds();h.setSGFBounds(c);gobanArea=new Board(a);gobanArea.initGoban(17,19);gobanArea.renderDia(h.m_left,h.m_top,h.m_right,h.m_bottom);for(i=0;i<f.m_nodes.length;i++){for(j=0;j<f.m_nodes[i].m_nodeInfo.length;j++){g=f.m_nodes[i].m_nodeInfo[j];if((g.m_x>=(h.m_left-1))&&(g.m_x<h.m_right)&&(g.m_y>=(h.m_top-1))&&(g.m_y<h.m_bottom)){g.m_x-=(h.m_left-1);g.m_y-=(h.m_top-1);gobanArea.makeMove(g)}}}gobanDiagram.push(gobanArea);return gobanArea.getWidth()}function Prop(){this.m_x=0;this.m_y=0;this.m_id=0;this.m_lbl=null;this.m_div=null}function NodeInfo(){this.m_nodeInfo=null}NodeInfo.prototype.addProp=function(a){if(this.m_nodeInfo==null){this.m_nodeInfo=new Array()}this.m_nodeInfo.push(a)};NodeInfo.prototype.addStringProp=function(a){var b;if(this.m_nodeInfo==null){this.m_nodeInfo=new Array()}switch(a[0]){case"B":case"W":b=new Prop();b.m_x=a[1].charCodeAt(0)-97;b.m_y=a[1].charCodeAt(1)-97;b.m_id=a[0];this.m_nodeInfo.push(b);break;case"AW":case"AB":case"TR":case"LB":for(i=1;i<a.length;i++){b=new Prop();b.m_x=a[i].charCodeAt(0)-97;b.m_y=a[i].charCodeAt(1)-97;b.m_id=a[0];if(a[0]=="LB"){b.m_lbl=a[i].charAt(3)}this.m_nodeInfo.push(b)}break}};function List(){this.m_str="";this.m_next=null;this.m_prev=null;this.m_childs=null;this.m_nodes=null;this.m_comments=""}List.prototype.setNodeInfo=function(){var f,b;var c,a,g;var e,d;f=this.m_str.split(";");for(e=0;e<f.length;e++){b=f[e].match(/[A-Z]+(\[.*?\])+/g);if(b!=null){c=new NodeInfo();for(d=0;d<b.length;d++){if(b[d]!=null){a=b[d].split("[");c.addStringProp(a)}}if(this.m_nodes==null){this.m_nodes=new Array()}this.m_nodes.push(c)}}};List.prototype.addChild=function(a){if(this.m_childs==null){this.m_childs=new Array()}this.m_childs.push(a)};function NodeList(){this.m_head=null;this.m_current=null;this.lstack=new Array()}NodeList.prototype.getCurrent=function(){return this.m_current};NodeList.prototype.goHead=function(){this.m_current=this.m_head};NodeList.prototype.goNext=function(){if(this.m_current!=null){this.m_current=this.m_current.m_next}};NodeList.prototype.push=function(){this.lstack.push(this.m_current)};NodeList.prototype.pop=function(){this.m_current=this.lstack.pop()};NodeList.prototype.addList=function(a){if(this.m_head==null){this.m_head=a;this.m_current=this.m_head}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}}};function Bounds(){this.m_left=1;this.m_top=1;this.m_right=19;this.m_down=19}Bounds.prototype.setBounds=function(c){var b=c[0].split("=");var a=b[1].split(",")};function SGFparser(){this.m_nodelist=new NodeList();this.m_dim=new Bounds()}SGFparser.prototype.parseSGF=function(f){var a;var e;var c;var b;var d;d=f.match(/bounds.*\n/);if(d!=null){this.m_dim.setBounds(d)}a=f.length;e=new List();this.m_nodelist.addList(e);b=0;for(i=0;i<a;i++){c=f.charAt(i);switch(c){case"(":if(b>0){this.m_nodelist.push();e=new List();this.m_nodelist.addList(e)}b++;break;case")":b--;if(b>0){this.m_nodelist.pop()}break;case"\n":case"\r":break;default:e.m_str+=c;break}}};var gobanDiagram=new Array();var gobanID=0;var SGoArr=new Array();function SGoObj(){this.m_type=null;this.m_text=null;this.m_parentDiv=null;this.m_option=null;this.m_preTag=null;this.m_bounds=null;this.m_fileType=null}function createDiaFull(){var a;a=document.createElement("div");a.className="fullDia";a.style.cssFloat="left";a.style.margin="5px";return a}function addDiaTitle(a,c){var b=document.createElement("div");b.className="diaTitle";b.innerHTML=c;a.appendChild(b);return b}function addDiaControl(k,h){var d;var c;var e;var a=new Array("start","prev","next","end");var b=document.createElement("div");var f="/tools/sgo/img/";b.className="eventHandler";b.style.marginTop=5+"px";b.style.marginBottom=5+"px";for(d=0;d<a.length;d++){var g=a[d];c=document.createElement("a");e=document.createElement("img");e.src=f+a[d]+".gif";e.title=a[d];e.className=g;e.id=g+"-"+h;e.onclick=handleEvent;e.style.display="inline";e.style.cursor="default";e.style.paddingRight=8+"px";c.appendChild(e);b.appendChild(c)}k.appendChild(b)}function handleEvent(d){var a;var f;var c;var b;if(!d){var d=window.event}d.cancelBubble=true;if(d.stopPropagation){d.stopPropagation()}if(d.target){a=d.target}else{if(d.srcElement){a=d.srcElement}}if(a.nodeType==3){a=a.parentNode}c=a.id;f=c.split("-");b=parseInt(f[1]);if(!isNaN(b)){gobanDiagram[b].moveTo(f[0])}}function extractId(c,b){var a;a=b.split("-");c=a[0];return parseInt(a[1])}function drawZGoDia(a){var e="";var b;var c;var d;jarFile='"/tools/zgo/ZGo.jar"';if(a.m_fileType=="problem"){b='width="340" height="500"';c='"true,true"'}else{if(a.m_option=="config_1"){b='width="710" height="510"';c='"true,true"'}else{b='width="535" height="590"';c='"false,false"'}}e+='<applet code="ZGoMain.class"'+b+"archive="+jarFile+">";if(a.m_fileType=="problem"){e+='<param name="fileType" value="problem">'}e+='<param name="appletWindow" value="false">';e+='<param name="showInfoControls" value='+c+">";e+='<param name="rawSGF" value="'+a.m_text+'">';e+="</applet>";a.m_parentDiv.innerHTML=e}function drawSGFDia(a){var c;var f;var e;var b;var d=new SGFparser();d.parseSGF(a.m_text);sgfList=traverse(d,a.m_option);for(c=0;c<sgfList.length;c++){e=createDiaFull();diaTitle=addDiaTitle(e,"Dia."+(c+1));sgfList[c].setNodeInfo();b=renderSGFDia(sgfList[c],e,a.m_bounds);addDiaControl(e,gobanID);gobanID++;a.m_parentDiv.appendChild(e);e.style.width=b}}function drawSLDia(a){var c,b;var d=new SLparser();d.parseSL(a.m_text);dia=createDiaFull();diaTitle=addDiaTitle(dia,d.diaTitle);b=renderSLDia(d.m_nodeList,dia);if(a.m_option!="noNav"){addDiaControl(dia,gobanID)}gobanID++;a.m_parentDiv.appendChild(dia);dia.style.width=b;diaTitle.style.width=b}function sgo_render(){var e,c,b;var h;var d;var f=document.getElementsByTagName("div");var a=new Array();for(e=0;e<f.length;e++){h=f[e];if((f[e].className=="SLDia")||(f[e].className=="SGFDia")||(f[e].className=="ZGoDia")){for(c=0;c<f[e].childNodes.length;c++){if(f[e].childNodes[c].nodeName=="PRE"){var g=new SGoObj();g.m_type=f[e].className.slice(0);d=f[e].childNodes[c];g.m_text=d.innerHTML.slice(0);g.m_option=d.className.slice(0);g.m_parentDiv=h;g.m_bounds=d.getAttribute("boardBounds");g.m_fileType=d.getAttribute("fileType");g.m_preTag=d;SGoArr.push(g)}}}}for(e=0;e<SGoArr.length;e++){var g=SGoArr[e];if(g.m_preTag!=null){g.m_parentDiv.removeChild(g.m_preTag);g.m_preTag=null}}for(e=0;e<SGoArr.length;e++){var g=SGoArr[e];if(g.m_type=="SLDia"){drawSLDia(g)}else{if(g.m_type=="SGFDia"){drawSGFDia(g)}else{if(g.m_type=="ZGoDia"){drawZGoDia(g)}}}}}function SGoBounds(){this.m_top=0;this.m_left=0;this.m_right=0;this.m_bottom=0}SGoBounds.prototype.setSLBounds=function(b,a){if(this.m_top!=1||this.m_bottom!=19){if(this.m_top==1){this.m_bottom=this.m_top+a-1}else{this.m_top=this.m_bottom-a+1}}if(this.m_left!=1||this.m_right!=19){if(this.m_left==1){this.m_right=this.m_left+b-1}else{this.m_left=this.m_right-b+1}}};SGoBounds.prototype.setSGFBounds=function(c){var b;this.m_top=1;this.m_left=1;this.m_right=19;this.m_bottom=19;if(c==null){return}b=c.split(",");if(b.length!=4){alert("Invalid SGF bounds")}else{for(i=0;i<b.length;i++){var a=parseInt(b[i]);if(i==0&&!isNaN(a)){this.m_top=a}else{if(i==1&&!isNaN(a)){this.m_left=a}else{if(i==2&&!isNaN(a)){this.m_right=a}else{if(i==3&&!isNaN(a)){this.m_bottom=a}}}}}}};function SLProp(){this.m_x=0;this.m_y=0;this.m_id=0;this.m_lbl=null;this.m_index=0;this.m_div=null}function SLNodeList(){this.m_plainProp=new Array();this.m_indexProp=new Array();this.m_bounds=new SGoBounds()}SLNodeList.prototype.sort=function(){var c,b;var a;for(c=1;c<this.m_indexProp.length;c++){a=this.m_indexProp[c];b=c;while((b>0)&&(this.m_indexProp[b-1].m_index>a.m_index)){this.m_indexProp[b]=this.m_indexProp[b-1];b=b-1}this.m_indexProp[b]=a}};function SLparser(){this.nextToPlay="B";this.diaTitle="Add diagram Title";this.m_nodeList=new SLNodeList()}SLparser.prototype.addTitle=function(a){var b="";if((a[1]=="W")||(a[1]=="B")){this.nextToPlay=a[1]}for(i=2;i<a.length;i++){b+=a[i]+" "}this.diaTitle=b};SLparser.prototype.parseSL=function(f){var a;var c,b;var e;var d;str="";a=f.match(/\$\$.*(\n|\r)/g);if(a==null){alert("SL text diagram invalid\n"+f);return}this.addTitle(a[0].split(/\s+/g));a.shift();if(a[0].match("---")){this.m_nodeList.m_bounds.m_top=1;a.shift()}if(a[a.length-1].match("---")){this.m_nodeList.m_bounds.m_bottom=19;a.pop()}for(c=0;c<a.length;c++){e=this.processLine(a[c],c)}this.m_nodeList.m_bounds.setSLBounds(e,c)};SLparser.prototype.processLine=function(a,c){var d;var h,f;var g;var e;h=a.split(/\s+/g);h.shift();f=0;for(d=0;d<h.length;d++){ch=h[d].charAt(0);switch(ch){case"|":if(d==0){this.m_nodeList.m_bounds.m_left=1}else{this.m_nodeList.m_bounds.m_right=19}break;case"X":case"O":case"Y":case"Q":case"T":case",":g=new SLProp();g.m_y=c;g.m_x=f;g.m_id=""+ch;this.m_nodeList.m_plainProp.push(g);f++;break;case".":f++;break;case"\n":case"\r":case" ":case"":break;default:g=new SLProp();g.m_x=f;g.m_y=c;e=parseInt(h[d]);if(isNaN(e)){g.m_lbl=h[d];g.m_id="LB";this.m_nodeList.m_plainProp.push(g)}else{var b=e;if(this.nextToPlay=="W"){b++}if(b%2==0){g.m_id="W"}else{g.m_id="B"}g.m_index=e;this.m_nodeList.m_indexProp.push(g)}f++;break}}return f};function renderSLDia(d,g){var a;var c=0;var f,e,h,b;e=d.m_bounds.m_left;f=d.m_bounds.m_top;h=d.m_bounds.m_bottom;b=d.m_bounds.m_right;gobanArea=new Board(g);gobanArea.initGoban(17,19);gobanArea.renderDia(e,f,b,h);d.sort();for(i=0;i<d.m_plainProp.length;i++){a=d.m_plainProp[i];gobanArea.makeMoveSL(a)}for(i=0;i<d.m_indexProp.length;i++){a=d.m_indexProp[i];gobanArea.makeMoveSL(a)}gobanDiagram.push(gobanArea);return gobanArea.getWidth()};