var ichess_isIE=(document.all?true:false);function ichess_get_base_obj(id){var name=id;var d=document;if(d.getElementById)return d.getElementById(name);if(d.all)return d.all[name];return null;}function ichess_get_dist(x1,y1,x2,y2){return Math.sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2));}function ichess_find_obj(el){var o=el;while(o){if(o.ichess&&o.ichess.id){return o.ichess;}o=o.parentNode;}return null;}function ichess_handler_mouseover(el,mv){if(!el||!el.style)return;var obj=ichess_find_obj(el);if(!obj)return;if(mv!=obj.current_move){el.style.cursor='pointer';}else{el.style.cursor='default';}obj.update_move_cell(mv,1);}function ichess_handler_mouseout(el,mv){var obj=ichess_find_obj(el);if(!obj)return;obj.update_move_cell(mv,0);}function ichess_handler_click_move(el,num){var obj=ichess_find_obj(el);if(!obj)return;obj.click_move(num);}function ichess_call_handler(elm,func){var obj=ichess_find_obj(elm);if(!obj)return false;if((typeof obj[func])!='function')return false;obj[func](elm);return true;}function ichess_handler_submit_promo_screen(id,pc){var obj=ichess_get_base_obj(id);if(!obj)return false;obj.ichess.submit_promo_screen(pc);return true;}function ichess_handler_update_movers(id){var obj=ichess_get_base_obj(id);if(!obj)return false;obj.ichess.update_movers();return true;}function get_delayed_image_load_handler(o,s){var el=o;var ss=s;return function(){el.src=ss;}}function handler_on_image_abort(){if(!event)return;var o=event.srcElement;if(!o||!o.src)return;if(o.b_reloaded_on_abort)return;o.b_reloaded_on_abort=1;setTimeout(get_delayed_image_load_handler(o,o.src),10);return false;}function ichess_create(_vers,_id,_static_prefix){this.version=_vers;this.id=_id;this.static_prefix=_static_prefix?_static_prefix:'';this.chess_set_img=null;this.chess_set_img_small=null;this.canmove_img=null;this.board=null;this.board_cell_size=40;this.captive_cell_size=20;this.b_flip=0;this.max_captives=16;this.g_board=new Array(8*8);this.g_captives_w=new Array(this.max_captives);this.g_captives_b=new Array(this.max_captives);this.bitboard=[];this.current_move=0;this.max_move=0;this.initial_move=0;this.refresh_cells=[];this.moves=[];this.moves_list=[];this.cur_captive_w=0;this.cur_captive_b=0;this.sel_x=-1;this.sel_y=-1;this.sel_pc=0;this.cur_to_move='w';this.mouse_move=0;this.color_bsq='#888899';this.color_wsq='#FFFFFF';this.color_bdb='#009900';this.bdbg_img=0;this.bdbg_tile=0;this.b_reload_on_style_change=0;this.graphics_style=1;this.graphics_size=4;this.callback_record_move=null;this.callback_click_move=null;this.callback_validate_move=null;this.callback_mouse_click=null;this.callback_update_move=null;this.callback_reset_board=null;this.callback_setup_board_elements=null;this.callback_update_moves_list=null;this.callback_get_buttons_below_board=null;this.callback_add_buttons_below_board=null;this.callback_get_links_below_board=null;this.en_passant_x=-1;this.en_passant_y=-1;this.king_w=[];this.king_b=[];this.b_checkmate=0;this.b_in_check=0;this.b_legal_moves=1;this.b_force_legal_moves=1;this.b_short_notation=1;this.b_force_short_notation=1;this.b_show_legal_moves=1;this.b_movelist_text=0;this.b_movelist_fig=0;this.b_allow_new_moves=1;this.orig_max_move=0;this.orig_moves=[];this.all_promos='qrbn';this.start_fen='rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq -';this.start_move_offset=0;this.game_info='';this.b_show_captures=1;this.b_show_moves=1;this.b_full_page=0;this.b_integrated=0;this.b_board_only=0;this.b_below_board_wide=0;this.b_update_moves_list_on_flip=0;this.b_freestyle=0;this.promo_move_coords=null;this.snapshot_bitboard=[];this.movers=[];this.mover_timeout=10;this.mover_timer=0;this.animation_speed=4;this.canmoves=[];this.force_animation_time=0;this.board_frame_size=40*8;this.free_mover=null;this.free_mover_x=0;this.free_mover_y=0;this.b_printable_mode=0;this.red_move_num=-1;this.mover_divs=[];this.piece2code={'k':1,'q':2,'r':3,'n':4,'b':5,'p':6};this.code2piece={1:'k',2:'q',3:'r',4:'n',5:'b',6:'p'};this.code2pieceU={1:'K',2:'Q',3:'R',4:'N',5:'B',6:'P'};this.color2code={'w':0x10,'b':0};this.b_allow_any_fen=0;this.init_position=function(fen,flip_accordingly,b_allow_any_starting_postion){var plyr=this.start_plyr();this.start_fen=fen;this.b_allow_any_fen=b_allow_any_starting_postion?1:0;if(flip_accordingly&&(this.start_plyr()!=plyr))this.flip();};this.start_plyr=function(){return this.start_fen.match(/ w /)?'w':'b';};this.init_moves_and_promos=function(_moves,move_len){if(!move_len)var move_len=5;this.orig_max_move=Math.floor(_moves.length/move_len);this.orig_moves=[];for(i=0;i<this.orig_max_move;i++){var promo='-';var move=_moves.substr(i*move_len,4);if(move_len>4){promo=_moves.substr(i*move_len+4,1).toLowerCase();if(!promo||this.all_promos.indexOf(promo)<0)promo='-';}this.orig_moves.push(move+promo);}};this.init_header=function(info){this.game_info=info;};this.init_options=function(_reserved,_show_captures,_show_moves,_full_page,_integrated,_board_only){this.b_show_captures=_show_captures?1:0;this.b_show_moves=_show_moves?1:0;this.b_full_page=_full_page?1:0;this.b_integrated=_integrated?1:0;this.b_board_only=_board_only?1:0;};this.init_colors=function(_color_bg,_color_bg2,_color_border,_color_hl){};this.get_obj=function(name){if(name)name=this.id+'-'+name;else name=this.id;var d=document;if(d.getElementById)return d.getElementById(name);if(d.all)return d.all[name];return null;};this.bd_colors=['888899','FFFFFF','009900',0,0,'696992','d2d2df','595969',0,0,'476bb2','c8d3e8','4b5976',0,0,'669999','d2e1e1','5e7373',0,0,'6ea155','d4e3cc','5b6f51',0,0,'6e886e','d4dcd4','5b655b',0,0,'be6c6c','ecd3d3','826161',0,0,'956c95','e0d3e0','716171',0,0,'664c7f','d2cad9','50465a',0,0,'b2997f','e8e1d9','83796f',0,0,'715746','d5cdc8','50463f',0,0,'c86c2f','efd3c1','7f5a41',0,0,'9F6E5B','FBD19E','805849',1,0,'8A5451','EFC88E','94504B',2,0,'88664F','F9CD9A','8F6447',3,0,'97664E','E0B597','7D5541',4,0,'76837A','E8ECE7','55615A',5,1,'73778D','D9D1D3','4F5469',6,1,'BC936E','DECFBA','99785A',7,1,'827C74','DCD7CF','69645C',8,1,'8C7C6F','C2BAB4','73665B',9,1,'888899','FFFFFF','009000'];this.init_board_color=function(idx){idx*=5;if(idx<0)idx=0;if(idx>(this.bd_colors.length-5))idx=this.bd_colors.length-5;this.color_bsq='#'+this.bd_colors[idx];this.color_wsq='#'+this.bd_colors[idx+1];this.color_bdb='#'+this.bd_colors[idx+2];this.bdbg_img=this.bd_colors[idx+3];this.bdbg_tile=this.bd_colors[idx+4];var o=this.get_obj('board_decor_inner');if(o)o.style.background=this.color_bdb;};this.init_graphics_gk=function(gfx){var ext_gfx=1;var cell_size=40;if(gfx==0){ext_gfx=2;cell_size=40;}else if(gfx==1){ext_gfx=3;cell_size=40;}else if(gfx==2){ext_gfx=1;cell_size=40;}else if(gfx==3){ext_gfx=1;cell_size=60;}this.init_graphics_set(ext_gfx,cell_size);};this.init_graphics_set=function(_style,_size){if(_style<1)_style=1;if(_style>10)_style=10;_size=Math.floor(_size/10);if(_size<=2)_size=2;else if(_size<=3)_size=3;else if(_size<=4)_size=4;else if(_size<=5)_size=5;else if(_size<=6)_size=6;else _size=8;this.graphics_style=_style;this.graphics_size=_size;this.init_graphics();};this.reset_graphics=function(){this.chess_set_img=null;this.chess_set_img_small=null;var o=this.free_mover;if(o&&o.parentNode)o.parentNode.removeChild(o);this.free_mover=null;this.mover_divs=[];};this.init_graphics=function(chess_set_url,cell_size){if(!chess_set_url)var chess_set_url=this.static_prefix+'/img/chess'+this.graphics_style+this.graphics_size+'.png';if(!cell_size)var cell_size=this.graphics_size*10;this.chess_set_img=new Image(cell_size*3,cell_size*6);fix_alpha(this.chess_set_img,chess_set_url);var ts=this.chess_set_img.style;ts.position='absolute';ts.border='0px';ts.visibility='hidden';ts.display='block';this.board_cell_size=cell_size;var small_size=Math.floor(cell_size/2);this.chess_set_img_small=new Image(small_size*3,small_size*6);fix_alpha(this.chess_set_img_small,chess_set_url);this.chess_set_img_small.style.width=3*small_size+'px';this.chess_set_img_small.style.height=6*small_size+'px';ts=this.chess_set_img_small.style;ts.position='absolute';ts.border='0px';ts.visibility='hidden';ts.display='block';this.captive_cell_size=small_size;};this.get_chess_set_img=function(){if(!this.chess_set_img)this.init_graphics();var img=this.chess_set_img.cloneNode(true);img.onmousedown=function(){return false;};img.ondragstart=function(){return false;};img.onselectstart=function(){return false;};return img;};this.get_chess_set_img_small=function(){if(!this.chess_set_img_small)this.init_graphics();var img=this.chess_set_img_small.cloneNode(true);img.onmousedown=function(){return false;};img.ondragstart=function(){return false;};img.onselectstart=function(){return false;};return img;};this.get_moves=function(){var all='';for(var i=0;i<this.moves.length;i++){var mv=this.moves[i].substr(0,4);var promo=this.validate_promo(this.moves[i].substr(4,1));all+=mv+promo;}return all;};this.get_moves_count=function(){return this.moves?this.moves.length:0;};this.validate_promo=function(p){if(!p||p=='-')return '-';if(this.all_promos.indexOf(p)<0)return '-';return p;};this.get_handler_on_resize=function(){var obj=this;return function(){return obj.on_resize();}};this.on_resize=function(){this.update_dims();this.update_moves_scroll();if((typeof this.old_on_resize)=='function'){this.old_on_resize();}return true;};this.old_on_resize=window.onresize;window.onresize=this.get_handler_on_resize();this.option_legal_moves=function(){if(this.b_force_legal_moves)return 1;return this.b_legal_moves;};this.option_short_notation=function(){if(this.b_force_short_notation)return 1;return this.b_short_notation;};this.go=function(_b_flip,_starting_move){this.b_flip=_b_flip;this.initial_move=_starting_move;this.generate_all();var _this=this;setTimeout(function(){_this.update_moves_scroll();},1);var dummy_mover=new this.create_mover(this,0,0,0,0,0);if(dummy_mover)dummy_mover.destroy();};this.set_styles=function(css){var eid=this.id+'-css';var o=gk_get_el(eid);if(o)o.parentNode.removeChild(o);var s=document.createElement('style');s.type='text/css';s.id=eid;if(s.styleSheet)s.styleSheet.cssText=css;else s.appendChild(document.createTextNode(css));document.getElementsByTagName('head')[0].appendChild(s);};this.generate_all=function(){var b_fix_border=0;if(document.body){var t=document.createElement('div');t.style.border='2px solid #888';t.style.width='2px';t.style.height='2px';t.style.padding='0px';t.style.margin='0px';document.body.appendChild(t);if(t.offsetWidth>0&&t.offsetWidth<6)b_fix_border=1;document.body.removeChild(t);}var dn=this.id+'-';var label_size=10+Math.floor(this.board_cell_size/3);var label_font=60+Math.floor(this.board_cell_size/2);var brdr=1+Math.floor((this.board_cell_size-10)/20);var brdr2=1+Math.floor(brdr*0.4);var score_size=20+Math.floor(this.board_cell_size/4);var cz=this.board_cell_size;var cz_sel=b_fix_border?cz:cz-4;var cz_cap=this.captive_cell_size;this.set_styles('.'+dn+'piece { width: '+cz+'px; height: '+cz+'px; position: absolute; overflow: hidden; }\n.'+dn+'piece IMG { position: absolute; }\n.'+dn+'cap_piece { width: '+cz_cap+'px; height: '+cz_cap+'px; position: absolute; overflow: hidden; padding: 0px; }\n.'+dn+'cap_piece IMG { position: absolute; }\n.'+dn+'promo_piece { width: '+cz+'px; height: '+cz+'px; position: relative; padding: 0px; border: 1px solid #000; background-color: #888899; }\n.'+dn+'piece_sel { width: '+cz_sel+'px; height: '+cz_sel+'px; position: absolute; overflow: hidden; border: 2px solid #0000ff; }\n.'+dn+'piece_sel IMG { position: absolute; margin: -2px; }\n.'+dn+'piece_hilite { width: '+cz_sel+'px; height: '+cz_sel+'px; position: absolute; overflow: hidden; border: 2px solid #3366ff; }\n.'+dn+'piece_hilite IMG { position: absolute; margin: -2px; }\n.'+dn+'piece_red { width: '+cz_sel+'px; height: '+cz_sel+'px; position: absolute; overflow: hidden; border: 2px solid #ff0000; }\n.'+dn+'piece_red IMG { position: absolute; margin: -2px; }\n#'+dn+'captives_w,#'+dn+'captives_b { position: relative; padding: 0px; }\n#'+dn+'board_decor { margin: 0px; border-top: '+brdr+'px solid #ddd; border-left: '+brdr+'px solid #ddd; border-bottom: '+brdr+'px solid #999; border-right: '+brdr+'px solid #999; padding: 0px; }\n#'+dn+'board_decor_inner { border-top: '+brdr2+'px solid #999; border-left: '+brdr2+'px solid #999; border-bottom: '+brdr2+'px solid #ddd; border-right: '+brdr2+'px solid #ddd; padding: '+brdr+'px; background-color: '+this.color_bdb+'; }\n.'+dn+'vlbl TD { color: #888888; width: '+label_size+'px; text-align: center !important; font-size: '+label_font+'%; }\n.'+dn+'hlbl TD { color: #888888; height: '+label_size+'px; text-align: center !important; font-size: '+label_font+'%; }\n#'+dn+'score { width: '+score_size+'px; text-align: center; }\n');var all_t=[];all_t.push('<div style="overflow: hidden; white-space: nowrap; padding: 2px;" id="'+dn+'header">'+this.game_info+'</div>');var bd_size=this.board_cell_size*8;this.board_frame_size=bd_size+label_size+2*(2*brdr+brdr2);all_t.push('<table class=row><tr><td style="vertical-align: top;"><table class=row style="width: '+this.board_frame_size+'px;"><tr><td><div id="'+dn+'vlbls">VL</div></td><td><div id="'+dn+'board_decor"><div id="'+dn+'board_decor_inner"><div><div id="'+dn+'acboard" style="width: '+bd_size+'px; height: '+bd_size+'px; position: relative; padding: 0px;"></div></div></div></div></td></tr><tr><td></td><td><div id="'+dn+'hlbls">HL</div></td></tr></table>');var html_below_board='';if(this.b_show_captures){html_below_board+='<div id="'+dn+'show_captives" class=clearfix><div style="float: left;">'+pre_round_frame(0,'padding: 0px; border: 0px;')+'<table class=row><tr><td><div id="'+dn+'score"></div></td><td><div id="'+dn+'captives_'+(this.b_flip?'w':'b')+'"></div><div id="'+dn+'captives_'+(this.b_flip?'b':'w')+'"></div></td></tr></table>'+post_round_frame()+'</div></div>';}html_below_board+='<div style="visibility: hidden; display: none; width: '+this.board_frame_size+'px;" id="'+dn+'below-board"></div>';if(!this.b_below_board_wide){all_t.push(html_below_board);}all_t.push('</td><td style="padding-left: '+label_size+'px; vertical-align: top; text-align: center; min-width: 160px;" id="'+dn+'right_pane">');if(!this.b_board_only){all_t.push('<div id="'+dn+'move_status" style="width: 120px;">-</div><div id="'+dn+'moves"></div><div id="'+dn+'check_status" style="width: 120px;">-</div>');}if(this.b_below_board_wide){all_t.push('</td></tr><tr><td colspan=2>'+html_below_board);}all_t.push('</td></tr></table>');if(!this.b_board_only){all_t.push('<div id="'+dn+'bottom-area" style="text-align: center; white-space: nowrap;"><div style="padding: 2px;">');if((typeof this.callback_get_buttons_below_board)=='function'){all_t.push(this.callback_get_buttons_below_board());}else{all_t.push('<input type="button" onClick="ichess_call_handler(this,\'goto_first_move\');" value="   &laquo;   "> <input type="button" onClick="ichess_call_handler(this,\'prevmove\');" onDblClick="if (ichess_isIE) ichess_call_handler(this,\'prevmove\');" value="   &lt;   "> <input type="button" onClick="ichess_call_handler(this,\'nextmove\');" onDblClick="if (ichess_isIE) ichess_call_handler(this,\'nextmove\');" value="   &gt;   "> <input type="button" onClick="ichess_call_handler(this,\'goto_last_move\');" value="   &raquo;   ">');}if((typeof this.callback_add_buttons_below_board)=='function'){all_t.push(this.callback_add_buttons_below_board());}all_t.push('</div><div id="'+dn+'footer" style="padding: 1px;">');if((typeof this.callback_get_links_below_board)=='function'){all_t.push(this.callback_get_links_below_board());}else{all_t.push('[ <a href="javascript:void(0);" onClick="ichess_call_handler(this,\'reset_all\')" title="reset to the original position on the board">Reset</a> | <a href="javascript:void(0);" onClick="ichess_call_handler(this,\'flip\')" title="flip the board, as if playing the opposite side">Flip</a> | <a href="javascript:void(0);" onClick="ichess_call_handler(this,\'show_printable\')" title="printable version of this page">Print</a> | <a href="javascript:void(0);" onClick="ichess_call_handler(this,\'show_options\')" title="change options">Options&hellip;</a> ]');}all_t.push('</div><div style="white-space: normal; padding: 1px;" id="'+dn+'help">&nbsp;</div></div>');}var o=this.get_obj();if(o){if(this.b_full_page||this.b_integrated){}else{o.style.padding='3px';}o.innerHTML=all_t.join('');}this.init_board();if((typeof this.callback_setup_board_elements)=='function'){this.callback_setup_board_elements();}this.update_labels();this.reset_board();this.redraw_all();};this.get_mouse_down_handler=function(){var obj=this;return function(event){if(!event)var event=window.event;return obj.on_mouse_down(event);};};this.get_mouse_drag_handler=function(){var obj=this;return function(event){if(!event)var event=window.event;return obj.on_mouse_drag(event);};};this.get_mouse_drag_stop_handler=function(){var obj=this;return function(event){if(!event)var event=window.event;return obj.on_mouse_drag_stop(event);};};this.get_free_mover=function(b_dont_create){if(!this.free_mover){if(b_dont_create)return null;var pc=document.createElement('div');pc.className=this.id+'-piece';pc.ondragstart=function(){return false;};pc.onselectstart=function(){return false;};document.body.appendChild(pc);pc.appendChild(this.get_chess_set_img());pc.style.visibility='hidden';pc.style.display='none';this.free_mover=pc;}return this.free_mover;};this.encode_pos=function(x,y){return String.fromCharCode(97+x)+(y+1);};this.decode_pos=function(pos){return[pos.charCodeAt(0)-97,pos.charCodeAt(1)-49];};this.decode_fen=function(fen,b_no_corrections){var char_chess='kqrnbpKQRNBP';var char_digit='12345678';var repeat=0;var cur_pos=0;var x=0;var y=7;this.bitboard.splice(0,this.bitboard.length);this.king_w=[];this.king_b=[];while(cur_pos<fen.length){var cc=fen.charAt(cur_pos);if(cc=='/'){while(x<8){this.bitboard[x+8*y]=0;x++;}x=0;y--;if(y<0)break;}else
if(char_digit.indexOf(cc)>=0){repeat=parseInt(cc);while(repeat>0){if(x>=8){x=0;y--;if(y<0)break;}this.bitboard[x+8*y]=0;x++;repeat--;}}else{var color=0;var code=this.piece2code[cc];if(!code){cc=cc.toLowerCase();code=this.piece2code[cc];if(!code)break;color=0x10;}var b_not_moved=(code==6)&&((color&&y==1)||(!color&&y==6))?1:0;if(x>=8){x=0;y--;if(y<0)break;}if(code==1){var kcc=color?this.king_w:this.king_b;if(kcc.length){code=0;color=0;b_not_moved=0;}else{if(color)this.king_w=[x,y];else this.king_b=[x,y];}}this.bitboard[x+8*y]=code|color|(b_not_moved?0x20:0);x++;}cur_pos++;}while(y>=0){while(x<8){this.bitboard[x+8*y]=0;x++;}x=0;y--;}if(!b_no_corrections){if(!this.king_w.length){this.bitboard[4]=0x11;this.king_w=[4,0];}if(!this.king_b.length){this.bitboard[4+8*7]=0x01;this.king_b=[4,7];}for(var i=0;i<8;i++){var p=this.bitboard[i]&0x0f;if(p==6)this.bitboard[i]=0;p=this.bitboard[i+8*7]&0x0f;if(p==6)this.bitboard[i+8*7]=0;}}var plyr='w';while(cur_pos<fen.length){var c=fen.charAt(cur_pos++);if(c==' '||c=='\t')continue;plyr=(c=='b')?'b':'w';break;}var b_data=0;while(cur_pos<fen.length){var c=fen.charAt(cur_pos++);if(c==' '||c=='\t'){if(!b_data)continue;else break;}b_data=1;if(c=='Q'){if(this.bitboard[4]==0x11)this.bitboard[4]|=0x20;if(this.bitboard[0]==0x13)this.bitboard[0]|=0x20;}else
if(c=='K'){if(this.bitboard[4]==0x11)this.bitboard[4]|=0x20;if(this.bitboard[7]==0x13)this.bitboard[7]|=0x20;}else
if(c=='q'){if(this.bitboard[4+8*7]==0x01)this.bitboard[4+8*7]|=0x20;if(this.bitboard[8*7]==0x03)this.bitboard[8*7]|=0x20;}else
if(c=='k'){if(this.bitboard[4+8*7]==0x01)this.bitboard[4+8*7]|=0x20;if(this.bitboard[7+8*7]==0x03)this.bitboard[7+8*7]|=0x20;}}this.en_passant_x=-1;this.en_passant_y=-1;while(cur_pos<fen.length){var c=fen.charAt(cur_pos++);if(c==' '||c=='\t')continue;if(cur_pos<fen.length){var c2=fen.charAt(cur_pos++);var pos=this.decode_pos(c+c2);if(pos[0]>=0&&pos[0]<8&&(pos[1]==2||pos[1]==5)){var pp=pos[0]+8*pos[1];if(!this.bitboard[pp]){this.en_passant_x=pos[0];this.en_passant_y=pos[1];}}}break;}return plyr;};this.encode_fen=function(plyr){var plyr=plyr||this.cur_to_move;var fen='';var enpassant=this.en_passant_x<0?'-':this.encode_pos(this.en_passant_x,this.en_passant_y);for(var yy=0;yy<=7;yy++){if(yy>0)fen+='/';var y=7-yy;var count=0;for(var x=0;x<=7;x++){var pc=this.bitboard[x+y*8];if(!pc){count++;}else{var pc_c=pc&0x10;var pc_t=pc&0x0f;if(count){fen+=count;count=0;}if(pc_c)fen+=this.code2pieceU[pc_t];else fen+=this.code2piece[pc_t];}}if(count>0)fen+=count;}var castle='';if(this.bitboard[4+0*8]==0x31){if(this.bitboard[7+0*8]==0x33)castle+='K';if(this.bitboard[0+0*8]==0x33)castle+='Q';}if(this.bitboard[4+7*8]==0x21){if(this.bitboard[7+7*8]==0x23)castle+='k';if(this.bitboard[0+7*8]==0x23)castle+='q';}if(castle=='')castle='-';return fen+' '+plyr+' '+castle+' '+enpassant;};this.get_width=function(){var cz=this.board_cell_size;var side_w=(this.b_show_moves?180:0)+30+cz*2;return cz*9+side_w;};this.get_height=function(){var cz=this.board_cell_size;var side_h=(this.b_show_captures?60:0)+160+cz;return cz*9+side_h;};this.update_dims=function(){if(this.b_integrated)return;var o=this.get_obj();if(!o)return;var footer=this.get_obj('footer');if(!footer)return;var w=this.b_full_page?gk_doc_width()-10:this.get_width();var h=this.b_full_page?gk_doc_height()-10:this.get_height();if(!this.b_full_page){h=gk_get_absolute_pos_top(footer)+footer.offsetHeight-gk_get_absolute_pos_top(o)+10;}if(!w||w<100)w=100;if(!h||h<100)h=100;o.style.width=w+'px';o.style.height=h+'px';var header=this.get_obj('header');if(header)header.style.width=(w-8)+'px';};this.set_square_background=function(el,x,y,b_sel,b_pos_only){var bg_col=(x+y)&1?this.color_wsq:this.color_bsq;if(!this.bdbg_img){el.style.background=bg_col;}else{var dd=Math.floor((this.bdbg_tile?2.5:1.5)*this.board_cell_size);var r=new prnd(x*43+y*3);var bx=r.rnd(dd);if(b_sel)bx+=2;var by=r.rnd(dd);if(b_sel)by+=2;if(b_pos_only){el.style.backgroundPosition='-'+bx+'px -'+by+'px';}else{var col=(x+y)&1?'w':'b';el.style.background=bg_col+' url('+this.static_prefix+'/img/chess-bg'+col+this.bdbg_img+this.graphics_size+'.jpg) repeat -'+bx+'px -'+by+'px';}}};this.init_board=function(){var board=this.get_obj('acboard');this.board=board;board.innerHTML='';board.style.width=8*this.board_cell_size+'px';board.style.height=8*this.board_cell_size+'px';board.onmousedown=this.get_mouse_down_handler();board.ondragstart=function(){return false;};board.onselectstart=function(){return false;};var board_parent=board.parentNode;board_parent.removeChild(board);for(var x=0;x<8;x++){for(var y=0;y<8;y++){var pc=document.createElement('div');pc.className=this.id+'-piece';pc.cur_class=this.id+'-piece';pc.style.left=(this.b_flip?7-x:x)*this.board_cell_size+'px';pc.style.top=(this.b_flip?y:7-y)*this.board_cell_size+'px';this.set_square_background(pc,x,y);pc.appendChild(this.get_chess_set_img());this.g_board[x+y*8]=pc;board.appendChild(pc);}}board_parent.appendChild(board);for(var i=0;i<2;i++){var cap=this.get_obj(i==0?'captives_w':'captives_b');if(!cap)continue;cap.innerHTML='';cap.style.width=this.max_captives*this.captive_cell_size+'px';cap.style.height=this.captive_cell_size+'px';for(var x=0;x<this.max_captives;x++){var pc=document.createElement('div');pc.className=this.id+'-cap_piece';pc.style.left=x*this.captive_cell_size+'px';pc.style.top='0px';pc.appendChild(this.get_chess_set_img_small());if(i==0)this.g_captives_w[x]=pc;else
this.g_captives_b[x]=pc;cap.appendChild(pc);this.update_captive_image(pc,0);}}};this.reset_board=function(){this.max_move=this.orig_max_move;this.moves=this.orig_moves.slice();o=this.get_obj('show_captives');if(o){o.style.visibility=this.b_show_captures?'visible':'hidden';o.style.display=this.b_show_captures?'':'none';}o=this.get_obj('moves');if(o){o.style.visibility=this.b_show_moves?'visible':'hidden';o.style.display=this.b_show_moves?'block':'none';}this.update_dims();this.goto_move(this.max_move,1);if(this.initial_move>=0)this.goto_move(this.initial_move);this.scroll_to(this.get_obj('mv1'),1);if((typeof this.callback_reset_board)=='function')this.callback_reset_board();};this.redraw_elem=function(o){if(!o)return;var d=o.style.display;o.style.display='none';o.style.display=d;};this.update_labels=function(){var o=this.get_obj('hlbls');if(o){var t='<table border=0 cellspacing=0 cellpadding=0 class="'+this.id+'-hlbl"><tr><td width=5></td>';for(var x=0;x<8;x++){t+='<td width='+this.board_cell_size+'>'+String.fromCharCode(97+(this.b_flip?7-x:x))+'</td>';}t+='</tr></table>';o.innerHTML=t;}o=this.get_obj('vlbls');if(o){var t='<table border=0 cellspacing=0 cellpadding=0 class="'+this.id+'-vlbl"><tr height=5><td></td></tr>';for(var y=0;y<8;y++){t+='<tr><td height='+this.board_cell_size+'>'+String.fromCharCode(49+(this.b_flip?y:7-y))+'</td></tr>';}t+='</table>';o.innerHTML=t;}};this.click_move=function(move_num){if((typeof this.callback_click_move)=='function'){this.callback_click_move(move_num);}else{this.anim_move(move_num);}};this.goto_first_move=function(){if(this.b_printable_mode)return;this.anim_move(0);};this.goto_last_move=function(){if(this.b_printable_mode)return;this.anim_move(this.max_move);};this.anim_move=function(move_num){this.board_snapshot();this.goto_move(move_num,0);this.board_snapshot_diff();this.redraw_all();};this.goto_move=function(move_num,b_update_all,b_append){this.abort_mouse_move(0,1);if(!b_append){this.cur_to_move=this.decode_fen(this.start_fen,this.b_allow_any_fen);}var plyr=(this.cur_to_move=='w')?0x10:0;var old_cur_move=this.current_move;this.current_move=move_num;if(this.current_move<0)this.current_move=0;if(this.current_move>this.max_move)this.current_move=this.max_move;this.update_move_cell(old_cur_move);if(b_update_all&&!b_append){this.moves_list=[];}if(!b_append){this.cur_captive_w=0;this.cur_captive_b=0;}this.b_in_check=0;this.b_checkmate=0;var from_x,from_y,to_x,to_y,mv,promo,cc;for(var ii=(b_append?this.current_move-1:0);ii<this.current_move;ii++){mv=this.moves[ii];promo=this.validate_promo(mv.substr(4,1));from_x=mv.charCodeAt(0)-97;from_y=mv.charCodeAt(1)-49;to_x=mv.charCodeAt(2)-97;to_y=mv.charCodeAt(3)-49;var cc=this.chess_make_move(from_x,from_y,to_x,to_y,promo);this.record_captive(cc[0]);plyr=plyr?0:0x10;if(b_update_all){var b_last_move=(ii+1)==this.current_move;var move_note=cc[1];if(!this.option_short_notation()){var from_l=mv.substr(0,2);var to_l=mv.substr(2,2);move_note=from_l+'-'+to_l;}this.b_in_check=0;if(this.chess_in_check(plyr)){this.b_in_check=1;if(b_last_move){if(this.chess_checkmate(plyr)){this.b_checkmate=1;}}move_note+=this.b_checkmate?'#':'+';}this.moves_list.push(move_note);}}this.cur_to_move=plyr?'w':'b';this.update_to_move();if(!b_update_all){this.b_in_check=this.chess_in_check(plyr);this.b_checkmate=this.chess_checkmate(plyr);}var o=this.get_obj('check_status');if(o)o.innerHTML=(this.b_checkmate?' <em class=wrn>CHECKMATE</em>':this.b_in_check?' <em class=wrn>CHECK</em>':'&nbsp;');if(b_update_all){this.update_moves_list();}};this.abort_mouse_move=function(b_animate_mover,b_no_update_visual){if(this.sel_x>=0&&this.sel_y>=0&&this.sel_x<8&&this.sel_y<8){if(b_animate_mover){this.movers.push(new this.create_mover_free(this,this.free_mover_x-gk_get_absolute_pos_left(this.board),this.free_mover_y-gk_get_absolute_pos_top(this.board),this.sel_x,this.sel_y,this.sel_pc));this.start_movers();}this.bitboard[this.sel_x+this.sel_y*8]=this.sel_pc;if(!b_no_update_visual)this.update_cell_image(this.sel_x,this.sel_y,0);}this.end_mouse_move(b_no_update_visual);};this.end_mouse_move=function(b_no_update_visual){this.reset_canmoves();this.sel_x=-1;this.sel_y=-1;this.sel_pc=0;if(!b_no_update_visual){var o=this.get_free_mover(1);if(o){o.style.visibility='hidden';o.style.display='none';}}this.mouse_move=0;};this.update_move_cell=function(mv,mouse_over){if(!this.b_show_moves)return;var o=this.get_obj('mv'+mv);if(!o)return;var cls='';if(mv==this.current_move)cls='hlt_text';else if(mouse_over)cls='hlt_text_mo';set_css_class(o,'hlt_text',cls=='hlt_text');set_css_class(o,'hlt_text_mo',cls=='hlt_text_mo');if(this.b_movelist_text)o.style.fontWeight=(mv==this.current_move)?'bold':'normal';};this.move_to_text=function(mv){if(!this.b_movelist_fig)return mv;var rx=/^(.*)([KQRNB])(.*)$/;var m=rx.exec(mv);if(m&&m.length>=4){return m[1]+'<span class="fig-'+m[2].toLowerCase()+'">'+m[3]+'</span>';}return mv;};this.get_movelist_text=function(){var plyr=this.start_plyr();var black_offset=plyr=='w'?1:0;var all_moves=this.moves_list.slice();if(plyr!='w')all_moves.unshift('...');var all=[];for(var i=0;i<all_moves.length;i++){var mv=this.move_to_text(all_moves[i]);var mn=this.start_move_offset+i/2+1;var ii=i+black_offset;all.push(((i&1)?'':mn+'. ')+mv);}return all.join(' ');};this.update_moves_list=function(){if(!this.b_show_moves)return;var div=this.get_obj('moves');if(!div)return;var st=div.style;var max_height=8*this.board_cell_size-8;if(ie_version>1&&ie_version<6.9)st.height=max_height+'px';else st.maxHeight=max_height+'px';st.overflow='auto';st.paddingRight='20px';var pfx=this.id+'-';var s_plyr=this.start_plyr();var black_offset=s_plyr=='w'?1:0;var all_moves=this.moves_list.slice();if(s_plyr!='w'){all_moves.unshift('...');}if(this.b_movelist_text){var all=[];for(var i=0;i<all_moves.length;i++){var mv=this.move_to_text(all_moves[i]);var mn=this.start_move_offset+i/2+1;var ii=i+black_offset;all.push(((i&1)?'':'<em class=sml>'+mn+'.</em>&nbsp;')+'<span id="'+pfx+'mv'+ii+'" onMouseOver="ichess_handler_mouseover(this,'+ii+')" onMouseOut="ichess_handler_mouseout(this,'+ii+')" onClick="ichess_handler_click_move(this,'+ii+')">'+mv+'</span>');}div.innerHTML='<div id="'+pfx+'moves_list" style="line-height: 18px;">'+all.join('<wbr> ')+'</div>';}else{var all_rows=[];for(var i=0;i<all_moves.length;i++){var mv=this.move_to_text(all_moves[i]);var mn=this.start_move_offset+i/2+1;var ii=i+black_offset;all_rows.push('<tr><th>'+mn+'.</th><td id="'+pfx+'mv'+ii+'" onMouseOver="ichess_handler_mouseover(this,'+ii+')" onMouseOut="ichess_handler_mouseout(this,'+ii+')" onClick="ichess_handler_click_move(this,'+ii+')">'+mv+'</td>');i++;ii++;mv='';if(i<all_moves.length)mv=this.move_to_text(all_moves[i]);all_rows.push('<td id="'+pfx+'mv'+ii+'" onMouseOver="ichess_handler_mouseover(this,'+ii+')" onMouseOut="ichess_handler_mouseout(this,'+ii+')" onClick="ichess_handler_click_move(this,'+ii+')">'+mv+'</td></tr>');}div.innerHTML='<table id="'+pfx+'moves_list" class=dtable style="margin: 2px;">'+all_rows.join('')+'</table>';}if(this.b_movelist_fig&&ie_version>=5.5&&ie_version<7){var all_img=div.getElementsByTagName('IMG');if(all_img){for(var i=0;i<all_img.length;i++){var e=all_img[i];e.style.height='13px';fix_alpha(e,e.src);}}}if((typeof this.callback_update_moves_list)=='function')this.callback_update_moves_list();this.update_current_move();};this.update_moves_scroll=function(){var n=this.current_move;if(n<1)n=1;this.scroll_to(this.get_obj('mv'+n),1);};this.update_scrollbar=function(div_name,content_name){};this.update_moves_list_printable=function(){var o=this.get_obj('moves');if(o){o.innerHTML='';o.style.display='none';o.style.visibility='hidden';}var t='';for(i=0;i<this.moves_list.length;i++){if(i==(this.current_move-1))t+='<b>';if((i%2)==0){var mn=this.start_move_offset+i/2+1;t+=mn+'.&nbsp;';}t+=this.moves_list[i]+' ';if(i==(this.current_move-1))t+='</b>';}var new_o=document.createElement('div');new_o.id='moves_printable';new_o.style.padding='5px';o.parentNode.insertBefore(new_o,o);new_o.innerHTML=t;};this.update_to_move=function(){var o=this.get_obj('move_status');if(!o)return;if(this.b_freestyle){o.innerHTML='('+(!this.b_flip?'white':'black')+' to move)';}else
if(this.b_checkmate){o.innerHTML='&nbsp;';}else{o.innerHTML='('+(this.cur_to_move=='w'?'white':'black')+' to move)';}};this.chess_make_move=function(from_x,from_y,to_x,to_y,_promo,b_just_move){var promo=_promo?this.piece2code[_promo.toLowerCase()]:0;if(!promo)promo=0;var pc=this.bitboard[from_x+from_y*8];var pc_c=pc&0x10;var pc_t=pc&0x0f;var b_not_moved=pc&0x20;var new_pc=pc_c|pc_t;var move_n=pc_t==6?'':this.code2pieceU[pc_t];var captive=0;var move_suff='';var pc2=this.bitboard[to_x+to_y*8];if(promo&&pc_t==6&&((pc_c&&to_y==7)||(!pc_c&&to_y==0))){new_pc=pc_c|promo;move_suff='='+_promo.toUpperCase();}else{if(this.en_passant_x==to_x&&this.en_passant_y==to_y&&pc_t==6&&((from_x+1)==to_x||(from_x-1)==to_x)){captive=this.bitboard[to_x+from_y*8];if((captive&0x0f)==6){this.bitboard[to_x+from_y*8]=0;move_suff=' e.p.';}else{captive=0;}}}if(pc_t==6&&b_not_moved&&from_x==to_x&&((from_y+2)==to_y||(from_y-2)==to_y)){this.en_passant_x=to_x;this.en_passant_y=(from_y+to_y)/2;}else{this.en_passant_x=-1;this.en_passant_y=-1;}if(!captive)captive=pc2;if(!b_just_move){var ambig=0;var from_l='';if(pc_t==6){if(captive)from_l=String.fromCharCode(97+from_x);}else{ambig=this.chess_find_ambiguity(from_x,from_y,to_x,to_y,pc);if(ambig==1)from_l=String.fromCharCode(97+from_x);else if(ambig==2)from_l=String.fromCharCode(97+from_x)+(from_y+1);}var to_l=String.fromCharCode(97+to_x)+(to_y+1);var separator='';if(captive)separator='x';move_n+=from_l+separator+to_l+move_suff;}this.bitboard[from_x+from_y*8]=0;this.bitboard[to_x+to_y*8]=new_pc;if(pc_t==1){if(pc_c)this.king_w=[to_x,to_y];else this.king_b=[to_x,to_y];}if(pc_t==1&&b_not_moved&&from_y==to_y&&(to_y==0&&pc_c||to_y==7&&!pc_c)){if((from_x+2)==to_x){this.bitboard[from_x+1+to_y*8]=this.bitboard[7+to_y*8]&~0x20;this.bitboard[7+to_y*8]=0;move_n='O-O';}else
if((from_x-2)==to_x){this.bitboard[from_x-1+to_y*8]=this.bitboard[0+to_y*8]&~0x20;this.bitboard[0+to_y*8]=0;move_n='O-O-O';}}if(b_just_move)return;return[captive,move_n];};this.chess_find_ambiguity=function(from_x,from_y,x0,y0,find_pc){find_pc&=0x1f;var b_ambig=0;for(var x=0;x<8;x++)for(var y=0;y<8;y++){if(x==from_x&&y==from_y)continue;if(x==x0&&y==y0)continue;var pc=this.bitboard[x+y*8]&0x1f;if(pc!=find_pc)continue;if(!this.chess_valid_move(x,y,x0,y0))continue;if(this.chess_collision(x,y,x0,y0))continue;b_ambig=1;if(x==from_x)return 2;}return b_ambig;};this.record_captive=function(pc){if(!this.b_show_captures)return;var type=pc&0x0f;if(!type)return;var color=pc&0x10;var el;if(color){if(this.cur_captive_w>=this.max_captives)return;el=this.g_captives_w[this.cur_captive_w];this.cur_captive_w++;}else{if(this.cur_captive_b>=this.max_captives)return;el=this.g_captives_b[this.cur_captive_b];this.cur_captive_b++;}this.update_captive_image(el,pc);};this.chess_in_check=function(plyr){var king_cc=plyr?this.king_w:this.king_b;if(!king_cc||king_cc.length<2)return 0;for(var dy=-1;dy<=1;dy++){for(var dx=-1;dx<=1;dx++){var x1=king_cc[0];var y1=king_cc[1];if(dx!=0||dy!=0){while(1){x1+=dx;y1+=dy;if(y1<0||y1>7||x1<0||x1>7)break;var pc=this.bitboard[x1+y1*8];if(!pc)continue;if(((pc&0x10)^ plyr)&&this.chess_valid_move(x1,y1,king_cc[0],king_cc[1]))return 1;break;}}}}for(var dy=-1;dy<=1;dy+=2){for(var dx=-1;dx<=1;dx+=2){for(var dz=2;dz<=3;dz++){var x1=king_cc[0]+dx*(4-dz);var y1=king_cc[1]+dy*(dz-1);if(x1<0||x1>7||y1<0||y1>7)continue;var pc=this.bitboard[x1+y1*8];if(((pc&0x10)^ plyr)&&((pc&0x0f)==4))return 1;}}}return 0;};this.chess_valid_move=function(x,y,x2,y2){var dx=x2-x;var dy=y2-y;var pc=this.bitboard[x+y*8];var b_not_moved=pc&0x20;var pc_t=pc&0x0f;var pc_c=pc&0x10;if(pc_t==1&&b_not_moved&&Math.abs(dx)==2&&dy==0){return this.chess_castling(x,y,dx,dy,pc_c);}if(pc_t==1){return this.chess_biggest(dx,dy)==1;}if(pc_t==2){if(dx!=0&&dy==0)return 1;if(dx==0&&dy!=0)return 1;if(dx==dy||dx==-dy)return 1;return 0;}if(pc_t==5){return dx==dy||dx==-dy;}if(pc_t==4){if(Math.abs(dx)==2&&Math.abs(dy)==1)return 1;if(Math.abs(dx)==1&&Math.abs(dy)==2)return 1;return 0;}if(pc_t==3){if(dx!=0&&dy==0)return 1;if(dx==0&&dy!=0)return 1;return 0;}if(pc_t==6){if(pc_c&&dy<=0)return 0;if(!pc_c&&dy>=0)return 0;var target_pc=this.bitboard[x2+y2*8];if(pc_t==6&&b_not_moved&&dx==0&&Math.abs(dy)==2)return target_pc?0:1;if(this.chess_biggest(dx,dy)!=1)return 0;if(dx!=0){if(target_pc)return 1;var capture_pc=this.bitboard[x2+y*8];return this.en_passant_x==x2&&this.en_passant_y==y2&&capture_pc&&((capture_pc&0x10)^ pc_c)?1:0;}else{return target_pc?0:1;}}return 0;};this.chess_collision=function(x,y,x2,y2){var dx=x2-x;if(dx>0)dx=1;else if(dx<0)dx=-1;var dy=y2-y;if(dy>0)dy=1;else if(dy<0)dy=-1;var pc=this.bitboard[x+y*8];var pc_c=pc&0x10;pc&=0xf;var hit=0;if(pc==4){x=x2;y=y2;}else{x+=dx;y+=dy;while(x!=x2||y!=y2){if(x<0||x>7||y<0||y>7)break;if(this.bitboard[x+y*8]){hit=1;break;}x+=dx;y+=dy;}}if(!hit){var pct=this.bitboard[x+y*8];if(pct&&!((pct&0x10)^ pc_c))hit=1;}return hit;};this.chess_castling=function(x,y,dx,dy,plyr){if(dy!=0)return 0;var x2=x+dx;var rx=dx<0?0:7;dx=rx>0?1:-1;var pcr=this.bitboard[rx+y*8];if((pcr&0x2f)!=0x23)return 0;if((pcr&0x10)^ plyr)return 0;for(var xx=x+dx;xx!=rx;xx+=dx){if(this.bitboard[xx+y*8])return 0;}if(this.chess_in_check(plyr))return 0;var kpos=x+y*8;var pck=this.bitboard[kpos];if((pck&0x2f)!=0x21)return 0;this.bitboard[kpos+dx]=pck&0x1f;this.bitboard[kpos]=0;if(plyr)this.king_w=[x+dx,y];else this.king_b=[x+dx,y];var b_check=this.chess_in_check(plyr);this.bitboard[kpos+dx]=0;this.bitboard[kpos]=pck;if(plyr)this.king_w=[x,y];else this.king_b=[x,y];return!b_check;};this.chess_save_state=function(){return{'ep_x':this.en_passant_x,'ep_y':this.en_passant_y,'board':this.bitboard.slice(),'king_w':this.king_w.slice(),'king_b':this.king_b.slice()};};this.chess_restore_state=function(s){this.en_passant_x=s.ep_x;this.en_passant_y=s.ep_y;this.bitboard=s.board.slice();this.king_w=s.king_w.slice();this.king_b=s.king_b.slice();};this.chess_can_move=function(x,y,x2,y2,plyr){if(x<0||x>7||y<0||y>7)return 0;if(x2<0||x2>7||y2<0||y2>7)return 0;if(x==x2&&y==y2)return 0;var pc=this.bitboard[x+y*8];if(!pc)return 0;if((pc&0x10)^ plyr)return 0;if(!this.chess_valid_move(x,y,x2,y2))return 0;if(this.chess_collision(x,y,x2,y2))return 0;var save_state=this.chess_save_state();this.chess_make_move(x,y,x2,y2,'q',1);var b_check=this.chess_in_check(plyr);this.chess_restore_state(save_state);return!b_check;};this.chess_checkmate_move_coords=function(coords){var a=this.chess_decode_move(coords);return this.chess_checkmate_move(a[0],a[1],a[2],a[3]);};this.chess_checkmate_move=function(x,y,x2,y2,promo){if(x<0||x>7||y<0||y>7)return 0;if(x2<0||x2>7||y2<0||y2>7)return 0;if(x==x2&&y==y2)return 0;var plyr=this.bitboard[x+y*8]&0x10;if(!this.chess_valid_move(x,y,x2,y2))return 0;if(this.chess_collision(x,y,x2,y2))return 0;var save_state=this.chess_save_state();this.chess_make_move(x,y,x2,y2,promo?promo:'q',1);var b_check=this.chess_in_check(plyr);var b_checkmate=0;if(!b_check){b_checkmate=this.chess_checkmate(plyr?0:0x10);}this.chess_restore_state(save_state);return b_checkmate;};this.chess_move_notation=function(x,y,x2,y2,promo){if(x<0||x>7||y<0||y>7)return '';if(x2<0||x2>7||y2<0||y2>7)return '';if(x==x2&&y==y2)return '';var from_l=this.encode_pos(x,y);var to_l=this.encode_pos(x2,y2);var plyr=this.bitboard[x+y*8]&0x10;plyr=plyr?0:0x10;var save_state=this.chess_save_state();var cc=this.chess_make_move(x,y,x2,y2,promo);var b_check=this.chess_in_check(plyr);this.chess_restore_state(save_state);var move_note=this.option_short_notation()?cc[1]:(from_l+'-'+to_l);if(b_check)move_note+='+';return move_note;};this.chess_checkmate_in_one=function(plyr){for(var x=0;x<8;x++)for(var y=0;y<8;y++){var pc=this.bitboard[x+y*8];if(!pc)continue;if((pc&0x10)^ plyr)continue;var all=this.chess_moves_available(x,y);for(var i=0;i<all.length;i++){var x2=all[i][0];var y2=all[i][1];if(this.is_pawn_promo_move(x,y,x2,y2)){if(this.chess_checkmate_move(x,y,x2,y2,'q'))return this.chess_encode_move(x,y,x2,y2,'q');if(this.chess_checkmate_move(x,y,x2,y2,'r'))return this.chess_encode_move(x,y,x2,y2,'r');if(this.chess_checkmate_move(x,y,x2,y2,'n'))return this.chess_encode_move(x,y,x2,y2,'n');if(this.chess_checkmate_move(x,y,x2,y2,'b'))return this.chess_encode_move(x,y,x2,y2,'b');}else{if(this.chess_checkmate_move(x,y,x2,y2))return this.chess_encode_move(x,y,x2,y2);}}}return '';};this.chess_biggest=function(a,b){a=Math.abs(a);b=Math.abs(b);return(a>b?a:b);};this.chess_smallest=function(a,b){a=Math.abs(a);b=Math.abs(b);return(a<b?a:b);};this.chess_moves_available=function(x,y,b_no_list){var pc=this.bitboard[x+y*8];var pc_c=pc&0x10;var pc_t=pc&0x0f;var all=[];if(pc_t==4){for(var dy=-1;dy<=1;dy+=2){for(var dx=-1;dx<=1;dx+=2){for(var dz=2;dz<=3;dz++){var x2=x+dx*(4-dz);var y2=y+dy*(dz-1);if(this.chess_can_move(x,y,x2,y2,pc_c)){if(b_no_list)return 1;all.push([x2,y2]);}}}}}else{for(var dy=-1;dy<=1;dy++){for(var dx=-1;dx<=1;dx++){if(dx==0&&dy==0)continue;var x2=x+dx;var y2=y+dy;while(x2>=0&&x2<8&&y2>=0&&y2<8){if(!this.chess_valid_move(x,y,x2,y2))break;if(this.chess_collision(x,y,x2,y2))break;var save_state=this.chess_save_state();this.chess_make_move(x,y,x2,y2,'q',1);var b_check=this.chess_in_check(pc_c);this.chess_restore_state(save_state);if(!b_check){if(b_no_list)return 1;all.push([x2,y2]);}x2+=dx;y2+=dy;}}}}if(b_no_list)return 0;return all;};this.match_absolute_pos=function(o){var o0=this.get_obj();if(!o0)return;o.style.top=gk_get_absolute_pos_top(o0)+'px';o.style.left=gk_get_absolute_pos_left(o0)+'px';o.style.height=o0.offsetHeight+'px';o.style.width=o0.offsetWidth+'px';};this.center_absolute_pos=function(o){var o0=this.board||this.get_obj();if(!o0)return;o.style.top=(gk_get_absolute_pos_top(o0)+Math.round((o0.offsetHeight-o.offsetHeight)/2))+'px';o.style.left=(gk_get_absolute_pos_left(o0)+Math.round((o0.offsetWidth-o.offsetWidth)/2))+'px';};this.redraw_all=function(){this.abort_mouse_move(0);this.refresh_cells=[];for(var x=0;x<8;x++)for(var y=0;y<8;y++)this.update_cell_image(x,y,0);this.highlight_last_move();if(this.b_show_captures){for(var x=this.cur_captive_w;x<this.max_captives;x++){this.update_captive_image(this.g_captives_w[x],0);}for(var x=this.cur_captive_b;x<this.max_captives;x++){this.update_captive_image(this.g_captives_b[x],0);}}this.update_current_move();this.update_score();if((typeof this.callback_update_move)=='function')this.callback_update_move();};this.hilite_move=function(move){if(!move){if(this.hilited_move){var mv=this.chess_decode_move(this.hilited_move);this.update_cell_image(mv[0],mv[1]);this.update_cell_image(mv[2],mv[3]);this.hilited_move=undefined;}}else{this.hilited_move=move;var mv=this.chess_decode_move(move);this.update_cell_image(mv[0],mv[1],2);this.update_cell_image(mv[2],mv[3],2);}};this.update_cell_cursor=function(x,y){var cell=this.g_board[x+y*8];if(!cell)return;cell.style.cursor=this.get_cell_cursor(x,y);};this.get_cell_cursor=function(x,y){return(this.b_allow_new_moves&&this.chess_can_start_move(x,y))?'pointer':'default';};this.update_cell_image=function(x,y,force_sel){var sel=force_sel;if(x<0||y<0)return;if(sel==undefined){if(this.sel_x>=0&&this.sel_y>=0){sel=(this.sel_x==x&&this.sel_y==y)?1:0;}else
if(this.current_move>0){var mv=this.chess_decode_move(this.chess_get_last_move());if(mv[0]==x&&mv[1]==y||mv[2]==x&&mv[3]==y)sel=1;}}var cell=this.g_board[x+y*8];this.update_tile_image(cell,x,y,sel,0);var _t=this;cell.onmouseover=function(){this.style.cursor=_t.get_cell_cursor(x,y);};};this.update_tile_image=function(el,x,y,sel,transp,override_pc,b_shadow){var ofs_cell=x+y*8;var pc=override_pc!=undefined?override_pc:this.bitboard[ofs_cell];var pc_t=pc&0x0f;var pc_c=pc&0x10;var cz=this.board_cell_size;var code=(pc&0x1f)+(sel?'s':'-')+(b_shadow?'s':'-');if(el.cur_code==code)return;el.cur_code=code;var ofs_y=0;var b_visible=1;if(pc_t>0&&pc_t<=6)ofs_y=(pc_t-1)*cz;else b_visible=0;var ofs_x=0;this.show_tile_shadow(el,b_shadow,ofs_x+2*cz,ofs_y);if(!pc_c)ofs_x+=cz;var class_name=this.id+'-piece';if(sel==1)class_name=this.id+((this.current_move==this.red_move_num)?'-piece_red':'-piece_sel');if(sel==2)class_name=this.id+'-piece_hilite';if(!transp)this.set_square_background(el,x,y,sel,1);if(el.cur_class!=class_name){el.className=class_name;el.cur_class=class_name;}var tile=el.firstChild;if(b_visible){tile.style.left=-1*ofs_x+'px';tile.style.top=-1*ofs_y+'px';tile.style.visibility='visible';}else{tile.style.visibility='hidden';}};this.show_tile_shadow=function(el,b_show,ofs_x,ofs_y){var sh=el.ichess_tile_shadow;if(!b_show){if(!sh)return;sh.style.visibility='hidden';sh.style.display='none';return;}if(!sh){var shadow_offset=Math.floor(this.board_cell_size/20);sh=document.createElement('div');sh.className=this.id+'-piece';sh.style.left=shadow_offset+'px';sh.style.top=shadow_offset+'px';sh.style.background='transparent';sh.style.zIndex=-3;sh.appendChild(this.get_chess_set_img());el.appendChild(sh);el.ichess_tile_shadow=sh;}var ts=sh.firstChild.style;ts.left=-1*ofs_x+'px';ts.top=-1*ofs_y+'px';ts.visibility='visible';sh.style.display='';sh.style.visibility='visible';};this.update_piece_image=function(el,pc,b_transp){var pc_t=pc&0x0f;var pc_c=pc&0x10;var cz=this.board_cell_size;var code=(pc&0x1f)+'-'+(b_transp?'s':'-');if(el.cur_code==code)return;el.cur_code=code;var b_visible=1;var ofs_y=0;if(pc_t>0&&pc_t<=6)ofs_y=(pc_t-1)*cz;else b_visible=0;var ofs_x=0;this.show_tile_shadow(el,b_transp,ofs_x+2*cz,ofs_y);if(!pc_c)ofs_x+=cz;var tile=el.firstChild;if(!tile)return;if(b_visible){tile.style.left=-1*ofs_x+'px';tile.style.top=-1*ofs_y+'px';tile.style.visibility='visible';}else{tile.style.visibility='hidden';}};this.update_captive_image=function(el,pc){var pc_t=pc&0x0f;var pc_c=pc&0x10;var cz=this.captive_cell_size;var b_visible=1;var ofs_y=0;if(pc_t>0&&pc_t<=6)ofs_y=(pc_t-1)*cz;else b_visible=0;var ofs_x=0;if(!pc_c)ofs_x+=cz;var tile=el.firstChild;if(b_visible){tile.style.left=-1*ofs_x+'px';tile.style.top=-1*ofs_y+'px';tile.style.visibility='visible';}else{tile.style.visibility='hidden';}};this.init_piece_image=function(el,piece){var pc=document.createElement('div');pc.className=this.id+'-piece';pc.style.left='0px';pc.style.top='0px';pc.style.cursor='pointer';pc.appendChild(this.get_chess_set_img());el.appendChild(pc);this.update_piece_image(pc,piece);};this.chess_can_start_move=function(x,y){if(x<0||x>7||y<0||y>7)return 0;var pc=this.bitboard[x+y*8];if(!pc)return 0;if(this.b_freestyle)return 1;var plyr=(this.cur_to_move=='w')?0x10:0;if((pc&0x10)^ plyr)return 0;return 1;};this.update_refresh_cells=function(){while(this.refresh_cells.length>0){var a=this.refresh_cells.pop();this.update_cell_image(a[0],a[1]);}};this.show_canmoves_xy=function(x,y,b_no_reset){if(!b_no_reset)this.reset_canmoves();var all=this.chess_moves_available(x,y);for(var i=0;i<all.length;i++){var p=all[i];this.canmoves.push(new this.create_canmove(this,p[0],p[1]));}};this.mouse_drag_time=0;this.mouse_drag_x=0;this.mouse_drag_y=0;this.mouse_drag_capture=null;this.on_mouse_down=function(event){if(this.b_printable_mode)return false;this.mouse_drag_time=get_time();this.mouse_drag_x=gk_mouse_x(event);this.mouse_drag_y=gk_mouse_y(event);var x=this.screen_to_cell_x(this.mouse_drag_x);var y=this.screen_to_cell_y(this.mouse_drag_y);if((typeof this.callback_mouse_click)=='function'){var b_skip=this.callback_mouse_click(x,y);if((typeof b_skip)!='undefined'&&b_skip)return false;}if(this.mouse_drag_capture){this.mouse_drag_capture.release_mouse_capture();this.mouse_drag_capture=null;this.mouse_move_complete(x,y);}else{this.mouse_drag_start(x,y);}gk_stop_event(event);return false;};this.mouse_drag_in_piece=function(mouse_x,mouse_y,pc){this.mouse_drag_x=mouse_x;this.mouse_drag_y=mouse_y;this.abort_mouse_move(0);this.mouse_drag_time=get_time();this.mouse_drag_mover_init(8,8,pc);this.sel_x=8;this.sel_y=8;this.sel_pc=pc;};this.on_mouse_drag=function(event){if(!this.mouse_drag_capture)return false;var m_x=gk_mouse_x(event);var m_y=gk_mouse_y(event);this.display_pc_mover(m_x,m_y);gk_stop_event(event);return false;};this.on_mouse_drag_stop=function(event){if(!this.mouse_drag_capture)return false;var dt=get_time()-this.mouse_drag_time;this.mouse_drag_time=0;var b_drag_drop=dt>300?1:0;var m_x=gk_mouse_x(event);var m_y=gk_mouse_y(event);if(!b_drag_drop){if(this.sel_x>=0&&this.sel_y>=0&&(this.sel_x!=x||this.sel_y!=y)){var dist2=(this.mouse_drag_x-m_x)*(this.mouse_drag_x-m_x)+(this.mouse_drag_y-m_y)*(this.mouse_drag_y-m_y);if(dist2>100)b_drag_drop=1;}}if(b_drag_drop){var x=this.screen_to_cell_x(m_x);var y=this.screen_to_cell_y(m_y);this.mouse_drag_capture.release_mouse_capture();this.mouse_drag_capture=null;this.mouse_move_complete(x,y);}gk_stop_event(event);return false;};this.mouse_drag_start=function(x,y){if(x<0||x>7||y<0||y>7){this.abort_mouse_move(1);return;}if(this.b_show_legal_moves&&!this.b_freestyle){this.show_canmoves_xy(x,y);}if(this.b_allow_new_moves&&this.chess_can_start_move(x,y)){var pc=this.bitboard[x+y*8];if(!this.mouse_drag_mover_init(x,y))return;this.sel_x=x;this.sel_y=y;this.sel_pc=pc;this.bitboard[this.sel_x+this.sel_y*8]=0;this.update_cell_image(this.sel_x,this.sel_y,this.b_freestyle?0:1);this.bitboard[this.sel_x+this.sel_y*8]=pc;}};this.mouse_drag_mover_init=function(x,y,pc){this.update_refresh_cells();var o=this.get_free_mover();if(!o)return false;this.mouse_move=1;if(!pc)this.update_tile_image(o,x,y,0,1,undefined,1);else this.update_piece_image(o,pc,1);o.style.background='transparent';o.style.zIndex=40;this.display_pc_mover(this.mouse_drag_x,this.mouse_drag_y);o.style.visibility='visible';o.style.display='';o.onmouseup=this.get_mouse_drag_stop_handler();o.onmousemove=this.get_mouse_drag_handler();this.mouse_drag_capture=new gk_create_mouse_capture(o);this.mouse_drag_capture.b_autorelease=0;return true;};this.mouse_move_complete=function(x,y){if(x<0||x>7||y<0||y>7){if(this.b_freestyle&&this.sel_pc&&(this.sel_pc&0x0f)!=1){if(this.sel_x>=0&&this.sel_y>=0&&this.sel_x<8&&this.sel_y<8){this.bitboard[this.sel_x+this.sel_y*8]=0;}this.end_mouse_move();}else{this.abort_mouse_move(1);}return;}var b_cm=1;if(this.option_legal_moves()&&!this.b_freestyle)b_cm=this.chess_can_move(this.sel_x,this.sel_y,x,y,this.cur_to_move=='w'?0x10:0);var b_promo=this.is_pawn_promo(this.sel_pc,y)?1:0;if(!b_promo&&b_cm&&((typeof this.callback_validate_move)=='function'))b_cm=this.callback_validate_move(this.sel_x,this.sel_y,x,y);if(!b_cm||(this.sel_x==x&&this.sel_y==y)){this.abort_mouse_move(1);return;}if(this.b_freestyle){if((this.bitboard[x+y*8]&0x0f)==1){this.abort_mouse_move(0);return;}var pc_t=this.sel_pc&0x0f;if(pc_t==6&&(y==0||y==7)){this.abort_mouse_move(0);return;}var pc_c=this.sel_pc&0x10;this.sel_pc=pc_c|pc_t;if(pc_t==6){if((y==1&&pc_c)||(y==6&&!pc_c)){this.sel_pc|=0x20;}}if(this.sel_x>=0&&this.sel_y>=0&&this.sel_x<8&&this.sel_y<8){this.bitboard[this.sel_x+this.sel_y*8]=0;}this.bitboard[x+y*8]=this.sel_pc;if(pc_t==1){if(pc_c)this.king_w=[x,y];else this.king_b=[x,y];}this.update_cell_image(x,y,0);this.end_mouse_move();}else{if(b_promo){this.show_promo_screen(this.sel_pc&0x10,this.sel_x,this.sel_y,x,y);}else{this.record_move(this.sel_x,this.sel_y,x,y);}this.end_mouse_move();}};this.submit_move=function(move){var mv=this.chess_decode_move(move);var target_pc=this.bitboard[mv[2]+8*mv[3]];this.record_move(mv[0],mv[1],mv[2],mv[3],mv[4]);this.movers.push(new this.create_mover(this,mv[0],mv[1],mv[2],mv[3],target_pc));this.start_movers();};this.is_pawn_promo=function(pc,y){if((pc&0x0f)!=6)return 0;var pc_c=pc&0x10;if(pc_c&&y==7)return 1;if(!pc_c&&y==0)return 1;return 0;};this.is_pawn_promo_move=function(x1,y1,x2,y2){return this.is_pawn_promo(this.bitboard[x1+y1*8],y2);};this.is_pawn_promo_move_coords=function(coords){var a=this.chess_decode_move(coords);return this.is_pawn_promo_move(a[0],a[1],a[2],a[3]);};this.display_pc_mover=function(x,y){var o=this.get_free_mover();if(!o)return;x=Math.floor(x-this.board_cell_size/2);y=Math.floor(y-this.board_cell_size/2);this.free_mover_x=x;this.free_mover_y=y;o.style.left=x+'px';o.style.top=y+'px';};this.screen_to_cell_x=function(x){var board_x=gk_get_absolute_pos_left(this.board);x=Math.floor((x-board_x)/this.board_cell_size);return this.b_flip?7-x:x;};this.screen_to_cell_y=function(y){var board_y=gk_get_absolute_pos_top(this.board);y=Math.floor((y-board_y)/this.board_cell_size);return this.b_flip?y:7-y;};this.nextmove=function(){if(this.b_printable_mode)return;this.anim_move(this.current_move+1);};this.prevmove=function(){if(this.b_printable_mode)return;this.anim_move(this.current_move-1);};this.update_current_move=function(){if(!this.b_show_moves)return;this.update_move_cell(this.current_move);this.update_moves_scroll();};this.flip_normal=function(){if(this.start_plyr()=='w'){if(this.b_flip)this.flip();}else{if(!this.b_flip)this.flip();}};this.flip=function(){this.b_flip=!this.b_flip;for(var x=0;x<8;x++){for(var y=0;y<8;y++){var ps=this.g_board[x+y*8].style;ps.left=(this.b_flip?7-x:x)*this.board_cell_size+'px';ps.top=(this.b_flip?y:7-y)*this.board_cell_size+'px';}}if(this.b_show_captures){var c_w=this.get_obj('captives_w');var c_b=this.get_obj('captives_b');if(this.b_flip){c_w.parentNode.removeChild(c_w);c_b.parentNode.insertBefore(c_w,c_b);}else{c_b.parentNode.removeChild(c_b);c_w.parentNode.insertBefore(c_b,c_w);}}this.update_labels();if(this.b_update_moves_list_on_flip)this.update_moves_list();this.redraw_all();};this.highlight_last_move=function(){this.update_refresh_cells();if(this.current_move<=0)return;var mv=this.chess_decode_move(this.chess_get_last_move());this.update_cell_image(mv[0],mv[1],1);this.update_cell_image(mv[2],mv[3],1);this.refresh_cells.push([mv[0],mv[1]]);this.refresh_cells.push([mv[2],mv[3]]);};this.get_material_score=function(){var pc_scores=[0,0,9,5,3,3,1];var score=0;for(var i=0;i<64;i++){var pc=this.bitboard[i];if(!pc)continue;var v=pc_scores[pc&0x0f];if(!v)continue;if(!(pc&0x10))v=-v;score+=v;}return score;};this.update_score=function(){if(!this.b_show_captures)return;var score=this.get_material_score();if(this.b_flip)score=-score;if(score>0)score='+'+score;var o=this.get_obj('score');if(o)o.innerHTML=score;};this.scroll_to=function(elm,only_if_needed){if(!elm)return;var o=this.get_obj('moves');if(!o)return;var e0=elm.offsetTop;var scroll_to=e0;if(only_if_needed){var e1=e0+elm.offsetHeight;var s0=o.scrollTop;var s1=s0+o.clientHeight;if(e0<s0){scroll_to=e0;}else
if(e1>s1){scroll_to=s0+(e1-s1)+2;}else{return;}}o.scrollTop=scroll_to;};this.undo_last_move=function(num_moves){if(!num_moves)num_moves=1;this.board_snapshot();for(var i=0;i<num_moves;i++){if(this.max_move<=0)return;this.truncate_moves(this.max_move-1);}this.goto_move(this.max_move,1);this.board_snapshot_diff();this.redraw_all();};this.truncate_moves=function(move_num){var old_max_move=this.max_move;if(this.current_move>move_num)this.current_move=move_num;if(this.moves.length>move_num){this.moves.splice(move_num,this.moves.length-move_num);}this.max_move=this.moves.length;return old_max_move>this.max_move?1:0;};this.truncate_after_move=function(move_num){if(move_num==undefined)var move_num=this.current_move;if(move_num>=this.max_move)return;if(move_num<0)move_num=0;this.board_snapshot();this.truncate_moves(move_num);this.goto_move(this.max_move,1);this.board_snapshot_diff();this.redraw_all(move_num);};this.record_move=function(from_x,from_y,to_x,to_y,promo){var from_l=this.encode_pos(from_x,from_y);var to_l=this.encode_pos(to_x,to_y);var b_truncated=this.truncate_moves(this.current_move);var promo=this.validate_promo(promo);this.moves.push(from_l+to_l+promo);this.max_move=this.moves.length;this.goto_move(this.max_move,1,!b_truncated);if((typeof this.callback_record_move)=='function')this.callback_record_move();this.redraw_all();};this.chess_checkmate=function(plyr){if(!this.chess_in_check(plyr))return 0;for(var x=0;x<8;x++){for(var y=0;y<8;y++){var pc=this.bitboard[x+y*8];if(!pc)continue;if((pc&0x10)^ plyr)continue;if(this.chess_moves_available(x,y,1))return 0;}}return 1;};this.chess_stalemate=function(plyr){if(this.chess_in_check(plyr))return 0;for(var x=0;x<8;x++)for(var y=0;y<8;y++){var pc=this.bitboard[x+y*8];if(!pc)continue;if((pc&0x10)^ plyr)continue;if(this.chess_moves_available(x,y,1))return 0;}return 1;};this.reset_all=function(_initial_move,b_no_movers){if(_initial_move!=undefined)this.initial_move=_initial_move;if(!b_no_movers)this.board_snapshot();this.abort_mouse_move(0);this.update_move_cell(this.current_move);this.reset_board();if(!b_no_movers)this.board_snapshot_diff();this.redraw_all();this.update_to_move();};this.board_snapshot=function(){if(!this.force_animation_time&&(this.animation_speed<1||this.animation_speed>=7)){this.snapshot_bitboard=undefined;return;}this.snapshot_bitboard=this.bitboard.slice();};this.board_snapshot_diff=function(){this.reset_movers();if(!this.force_animation_time&&(this.animation_speed<1||this.animation_speed>=7)){this.snapshot_bitboard=undefined;return;}if(this.snapshot_bitboard==undefined)return;var used_mover=[];var diffs=0;for(var x0=0;x0<8&&diffs<8;x0++)for(var y0=0;y0<8&&diffs<8;y0++){var ofs0=x0+y0*8;var pc0=this.bitboard[ofs0]&0x1f;if(!pc0)continue;var pc1=this.snapshot_bitboard[ofs0]&0x1f;if(pc0==pc1)continue;var min_dist=999;var x2=-1,y2=-1;for(var x1=0;x1<8;x1++)for(var y1=0;y1<8;y1++){var ofs1=x1+y1*8;if(used_mover[ofs1])continue;var dist=(x0-x1)*(x0-x1)+(y0-y1)*(y0-y1);if(dist>min_dist)continue;pc1=this.snapshot_bitboard[ofs1]&0x1f;if(pc0!=pc1)continue;if((this.bitboard[ofs1]&0x1f)==pc1)continue;min_dist=dist;x2=x1;y2=y1;}if(x2<0||y2<0)continue;used_mover[x2+y2*8]=1;this.movers.push(new this.create_mover(this,x2,y2,x0,y0,this.snapshot_bitboard[ofs0]));diffs++;}this.snapshot_bitboard=undefined;if(diffs>0)this.start_movers();};this.position_on_board=function(o,x,y,ofs){if(ofs)ofs=(this.board_cell_size-ofs)/2;else ofs=0;x=(this.b_flip?7-x:x)*this.board_cell_size+ofs;y=(this.b_flip?y:7-y)*this.board_cell_size+ofs;o.style.left=Math.floor(x)+'px';o.style.top=Math.floor(y)+'px';};this.create_mover=function(_ichess,from_x,from_y,to_x,to_y,old_pc){this.ichess=_ichess;this.update=function(){var dt=(get_time()-this.start_time)/this.total_time;if(dt<0)dt=0;if(dt>1){this.done=1;return;}var x=this.from_x+dt*(this.to_x-this.from_x);var y=this.from_y+dt*(this.to_y-this.from_y);this.ichess.position_on_board(this.div,x,y);};this.destroy=function(){if(this.target_div){this.target_div.ichess_free_mover_done=1;this.board.removeChild(this.target_div);this.target_div=null;}if(this.div){this.div.ichess_free_mover_done=1;this.board.removeChild(this.div);this.div=null;}};this.from_x=from_x;this.from_y=from_y;this.to_x=to_x;this.to_y=to_y;this.total_time=this.ichess.get_animation_speed_time()*Math.sqrt(ichess_get_dist(from_x,from_y,to_x,to_y));if(this.total_time<1)this.total_time=1;this.start_time=get_time();this.done=0;this.board=this.ichess.board;var o=_ichess.allocate_mover_div(to_x,to_y,1,undefined,20);this.div=o;this.update();o.style.visibility='visible';o=_ichess.allocate_mover_div(to_x,to_y,0,old_pc,10);this.target_div=o;o.style.visibility='visible';};this.create_mover_free=function(_ichess,from_x,from_y,to_sq_x,to_sq_y,pc){this.ichess=_ichess;this.update=function(){var dt=(get_time()-this.start_time)/this.total_time;if(dt<0)dt=0;if(dt>1){this.done=1;return;}dt=Math.sqrt(dt);var x=this.from_x+dt*(this.to_x-this.from_x);var y=this.from_y+dt*(this.to_y-this.from_y);if(x<=0||isNaN(x))x=0;if(y<=0||isNaN(y))y=0;this.div.style.left=Math.floor(x)+'px';this.div.style.top=Math.floor(y)+'px';};this.destroy=function(){if(this.target_div){this.target_div.ichess_free_mover_done=1;this.board.removeChild(this.target_div);this.target_div=null;}if(this.div){this.div.ichess_free_mover_done=1;this.board.removeChild(this.div);this.div=null;}};this.from_x=from_x;this.from_y=from_y;this.to_x=(_ichess.b_flip?7-to_sq_x:to_sq_x)*_ichess.board_cell_size;this.to_y=(_ichess.b_flip?to_sq_y:7-to_sq_y)*_ichess.board_cell_size;this.total_time=Math.min(300,20*Math.sqrt(ichess_get_dist(this.from_x,this.from_y,this.to_x,this.to_y)));if(this.total_time<1)this.total_time=1;this.start_time=get_time();this.done=0;this.board=this.ichess.board;var o=_ichess.allocate_mover_div(0,0,1,pc,20);this.div=o;this.update();o.style.visibility='visible';o=_ichess.allocate_mover_div(to_sq_x,to_sq_y,0,'--',10);this.target_div=o;o.style.visibility='visible';};this.allocate_mover_div=function(x,y,b_transp,pc,zIndex){if(!this.mover_divs)this.mover_divs=[];var o=null;for(var i=0;i<this.mover_divs.length;i++){o=this.mover_divs[i];if(o.ichess_free_mover_done)break;o=null;}if(!o){o=document.createElement('div');o.appendChild(this.get_chess_set_img());if(this.mover_divs.length<100){this.mover_divs.push(o);}}o.style.visibility='hidden';o.ichess_free_mover_done=0;o.style.zIndex=zIndex;this.update_tile_image(o,x,y,0,b_transp,pc);if(b_transp)o.style.background='transparent';else this.set_square_background(o,x,y);this.position_on_board(o,x,y);this.board.appendChild(o);o.style.visibility='visible';return o;};this.reset_movers=function(){for(var i=0;i<this.movers.length;i++){this.movers[i].destroy();}this.movers=[];};this.create_canmove=function(_ichess,x,y){this.ichess=_ichess;this.destroy=function(){this.board.removeChild(this.div);};this.x=x;this.y=y;if(!this.ichess.canmove_img){this.ichess.canmove_img=new Image(8,8);fix_alpha(this.ichess.canmove_img,this.ichess.static_prefix+'/img/canmove.png');var os=this.ichess.canmove_img.style;os.width='8px';os.height='8px';os.padding='0px';os.margin='0px';os.display='block';}this.board=this.ichess.board;var o=document.createElement('div');o.style.visibility='hidden';o.style.background='transparent';o.style.zIndex=15;o.appendChild(this.ichess.canmove_img.cloneNode(true));o.style.position='absolute';this.div=o;this.ichess.position_on_board(o,x,y,8);this.board.appendChild(o);o.style.visibility='visible';};this.reset_canmoves=function(){for(var i=0;i<this.canmoves.length;i++){this.canmoves[i].destroy();}this.canmoves=[];};this.update_movers=function(){var further_movers=[];for(var i=0;i<this.movers.length;i++){var m=this.movers[i];m.update();if(m.done){m.destroy();}else{further_movers.push(m);}}this.movers=further_movers;if(this.movers.length<=0){clearInterval(this.mover_timer);this.mover_timer=0;}};this.start_movers=function(){if(this.mover_timer)return;this.mover_timer=setInterval("ichess_handler_update_movers('"+this.id+"')",this.mover_timeout);};this.movers_active=function(){if(!this.mover_timer)return 0;if(this.movers.length<=0)return 0;return 1;};this.start_new_mover=function(x1,y1,x2,y2){this.movers.push(new this.create_mover(this,x1,y1,x2,y2,this.bitboard[x1+y1*8]));this.start_movers();};this.update_options_cookie=function(){gk_glop[1]=this.b_legal_moves?1:0;gk_glop[2]=this.b_short_notation?0:1;gk_glop[3]=this.animation_speed;gk_glop[4]=this.b_show_legal_moves?1:0;gk_glop[8]=this.b_movelist_text?1:0;gk_glop[9]=this.graphics_style-1;gk_glop[10]=this.graphics_size-1;gk_glop[18]=this.b_movelist_fig?1:0;save_global_options();};this.get_animation_speed_time=function(){if(this.force_animation_time>0)return this.force_animation_time;if(this.animation_speed<1)this.animation_speed=4;if(this.animation_speed>7)this.animation_speed=7;return(8-this.animation_speed)*(8-this.animation_speed)*11;};this.update_board_preview=function(){var o=this.get_obj('frm_board_preview');if(!o)return;var dn=this.id+'-';var size=gui_get_selected_option_value(dn+'frm_board_size',4);if(size<3)size=3;if(size>5)size=5;var sz=size*10;var es=o.style;es.position='relative';es.width=3*sz+'px';es.height=2*sz+'px';var c_idx=gui_get_selected_option_value(dn+'frm_board_color',0);c_idx*=5;var color_bsq='#'+this.bd_colors[c_idx];var color_wsq='#'+this.bd_colors[c_idx+1];var color_bdb='#'+this.bd_colors[c_idx+2];var bdbg_img=this.bd_colors[c_idx+3];var bdbg_tile=this.bd_colors[c_idx+4];es.border='2px solid '+color_bdb;var s_idx=gui_get_selected_option_value(dn+'frm_board_style',1);var chess_set_img=new Image(sz*3,sz*6);fix_alpha(chess_set_img,this.static_prefix+'/img/chess'+s_idx+size+'.png');es=chess_set_img.style;es.position='absolute';es.border='0px';es.width=3*sz+'px';es.height=6*sz+'px';var r=new prnd(1);var dd=Math.floor((bdbg_tile?2.5:1.5)*sz);var all=o.getElementsByTagName('DIV');for(var i=0;i<all.length;i++){var el=all[i];es=el.style;es.width=sz+'px';es.height=sz+'px';es.position='absolute';es.overflow='hidden';es.left=(i%3)*sz+'px';es.top=Math.floor(i/3)*sz+'px';var col=i&1?'b':'w';if(!bdbg_img){es.background=col=='w'?color_wsq:color_bsq;}else{var bx=r.rnd(dd);var by=r.rnd(dd);el.style.background='url('+this.static_prefix+'/img/chess-bg'+col+bdbg_img+size+'.jpg) repeat -'+bx+'px -'+by+'px';}var img=chess_set_img.cloneNode(true);if(!el.firstChild)el.appendChild(img);else el.replaceChild(img,el.firstChild);es=img.style;es.left=-1*((1-Math.floor(i/3))*sz)+'px';es.top=-1*((5-i)*sz)+'px';}};this.show_options=function(el){load_global_options();var dn=this.id+'-';var extra_sel='class=ftx onChange="ichess_call_handler(this,\'update_board_preview\');"';var all_size_options=['Tiny',2,'Small',3,'Medium',4,'Intermediate',5,'Large',6,'Very Large',8];var size_options=gui_generate_select(dn+'frm_board_size',all_size_options,this.graphics_size,extra_sel);var all_style_options=['GameKnot',1,'3D',2,'Merida',3,'Modern',4,'Alpha',5,'Leipzig',6,'Cases',7,'Condal',8,'Maya',9,'Wooden',10];var style_options=gui_generate_select(dn+'frm_board_style',all_style_options,this.graphics_style,extra_sel);var all_color_options=['Classic',0,'Storm blue',1,'Ocean blue',2,'Sea foam',3,'Emerald green',4,'Moss green',5,'Brick red',6,'Amethyst',7,'Grape',8,'Sand',9,'Pumpkin',11,'TX: Cedar',12,'TX: Redwood',13,'TX: Walnut',14,'TX: Elm',15,'TX: Green Marble',16,'TX: Blue Marble',17,'TX: Tan Marble',18,'TX: Granite',19,'TX: Rusted Metal',20];var color_options=gui_generate_select(dn+'frm_board_color',all_color_options,gk_get_glop(17,0),extra_sel);var txt='<table border=0 cellspacing=5 cellpadding=0 style="margin: 2px;"><tr><td align="right"><INPUT TYPE="checkbox" id="'+dn+'frm_legal_moves" VALUE="1" DISABLED></td><td align="left">- allow legal moves only</td></tr><tr><td align="right"><INPUT TYPE="checkbox" id="'+dn+'frm_short_notation" VALUE="1" DISABLED></td><td align="left">- use short algebraic notation</td></tr><tr><td align="center" nowrap colspan=2>movement/animation speed:<br>(slow) <INPUT TYPE="radio" name="anim_speed" id="'+dn+'frm_anim_speed1" VALUE="1"><INPUT TYPE="radio" name="anim_speed" id="'+dn+'frm_anim_speed2" VALUE="2"><INPUT TYPE="radio" name="anim_speed" id="'+dn+'frm_anim_speed3" VALUE="3"><INPUT TYPE="radio" name="anim_speed" id="'+dn+'frm_anim_speed4" VALUE="4"><INPUT TYPE="radio" name="anim_speed" id="'+dn+'frm_anim_speed5" VALUE="5"><INPUT TYPE="radio" name="anim_speed" id="'+dn+'frm_anim_speed6" VALUE="6"><INPUT TYPE="radio" name="anim_speed" id="'+dn+'frm_anim_speed7" VALUE="7"> (fast)</td></tr><tr><td align="right"><INPUT TYPE="checkbox" id="'+dn+'frm_show_legal" VALUE="1"></td><td align="left">- show available legal moves</td></tr><tr><td align="right"><INPUT TYPE="checkbox" id="'+dn+'frm_movelist_table" VALUE="1"></td><td align="left">- show the list of moves as a table</td></tr><tr><td align="right"><INPUT TYPE="checkbox" id="'+dn+'frm_movelist_fig" VALUE="1"></td><td align="left">- use pictograms in the list of moves</td></tr><tr><td colspan=2><table border=0 cellspacing=5 cellpadding=0 style="text-align: center; vertical-align: middle;"><tr><td>Board size: '+size_options+'</td><td rowspan=3 style="padding-left: 4px;"><div id="'+dn+'frm_board_preview"><div></div><div></div><div></div><div></div><div></div><div></div></div></td></tr><tr><td>Chess set: '+style_options+'</td></tr><tr><td>Board: '+color_options+'</td></tr></table></td></tr><tr><td colspan=2 style="text-align: center; padding: 6px;"><INPUT TYPE="button" VALUE="  Submit  " onClick="ichess_call_handler(this,\'submit_options\');"> <INPUT TYPE="button" VALUE="Cancel" onClick="ichess_call_handler(this,\'cancel_options\');"></td></tr></table>';var menu_div=gui_pop_form(txt,el,{'dir':0,'align':1,'header':'Chess board settings:'});menu_div.ichess=this;var o=this.get_obj('frm_legal_moves');if(o){o.checked=this.option_legal_moves()?1:0;o.disabled=this.b_force_legal_moves?1:0;}o=this.get_obj('frm_short_notation');if(o){o.checked=this.option_short_notation()?1:0;o.disabled=this.b_force_short_notation?1:0;}for(var i=0;i<10;i++){o=this.get_obj('frm_anim_speed'+i);if(!o)continue;o.checked=(this.animation_speed==o.value)?1:0;}o=this.get_obj('frm_show_legal');if(o)o.checked=this.b_show_legal_moves?1:0;o=this.get_obj('frm_movelist_table');if(o)o.checked=this.b_movelist_text?0:1;o=this.get_obj('frm_movelist_fig');if(o)o.checked=this.b_movelist_fig?1:0;this.update_board_preview();};this.submit_options=function(){var o=this.get_obj('frm_legal_moves');if(o&&!this.b_force_legal_moves){this.b_legal_moves=o.checked?1:0;}o=this.get_obj('frm_short_notation');if(o&&!this.b_force_short_notation){this.b_short_notation=o.checked?1:0;}for(var i=0;i<10;i++){o=this.get_obj('frm_anim_speed'+i);if(!o||!o.checked)continue;this.animation_speed=to_int(o.value);break;}o=this.get_obj('frm_show_legal');if(o)this.b_show_legal_moves=o.checked?1:0;o=this.get_obj('frm_movelist_table');if(o)this.b_movelist_text=o.checked?0:1;o=this.get_obj('frm_movelist_fig');if(o)this.b_movelist_fig=o.checked?1:0;var dn=this.id+'-';var new_color=gui_get_selected_option_value(dn+'frm_board_color',0);if(gk_get_glop(17,0)!=new_color){gk_glop[17]=new_color;this.init_board_color(new_color);for(var x=0;x<8;x++){for(var y=0;y<8;y++){var pc=this.g_board[x+y*8];this.set_square_background(pc,x,y);}}}var new_style=gui_get_selected_option_value(dn+'frm_board_style',1);var new_size=gui_get_selected_option_value(dn+'frm_board_size',4);if(new_style!=this.graphics_style||new_size!=this.graphics_size){this.reset_graphics();this.init_graphics_set(new_style,10*new_size);if(this.b_reload_on_style_change){this.update_options_cookie();location.reload();}else{this.generate_all();}}this.update_options_cookie();this.cancel_options();this.refresh_board();};this.cancel_options=function(){gui_pop_form_remove();};this.show_promo_screen=function(col,from_x,from_y,to_x,to_y){this.promo_move_coords={x1:from_x,y1:from_y,x2:to_x,y2:to_y};var dn=this.id+'-';gui_show_fullpage_form('<table class=dialog style="margin: 10px;"><tr><td colspan=2><em>Promote pawn to:</em><div class=divider></div></td></tr><tr><td onClick="ichess_handler_submit_promo_screen(\''+this.id+'\',2);"><div id="'+dn+'promo_2" class="'+dn+'promo_piece"></div></td><td>- Queen</td></tr><tr><td onClick="ichess_handler_submit_promo_screen(\''+this.id+'\',3);"><div id="'+dn+'promo_3" class="'+dn+'promo_piece"></div></td><td>- Rook</td></tr><tr><td onClick="ichess_handler_submit_promo_screen(\''+this.id+'\',4);"><div id="'+dn+'promo_4" class="'+dn+'promo_piece"></div></td><td>- Knight</td></tr><tr><td onClick="ichess_handler_submit_promo_screen(\''+this.id+'\',5);"><div id="'+dn+'promo_5" class="'+dn+'promo_piece"></div></td><td>- Bishop</td></tr></table>');var pc;for(var i=2;i<=5;i++){var tc=this.get_obj('promo_'+i);if(!tc)continue;if(!tc.firstChild){pc=document.createElement('div');pc.className=this.id+'-piece';pc.style.left='0px';pc.style.top='0px';pc.style.cursor='pointer';pc.appendChild(this.get_chess_set_img());tc.appendChild(pc);}else{pc=tc.firstChild;}this.update_piece_image(pc,col|i);}};this.submit_promo_screen=function(pc){gui_hide_fullpage_form();var promo=this.code2piece[pc&0x0f];var b_cm=1;if((typeof this.callback_validate_move)=='function'){b_cm=this.callback_validate_move(this.promo_move_coords.x1,this.promo_move_coords.y1,this.promo_move_coords.x2,this.promo_move_coords.y2,promo);}if(b_cm){this.record_move(this.promo_move_coords.x1,this.promo_move_coords.y1,this.promo_move_coords.x2,this.promo_move_coords.y2,promo);}else{this.update_cell_image(this.promo_move_coords.x1,this.promo_move_coords.y1,0);}this.promo_move_coords=null;};this.show_printable=function(){this.abort_mouse_move(0);var o=this.get_obj('bottom-area');if(!o)return;o.style.display='none';o.style.visibility='hidden';var new_o=document.createElement('div');new_o.id='menu_bottom_printable';new_o.style.padding='5px';o.parentNode.insertBefore(new_o,o);new_o.innerHTML='<div style="text-align: center; padding: 20px;"><input type="button" value=" Print " onClick="window.print();"> <input type="button" value="Cancel" onClick="ichess_call_handler(this,\'hide_printable\');"></div>';this.b_printable_mode=1;this.update_moves_list_printable();for(var x=0;x<8;x++){for(var y=0;y<8;y++){var p=this.g_board[x+y*8];p.style.visibility='hidden';p.firstChild.style.visibility='hidden';p.cur_code='';}}o=this.get_obj('acboard');if(o){var bdsz=this.board_cell_size*8;var img=new Image(bdsz,bdsz);img.onabort=handler_on_image_abort;img.id='img-printable-board';var fen=this.encode_fen();var lm=this.chess_get_last_move().substr(0,4);img.src='/chess-diagram-img.pl?f='+escape(fen)+'&fb='+(this.b_flip?1:0)+'&lm='+lm+'&t='+to_int(this.graphics_style)+'&s='+to_int(this.graphics_size);var s=img.style;s.width=bdsz+'px';s.height=bdsz+'px';s.position='absolute';s.padding='0px';s.left='0px';s.top='0px';s.border='1px solid black';s.margin='-1px';o.appendChild(img);}o=document.body;o.className+=' printable';};this.hide_printable=function(){this.b_printable_mode=0;var o=gk_get_el('menu_bottom_printable');if(o)o.parentNode.removeChild(o);o=gk_get_el('moves_printable');if(o)o.parentNode.removeChild(o);o=gk_get_el('img-printable-board');if(o)o.parentNode.removeChild(o);for(var x=0;x<8;x++){for(var y=0;y<8;y++){var p=this.g_board[x+y*8];p.style.visibility='visible';}}o=document.body;o.className=String(o.className).replace(/\bprintable\b/,'');o=this.get_obj('bottom-area');if(o){o.style.display='';o.style.visibility='visible';}o=this.get_obj('moves');if(o){o.style.display='';o.style.visibility='visible';}this.update_moves_list();this.redraw_all();};this.refresh_board=function(){var old_current_move=this.current_move;this.goto_move(this.max_move,1);if(old_current_move!=this.max_move){this.goto_move(old_current_move);}this.redraw_all();};this.chess_get_last_move=function(){if(this.current_move<1)return '';return this.moves[this.current_move-1];};this.chess_get_move=function(num){return this.moves[num];};this.chess_get_last_move_notation=function(){if(this.current_move<1)return '';return this.moves_list[this.current_move-1];};this.chess_encode_move=function(x,y,x2,y2,promo){return this.encode_pos(x,y)+this.encode_pos(x2,y2)+(promo?promo:'-');};this.chess_decode_move=function(move){var l=move.length;return[l>0?move.charCodeAt(0)-97:-1,l>1?move.charCodeAt(1)-49:-1,l>2?move.charCodeAt(2)-97:-1,l>3?move.charCodeAt(3)-49:-1,l>4?move.substr(4,1):'-'];};var o=this.get_obj();if(o){o.ichess=this;}this.b_legal_moves=gk_get_glop(1,1);this.b_short_notation=!gk_get_glop(2,0);this.animation_speed=gk_get_glop(3,4);this.b_show_legal_moves=gk_get_glop(4,1);this.b_movelist_text=gk_get_glop(8,0);this.init_graphics_set(gk_get_glop(9,0)+1,(gk_get_glop(10,3)+1)*10);this.init_board_color(gk_get_glop(17,0));this.b_movelist_fig=gk_get_glop(18,0);}
