//========================================================================//   "MemorySquares" JavaScript Memory Game v1.1////   Copyright (C) 2000,2002  Jan Mulder////   This program is free software; you can redistribute it and/or modify//   it under the terms of the GNU General Public License as published by//   the Free Software Foundation; either version 2 of the License, or//   (at your option) any later version, and as long as this notice is//   kept unmodified at the top of the JavaScript source code.////   This program is distributed in the hope that it will be useful,//   but WITHOUT ANY WARRANTY; without even the implied warranty of//   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the//   GNU General Public License (license.txt) for more details.////   Contact: Jan Mulder info@EnglishCafe.co.uk////   V1.1//     You can now have more images pairs than the number of grid squares.//     If this is the case then only a randomly chosen selection will be//     used for each game...thus giving the impression of a slightly different//     game each time it is loaded.////   V1.0//     Original public release.//=======================================================================imageCount = 0;numCorrect = 0;numWrong = 0;numAttempts = 0;numMinutes = 0;numSeconds = 0;timeLeft = 0;isTimed = false;timerOn = false;gameOver = false;var progressBar = '||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||';var solvedItems = new Array();var randArray = new Array();var tempArray = new Array();function arrayDeletePair( arrayName, delIndex ){  var ar = new Array();  for (var ii = 0; ii < arrayName.length; ii++)  {     if ((ii != delIndex) && (ii != (delIndex+1)))      ar[ar.length] = arrayName[ii];  }  return ar;}function randomizeArray(){  for (var i=0; i < matchingPairs.length; i=i+2)  {    tempArray[tempArray.length] = "item"+i;    tempArray[tempArray.length] = matchingPairs[i];    tempArray[tempArray.length] = "item"+i;    tempArray[tempArray.length] = matchingPairs[i+1];  }  var count = matchingPairs.length;  for (var i = 0; i < count; i++)  {    imageRem = count - i;    var rand = Math.floor(Math.random()*imageRem);    randArray[randArray.length] = tempArray[rand*2];    randArray[randArray.length] = tempArray[(rand*2)+1];    tempArray = arrayDeletePair(tempArray, rand*2);  }}function trimArray( newSize ) {  var trimBy = (randArray.length/2 - newSize)/2;  imageCount = newSize;    for (var i = 0; i < trimBy; i++)  {    var numImages = randArray.length/2;  	    var strItem = randArray[0];    randArray = arrayDeletePair(randArray, 0);    for (var ii = 0; ii < randArray.length; ii++)      if (randArray[ii] == strItem) {      	randArray = arrayDeletePair(randArray, ii);      	break;      }	  }}function doFunction(aFunction){ if (aFunction.indexOf('(') > -1)   eval( aFunction ); else   eval(aFunction+'()');}function updateTime(){  if (!timerOn) return;  if(numSeconds == 59)  {    numMinutes++;    numSeconds=0;  }  else    numSeconds++;  if ((isTimed) && !(gameOver))  {    timeLeft--;    document.timerForm.timeLeft.value = timeLeft;	if (timeLeft == 0)	{	  gameOver = true;      numAttempts = numCorrect + numWrong;      doFunction(doneAction);	}  }	}function startTimer(){  numSeconds = 0;  numMinutes = 0;  timerOn = true;  timer=setInterval('updateTime()',1000);}function stopTimer(){  timerOn = false;  clearInterval(timer);}function onComplete(){ window.status='Done';}function updateProgress(ims){ var cnt=0; for(var i = 0; i < ims.length; i++)  if(ims[i].complete || ims[i].errored) cnt++; if(ims.length > 0)    window.status='Loading ['+Math.round((cnt / imageCount)*100)+'%] ' + progressBar.substring(0, cnt); if(cnt < ims.length) {  tempArray = ims;  setTimeout("updateProgress(tempArray)",200); } else  onComplete();}function preloadImages(){ imageCount = matchingPairs.length; this.length = imageCount; for (var i = 0; i < matchingPairs.length; i++) {  this[i] = new Image();  this[i].errored=false;  this[i].onerror=new Function("this["+i+"].errored=true");  this[i].src = matchingPairs[i]; } updateProgress(this);}function drawTable(width, height){ // First check if there are more images than squares // And if so, then trim the array of images accordingly if (width*height < randArray.length/2)   trimArray(width*height); window.defaultStatus = ""; var isNetscape = (navigator.appName == "Netscape"); document.write('<table border="0" cellspacing="0" cellpadding="0">'); for (i = 0; i < height; i++) {  document.write('<tr>');  for (j = 0; j < width; j++)  {    document.write(    '<td background="match_card.gif">'    );    if (isNetscape)      document.write(      '<A  href="javascript:void(check(\'box'+(i*width+j)+'\'))"'	  + ' onMouseOut="imgMouseOut()">'      );    document.write(    '<img border="0" src="/bin/y/v/match_card.gif" width="'    + imageWidth    + '" height="'    + imageHeight    + '" name="box'    + (i*width+j)    + '"'    );    if (!isNetscape)      document.write(      ' onClick="check(\'box' + (i*width+j) + '\')"'	  + ' onMouseOut="imgMouseOut()"'      );    document.write(    '></A><br></td>'    );  }  document.write('</tr>'); } document.write("</table>");}var lastID="none";var lastItem="none";var currentID = "";var currentItem = "";var numVisible = 0;var wrongAnswer = false;function resetChecked(){  if (!wrongAnswer) return;  wrongAnswer = false;  numVisible = 0;  eval("document."+currentID+".src = '/bin/y/v/match_card.gif';");  eval("document."+lastID+".src = '/bin/y/v/match_card.gif';");  lastID="none";  lastItem="none";}function imgMouseOut(){  if (numVisible == 2)    if ((delay == 0) && (wrongAnswer))	  resetChecked();}function check(boxID){  if (numVisible == 2)    if (wrongAnswer)	  resetChecked();	else      return;  if ((gameOver) || (boxID == lastID)) return;    itemIndex = parseInt(boxID.slice(3, boxID.length));  if (checkSolved(randArray[2*itemIndex])) return;  if (!timerOn) startTimer();  if (numVisible==0)  {    numVisible = 1;    lastID = boxID;    itemIndex = parseInt(boxID.slice(3, boxID.length));    lastItem = randArray[2*itemIndex];    return eval("document."+boxID+".src = '"+randArray[2*itemIndex+1]+"';");  }  else if (numVisible==1)  {    numVisible=2;    itemIndex = parseInt(boxID.slice(3, boxID.length));    eval("document."+boxID+".src = '"+randArray[2*itemIndex+1]+"';");    currentID = boxID;    currentItem = randArray[2*itemIndex];    checkItem2(currentItem);  }}function checkItem2(item){  if(lastItem==item)  {    numVisible=0;    wrongAnswer = false;    if (!checkSolved(item))    {      solvedItems[solvedItems.length] = item;      numCorrect++;    }    if (numCorrect >= imageCount/2)    {      stopTimer();      if (doneAction != '')      {        gameOver = true;        numAttempts = numCorrect + numWrong;        doFunction(doneAction);      }    }  }  else if (lastItem!=item)  {    numWrong++;    wrongAnswer = true;	if (delay > 0)      window.setTimeout("resetChecked()", delay);  }}function checkSolved(iSolved){  found = false;  for (var i=0; i < solvedItems.length; i++)    if (solvedItems[i] == iSolved)      found = true;  return found;}randomizeArray();var pictures = new preloadImages();
