/* javascript : conc.js (Concentration game) 
   web page : concentration.html
   © Jean-Paul Davalan 23/09/2002
*/
var nbimges=8;

var Im = new Array();
for(var i=0;i<8;i++) {
  Im[i] = new Image; Im[i].src="images/pap0"+(i+1)+".jpg";
}
Im[8] = new Image; Im[8].src="images/masque.jpg";
Im[9] = new Image; Im[9].src="images/concentration.png";
Im[10] = new Image; Im[10].src="images/fermeup.png";
Im[11] = new Image; Im[11].src="images/fermedown.png";

var tbcorr = new Array(); /* tbcorr[k] = num image, k<36 num<18*/
var tbsol = new Array();  /* tbsol[k] = 0 ou 1, k<36 */
var dc = new Array();
var okjoue, second, id=null, tempsmis, stopchrono, idchrono;

function reset_init() {
  var i, j, c;
  clearTimeout(idchrono);
  //stopchrono = true;
  tempsmis=0;
  okjoue=1;
  second=0;
  nsol=0;
  for(i=0;i<16;i++){
    chg(i, nbimges);
    tbsol[i] = 0;
    tbcorr[i] = Math.floor(i/2);
  }
  for(i=0;i<15 ;i++) {
    j = i+1+Math.floor((15-i)*Math.random());
    c = tbcorr[i]; tbcorr[i]=tbcorr[j];tbcorr[j]=c;
  }
  document.frm.but.value="                 0 s";
  stopchrono = false; 
  //if(idchrono==null) 
  chrono();
}

function ecristable() {
  var i, j, u;
  document.write("<center><table bgcolor='#2e4537' border=0 cellspacing=0 cellpadding=0>\n");
  for(i=0;i<4;i++) {
    document.write("<tr><td>");
    for(j=0;j<4;j++) {
      u = 4*i+j;
      document.write('<a href="javascript:effectue('+u+')"><img name="c'+u+'" src="images/masque.jpg" border=0 width=120 height=120 alt=""></a>');
    }
    document.write("</td></tr>");
  }
  document.write("</table></center>\n");
}

function redraw() {
  for(var i=0;i<16;i++)
    if(tbsol[i]==0) chg(i, 8);
    else chg(i, tbcorr[i]);
}

function effectue(x) {
   var a, b, s, n;
   dc[second] = x;
   if(okjoue==1 && tbsol[x]==0 &&( second==0 || dc[0]!=x)) {
       chg(x, tbcorr[x]);
       if(second==1) {
         a = dc[0]; 
         b = dc[1];
         if(tbcorr[a] == tbcorr[b])  {
           tbsol[a] = 1; tbsol[b] = 1; nsol++;
           if(nsol==8) {
             alerte("<center><span style='{font-weight: bold;font-size:15px;color:#dd0000;}'> Bravo, vous avez trouvé la solution en"+duree()+"</span><br /><br /><span style='{font-weight: bold;font-size:13px;color:#00aa00;}'>Cliquez sur le bouton pour commencer une nouvelle partie</span></center>");a
          
             stopchrono = true;
             cleartimeout(idchrono);
             idchrono=null;
           //  redraw();
           
           // reset_init();
           }
         } else {
           okjoue = 0;
           //if(id!=null) cleartimeout(id);
           id = setTimeout("masque()",1000);
         }
       } 
       second = 1-second;
  }
}

function alerte(s) {
  document.getElementById("alrt").innerHTML=s;
}

function chg(a, b) { document.images["c"+a].src=Im[b].src; }

function masque() { 
  redraw();
  okjoue = 1; 
}

function chrono() {
  tempsmis++;
  if(!stopchrono) {
    s = duree();
    while(s.length < 20) s = " "+s;
    document.frm.but.value=s;
    idchrono = setTimeout('chrono()', 1000);
  }
}

function duree() {
  var s = "";
  nbsecondes = tempsmis%60;
  nbminutes = Math.floor(tempsmis/60)%60;
  nbheures = Math.floor(tempsmis/3600);
  if(nbheures>0) s = s+nbheures+" h ";
  if(nbheures>0 || nbminutes>0)
    s = s+" "+nbminutes+" min ";
  s = s +" "+nbsecondes+" s";
  return s;
}


