var lg="fr"
var m=5
var n=2*m+1;
var pion = new Array();
var reussi=0;
var bloque=0;
var suffixe=".gif";
var Im=new Array();
Im[0]=new Image; Im[0].src="gre_bleu"+suffixe;
Im[1]=new Image; Im[1].src="gre_blanc"+suffixe;
Im[2]=new Image; Im[2].src="gre_noir"+suffixe;
Im[3]=new Image; Im[3].src="fermeup"+suffixe;
Im[4]=new Image; Im[4].src="fermedown"+suffixe;
Im[5]=new Image; Im[5].src="newplay-out"+suffixe;
Im[6]=new Image; Im[6].src="newplay-on"+suffixe;
Im[7]=new Image; Im[7].src="jeuxup.png"+suffixe;
Im[8]=new Image; Im[8].src="jeuxdown"+suffixe;

function myrand(n) {
  return Math.floor(n*Math.random());
}

function init() {
  for(var i=0; i<n; i++) {
    if(i==m) pion[i] = 0
    else if(i<m) pion[i] = -1;
    else pion[i] = 1;
  }

  for(i=0;i<n;i++) {
    if(pion[i] == 0) document.images['gre_'+i].src=Im[0].src;
    else if(pion[i] == -1) document.images['gre_'+i].src=Im[2].src;
    else document.images['gre_'+i].src=Im[1].src;
  }
  reussi=0;
  bloque = 0;
}


function testfin() {
  for(var i=0; i<n; i++) {
    if(i<m && pion[i] != 1) return 0;
    else if(i>m && pion[i] != -1) return 0;
    else if(pion[m] != 0)  return 0;
  }
  return 1;
}


function fbloque() {
  for(var i=0;i<n;i++) {
    if(i<n-1 && pion[i]==-1 && pion[i+1]==0) return 0;
    if(i<n-2 && pion[i]==-1 && pion[i+2]==0 && pion[i+1]==1) return 0;
    if(i>0 && pion[i]==1 && pion[i-1]==0) return 0;
    if(i>1 && pion[i]==1  && pion[i-1]==-1 && pion[i-2]==0) return 0;
  }
  return 1;
}

function effectue(x){
  if(pion[x]==-1) {   // noir
    if(x<n-1 && pion[x+1]==0) {  // vide à droite
      pion[x]=0;
      pion[x+1] = -1;
      document.images['gre_'+x].src=Im[0].src;
      document.images['gre_'+(x+1)].src=Im[2].src;
    } else if ( x < n-2 && pion[x+1] == 1 && pion[x+2] == 0) {
      pion[x]=0;
      pion[x+2] = -1;
      document.images['gre_'+x].src=Im[0].src;
      document.images['gre_'+(x+2)].src=Im[2].src;
    }
  } else if (pion[x]==1) {   // blanc
    if(x>0 && pion[x-1]==0) {  // vide à gauche
      pion[x]=0;
      pion[x-1] = 1;
      document.images['gre_'+x].src=Im[0].src;
      document.images['gre_'+(x-1)].src=Im[1].src;
    } else if (x>=2 && pion[x-1]==-1 && pion[x-2]== 0) {
      pion[x]=0;
      pion[x-2] = 1;
      document.images['gre_'+(x)].src=Im[0].src;
      document.images['gre_'+(x-2)].src=Im[1].src;
    }
  }
  if(reussi==1) {
    if(lg=="fr")alert("Encore bravo, j'ai réinitialisé pour que vous puissiez rejouer.");
    else alert("New Game");
    init();
  } else if(reussi==0 && testfin() == 1) {
    if(lg=="fr") alert("Félicitations, vous avez réussi.");
    else alert("Congratulations");
    reussi=1
  } else if(reussi==0 && fbloque()==1) { 
    if(bloque == 0) {
      if(lg=="fr") alert("Le jeu est bloqué.");
      else alert("Lost! (Game over)");
      bloque=1;
    } else {
      if(lg=="fr") alert("Le jeu était bloqué, j'ai réinitialisé pour que vous puissiez rejouer.");
      else alert("New Game");
      init();
    } 
  }
}

function ecrtab() {
  var i;
  document.write('<center>\n');
  document.write('<table border="0" cellspacing="0" cellpadding="0">\n');
  document.write('<tr>\n');
  for(i=0;i<11;i++) {
    document.write('<td><a href="javascript:effectue('+i+')">');
    document.write('<img name="gre_'+i+'" border="0" src="gre_bleu.gif" width="35" height="35" alt=""></a></td>\n');
  }
  document.write('</tr>\n</table>\n');
  document.write('<br><a href="#gren" onmouseover="document.images[\'nouvelle\'].src=Im[6].src"\n');
  document.write(' onmouseout="document.images[\'nouvelle\'].src=Im[5].src" onclick="init()"><img\n');
  document.write(' name="nouvelle" border=0 src="newplay-out.gif" width="80" height="20" alt=""></a>\n');
	
  document.write('</center>\n');
}


