<!--
// Concentration game (javascript, html, and images) 
// Copyright 2002, 2003 Live Oak Games.  All Rights Reserved.
var imagecount = 10;
var rows = 6;
var columns = 8;
var numberofsquares = rows * columns;
var matchdata = new Array(numberofsquares);
var spotstatus = new Array(numberofsquares);
var bugs = new Array(imagecount);

// change location of images for different boards
var imagename= "../graphics/bug";
var covername= "../graphics/matchbck.gif"; 
var keeptryingimage= "../graphics/keeptrying.gif"; 
var successimage="../graphics/matchfound.gif";
var loadingimage="../graphics/loading.jpg";

var buggyintro = 0; // set to 1 to show all the bugs first.
var clicks=0;
var cNothingPressed = 0;
var success = 0;
var boardreset=0;
var bstatus=cNothingPressed;

function msg(txt) 
 {
  status = txt;
  return true;
 }

function pageloaded() {
  document.targetimage.src=bugs[0].src;
} 
function preloadImages() {
  for (var i=0; i<imagecount; i++) {
    bugs[i]=new Image; //(50,50);
	bugs[i].src=imagename+i+".gif";
  }    
}

preloadImages();

function loadImages() { 
  eval("document.targetimage.src= \""+loadingimage+"\";");
//  preloadImages();
}
function showspace(index)
{
  eval("document.space" + index + ".src = \""+bugs[matchdata[index]].src+"\";");
  document.targetimage.src = bugs[matchdata[index]].src;
}
function hidespace(index)
{
  eval("document.space" + index + ".src = \""+covername+"\";");  
}
function showboard()
{  
  boardreset=1;
  for (var index=0; index < numberofsquares; index++)  {
    if ((spotstatus[index] == 0) && (buggyintro < 1)) { hidespace(index) } else { showspace(index) }
//     if (spotstatus[index] == 0) { hidespace(index) } else { showspace(index) }
  }
}
function initializeBoard(reshow)
{
  // start everything at 0
  var index = 0;
  var imageid = 0;
  var tgt = 0;
  var dupcounter = new Array(imagecount);
  var tgtduplevel=0;
  clicks = 0;
  success = 0;
  cNothingPressed = 0;
  boardreset=0;
  for (index=0; index<imagecount; index++) {
    dupcounter[index]=0;
  }
  for (index = 0; index < numberofsquares; index++)  {  
    matchdata[index] = -1; 
    spotstatus[index]= 0;	   
  }
  tgtduplevel=0;
  for (index = 0; index < numberofsquares; index++)  { 
     if (matchdata[index] < 0) {  // if spot is not already filled
        // find id for image
		imageid=Math.round(Math.random()*(imagecount-1));
		// make sure image does not appear too many times
		while (dupcounter[imageid] > 3) {     		  
		  imageid=0;
		  // loop looking for image with tgtduplevel.
		  while(imageid<imagecount) { 
 		    // if this isn't found, then start again, looking for next higher tgtduplevel
			if (dupcounter[imageid]<=tgtduplevel) { 
			  break; 
			}
       	    imageid++;
		    if (imageid==imagecount) {
			  tgtduplevel++;
			  imageid=0;
			}
		  }
		}		
		dupcounter[imageid]++;
	    matchdata[index] = imageid; 
		tgt = index + Math.round(Math.random()*(numberofsquares-index-1));		
		if (tgt>numberofsquares) { tgt=numberofsquares };
		while (tgt < numberofsquares)  {   // find next spot to put image
		  if (matchdata[tgt]<0) {          // if spot empty, use it
			   matchdata[tgt]=matchdata[index];
			   break;
		  }  else { tgt++ }
	    }
	    if (tgt == numberofsquares) {  // if no spot was found empty
		  tgt = index+1;
          while (tgt < numberofsquares)  {   // find next spot to put image
		    if (matchdata[tgt]<0) {          // if spot empty, use it
			    matchdata[tgt]=matchdata[index];
			    break;
		    }  else { tgt++ }
          } // searching for empty spot before randomly found spot
	    }  // if all spots after random spot are already filled
	 }
  }
  if (reshow==1) {
    showboard();
  }
}
function resetGame()
{
  initializeBoard(1);
  document.score.clickcounter.value=clicks;
  bstatus=cNothingPressed;
}
function writescoreboard()
{
  document.write("<table width=\"100%\">");
  document.write("<tr>");
	document.write("<th valign=\"top\" align=\"left\"><A HREF=\"index.htm\" onClick=\"resetGame(); return false\">Restart Game</A></th>"); 
    document.write("<th valign = \"middle\" align=\"right\">");
    document.write("<form name=\"score\">");
    document.write("Clicks <input readonly type = \"text\" size=\"5\" name = \"clickcounter\"></input>");
    document.write("</form>");
    document.write("</th>");
  document.write("</tr></table>"); 
}
function createBoard() // createboard creates the game board
{ 
  var index=0;
  var x = 0;
  var y = 0;  
  document.write("<table BORDER=0 CELLSPACING=\"0\" CELLPADDING=\"0\" ALIGN=\"center\" VALIGN=\"top\">");

  document.write("<tr align=\"center\"><th>Click a tile to reveal a bug</th></tr>");
  document.write("<tr><td align=\"center\">");
  document.write("<IMG SRC=\"graphics/bug0.gif\" BORDER=\"0\" NAME=\"targetimage\" WIDTH=50 HEIGHT=50>");
  document.write("</td></tr>");
  document.write("<tr><td width=\"401\">");
  for (y=0; y<rows; y++) {
    for (x=0; x<columns; x++)  {    
      document.write("<IMG SRC=\""+covername+"\" BORDER=0 NAME=\"space" +index+ "\" WIDTH=50 HEIGHT=50 onClick=\"clickSpace("+ index + "); return false;\" onMouseOver=\"overSpace(); return false;\">"); 
	  index++;
	}
	document.write("<BR>"); // move to next line  
  }
  document.write("</td></tr>");
  // start table at bottom
  document.write("<tr><td>");
  writescoreboard();
  document.write("</td></tr></table>");
}

function showsuccess()
{  
  if (success==1) { 
    eval("document.targetimage.src = \""+successimage+"\";");
  } else { 
       eval("document.targetimage.src = \""+keeptryingimage+"\";");
	   }	 
}
function clickSpace(index)  // respond to the game
{ var victory = 0; 
  var i=0;
  if (buggyintro > 0) {
    buggyintro=0;
	resetGame();
	return;
  } 
  if ((spotstatus[index] == 0) && ((bstatus-1)!=index)) {
  clicks = clicks+1;
  success=0;
  if (bstatus == cNothingPressed) {
 	showspace(index);
	bstatus = index+1;
  }  else {
    if (spotstatus[index] != 0) {
	  showboard();
	} else {
         if ( matchdata[bstatus-1] == matchdata[index] ) { 
           spotstatus[index] = 1;
	       spotstatus[bstatus-1] = 1;		
		   success=1;
         } else {
		   boardreset=0;
		 }
		 showspace(index);		 
         showsuccess();
		 i=0;
		 victory=1;
		 while ((i<numberofsquares) && (victory==1)) {
		   if (spotstatus[i]==0) {
		     victory=0;
		   }
		   i++;
		 }
	 }	 	 
	 bstatus=cNothingPressed;  // reset status
	 if (victory==1) {
	   gamewon();
	 }
    } 
  document.score.clickcounter.value=clicks;
  }
}

function successmessage()
{
  if (clicks<49) { 
   return "Perfect!<br>You have a strange psychic connection with your computer."
  }; 
  if (clicks<97) { 
   return "Amazing!<br>You have either a photographic memory or phenomenal luck."
  };
  if (clicks<131) {
    return "Impressive.<br>You have a gift for bughunting!"
  };
  if (clicks<150) {
    return "Not too bad.<br>Maybe you should try again..."
  };
  if (clicks<200) {
    return "Hmm...<br>Hard to concentrate with all those bugs, isn't it?"
  };
  return "Over 200?<br>You were just clicking randomly, weren't you?"  
}
function showcredits(designer,artist,coder) {
  var w = 480;
  var h = 340;
  if (document.all || document.layers) {
   w = screen.availWidth;   
   h = screen.availHeight;
  }
  var popW = 300, popH = 300;
  if (w<popW) { w = popW  };
  if (h<popH) { h = popH };
  var leftPos = (w-popW)/2;
  var topPos = (h-popH)/2;

  CreditWindow=window.open("","credits","width=" + popW + ",height="+popH+",top="
                            +topPos+",left="+leftPos+",screenX="+leftPos+",screenY="+topPos);
  
  var CreditText="<html><head><title>Insectration Credits</title></head>"
        +"<body bgcolor='#FFFFFF' text='#000000' leftmargin='0' topmargin='0' marginwidth='0' marginheight='0'>"
		+"<p align='center'><img src='graphics/bug3.gif'><br>"
		+"Insectration is based on that ancient game known as Concentration.<br>"
		+"</p><p align='center'>"
		+"This version was designed by <b>Pat Matthews</b>.<br>"
		+"The graphics were created by <b>Billy Matter</b>.<br><br>"
		+"And, as always, the coding was done by the multitalented Albino Toad! (that's me)</p>"
		+"<p align='center'>Copyright(c) 2002, 2003 Live Oak Games.<br>All Rights Reserved.</p></body></html>"; 
  CreditWindow.document.clear();
  CreditWindow.document.write(CreditText);    
}
function gamewon()
{ 
  var w = 480;
  var h = 340;
  if (document.all || document.layers) {
   w = screen.availWidth;   
   h = screen.availHeight;
  }
  var popW = 300, popH = 200;
  if (w<popW) {
    w = popW
  };
  if (h<popH) {
    h = popH
  };
  var leftPos = (w-popW)/2;
  var topPos = (h-popH)/2;

  SuccessWindow=window.open("","Success","width=" + popW + ",height="+popH+",top="
                            +topPos+",left="+leftPos+",screenX="+leftPos+",screenY="+topPos);
  
  var successtext="<html><head><title>Insectration Score</title></head>"
        +"<body bgcolor='#00FF00' text='#000000' leftmargin='0' topmargin='0' marginwidth='0' marginheight='0'>"
		+"<p align='center'><img src='graphics/bug2.gif'><br>"
		+"Congratulations on Winning Insectration!<br>"
		+"</p><p align='center'>"
		+"<b><i>Your score = "+clicks+"</i></b><br>"
		+successmessage()+"</p></body></html>"; 
  SuccessWindow.document.clear();
  SuccessWindow.document.write(successtext);  
}

function overSpace()  // clear out items if none have been found
{
  if ((success==0) && (bstatus==cNothingPressed) && (boardreset==0) )
    { showboard(); }  
}
function showmatchgame()
{
	var w = 640;
  var h = 480;
  if (document.all || document.layers) {
   w = screen.availWidth;   
   h = screen.availHeight;
  }
  var popW = 460, popH = 480;
  if (w<popW) w = popW;
  if (h<popH) h = popH;
  var leftPos = (w-popW)/2;
  var topPos = (h-popH)/2;
  
  GameWindow=window.open("Insectrationpopup.htm","Insectration","width=" + popW + ",height="+popH+",top="
                            +topPos+",left="+leftPos+",screenX="+leftPos+",screenY="+topPos);
//  GameWindow.document.clear(); 
//  GameWindow.document.write(gamecode);    
}

// -->