//system variables
width = 6;
height = 4;
compleated = 0;
bkgImg = "";
crdBkg = "";
cardList = "";
allCardLists = new Array();
allCrdBks = new Array();
allBkgImg = new Array();
clicks = new Array();
board = new Array();
cheat = "false";


//sets the scoring attributes, number of moves and the time it took
moves = 0;
startTime = 0;


//adds all posible backgrounds
allBkgImg.push("/imgs/games/memory/1.jpg");
allBkgImg.push("/imgs/games/memory/2.jpg");
allBkgImg.push("/imgs/games/memory/3.jpg");
allBkgImg.push("/imgs/games/memory/4.jpg");
allBkgImg.push("/imgs/games/memory/5.jpg");
allBkgImg.push("/imgs/games/memory/6.jpg");
allBkgImg.push("/imgs/games/memory/7.jpg");
allBkgImg.push("/imgs/games/memory/8.jpg");
allBkgImg.push("/imgs/games/memory/10.jpg");

//adds all the card backs
allCrdBks.push("/imgs/games/memory/card/card_back_5.jpg");
allCrdBks.push("/imgs/games/memory/card/card_back_6.jpg");

//Adds all possible card sets

//adds generic random cards
allCardLists[0] = new Array();
allCardLists[0].push("/imgs/games/memory/card/memory_rel_2.jpg");
allCardLists[0].push("/imgs/games/memory/card/memory_rel_3.jpg");
allCardLists[0].push("/imgs/games/memory/card/memory_rel_4.jpg");
allCardLists[0].push("/imgs/games/memory/card/memory_rel_5.jpg");
allCardLists[0].push("/imgs/games/memory/card/memory_rel_6.jpg");
allCardLists[0].push("/imgs/games/memory/card/memory_rel_7.jpg");
allCardLists[0].push("/imgs/games/memory/card/memory_rel_8.jpg");
allCardLists[0].push("/imgs/games/memory/card/memory_rel_9.jpg");
allCardLists[0].push("/imgs/games/memory/card/memory_cross_5.jpg");
allCardLists[0].push("/imgs/games/memory/card/memory_cross_11.jpg");
allCardLists[0].push("/imgs/games/memory/card/memory_cross_2.jpg");
allCardLists[0].push("/imgs/games/memory/card/memory_cross_10.jpg");

/*/adds crosses
allCardLists[0] = new Array();
allCardLists[0].push("/imgs/games/memory/card/memory_cross_2.jpg");
allCardLists[0].push("/imgs/games/memory/card/memory_cross_3.jpg");
allCardLists[0].push("/imgs/games/memory/card/memory_cross_4.jpg");
allCardLists[0].push("/imgs/games/memory/card/memory_cross_5.jpg");
allCardLists[0].push("/imgs/games/memory/card/memory_cross_6.jpg");
allCardLists[0].push("/imgs/games/memory/card/memory_cross_7.jpg");
allCardLists[0].push("/imgs/games/memory/card/memory_cross_8.jpg");
allCardLists[0].push("/imgs/games/memory/card/memory_cross_9.jpg");
allCardLists[0].push("/imgs/games/memory/card/memory_cross_10.jpg");
allCardLists[0].push("/imgs/games/memory/card/memory_cross_11.jpg");
allCardLists[0].push("/imgs/games/memory/card/memory_cross_12.jpg");
allCardLists[0].push("/imgs/games/memory/card/memory_cross_13.jpg");
*/

//randomizes the array of card data
function randomizeArray(myArray)
{
  var i = myArray.length;
  if ( i == 0 ) return false;
  while ( --i )
  {
    var j = Math.floor( Math.random() * ( i + 1 ) );
    var tempi = myArray[i];
    var tempj = myArray[j];
    myArray[i] = tempj;
    myArray[j] = tempi;
  }
  return myArray;
}

//parses  URland returns an array with the attributes
function parseURL()
{
  out = new Array();
  prm = location.search;

  if (prm.length > 0)
  {
    prm = prm.substr(1);
    spl1 = prm.split("&");
    
    for (tmpN=0;tmpN<spl1.length;tmpN++)
    {
      spl2 = spl1[tmpN].split("=");
      out[spl2[0]] = spl2[1];
    }
  }
  return out;
}//end parseURL


//sets the paramaters if passed by URL
urlPram = parseURL();

//if a paramater set is specified
if (urlPram["cardSet"])
{
  switch (urlPram["cardSet"])
  {
    case ("cross"):
    break;
  }
}

//sets the indivigual paramaters
if (urlPram["bkgImg"] && (urlPram["bkgImg"] != "rand"))
  bkgImg = allBkgImg[urlPram["bkgImg"]];
else if (!urlPram["cardSet"] || (urlPram["bkgImg"] == "rand"))
  bkgImg = randomizeArray(allBkgImg)[0];
  
if (urlPram["crdBkg"] && (urlPram["crdBkg"] != "rand"))
  crdBkg = allCrdBks[urlPram["crdBkg"]];
else if (!urlPram["cardSet"] || (urlPram["crdBkg"] == "rand"))
  crdBkg = randomizeArray(allCrdBks)[0];
  
if (urlPram["cardList"] && (urlPram["cardList"] != "rand"))
  cardList = randomizeArray(allCardLists[urlPram["cardList"]]);
else if (!urlPram["cardSet"] || (urlPram["cardList"] == "rand"))
  cardList = randomizeArray(randomizeArray(allCardLists)[0]);

if (urlPram["cheat"])
  cheat = urlPram["cheat"];
  
  
  
/***************************************************************
 * image preloading code
 ***************************************************************/
function preloadImages()
{
  imgLst = new Array();
  // start preloading
  for(i=0; i< cardList.length; i++) 
  {
    imgLst[imgLst.length] = new Image();
    imgLst[imgLst.length-1].src = cardList[i];
  }
  
  imgLst[imgLst.length] = new Image();
  imgLst[imgLst.length-1].src = "/imgs/games/memory/card/card_mask.gif";
}

/***************************************************************/


/***************************************************************
 * Base classes
 ***************************************************************/

 //holds one click
 function clickStore(val, x, y)
 {
   this.value = val;
   this.cordX = x;
   this.cordY = y;
 }
/***************************************************************/

//displays the back of a card
function writeBack(cardCl)
{
  document.getElementById(cardCl.cordX+"-"+cardCl.cordY).style.backgroundColor = "#FFFFFF";
  document.getElementById(cardCl.cordX+"-"+cardCl.cordY).innerHTML = "<a href='' onclick='testLoc("+ cardCl.cordX +","+ cardCl.cordY +");return false'><img src='"+ crdBkg +"' border='0'></a>";
}

//displays the front of a card
function writeFront(cardCl)
{
  if (startTime == 0)
  {
    var d = new Date();
    startTime = d.getTime();
  }

  document.getElementById(cardCl.cordX+"-"+cardCl.cordY).style.backgroundColor = "#FFFFFF";
  document.getElementById(cardCl.cordX+"-"+cardCl.cordY).innerHTML = "<img src='"+ cardList[cardCl.value] +"' border='0'>";
}

//displays the masked image
function writeMask(cardCl)
{
  document.getElementById(cardCl.cordX+"-"+cardCl.cordY).style.backgroundColor = "";
  document.getElementById(cardCl.cordX+"-"+cardCl.cordY).innerHTML = "<img src='/imgs/games/memory/card/card_mask.gif' border='0'>";
}

//displays the masked image
function writeNoCard(cardCl)
{
  document.getElementById(cardCl.cordX+"-"+cardCl.cordY).innerHTML = "";
}

//adds data to the board locations
function fillBoard(fillCode)
{
  for (tmpA=0;tmpA<height;tmpA++)
    for (tmpB=0;tmpB<width;tmpB++)
      fillCode(new clickStore(board[tmpA][tmpB],tmpA,tmpB));
}


//checks a location when it is clicked
function testLoc(cordX, cordY)
{
  //if two incorect cards are picked then hide them
  if (clicks.length > 1)
  {
    writeBack(clicks[0]);
    writeBack(clicks[1]);
    moves++;
    clicks = new Array();
  }
  
  //check the currently selected card
  curClk = new clickStore(board[cordX][cordY],cordX,cordY);
  clicks.push(curClk);
  
  //checks for a match
  if (clicks.length > 1 && clicks[0].value == clicks[1].value)
  {
    writeMask(clicks[0]);
    writeMask(clicks[1]);
    compleated++;
    moves++;
    clicks = new Array();
  }
  else
    writeFront(curClk);
    
  //checks to see if the game has been finished
  if (compleated >= width*height/2)
  {
    d = new Date()
    d.setTime(d.getTime() - startTime);
    
    timeStr = d.getUTCMinutes() +" Minutes and "+ d.getUTCSeconds() +" Seconds";
    
    for (tmpA=0;tmpA<height;tmpA++)
      for (tmpB=0;tmpB<width;tmpB++)
        writeNoCard(new clickStore(0,tmpA,tmpB));

    alert("Completed in:\n"+ timeStr +"\nand it took you "+ moves +" moves.");
  }
}

//builds the origional board code
function buildBoard()
{
  //creates the sub arrays for the board
  for (tmpA=0;tmpA<height;tmpA++)
    board[tmpA] = new Array();
  
  //creates the list of pieces
  pieces = new Array();
  for (tmpA=0;tmpA<(width*height/2);tmpA++)
  {
    pieces.push(tmpA);
    pieces.push(tmpA);
  }

  //randomizes the pieces
  pieces = randomizeArray(pieces);

  //places the pieces on the board
  for (tmpA=0;tmpA<height;tmpA++)
  for (tmpB=0;tmpB<width;tmpB++)
    board[tmpA][tmpB] = pieces.pop();

  //writes the board    
  boardCoad = "<table border='0' cellspacing='0' cellpadding='0' style='background-image: url("+ bkgImg +")'>";
  for (tmpA=0;tmpA<height;tmpA++)
  {
    boardCoad += "<tr>";
    for (tmpB=0;tmpB<width;tmpB++)
      boardCoad += "<td id='"+ tmpA +"-"+ tmpB +"' style='width: 85px; height: 127px;vertical-align: middle;text-align: center'></td>";
    boardCoad += "</tr>";
  }
  boardCoad += "</table>";

  //writes the answer matrix out if neaded
  if (cheat == "true")
    for (tmpA=0;tmpA<height;tmpA++)
    {
      for (tmpB=0;tmpB<width;tmpB++)
        boardCoad += board[tmpA][tmpB]+ " ";
      boardCoad += "<br />";
    }
    
  document.getElementById("memoryArea").innerHTML = boardCoad;
  
  fillBoard(writeBack);
}

document.write("<div id='memoryArea'></div>");
preloadImages();
buildBoard();
