//Cluedo Pairs Game
//Written By: Paul Howls : http://www.cluedofan.com

function cards(){

tempCards = new Array("Peacock","Scarlett","White","Green","Mustard","Plum","Candlestick","Dagger","LeadPiping","Revolver","Rope","Spanner","Hall","Lounge","DiningRoom","Kitchen","Ballroom","Conservatory","BilliardRoom","Library","Study");

return tempCards;

}



function dealCards(){

  c1 = cards();
  c2 = cards();
  cardStack = (c1.join(",")+","+c2.join(",")).split(",");

  longGrid = shuffle(cardStack);

  highCnt = 1;

  for (i=0; i<42; i++){

      	if(i%7==0) document.write("<br>");
      	document.write("<a href='javascript:show("+i+");'><img src='../cluedoims/qmarkb.jpg' name='pic"+i+"' border='0'></a> ");

  }

}

var currCard = "";
var currNo = "";
var up = false;
var pairs = 0;
var doTick = true;
var lowestScore = 100000;
var numScores = 0;

function show(no){

	if(eval("document.pic"+no).src.indexOf("blankcard")>-1) {
		return;
	}

	if(up){

		if(currNo!=no){

			eval("document.pic"+no).src = "../cluedoims/"+longGrid[no]+".jpg";

			if(currCard==longGrid[no]){

				alert("Pair!");
				eval("document.pic"+no).src = "../cluedoims/blankcard.gif";
				eval("document.pic"+currNo).src = "../cluedoims/blankcard.gif";
				pairs++;

				if(pairs==21) {
				  doTick=false;
				  alert("You Win!");
				  
				  /*{Debug:"+(document.timer.time.value*1)+":"+lowestScore+":"+numScores
				  				  +":"+((document.timer.time.value*1)<lowestScore)
				  				  +":"+(numScores<10)
				  +":"+((((document.timer.time.value*1)<lowestScore)||(numScores<10)))+"}*/

				  if((((document.timer.time.value*1)<lowestScore)||(numScores<10))&&((document.timer.time.value*1)>0)) {

				  	winner = prompt("Your score of "+document.timer.time.value+" has made it into the top ten!\nPlease enter your name below:","");
					if((winner!="")&&(winner!="null")) {
						document.timer.winner.value=winner;
						document.timer.submit();
					} else {
						winner = prompt("No name entered! Your score of "+document.timer.time.value+" has made it into the top ten!\nPlease enter your name below or it will not be saved:","");
						if((winner!="")&&(winner!="null")) {
							document.timer.winner.value=winner;
							document.timer.submit();
						}
					}

				  } else alert("Sorry, you didn't make it into the top ten. Better luck next time.");
				}

			} else {

				alert("No Pair");
				eval("document.pic"+no).src = "../cluedoims/qmarkb.jpg";
				eval("document.pic"+currNo).src = "../cluedoims/qmarkb.jpg";

			}

			up = false;
			currCard = "";
			currNo = "";

		}

	} else {

		eval("document.pic"+no).src = "../cluedoims/"+longGrid[no]+".jpg";

		up = true;
		currCard = longGrid[no];
		currNo = no;

	}

}

var startTime;

function tick1(){

	startTime = new Date();
	setTimeout('tick();',1000);

}

function tick(){

	var now = new Date();
	if(doTick){
		duration = Math.floor((now - startTime)/1000);
		document.timer.time.value = duration;
		document.timer.timedisplay.value=duration;
		setTimeout('tick();',1000);
	}

}

var highScoresString = highScores();
var highScoresArray = highScoresString.split("_");

function showHighScores(){

	hsLen = Math.floor(highScoresArray.length / 2);

	for (i=0;i<hsLen;i++){

		document.write("<li>"+highScoresArray[i*2]+" (Time: "+highScoresArray[i*2+1]+")</li>")
		lowestScore = highScoresArray[i*2+1];
		numScores++;

	}

}

