// jQuery available Plugin 1.4.1 (20100124)
// By John Terenzio (http://terenz.io/) | MIT License
(function($){$.available={list:[],interval:null,poll:function(){for(var i=0;i<$.available.list.length;i++){if($($.available.list[i][0]).length&&($($.available.list[i][0]).next().length||$.isReady)){$.available.list[i][1].apply($($.available.list[i][0]).eq(0));$.available.list=$.available.list.slice(0,i).concat($.available.list.slice(i+1));i--;}}if(!$.available.list.length||$.isReady){$.available.interval=clearInterval($.available.interval);}}};$.fn.available=function(fn){$.available.list.push([this.selector,fn]);if(!$.available.interval){$.available.interval=setInterval($.available.poll,1);}return this;};})(jQuery);
(function(a){jQuery.fn.backgroundPosition=function(){var b=a(this).css("background-position");if(typeof(b)==="undefined"){return a(this).css("background-position-x")+" "+a(this).css("background-position-y")}else{return b}}})(jQuery);function Viewer(c,a){this.config=jQuery.extend({showMoveList:true,size:"medium"},a);this.rowHeight=50;this.colWidth=50;this.leftOffset=19;this.topOffset=23;this.pieceSize=50;this.captureWidth=155;this.boardsize=650;this.selectOffset=55;if(this.config.size=="small"){this.rowHeight=35;this.colWidth=35;this.leftOffset=17;this.topOffset=17;this.pieceSize=35;this.captureWidth=110;this.boardsize=450;this.selectOffset=40}this.moveTime=500;this.id=c;this.positions=[3,5,2,1,0,1,2,5,3,7,7,7,7,7,7,7,7,7,7,4,7,7,7,7,7,4,7,6,7,6,7,6,7,6,7,6,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,6,7,6,7,6,7,6,7,6,7,4,7,7,7,7,7,4,7,7,7,7,7,7,7,7,7,7,3,5,2,1,0,1,2,5,3];this.colors=[0,0,0,0,0,0,0,0,0,,,,,,,,,,,0,,,,,,0,,0,,0,,0,,0,,0,,,,,,,,,,,,,,,,,,,1,,1,,1,,1,,1,,1,,,,,,1,,,,,,,,,,,1,1,1,1,1,1,1,1,1];this.initState();this.renderBoard();var b=$("#"+c).offset();this.left=b.left+this.captureWidth+this.leftOffset;this.top=b.top+this.topOffset;this.renderPieces()}Viewer.prototype.setInitPosition=function(a,b){this.positions=a;this.color=b};Viewer.prototype.initState=function(){this.pieces=new Array(90);this.moveList=[];this.moveIndex=-1;this.captured=[];this.bcaptured=0;this.rcaptured=0};Viewer.prototype.renderBoard=function(){var c=this.id;var b=[];var a=$("#"+c);if(this.config.size=="medium"){a.addClass("container")}else{if(this.config.size=="small"){a.addClass("container_small")}}b.push("<div class='board' id='board"+c+"'/>");if(this.config.showMoveList){b.push("<div class='right'>");b.push("<div id='move"+c+"' class='moveList'/>");b.push("</div>")}else{a.css("width",this.boardsize+"px")}b.push("<div style='clear:both'/>");b.push("<div class='control' id='"+c+"control'>");b.push("<a id='"+c+"start' class='start'/>");b.push("<a id='"+c+"back' class='prev'/>");b.push("<a id='"+c+"next' class='next'/>");b.push("<a id='"+c+"end' class='end'/>");b.push("<div style='clear:both'>");a.html(b.join(""));$("#"+c+"next").bind("click",this,this.next);$("#"+c+"back").bind("click",this,this.back);$("#"+c+"start").bind("click",this,this.start);$("#"+c+"end").bind("click",this,this.end)};Viewer.prototype.setListOfMove=function(b){if(!b){alert("Move list is missing")}else{var c=this.convertToStandard(b.split("/"));this.moveList=c;var b=[];var a;for(i in c){a=this.parseMove(c[i]);b.push("<div id='mv"+this.id+i+"'");if(a.color==1){b.push("class='red'");b.push(" > Red: ")}else{b.push("class='black'");b.push("> Black: ")}b.push("("+this.intToChar(a.fx));b.push(","+(9-a.fy)+")");b.push(" -> ");b.push("("+this.intToChar(a.tx));b.push(","+(9-a.ty)+")");b.push("</div>")}$("#move"+this.id).html(b.join(""))}};Viewer.prototype.convertToStandard=function(g){var d,f,c,e,b,a;for(a=0;a<g.length;a++){d=g[a];f=parseInt(d.charAt(0));e=9-parseInt(d.charAt(1));c=parseInt(d.charAt(2));b=9-parseInt(d.charAt(3));if(a%2==0){g[a]="1"+f+e+c+b}else{g[a]="0"+f+e+c+b}}return g};Viewer.prototype.getBcaptured=function(){var b=$("#board"+this.id).offset();var a={};a.left=b.left;a.top=b.top;return a};Viewer.prototype.getRcaptured=function(){var b=$("#board"+this.id);var a={};a.left=b.offset().left;a.top=b.offset().top+b.innerHeight()/2;return a};Viewer.prototype.addPiece=function(a){$("#"+this.id).append(a)};Viewer.prototype.applyMove=function(c){if(this.active){var a=this.active.backgroundPosition();a=a.substring(2,a.length);this.active.css("background-position","0"+a)}var b=this.parseMove(c);var e=this.pieces[b.from];var d;this.pieces[b.to]=e;this.pieces[b.from]=null;if(e){d=$(e);this.active=d;d.addClass("moving");d.animate(this.getTargetCoor(b),this.moveTime,function(){$(this).removeClass("moving")});var a=d.backgroundPosition();a=a.substring(1,a.length);d.css("background-position",""+this.selectOffset+a)}};Viewer.prototype.next=function(g){var b=g.data;var f=b.moveList;var c=b.moveIndex;if(c<f.length-1){b.moveIndex++;var a=f[c+1];if(c>-1){$("#mv"+b.id+c).removeClass("select")}$("#mv"+b.id+(c+1)).addClass("select");var e=b.parseMove(a);var d=b.pieces[e.to];b.applyMove(a);b.captured.push(d);if(d){if(e.color==1){$(d).animate(b.getRedCapturedRegion(),b.moveTime);b.rcaptured++}else{if(e.color==0){$(d).animate(b.getBlackCapturedRegion(),b.moveTime);b.bcaptured++}}}}};Viewer.prototype.back=function(g){var c=g.data;var d=c.moveIndex;var e;if(d>-1){var b=c.moveList[d];if(c.moveIndex>0){$("#mv"+c.id+(d-1)).addClass("select")}$("#mv"+c.id+d).removeClass("select");var a=b.slice(0,1)+b.substring(3)+b.substring(1,3);c.applyMove(a);var f=c.parseMove(b);e=c.captured.pop();if(e){c.pieces[f.to]=e;if(e.color==0){c.rcaptured--}else{if(e.color==1){c.bcaptured--}}$(e).animate(c.getTargetCoor(f),this.moveTime)}c.moveIndex--}};Viewer.prototype.start=function(c){var a=c.data;a.moveTime=0;var b=a.moveIndex;while(a.moveIndex>-1){a.back(c)}a.moveTime=500};Viewer.prototype.end=function(c){var a=c.data;var b=a.moveIndex;a.moveTime=0;while(a.moveIndex<a.moveList.length-1){a.next(c)}a.moveTime=500};Viewer.prototype.renderPieces=function(){var c,b,a;for(pos in this.positions){b=this.positions[pos];a=this.colors[pos];if(b!=7){this.pieces[pos]=new Piece(this.config.size,""+pos+a+b,b,a)}}this.render()};Viewer.prototype.render=function(){var a;var c;var b;for(i in this.pieces){b=this.pieces[i];if(b){c=Math.floor(i/9);a=i%9;b.css("top",this.top+this.rowHeight*c+"px");b.css("left",this.left+this.colWidth*a+"px");this.addPiece(b)}}};Viewer.prototype.parseMove=function(a){var b={};b.color=parseInt(a.charAt(0));b.fx=parseInt(a.charAt(1));b.fy=9-parseInt(a.charAt(2));b.tx=parseInt(a.charAt(3));b.ty=9-parseInt(a.charAt(4));b.from=b.fx+b.fy*9;b.to=b.tx+b.ty*9;return b};Viewer.prototype.getRedCapturedRegion=function(){var a={};a.left=this.getRcaptured().left+this.pieceSize*Math.floor(this.rcaptured/5)+"px";a.top=this.getRcaptured().top+this.pieceSize*(this.rcaptured%5)+"px";return a};Viewer.prototype.getBlackCapturedRegion=function(){var b={};var a=this.getBcaptured();b.left=a.left+this.pieceSize*Math.floor(this.bcaptured/5)+"px";b.top=a.top+this.pieceSize*(this.bcaptured%5)+"px";return b};Viewer.prototype.getTargetCoor=function(a){var b={};b.left=this.left+a.tx*this.colWidth+"px";b.top=this.top+a.ty*this.rowHeight+"px";return b};Viewer.prototype.intToChar=function(a){switch(a){case 0:return"a";case 1:return"b";case 2:return"c";case 3:return"d";case 4:return"e";case 5:return"f";case 6:return"g";case 7:return"h";case 8:return"i";case 9:return"j"}};Move=function(a){this.color;this.formx;this.fromy;this.tox;this.toy};Piece=function(b,e,d,a){this.colorOffset=385;this.pieceOffset=55;if(b=="small"){this.colorOffset=280;this.pieceOffset=40}var c=$("<div class='piece' id='"+e+"'></div>");c.css("background-position","0 -"+(this.colorOffset*a+this.pieceOffset*d)+"px");c.color=a;return c};
