
// Code (c) CJ Hammond, 2001
// You can use it for free, as long as you don't claim it's copyright !!!
// If you use it, please also give a note where you got it from. Thank you!

function fMemoryImgFilename(intIImgNum) {

  return (strPath + strFTag + ((intIImgNum < 10) ? '0' : '') + intIImgNum + strFType);

}


function fMemoryInit(intIPause, intIXMax, intIYMax, strIXWidth, strIYHeight, strIPath, strIFTag, strIFType, strIBTile) {

intPause	= intIPause;
intXMax		= intIXMax;
intYMax		= intIYMax;
strXWidth	= strIXWidth;
strYHeight	= strIYHeight;

strPath		= strIPath;
strFTag		= strIFTag;
strFType	= strIFType;
strBTile	= strIBTile;

arrImages	= new Array();
arrIFound	= new Array();
arrIMixing	= new Array();
arrICheck	= new Array();
intCellsMax	= intXMax*intYMax;
dblArrayMax	= intCellsMax/2;
strBTileFile	= strPath + strFTag + strBTile + strFType;

imgBackTile	= new Image();
imgBackTile.src	= strBTileFile;

for (intI=0;intI<dblArrayMax;intI++) {

  arrImages[intI]		= new Image();
  arrImages[intI].src		= fMemoryImgFilename(intI);
  arrIFound[2 * intI]		= false;
  arrIFound[2 * intI + 1]	= false;
  arrIMixing[intI]		= intI;
  arrIMixing[intCellsMax-intI-1]= intI; }

intSelected	= -1;
intNSelected	= 0;
intNToFind	= intCellsMax;

}

function fMemoryPrintTable() {

for (intI=0;intI<intCellsMax*20;intI++) {
  intRandomA	= Math.round(Math.random()*intCellsMax*100) % intCellsMax;
  intRandomB	= Math.round(Math.random()*intCellsMax*100) % intCellsMax;
  intChange	= arrIMixing[intRandomA];
  arrIMixing[intRandomA] = arrIMixing[intRandomB];
  arrIMixing[intRandomB] = intChange; }

for (intI=0;intI<intCellsMax;intI++) {
  if ((intI % intXMax) == 0) document.write('<tr valign="center">\n');

  document.write(	'  <td align="center"><a href="javascript:fMemoryCheckTile(' + intI + ');">' +
			'<img name="img' + intI + '" src="' + strBTileFile + '" width="' +
			strXWidth + '" height="' + strYHeight + '" border="0"></a></td>\n');

  if ((intI % intXMax) == (intXMax - 1)) document.write('</tr>\n'); }

}


function fMemoryCheckTile(intImage) {

  if (intNSelected == 2) { return; }
  if (arrIFound[intImage] == true) { return; }
  if (intImage	== intSelected) { return; }

  document['img' + intImage].src = fMemoryImgFilename(arrIMixing[intImage]);

  if (intNSelected == 0) {
    intNSelected	= 1;
    intSelected		= intImage; }
   else {
    intTTest1	= intSelected;
    intTTest2	= intImage;
    intSelected	= -1;
    intNSelected= 2;
    intTTile1	= arrIMixing[intTTest1];
    intTTile2	= arrIMixing[intTTest2];
    if (intTTile1 == intTTile2) {
      arrIFound[intTTest1] = true;
      arrIFound[intTTest2] = true;
      intNToFind -= 2;
      if (intNToFind == 0) { alert('Congratulations !!!'); }
      intNSelected  = 0; }
     else {
      tmoNextPart	= setTimeout('fMemoryCheckTileCont()', intPause); } }

}


function fMemoryCheckTileCont() {
  
  document['img' + intTTest1].src = imgBackTile.src;
  document['img' + intTTest2].src = imgBackTile.src;
  intNSelected	= 0;

}

