var cell_size=50;var chess_set_img=new Image(cell_size*3,cell_size*6);fix_alpha(chess_set_img,'/img/chess15.png');var ts=this.chess_set_img.style;ts.position='absolute';ts.border='0px';ts.visibility='hidden';function render_tile(o,pc){o.style.position='relative';o.style.width=cell_size+'px';o.style.height=cell_size+'px';o.style.backgroundColor='#777788';var ofs_x=0;var ofs_y=0;var pp=pc.charAt(1);var col=pc.charAt(0);if(pp=='k')ofs_y=0;else if(pp=='q')ofs_y=cell_size;else if(pp=='r')ofs_y=2*cell_size;else if(pp=='n')ofs_y=3*cell_size;else if(pp=='b')ofs_y=4*cell_size;else if(pp=='p')ofs_y=5*cell_size;var sh_y=ofs_y;var sh_x=ofs_x+2*cell_size;if(col=='b')ofs_x+=cell_size;var tl=document.createElement('div');tl.style.position='absolute';tl.style.overflow='hidden';tl.style.width=cell_size+'px';tl.style.height=cell_size+'px';tl.style.zIndex=1;tl.appendChild(chess_set_img.cloneNode(true));var ts=tl.firstChild.style;ts.left=-ofs_x+'px';ts.top=-ofs_y+'px';ts.visibility='visible';var shadow_offset=1;var sh=document.createElement('div');sh.style.position='absolute';sh.style.overflow='hidden';sh.style.width=cell_size+'px';sh.style.height=cell_size+'px';sh.style.left=shadow_offset+'px';sh.style.top=shadow_offset+'px';sh.style.backgroundColor='transparent';sh.style.zIndex=-1;sh.appendChild(chess_set_img.cloneNode(true));ts=sh.firstChild.style;ts.left=-sh_x+'px';ts.top=-sh_y+'px';ts.visibility='visible';tl.appendChild(sh);o.appendChild(tl);}function draw_board(id,fen,moves,num_moves){var o=gk_get_el('board'+id);if(!o)return;var bob=new ichess_create(14,'board'+id);o._bob=bob;bob.b_legal_moves=1;bob.b_show_legal_moves=1;bob.init_graphics_set(bob.graphics_style,30);bob.init_position(fen,0,1);bob.init_moves_and_promos(moves?moves:'',4);bob.init_options(0,0,0,0,1,1);bob.go(0,num_moves?num_moves:0);return bob;}function delete_canmove(bob,x,y){for(var i=0;i<bob.canmoves.length;i++){var cn=bob.canmoves[i];if(cn.x!=x||cn.y!=y)continue;cn.destroy();bob.canmoves.splice(i,1);break;}}function show_canmoves(bob,pc,b_no_reset){var cc=find_piece_on_board(bob,pc);if(!cc||cc[0]<0||cc[1]<0)return;var x0=cc[0];var y0=cc[1];if(bob._force_canmoves&&bob._force_canmoves[pc]){if(!b_no_reset)bob.reset_canmoves();var all=bob._force_canmoves[pc];for(var i=0;i<all.length;i++){var p=all[i];bob.canmoves.push(new bob.create_canmove(bob,p[0],p[1]));}return;}bob.show_canmoves_xy(x0,y0);}function animate_possible_moves(bob,pc){bob.reset_movers();bob.force_animation_time=600;if(bob._animate_timer){clearInterval(bob._animate_timer);bob._animate_timer=0;}bob._b_kill_animation=0;bob._counter_wait=999;bob._animate_timer=setInterval(function(){var b=bob;if(b.movers_active())return;if(b._counter_wait++<8)return;b.reset_all(undefined,1);var cc=find_piece_on_board(b,pc);if(!cc||cc[0]<0||cc[1]<0)return;var x0=cc[0];var y0=cc[1];show_canmoves(b,pc);if(b._b_kill_animation||!b.canmoves.length){if(b._animate_timer){clearInterval(b._animate_timer);b._animate_timer=0;}return;}var x=0;var y=0;for(var loop=0;loop<3;loop++){var n=Math.floor(b.canmoves.length*Math.random());x=b.canmoves[n].x;y=b.canmoves[n].y;if(x!=b._previous_x||y!=b._previous_y)break;}var save_canmoves=b.canmoves;b.canmoves=[];b._counter_wait=0;b.submit_move(b.chess_encode_move(x0,y0,x,y));b.canmoves=save_canmoves;delete_canmove(b,x,y);b._previous_x=x;b._previous_y=y;},100);}function animate_moves(id,b_on,pc){var o=gk_get_el('board'+id);if(!o)return;var bob=o._bob;if(!bob)return;if(!b_on){bob._b_kill_animation=1;return;}animate_possible_moves(bob,pc);}function generate_div(id,links){var ll='';for(var i=0;i<links.length;i+=2){var title=links[i];var param=links[i+1];if(param){ll+='&raquo; <a href="javascript:void(0)" onMouseOver="animate_moves('+id+',1,\''+param+'\')" onMouseOut="animate_moves('+id+',0)">'+title+'</a><br>';}else{ll+=title+'<br>';}}document.write('<table border=0 cellspacing=0 cellpadding=0><tr><td><div id="board'+id+'">Loading...</div></td><td valign=top>'+ll+'</td></tr></table>');}function find_piece_on_board(bob,find_pc){find_pc=(find_pc.charAt(0)=='w'?0x10:0)|bob.piece2code[find_pc.charAt(1).toLowerCase()];for(var x=0;x<8;x++)for(var y=0;y<8;y++){var pc=bob.bitboard[x+y*8]&0x1f;if(pc==find_pc)return[x,y];}return[-1,-1];}
