var bob;var movelist='';var current_node=0;function init_page(main_div_name,b_flip){if(current_node>0&&!movelist){movelist=build_movelist(current_node);}if(!main_div_name)var main_div_name='eco';bob=new ichess_create(12,main_div_name);bob.init_moves_and_promos(movelist);bob.init_options(0,0,0,0,1,1);bob.b_force_legal_moves=0;bob.b_force_short_notation=0;bob.b_freestyle=0;bob.b_allow_new_moves=1;bob.callback_get_buttons_below_board=function(){return '';};bob.callback_get_links_below_board=function(){return '';};bob.callback_validate_move=eco_callback_validate_move;bob.callback_record_move=eco_callback_record_move;bob.go(b_flip,-1);var o=bob.get_obj('below-board');if(!o)return;var info_width=bob.board_frame_size;o.innerHTML='';o.style.visibility='visible';o.style.display='';o.style.textAlign='left';o.style.width=info_width+'px';o.style.padding='5px';redraw_page();}function build_movelist(node_id){var all=[];while(node_id>0){var node=eco_node[node_id];var parent_id=node['from'];if((typeof parent_id)=='undefined')break;var parent_node=eco_node[parent_id];for(var mv in parent_node){if(mv=='from')continue;var node_link=parent_node[mv];if(node_link[1]==node_id){all.push(mv+'-');break;}}node_id=parent_id;}return all.reverse().join('');}function get_parent_link(){var node=eco_node[current_node];var parent_node=node['from'];if((typeof parent_node)=='undefined')return undefined;parent_node=eco_node[parent_node];for(var mv in parent_node){if(mv=='from')continue;var node_link=parent_node[mv];if(node_link[1]==current_node)return node_link;}return undefined;}function redraw_page(){var o=bob.get_obj('right_pane');if(!o)return;o.style.width='600px';o.style.textAlign='left';var cols=2;var t='<table border=0 cellspacing=1 cellpadding=3>';t+='<tr bgcolor="#666699"><td width=50><b class=sml>move</b></td><td><b class=sml>Opening / variation</b></td></tr>';var parent_link=get_parent_link();var parent_id=-1;if(parent_link){parent_id=parent_link[0];t+='<tr onMouseOver="this.style.backgroundColor=\'#404055\';" onMouseOut="this.style.backgroundColor=\'\';">';t+='<td><a href="javascript:void(0)" onClick="hilite_move();select_move_back();">&laquo; back</a></td>';t+='<td>'+get_eco_info(parent_id)+'</td>';t+='</tr>';}var node=eco_node[current_node];var all_moves=[];for(var mv in node){if(mv=='from')continue;all_moves.push({move:mv,link:node[mv]});}all_moves.sort(function(a,b){if(a.link[0]>b.link[0])return-1;if(a.link[0]<b.link[0])return 1;return 0;});for(var ii=0;ii<all_moves.length;ii++){var mv=all_moves[ii].move;var link=node[mv];var id=link[0];var move_notation=link[2];if(id<0||id>=eco_info.length){report_error('Unexpected eco id! id='+id+', max='+eco_info.length);break;}var lbl=(all_moves.length==1)&&(parent_id==id)?'...':get_eco_info(id);t+='<tr onMouseOver="hilite_move(\''+mv+'\');this.style.backgroundColor=\'#404055\';" onMouseOut="hilite_move();this.style.backgroundColor=\'\';">';t+='<td align=center><a href="javascript:void(0)" onClick="hilite_move();select_move(\''+mv+'\');">'+move_notation+'</a></td>';t+='<td>'+lbl+'</td>';t+='</tr>';}t+='</table>';o.innerHTML=t;redraw_movelist();}function redraw_movelist(){var o=bob.get_obj('below-board');if(!o)return;o.innerHTML='';var t='<div>';var t='';for(i=0;i<bob.moves_list.length;i++){if((i%2)==0){var mn=i/2+1;t+=mn+'. ';}t+=bob.moves_list[i]+' ';}t+='</div>';o.innerHTML=t;}function get_eco_info(id){var info=eco_info[id];var lbl=info[1]+' ('+info[0]+')';if(info[2].length>0)lbl+=', '+info[2];return lbl;}function hilite_move(move){bob.hilite_move(move);}function select_move(mv){bob.submit_move(mv);}function select_move_back(){var node=eco_node[current_node];var parent_node=node['from'];if((typeof parent_node)=='undefined')return;current_node=parent_node;bob.undo_last_move();redraw_page();}function pop_message(msg,duration){if(!duration)var duration=1000;gui_pop_message(msg,bob.get_obj('acboard'),duration);}function eco_callback_validate_move(x1,y1,x2,y2,promo){var move=bob.chess_encode_move(x1,y1,x2,y2,promo);move=move.substr(0,4);var node=eco_node[current_node];if((typeof node[move])!='undefined')return true;pop_message('No opening matching this move...',1000);return false;}function eco_callback_record_move(){var move=bob.chess_get_last_move();move=move.substr(0,4);var node=eco_node[current_node];current_node=node[move][1];redraw_page();}function report_error(msg){gk_log_error(msg);}
