Chess3= {};
Chess3.data=[];
Chess3.didx=0;

// optional, sometimes: ,monname:'mon1'; ,moncont:"<?=$PAGE_POS3?>"

Chess3.onew=function(name) {
 idx=Chess3.didx; // eq .length
 Chess3.data[idx]= {map:{},stack:{},parms:{},ops:{},vals:[],maplist:[]};
 Chess3.data[idx].stkidx= 0;
 Chess3.data[idx].store= [];
 n=name||'Chess3';
 Chess3.data[idx].name= n;
 Chess3.didx++;
 return idx;
}

Chess3.setdata= function(idx,name,ar) { // map, ops, stack
 Chess3.data[idx][name]=ar;
 // create maplist of tagIDs
 if(name=='map') for(p in ar) Chess3.data[idx].maplist.push(p);
}

Chess3.nextStep= function(idx) {
 if(!bInit) return;
 data= Chess3.data[idx];
 cidx= data.stkidx; //current cmd index
 if(data.parms['loop']==-1) {data.stkidx= 0; return;}
 if(cidx>=data.stack.length) {
  if(data.parms.loop==1) cidx= data.stkidx= 0;
  else if(!data.parms.loop) return;   
 }
 ar1= data.stack[cidx];
 tgtid=ar1[1]; opkey=ar1[0]; parm1=ar1[2]; parm2=ar1[3];// more ...
 subcmd= data.ops[opkey][0]; if(!subcmd) { alert("nextsttep for idx="+idx+" opkey="+opkey+" error in ops");
  return; }
 func= eval("Chess3."+subcmd); if(typeof func!='function') return; 
 res= func(idx,opkey,tgtid,parm1,parm2);
 //  data.stkidx++;  moved to individ commands
 setTimeout("Chess3.nextStep("+idx+");",5000);
 data.store[0]= "Chess3.nextStep("+idx+");"; data.store[1]=5000;
}

Chess3.start= function (idx) { if(!bInit) return; Chess3.data[idx].parms['loop']=1; Chess3.nextStep(idx);}
Chess3.stop= function (idx) { Chess3.data[idx].parms['loop']=0;}
Chess3.fullstop= function (idx) { Chess3.data[idx].parms['loop']=-1; Chess3.clear(idx);}

Chess3.clear= function (idx) {
 for(p in Chess3.data[idx]['map']) try {document.getElementById(p).innerHTML="";} catch(e) {}
}

Chess3.loadcell= function() {
 len= arguments.length;
 idx=arguments[0]; opkey=arguments[1]; tgtid=arguments[2];
 if(len>3) parm1=arguments[3];
 //  not needed here: ar1=Chess3.data[idx].ops[opkey]; 
 if(isNaN(tgtid)) toname=tgtid; else toname=Chess3.data[idx].maplist[tgtid-1];
 from=parm1; // index into vals 1-based
 len=Chess3.data[idx].vals.length;
 acell= Chess3.data[idx].vals[from-1];
 if(acell.length<2) return;
 var tclass=acell[1]; var cont= acell[2]; 
 //alert("loadCell1 entre, toidx="+toidx+", from="+from);
 L=getLayer(toname);  
 c=L.className; p=L.parentNode;
 try {L.className=tclass} catch(e) {alert("no prop in jcell1 for from="+from);}
 writeLayer(L,cont);
 data.stkidx++;
}

Chess3.loadseq= function() {
 len= arguments.length;
 idx=arguments[0]; opkey=arguments[1]; 
 //  not needed here: ar1=Chess3.data[idx].ops[opkey]; 
 stkidx= Chess3.data[idx].stkidx; 
 opar= Chess3.data[idx].stack[stkidx];
 seq1= Chess3.data[nidx].seq1;if(seq1==0) seq1= 1;
 tgtid= opar[seq1]; from= opar[seq1+1]; // index into vals 1-based
 if(isNaN(tgtid)) toname=tgtid; else toname=Chess3.data[idx].maplist[tgtid-1];
 len=Chess3.data[idx].vals.length;
 acell= Chess3.data[idx].vals[from-1];
 if(acell.length<2) return;
 var tclass=acell[1]; var cont= acell[2]; 
 //alert("loadCell1 entre, toidx="+toidx+", from="+from);
 L=getLayer(toname);  
 c=L.className; p=L.parentNode;
 try {L.className=tclass} catch(e) {alert("no prop in jcell1 for from="+from);}
 writeLayer(L,cont);
 seq1+=2;
 if(seq1>=opar.length) { Chess3.data[idx].stkidx++; seq1=0; }
 Chess3.data[nidx].seq1=seq1;

}

Chess3.swap=function() {
 // idx,opkey,tgtid,parm1,parm2);
 idx=arguments[0]; opkey=arguments[1]; to=arguments[2];from=arguments[3];
 list= Chess3.data[idx].maplist; 
 if(from<1) from=1; if(to<1) to=1; 
 fromid=list[from-1]; toid=list[to-1]; 
 L1= document.getElementById(fromid);  L2= document.getElementById(toid);
 var v1= L1.innerHTML; var v2= L2.innerHTML; 
 var c1= L1.getAttribute("class"); var c2= L2.getAttribute("class");
 // below is in cases when 
 //  var pr1= L1.parentNode; var pr2= L1.parentNode;
 // var c1= pr1.getAttribute("class"); var c2= pr2.getAttribute("class");
 // pr1.setAttribute("class",c2); pr2.setAttribute("class",c1);
 L1.setAttribute("class",c2); L2.setAttribute("class",c1);
 L1.innerHTML=v2; L2.innerHTML=v1; 
 data.stkidx++;
}

Chess3.callserv= function() {
 toidx=mySpace.args.shift(); 
}

Chess3.loadserv= function() {
 toidx=mySpace.args.shift(); 
 //alert("loadCell1 entre, toidx="+toidx+", from="+from);
 toname=mySpace.domblk+'_'+toidx;  
 L=getLayer(toname);  

 try {L.className=mySpace.jcells1[from][1];} catch(e) {alert("no prop in jcell1 for from="+from);}
 writeLayer(L,mySpace.jcells1[from][2]);
}

Chess3.loadmon= function() {
 mon1=getLayer(mySpace.monname);  
 writeLayer(mon1,mySpace.moncont);
}

upcell= function() { // update the tag of given id via a certain op 
 // arguments: idx,tgtID, opkey,parm1;
 subcmd='upcell'; cmd='GETME'; // make it a hash lookup 
 idx=arguments[0]; opkey=arguments[1]; tgtid= arguments[2]; parm1=arguments[3];
 parm2=upcell.arguments[4];
 // return if shit
 var jsidx= sajax.JSidx;
 // data prev saved
 // Chess3.ops= {  // op_key:func,opline core, count, status, parm1 
 ar1=Chess3.data[idx].ops[opkey]; 
 opline_core=ar1[1]; last_count=ar1[2];last_status=ar1[3];last_parm1=ar1[4];last_parm2=ar1[5];

 opline=opline_core; if(parm1) opline+=" "+parm1;if(parm2) opline+=" "+parm2;
 sajax.cmd_idx= sajax.findCmd(cmd); 
 if( sajax.cmd_idx==-1) {alert("Invalid cmd "+cmd); return }

 // save call data for a callback
 sar= window.JSobjects[jsidx]['args'];
 sar[0]= idx;  sar[1]= opkey; sar[2]= "chan="+tgtid; sar[3]= "parm1="+parm1; sar[4]= "parm2="+parm2;   

 // prepare a do_call: tgtid,subcmd,opline,count
 args= new Array(); // no need to pass cmd, it is passed via cmd_idx indirectly
 args[0]= subcmd; args[1]= opline; args[2]= last_count; 
 // hack
 sz="275|160";
 args[3]= sz; 

  //alert("upcell retrieved:last_count="+last_count)
 res= sajax.do_call(args);
 // save this call
 ar1=Chess3.data[idx].ops[opkey]; 
 ar1[4]=parm1; ar1[5]=parm2;
 // count and status are saved in do_call
 data.stkidx++;
}

Chess3.getme= function(dir,x) { //the client callback registered via load()
 // x: xmlhttp object
 oXML= x.xml; var cidx= oXML.cmd_idx; 
 if(dir=='out') {

 } else if(dir=='in') {
  var resp;
  if(x.status!=200) { resp= unescape(x.responseText); oXML.reportError(); return;}
  
  // normal 
   // target does not have to be in ajax args, since each call has a specific callback and the target can 
  //  be named right there
   var target; var sChan=oXML.findArg('chan'); if(!sChan) sChan= oXML.actions[cidx][4]; 
   target= document.getElementById(sChan);
   rh= {}; resp= x.responseText; mapQS(resp,rh);
   for(p in rh) { rh[p]= unescape(rh[p]); rh[p]=rh[p].replace(/\+/g, " ");}
   var msgst=rh['msgst']; var last_count=0; last_count= rh['last_count'];
   //alert("upcell: last_count from server:"+last_count);
   if(target.tagName == 'INPUT' ) {
    target.value= rh['atext'];
   } else {
    // set the bg ...
    s1=rh['atext']; s1=s1.replace(/\^/g,'&');
    //     alert(s1);
    var out=s1;
    target.innerHTML= out
   }
   // get the stud stashed in JSobjects
   jsidx= oXML.JSidx; ar1=window.JSobjects[jsidx]['args'];
   chsidx=ar1[0]; opkey= ar1[1];
   tgtid= sChan;  last_parm1= oXML.findArg('parm1'); //last_parm2= oXML.findArg('parm2');
   // now server returns last_count

   //update Chess3.ops as:  op_key:[func,opline core, count, status, parm1, ...]
   opar= Chess3.data[idx].ops[opkey];
   opar[2]=parseInt(last_count)+1;
   opar[3]=3; // status OK
   //alert("upcell: last_count saved:"+opar[3]);
   ar2= Chess3.data[chsidx].store;
   if(ar2[0]) {
    if(!(wait=ar2[1])) wait=5000;
    setTimeout(ar2[0],wait);
    Chess3.data[chsidx].store=[];
   }
  } // end 'in' action
}

