
var gsize, ghrow, ghcol, gtime, gmoves=0, gintervalid=-1, gshuffling,deb=-1;

function returnobj(id) {
  if (document.getElementById) {
    return document.getElementById(id);
  } else if (document.all) {
    return document.all[id];
  } else if (document.layers) {
    return document.layers[id];
  } else {
    return false;
  }
}



function toggleHelp()

{

  if (butHelp.value == "Hide Help")

  {

    help.style.display = "none";

    butHelp.value = "Show Help";

  }

  else

  {

    help.style.display = "";

    butHelp.value = "Hide Help";

  }  

}




function r(low,hi)

{

  return Math.floor((hi-low)*Math.random()+low); 

}




function r1(hi)

{

  return Math.floor((hi-1)*Math.random()+1); 

}




function r0(hi)

{

  return Math.floor((hi)*Math.random()); 

}



function startGame()

{


  shuffle();

  gtime = 0;

  gmoves = 0;



}



function stopGame()

{


  gintervalid=-1;

}




function checkWin()

{

  var i, j, s;

  



  if (!isHole(gsize-1,gsize-1)) return;



  for (i=0;i<gsize;i++)

    for (j=0;j<gsize;j++)

    {

      if (!(i==gsize-1 && j==gsize-1)) 

      {

       if (getValue(i,j)!=(i*gsize+j+1).toString()) { return;}

      }

    }

  stopGame();

  alert("Partie finie en "+gmoves+" mouvement(s) !");
 gameRequestInformation(); 


}

function reinit()
{
  shuffle();

}





function showTable()

{

  var i, j, s;

  

  stopGame();

  s = "<table border=2 cellpadding=0 cellspacing=0 bgcolor='#666655'><tr><td class=bigcell>";

  s = s + "<table border=0 cellpadding=0 cellspacing=0>";

  for (i=0; i<gsize; i++)

  {

    s = s + "<tr>";    

    for (j=0; j<gsize; j++)

    {

      s = s + "<td id=a_" + i + "_" + j + " onclick='amove(this)' class=cell>" + (i*gsize+j+1) + "</td>";
    }

    s = s + "</tr>";        

  }

  s = s + "</table>";

  s = s + "</td></tr></table>";      

  return s;

}



function getCell(row, col)

{

return returnobj("a_" + row + "_" + col);
//  return eval("a_" + row + "_" + col);

}



function setValue(row,col,val)

{

  var v = getCell(row, col);


  v.innerHTML = val;

  v.className = "cell";

}



function getValue(row,col)

{




  var v = getCell(row, col);

  return v.innerHTML;

}



function setHole(row,col)

{ 


  var v = getCell(row, col);


  v.innerHTML = "";

  v.className = "hole";

  ghrow = row;

  ghcol = col;



}



function getRow(obj)

{

  var a = obj.id.split("_");

  return a[1];

}



function getCol(obj)

{

  var a = obj.id.split("_");

  return a[2];

}



function isHole(row, col)

{

  return (row==ghrow && col==ghcol) ? true : false;

}



function getHoleInRow(row)

{

  var i;

  

  return (row==ghrow) ? ghcol : -1;

}



function getHoleInCol(col)

{

  var i;



  return (col==ghcol) ? ghrow : -1;

}



function shiftHoleRow(src,dest,row)

{

  var i;




  src = parseInt(src);

  dest = parseInt(dest);



  if (src < dest)

  {

    for (i=src;i<dest;i++)

    {

      setValue(row,i,getValue(row,i+1));

      setHole(row,i+1);

    }

  }

  if (dest < src)

  {

    for (i=src;i>dest;i--)

    {

      setValue(row,i,getValue(row,i-1));

      setHole(row,i-1);

    }

  }

}



function shiftHoleCol(src,dest,col)

{

  var i;

  


  src = parseInt(src);

  dest = parseInt(dest);

    

  if (src < dest)

  {

    for (i=src;i<dest;i++)

    {

      setValue(i,col,getValue(i+1,col));

      setHole(i+1,col);

    }

  }

  if (dest < src)

  {

    for (i=src;i>dest;i--)

    {

      setValue(i,col,getValue(i-1,col));

      setHole(i-1,col);

    }

  }

}



function amove(obj)
{


  var r, c, hr, hc;

if(deb==-1) {
deb=0;
alert("Le jeu gratuit commence !");
}

  r = getRow(obj);

  c = getCol(obj);

  if (isHole(r,c)) return;

  hc = getHoleInRow(r);

  if (hc != -1)

  {

    shiftHoleRow(hc,c,r);

    gmoves++;

    checkWin();

    return;

  }

  

  hr = getHoleInCol(c);



  if (hr != -1)

  {

    shiftHoleCol(hr,r,c);

    gmoves++;

    checkWin();

    return;

  }

}

function amove1(obj)
{


  var r, c, hr, hc;


  r = getRow(obj);

  c = getCol(obj);

  if (isHole(r,c)) return;

  hc = getHoleInRow(r);

  if (hc != -1)

  {

    shiftHoleRow(hc,c,r);



    return;

  }

  

  hr = getHoleInCol(c);



  if (hr != -1)

  {

    shiftHoleCol(hr,r,c);



    return;

  }

}



function shuffle()

{



  var t,i,j,s,frac;

deb=-1;

  gmoves = 0;
  gshuffling =  true;

  frac = 100.0/(gsize*(gsize+10));

  s = "% ";

  for (i=0;i<gsize;i++)

  {

    s += "|";

    for (j=0;j<gsize+10;j++)

    {  


      if (j%2==0)

      {

        t = r0(gsize);

        while (t == ghrow) t = r0(gsize); 

	amove1(getCell(t,ghcol));
       // getCell(t,ghcol).click();

      } 

      else

      {

        t = r0(gsize);

        while (t == ghcol) t = r0(gsize); 

	amove1(getCell(ghrow,t));
//        getCell(ghrow,t).click();

      }

    }

  }

  window.status = "";

  gshuffling = false;

}



function loadBoard(size)

{

  gsize = size;


//alert(returnobj("board"));

var target=returnobj("board");

 target.innerHTML = showTable(gsize);

  setHole(gsize-1,gsize-1);


}



function debug()
{
gmoves=430;
  alert("Partie finie en "+gmoves+" mouvement(s) !");
 gameRequestInformation();
 alert("C'est reparti !");

}

function displayme() {
loadBoard(4);
startGame();
}
