/*
 SameThryn v1.01 © 2002 Mat Connolley
Feel free to use a small part of this code
for your own javascript; I'm always happy to help,
and believe that sharing code is a good idea
but don't steal the whole thing and pretend you
wrote it - it's bad karma...

Oh, and sorry if the documentation is a bit
lousy... it's not my forte.
*/

sizeX = 15; // width of board
sizeY = 10; // height of board
tileTypes = 3; // number of different types to start with

playerName = ""
playing = false; // are we playing at the moment?
colsGone = 0; // how many columns have been entirely removed
inRemove = false; // are we currently removing tiles? if so, don't let them click!
goScore = 0; // score for this click
totalScore = 0; // total score this game
level = 1; // starting level
hLevel = 1; // highest level reached
hScore = 1; // highest score
tiles = new Array(sizeX); // the main array holding information about the tiles
sTiles = new Array(sizeX); // an array the same size showing which are currently selected
for (x=0;x<sizeX;x++) { 
	tiles[x] = new Array(sizeY); // create the y dimension of the tiles array
	sTiles[x] = new Array(sizeY); //create the y dimension of the selected tiles array
}

//preload the images
blankImg = new Image();
blankImg.src = "blank.gif";

img1 = new Image();
img1h = new Image();
img2 = new Image();
img2h = new Image();
img3 = new Image();
img3h = new Image();

function loadImages(set) {
	switch (set) {
		case "fruit":
			img1.src = "fruit/orange.gif";
			img1h.src = "fruit/orange_h.gif";
			img2.src = "fruit/apple.gif";
			img2h.src = "fruit/apple_n.gif";
			img3.src = "fruit/melon.gif";
			img3h.src = "fruit/melon_h.gif";
			break;
		case "planets":
			img1.src = "planets/earth.gif";
			img1h.src = "planets/earth_h.gif";
			img2.src = "planets/jupiter.gif";
			img2h.src = "planets/jupiter_h.gif";
			img3.src = "planets/neptune.gif";
			img3h.src = "planets/neptune_h.gif";
			break;
		case "closes":
			img1.src = "closes/lesley.gif";
			img1h.src = "closes/lesley_h.gif";
			img2.src = "closes/john.gif";
			img2h.src = "closes/john_h.gif";
			img3.src = "closes/margaret.gif";
			img3h.src = "closes/margaret_h.gif";
			break;
		case "pro" :
			img1.src = "pro/r.gif";
			img1h.src = "pro/r_h.gif";
			img2.src = "pro/g.gif";
			img2h.src = "pro/g_h.gif";
			img3.src = "pro/b.gif";
			img3h.src = "pro/b_h.gif";
			
	}
}


function drawBoard() { // draw the board out
	loadImages('fruit');
	document.write ('<table class="board" border="0" cellpadding="0" cellspacing="0">');
	for (y=0;y<sizeY;y++) {
		document.write('<tr>');
		for (x=0; x<sizeX;x++) {
			document.write('<td>');
			document.write('<img src="blank.gif" onclick="remove()" onmouseover="light(' + x + ',' + y + ')" id="x' + x + 'y' + y + '" />');
			document.write('</td>');
		}
		document.write('</tr>');
	}	
	document.write('</table>');
	newGame();//start the game
}

function newGame() { // start the game, resetting all values as necessary
	playing = false;
	colsGone = 0;
	inRemove = false;
	for (y=0;y<sizeY;y++) {
		for (x=0; x<sizeX;x++) {
			tiles[x][y] = Math.ceil(Math.random()*tileTypes); // regenerate the tiles
			sTiles[x][y] = false; //set the selected array to false
			document.images["x" + x + "y" + y].src = eval('img' + tiles[x][y]).src;
		}
	}
	playing = true;
}

function reset() {
	totalScore = 0;
	level = 1;
	document.getElementById("cScore").innerHTML = 0;
	document.getElementById("cLevel").innerHTML = 1;
	newGame();
}

function clearSelected() {
	for (y=0;y<sizeY;y++) {
		for (x=0; x<sizeX;x++) {
			if (sTiles[x][y]) {
				sTiles[x][y] = false;
				//document.all["x" + x + "y" + y].style.backgroundColor = loColour;
				document.images["x" + x + "y" + y].src = eval('img' + tiles[x][y]).src;
			}
		}
	}	
}

function lightSelected(x,y) {
	sTiles[x][y] = true;
	//document.all["x" + x + "y" + y].style.backgroundColor = hiColour;
	document.images["x" + x + "y" + y].src = eval('img' + tiles[x][y] + 'h').src;
}

function light(x,y) {
	if ((tiles[x][y]!="")&&(!inRemove)) {
		clearSelected();
		lightSelected(x,y);
		foundOthers = false;
		keepChecking = true;
		while (keepChecking) {
			keepChecking = false;
			for (y=0;y<sizeY;y++) {
				for (x=0;x<sizeX;x++) {
					if (!sTiles[x][y]) {
						if ((y>0)&&(tiles[x][y-1]==tiles[x][y])&&(sTiles[x][y-1])) {
							lightSelected(x,y);
							foundOthers = true;
							keepChecking=true;
							break;
						}
						if ((y+1<sizeY)&&(tiles[x][y+1]==tiles[x][y])&&(sTiles[x][y+1])) {
							lightSelected(x,y);
							foundOthers = true;
							keepChecking=true;
							break;
						}
						if ((x>0)&&(tiles[x-1][y]==tiles[x][y])&&(sTiles[x-1][y])) {
							lightSelected(x,y);
							foundOthers = true;
							keepChecking=true;
							break;
						}
						if ((x+1<sizeX)&&(tiles[x+1][y]==tiles[x][y])&&(sTiles[x+1][y])) {
							lightSelected(x,y);
							foundOthers = true;
							keepChecking=true;
							break;
						}
					}
				}
			}
		}
	}
	if (!foundOthers) {
		clearSelected();
	}
}


function clearTile(x,y) {
	//alert(x + "," + y);
	tiles[x][y] = "";
	sTiles[x][y] = false;
	if (document.images["x" + x + "y" + y]) {
		document.images["x" + x + "y" + y].src = blankImg.src;
		//document.all["x" + x + "y" + y].style.backgroundColor = loColour;
	}
}

function drawTile(x,y) {
	if (document.images["x" + x + "y" + y]) {
		if (tiles[x][y] == "") {
			document.images["x" + x + "y" + y].src = blankImg.src;
		} else {
			document.images["x" + x + "y" + y].src = eval('img' + tiles[x][y]).src;
		}
	}				
}

function moveTile(xFrom,yFrom,xTo,yTo) {
	if ((tiles[xFrom][yFrom]!="")||(tiles[xTo][yTo]!="")) {
		tiles[xTo][yTo] = tiles[xFrom][yFrom];
		clearTile(xFrom,yFrom);				
		drawTile(xTo,yTo);
	}
}

function remove() {
	if(playing) {
		playing = false;
		inRemove = true;
		keepChecking = false;
		for (y=0;y<sizeY;y++) {
			for (x=0; x<sizeX;x++) {
				if (sTiles[x][y]) {
					goScore ++;
					clearTile(x,y);
					keepChecking = true;
				} else if ((tiles[x][y]!="")&&(y+1<sizeY)&&(tiles[x][y+1] == "")){
					yDown = parseInt(y+1);
					moveTile(x,y,x,yDown);
					sTiles[x][yDown]=false;
					keepChecking = true;
				}
			}
		}
		
		if (keepChecking) {
			playing = true;
			setTimeout("remove()",1);
		} else {
			addScore(goScore);
			goScore = 0;
			checkBlankCols();
			inRemove = false;
		}
	}
}

function checkBlankCols() {
	blankCols = false;
	for (x=0;x<sizeX-colsGone;x++) {
		hasTiles = false;
		for (y=0; y<sizeY;y++) {
			if (tiles[x][y]!="") {
				hasTiles = true;
			}
		}
		if (!hasTiles) {
			blankCols = true;
			for (y=0;y<sizeY;y++) {
				if (x<sizeX-1) {
					moveTile(x+1,y,x,y);
				}
			}
		}
	}
	if (blankCols==true) {
		colsGone ++;;
		setTimeout("checkBlankCols()",1);
	} else {
		checkPlayable();
	}
}

function checkPlayable() {
	foundMatch = false;
	for (y=0;y<sizeY;y++) {
		for (x=0;x<sizeX;x++) {
			if (tiles[x][y]!="") {
				if ((y>0)&&(tiles[x][y-1]==tiles[x][y])) {
					foundMatch = true;
					break;
				}
				if ((y+1<sizeY)&&(tiles[x][y+1]==tiles[x][y])) {
					foundMatch = true;
					break;
				}
				if ((x>0)&&(tiles[x-1][y]==tiles[x][y])) {
					foundMatch = true;
					break;
				}
				if ((x+1<sizeX)&&(tiles[x+1][y]==tiles[x][y])) {
					foundMatch = true;
					break;
				}
			}
		}
	}
	if (!foundMatch) {
		endGame();
	} else {
		playing = true;
	}
}

function endGame() {
	if (colsGone==sizeX) {
		totalScore += 1000;
		level ++;
		document.getElementById("cScore").innerHTML = totalScore;
		if (totalScore>hScore) {
			hScore = totalScore;
			document.getElementById("hScore").innerHTML = totalScore;
		}
		document.getElementById("cLevel").innerHTML = level;
		if (level>hLevel) {
			hLevel = level;
			document.getElementById("hLevel").innerHTML = level;
		}
		alert("well done");
		newGame();
	} else {
		playing = false;
		alert("game over");
		playerName = prompt("Please enter your name for the high score board : ", playerName);
		window.open("http://matnkat.com/toys/same/score.php?sco_score=" + totalScore + "&sco_name=" + playerName + "", "scoreWin", "height=350, width=300, scrollbars=yes, resizeable");
	}
}

function addScore(goScore) {
	if (goScore>0) {
		goScore --;
		totalScore += goScore*goScore*level*tileTypes;
		document.getElementById("cScore").innerHTML = totalScore;
		if (totalScore>hScore) {
			hScore = totalScore;
			document.getElementById("hScore").innerHTML = totalScore;
		}
	}
};