// Java_Game
/* 
	Ralph Tracy
	Imd 210: Fundamentals of Scripting Languages
	Fall 2005	
	V1.0
*/
if (document.images) {
	pic_0= new Image(140, 101);
	pic_0.src=("game/images/0.jpg");
	pic_1= new Image(140, 101);
	pic_1.src=("game/images/1.jpg") ;
	pic_2= new Image(140, 101);
	pic_2.src=("game/images/2.jpg") ;
	pic_3= new Image(140, 101);
	pic_3.src=("game/images/3.jpg") ;
	pic_4= new Image(140, 101);
	pic_4.src=("game/images/4.jpg") ;
	pic_5= new Image(140, 101);
	pic_5.src=("game/images/5.jpg") ;
	pic_6= new Image(140, 101);
	pic_6.src=("game/images/5.jpg") ;
	pic_back= new Image(144, 101);
	pic_7= new Image(140, 101);
	pic_7.src=("game/images/0.jpg") ;
	pic_8= new Image(140, 101);
	pic_8.src=("game/images/1.jpg") ;
	pic_9= new Image(140, 101);
	pic_9.src=("game/images/2.jpg") ;
	pic_10= new Image(140, 101);
	pic_10.src=("game/images/3.jpg") ;
	pic_11= new Image(140, 101);
	pic_11.src=("game/images/4.jpg") ;
	pic_12= new Image(140, 101);
	pic_12.src=("game/images/5.jpg") ;
	pic_back= new Image(144, 101);
	pic_back.src=("game/images/back.jpg");
	
	placement = new Array(12); //contains a list of numbers (0-5) that represents which tile is where
								// each number (0-5) appears only twice in the list
								// corresponds to the numbered tiles in the html file
								
	userPicks = new Array(); //keeps track of which tiles, and how many, the user clicked on.
								// used to evaluate stage of game (wait for 2nd click or evaluate for match)
								
	tilesRemaining = 12; // number of tile remaining in game.  When equal to 0, the game is over.							
}

function placeTiles(){
	// Set up the tile fronts by randomly assigning pictures to the tiles.
	// Along the way, check to make sure that an image is not used more than twice.
	// Use an array to keep track of which tile has which image (defined by the placement array)
	
	var picTracker = new Array(0, 0, 0, 0, 0, 0);
	var goodPic = false;
	var usePic;
	
	for (var i = 0; i < placement.length; i++) {
		do {
			usePic = Math.floor(Math.random() * 6);
			if (picTracker[usePic] <2) {
				placement[i] = usePic; 
				picTracker[usePic]++;
				goodPic = true;
			} else {
				goodPic = false;
			}			
		} while (!goodPic);
	}
}

function swapImage(useType, useImage) {
	if (useType == "front") {
		document["tile_" + useImage].src = window["pic_" + placement[useImage]].src;
	}
	else if (useType == "back") {
	    document["tile_" + useImage].src = pic_back.src;
	}
}

function flipTiles (useTile) {
	if (userPicks.length < 2) {
		swapImage("front", useTile);
		userPicks.push(useTile);
	}
	if (userPicks.length == 2) {
		tileCheck = window.setInterval("checkMatch()", 500);
	}
}

function checkMatch () {
	if (placement[userPicks[0]]== placement[userPicks[1]]) {
			document.getElementById("tile_" + userPicks[0]).style.visibility = "hidden";
			document.getElementById("tile_" + userPicks[1]).style.visibility = "hidden";
			tilesRemaining -= 2; // same as tilesRemaining = tilesRemaining - 2
		} else{
			swapImage("back", userPicks[0]);
			swapImage("back", userPicks[1]);
		}
		userPicks = [];
		
		if (tilesRemaining == 0) {
			gameOver();
		}
		window.clearInterval(tileCheck);
			
}

function gameOver() {
	window.alert("Game over!");
}
placeTiles();
