var NUM_CARDS=30;var NUM_ROWS = 5; var IMAGES_PATH	= location.href + "/../media/images";var STYLE='cards/pregame/';var initialGameStart = 0;function drawGameBoard(pregame) {if(pregame) {var ttlCards = NUM_CARDS;for (var rows = 0; rows < NUM_ROWS; rows++) {var tableRow = document.getElementById("gameBoard").insertRow(0);for (var card = 0; card < (NUM_CARDS / NUM_ROWS); card++) {var image= document.createElement("div");image.style.width="96px";image.style.height="84px";var src		= IMAGES_PATH + "/" + STYLE + card + '.png';image.style.backgroundImage = "url(" + src + ")";image.id= "card_" + ttlCards;var text = document.createElement("p");text.setAttribute("class", "frontText");image.appendChild(text);tableRow.insertCell(card).appendChild(image);ttlCards--;}}var playButton=document.createElement("input");playButton.type= "button";playButton.value= "Play";playButton.name= "playButton";playButton.id = "playButton";var element= document.getElementById("timer");element.appendChild(playButton);var timerDisplay  = document.getElementById("time");timerDisplay.innerHTML				= "CLICKS: 0 &nbsp;&nbsp;&nbsp;&nbsp; TIMER: 0:00";}else{var ttlCards = NUM_CARDS;for (var rows = 0; rows < NUM_ROWS; rows++) {for (var card = 0; card < (NUM_CARDS / NUM_ROWS); card++) {var cardID = "card_" + ttlCards;var image= document.getElementById(cardID);var src= IMAGES_PATH + "/" + "cards/back/" + sponsors[0];image.style.backgroundImage = "url(" + src + ")";ttlCards--;}}initialGameStart = 1;}}function preloadImages() {var buffer = new Array();for(var counter = 0; counter < 14; counter++){buffer[counter] = new Image();buffer[counter].src =IMAGES_PATH + "/cards/front/" + cardFront[counter];}}function updateButton() {var element = document.getElementById("playButton");if(element.value == 'Play') {element.value = "Stop";}else{element.value = "Play";}}function makeRandom(min, max) {var randomNo = Math.floor(Math.random() * (max - min + 1)) + min;return randomNo;}function isNumRepeated(randomNums, numElements, tempNum) {for(var counter = 0; counter < numElements; counter++) {if(randomNums[counter] == tempNum) {return (-1);}}}var counter = 0;var randomNums = new Array();var tempNum = makeRandom(1,NUM_CARDS);randomNums[counter] = tempNum;while(counter < NUM_CARDS) {var tempNum = makeRandom(1,NUM_CARDS);var numElements	= randomNums.length;var isRepeated 		= isNumRepeated(randomNums, numElements, tempNum);while(isRepeated == -1) {var tempNum = makeRandom(1,NUM_CARDS);var numElements	= randomNums.length;isRepeated = isNumRepeated(randomNums, numElements, tempNum);}randomNums[counter] = tempNum;counter++;}function timer() {this.hours= 0;this.minutes= 0;this.seconds= 0;this.clicks	= 0;this.isOnOff= false;this.switchOnOff= switchOnOff;this.incrementTimer		= incrementTimer;this.printElapsedTime	= printElapsedTime;}function switchOnOff() {if (this.isOnOff == false) {this.isOnOff = true;this.incrementTimer();}else{this.isOnOff = false;}}function printElapsedTime() {var timerDisplay  = document.getElementById("time");(this.seconds < 10) ? seconds= "0" + this.seconds : seconds = this.seconds;var timeElapsed	  = "CLICKS: " + this.clicks + "&nbsp;&nbsp;&nbsp;&nbsp;" + " TIMER: " + this.minutes + ":" + seconds;timerDisplay.innerHTML	= timeElapsed;}function incrementTimer() {if (this.isOnOff) {if (this.seconds < 59) {this.seconds += 1;}else if (this.minutes < 60) {this.minutes += 1;this.seconds  = 0;}else{this.hours  += 1;this.minutes=0;}this.printElapsedTime();setTimeout("timer.incrementTimer()", 1000);}}function gameMechanics(){this.lockBoard= false;this.moveHistory= new Array(2);this.moveHistory[0]	= false;this.moveHistory[1]	= false;this.cardHistory= new Array(2);this.cardHistory[0]= false;this.cardHistory[1]= false;this.matchHistory= 0;this.numCardsInPlay= 0;this.addMove= addMove;this.removeMove= removeMove;this.addMatch			= addMatch;}function addMove(cardIdentifier) {var cardID	= cardIdentifier;if(cardIdentifier == this.moveHistory[0]  || cardIdentifier == this.moveHistory[1]){}else if(this.moveHistory[0]==false){this.cardHistory[0] = cardIdentifier;var cardIdentifier=eval(cardIdentifier+'.faceImage;');this.moveHistory[0]=cardIdentifier;}else if(this.moveHistory[1]==false){this.cardHistory[1]=cardIdentifier;var cardIdentifier=eval(cardIdentifier+'.faceImage;');this.moveHistory[1]=cardIdentifier;}eval(cardID+'.isFaceUp'+'=true;');}function removeMove(cardIdentifier){var cardID=cardIdentifier;var cardIdentifier=eval(cardIdentifier+'.faceImage;');if(cardIdentifier==this.moveHistory[0]){this.moveHistory[0]=false;this.cardHistory[0]=false;}else if(cardIdentifier==this.moveHistory[1]){this.moveHistory[1]=false;this.cardHistory[1]=false;}eval(cardID+'.isFaceUp'+'=false;');}function addMatch(){if(this.moveHistory[0]==this.moveHistory[1]){this.matchHistory+=2;this.numCardsInPlay=0; document.getElementById(gameMechanics.cardHistory[1]).style.filter= "alpha(opacity = 50)"; document.getElementById(gameMechanics.cardHistory[1]).style.zoom= "1"; document.getElementById(gameMechanics.cardHistory[0]).style.filter= "alpha(opacity = 50)"; document.getElementById(gameMechanics.cardHistory[0]).style.zoom= "1"; document.getElementById(gameMechanics.cardHistory[0]).style.opacity= ".5"; document.getElementById(gameMechanics.cardHistory[1]).style.opacity= ".5"; if(this.matchHistory==NUM_CARDS){timer.isOnOff=false;leaderBoard();return;}eval(gameMechanics.cardHistory[0]+'.isFound'+'=true;');eval(gameMechanics.cardHistory[1]+'.isFound'+'=true;');this.moveHistory[0]=false;this.moveHistory[1]=false;}else{return-1;}}function card(image,text){this.isFound=false;this.isFaceUp=false;this.faceImage=image;this.cardText=text;this.flipCard=flipCard;this.drawCard=drawCard;}function flipCard(cardID){if(timer.isOnOff==true&&this.isFound!=true&&this.isFaceUp==false&&gameMechanics.numCardsInPlay<2&&gameMechanics.lockBoard==false){this.drawCard('front',cardID);gameMechanics.addMove(cardID);gameMechanics.numCardsInPlay+=1;timer.clicks+=1;var isMatch=gameMechanics.addMatch(cardID);if(timer.isOnOff==true&&isMatch==-1&&gameMechanics.numCardsInPlay==2){gameMechanics.lockBoard=true;setTimeout(function(){this.drawCard('back',gameMechanics.cardHistory[0]);this.drawCard('back',gameMechanics.cardHistory[1]);gameMechanics.removeMove(gameMechanics.cardHistory[0]);gameMechanics.removeMove(gameMechanics.cardHistory[1]);gameMechanics.numCardsInPlay=0;gameMechanics.lockBoard=false;},750);}}else if(timer.isOnOff==true&&this.isFaceUp==true&&this.isFound!=true&&gameMechanics.lockBoard==false){timer.clicks+=1;this.drawCard('back',cardID);gameMechanics.removeMove(cardID);gameMechanics.numCardsInPlay-=1;}}function drawCard(side,cardID){if(side=='front'){var htmlIMG=document.getElementById(cardID);htmlIMG.style.backgroundImage="url(media/images/cards/front/"+this.faceImage+")";htmlIMG.lastChild.innerHTML=this.cardText;htmlIMG.lastChild.style.display="block";}else if(side=='back'){var htmlIMG=document.getElementById(cardID);var src=IMAGES_PATH+"/"+"cards/back/"+sponsors[0];htmlIMG.style.backgroundImage="url(" + src + ")";htmlIMG.lastChild.style.display="none";}}function leaderBoard(){document.getElementById("userData").style.display="block"; document.getElementById("gameBoard").style.display="none"; document.getElementById("timer").style.display="none"; var playerTime=document.getElementById("playerTime");var tmpMinutes=timer.minutes;tmpMinutes *= 60;var tmpTime=tmpMinutes+timer.seconds;playerTime.value=tmpTime;var playerClicks=document.getElementById("playerClicks");playerClicks.value=timer.clicks;}