
/***********************************************************************
 *  This code generates a memory style game for the words            * 
 *  listed in the file wordlist.js and illustrated by the image files. *
 *  The game is complete when all words have been found                *
 *  (c) Alan Hess - manxman@bluewin.ch                   *
 *  Sonderpädagogsiche Tagesschule Oberglatt, Switzerland.             * 
 *  FREE FOR NON_COMMERCIAL USE. PLEASE ADVISE OF ANY CHANGES          *
 *  Edit the "wordlist.js" file to set new words and pictures.         *    
 ***********************************************************************/
// Version 28.06.2007
var version = "2.08";
var map = new Array();    // map of image 
var amap = new Array(); // map of answer pics
var awmap = new Array(); // map of answer words
var state = new Array();    // boolean state map
var astate = new Array();   // state map of answer tiles
var preload = new Array();    // preload jpgs for slow internet conns.
var endgame;      // check if all stones are flipped
var n,i,last;
var totalwords,c,rows,cols,clickno,rnd1,rnd2;
var clickok,answerok;
var table,answer,right,wrong;
var hist = new Array();

var mp3file;
var audiofiles;
var helpbuttontext;
if (audiofiles!=null){audiofiles = true;} else {audiofiles=false;} // if audiofiles not already defined - define it

//------------------------------
function updatehist(n){
for (i=0;i<=10;i++){
  hist[i+1]=hist[i]
 }
hist[0]=n
}
//------------------------------------
function wait(n,last,delay){
string="clearlast("+n+","+last+");";
setTimeout(string,delay);
}

//-------------------------------
function clearlast(n,last){
       
  document.board[('img'+n)].src = progpath+"0.gif";
  document.board[('img'+last)].src = progpath+"0.gif";
  state[n]=false;
  state[last]=false;  
  clickok=true;
  answerok=false
}

//-------------------------------
function init() {
for (i=1;i<=totalwords;i++) { 
  map[i]=i;
  map[i+totalwords]=i;
  state[i]=false;
  state[i+totalwords]=false;
	astate[i]=false;
  amap[i]=i;
  amap[i+totalwords]=i;

 }
for (i = 0; i <= totalwords; i++){
   preload[i] = new Image()
   preload[i].src = datapath+map[i]+'.'+imagetyp
  }
for (i=0;i<=10;i++){hist[i]=0}
}
//----------------------------

function pointeron() {
board.style.cursor="pointer";
}
//------------------------------

function pointeroff() {
board.style.cursor="default";
}
//------------------------------

function showstones() {
document.write('<TABLE align="center" cellSpacing=0 cellPadding=1 border=2>');
for (r=0;r<=rows-1;r++){
  document.write('<tr>');
  for (c=0;c<=cols-1;c++) {
  document.write('<td align="center" width="150">');
  document.write('<img src="'+progpath+'0.gif" width="80" height="80" name="img'+((r*cols)+1+c)+'" onmouseover="pointeron()" onmouseout="pointeroff()" onclick="flipstone('+((r*cols)+1+c)+')">');
	  if (audiofiles==true){
	    mp3file=datapath+(map[(r*cols)+c+1])+".mp3";
	    embedplayer(mp3file);
	   }
    }
 document.write('<tr>');
  }
}
//--------------------------------
function showhappy(n,last){
  document.board[('img'+n)].src = progpath+"happy.gif";
  document.board[('img'+last)].src = progpath+"happy.gif";

  right++;
  endcheck()
}
//------------------------------------
function showsad(n,last){
  document.board[('img'+n)].src = progpath+'sad.gif';
  document.board[('img'+last)].src = progpath+'sad.gif';
  wrong++
}
//---------------------------------
function showsolutions(n,last){
  document.board[('img'+n)].src = datapath+map[n]+'.'+imagetyp;
  document.board[('img'+last)].src = datapath+map[last]+'.'+imagetyp;
  string="showhappy("+n+","+last+");";
  setTimeout(string,1000)
} 
//----------------------------------
function showanswers(){
  document.write('<tr>');
  for (i=1;i<=(totalwords);i++){
    document.write('<td align="center">');
      var picheight;
      if(audiofiles==true){picheight="60"}else{picheight="80"}
		  document.write('<img src="'+progpath+'blank.gif" width="80" height='+picheight+' name="anspic'+(amap[i])+'" onmouseover="pointeron()" onmouseout="pointeroff()" onclick="readanswer('+amap[i]+')">');
			document.write('<br><input style="font-size:9px;"  type="button" value="'+w[(amap[i]-1)]+'"  name="ans'+(amap[i])+'" onmouseover="pointeron()" onmouseout="pointeroff()" onclick="readanswer('+amap[i]+')">');
		  if (audiofiles==true){
	      mp3file=datapath+(amap[i])+".mp3";
	      embedplayer(mp3file);
	   }
		
		
		
    if (i==totalwords/2){document.write('<tr>');
     document.write('<td align="center">');
     document.write('<tr>');
   }
  }  
  document.write('<tr>');
	document.write('</table><br>');
	document.write('STO Memory Game Ver. '+version+' ');
  if (audiofiles==true){document.write('<input style="font-size:9px;"  type="button" value="Help"  name="helpbutton" onmouseover="pointeron()" onmouseout="pointeroff()" onclick="openhelp()">');}

}
//-----------------------------------------
function endcheck(){
endgame=true;
for (i=1;i<=(totalwords*2);i++){
  if (state[i]==false){endgame=false}
  }
  if (endgame==true){
  //for (i=1;i<=totalwords;i++){document.board[('ans'+i)].src = i+'.'imagetyp}
    window.alert(endtext+" "+wrong);
    history.go()
  }
}
//--------------------------------
function flipstone(n) {
var j;
if (state[n]!=true){
if (clickok==true) {
clickok=false;
if (state[n]==false) {
  clickok=true;
  clickno++
  } else {
  clickok=false
  }
if (clickok==true) {
  document.board[('img'+n)].src = datapath+map[n]+'.'+imagetyp;
  state[n]=true
  }
if (clickno>=2){
  clickok=false;
  answerok=true;
	for (j=1;j<=totalwords;j++){}
  clickno=0;
  if (map[n]!=map[last]){ wait(n,last,750);} 

    }
  last=n;
  updatehist(n);
  if (clickno>=2){clickno=0;}
 
   }
 }
}
//-------------------------------------
function answait(i,delay){
string="clrans("+i+")";
setTimeout(string,delay);
}
function clrans(i){document.board[('anspic'+i)].src =progpath+"blank.gif";}
//----------------------------------------------
function readanswer(i){

if (answerok==true){
  
  n=hist[0];
  answer=i ;
  if (answer==map[last] ){
	  astate[i]=true;
    //window.alert("Richtig!");
    document.board[('anspic'+i)].src =datapath+i+"."+imagetyp;
    showsolutions(hist[0],hist[1])
  }
  if (answer!=map[last] ){
    showsad(hist[0],hist[1]);
    wait(hist[0],hist[1],1000)  
  }
  } else {
	   // maybe show the picture "flash card" style if blank answer clicked on during game ??
	   //if (astate[i]==false){ // is the word already solved?
		  // document.board[('anspic'+i)].src =(i)+"."+imagetyp; //show the pic briefly.
	    // answait(i,500);
			// }

	}
answerok=false;
clickok=true
}
//----------------------------------------------
function scramble(){
  for (z=0;z<=10;z++){ // shuffle the pictures in grid
  for (i=1;i<=(totalwords*2);i++){
    rnd1=Math.floor(Math.random()*(totalwords*2+1));
    rnd2=Math.floor(Math.random()*(totalwords*2+1));
    if (rnd1!=0 && rnd2!=0){
      temp=map[rnd1];
      map[rnd1]=map[rnd2];
      map[rnd2]=temp;
		}
    }
  }
}
//-------------------------------------------------
function ascramble(){	
  for (z=0;z<=10;z++){ // shuffle the answer and text button tiles
  for (i=1;i<=(totalwords*2);i++){
    rnd1=Math.floor(Math.random()*(totalwords+1));
    rnd2=Math.floor(Math.random()*(totalwords+1));
    if (rnd1!=0 && rnd2!=0){
      temp=amap[rnd1];
      amap[rnd1]=amap[rnd2];
      amap[rnd2]=temp;
		}	
  }
  }
}
//-----------------------------------
function detectbrowser()
{
var browserName=navigator.appName; 
var A,browserVer=parseInt(navigator.appVersion); 

if (browserName=="Netscape")
{ 
 //alert("Hi Netscape User!");
}
else 
{ 
 if (browserName=="Microsoft Internet Explorer")
 {
  if (browserVer>=4){A=window.prompt("Please allow user input for this site. - Bitte das Dateneingeben für diese Seite erlauben")}
 }
 else
  {
    //alert("What ARE you browsing with here?");
   }
}

}
//-----------------------------
function embedplayer(soundfile){
  var soundfile;
	//window.alert(soundfile);
  
  document.write('<br><object name ="mp3player" width="80" height="10" classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000"');
  document.write('codebase="http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=8,0,0,0">');
 
	document.write('<param name="movie" value="'+progpath+'mp3player.swf?src='+soundfile+'" />');
	
  document.write('<embed src="'+progpath+'mp3player.swf?src='+soundfile+'" width="80" height="10" type="application/x-shockwave-flash"');
  document.write('pluginspage="http://www.macromedia.com/go/getflashplayer" /></object>');
}
//-----------------------------------
function openhelp(){
  
	showhelp=window.open('','showhelp','resizable=1,location=0,directories=0,status=0,menubar=0,scrollbars=0,toolbar=0,width=600,height=600');
  
}	
//PROGRAMM START------------------------------
detectbrowser();

totalwords=w.length; // calculate total word count


//window.alert(totalwords);

rows=1; //default values
cols=4;
if (totalwords==4){rows=2; cols=4}
if (totalwords==5){rows=2; cols=5}
if (totalwords==6){rows=3; cols=4}
if (totalwords==7){rows=2; cols=7}
if (totalwords==8){rows=4; cols=4} //adjust to best layout
if (totalwords==9){rows=3; cols=6}
if (totalwords==10){rows=4; cols=5}

if (totalwords>=12){rows=4; cols=6}

if (totalwords>=14){rows=4; cols=7}

if (totalwords>=16){rows=4; cols=8}

if (totalwords>=18){rows=4; cols=9}

if (totalwords>=20){rows=5; cols=8}


//window.alert(rows+" "+cols);




answer=0;
clickno=0;
clickok=true;
answerok=false;
right=0;
wrong=0;
last=0;
n=0;
map[0]=0;
state[last]=false;
init();
scramble();
ascramble();
showstones();
showanswers();
//if (audiofiles==true){document.write('<input style="font-size:9px;"  type="button" value="Help"  name="helpbutton" onmouseover="pointeron()" onmouseout="pointeroff()" onclick="openhelp()">');}



