/***************************************
*	Diamond Slide
*	version 1.0
*	last revision: 02.01.2004
*	steve@slayeroffice.com
*
*	source & images (c) Steven G. Chipman
*	-- please do not use without permission! --
*
***************************************/

this.onload = init;
document.onkeyup = captureKeyEvent;

var sqrData = new Array();
var curSelected = 0; 
var isMoving = new Array(0,0);
var curRow=0,curCol=0;
var score = 0;
var clearIncrement = 0, clearType = 0, clearIndex = 0;
var opacity = document.all?100:1.0;
var gameTime = "120";
var nGameTime = parseInt(gameTime);
var arrowDirection = new Array(0,0);
var globalOrigin, globalDirection;
var additionals = new Array();

var columns = new Array();
columns[0] = new Array(0,5,10,15,20);
columns[1] = new Array(1,6,11,16,21);
columns[2] = new Array(2,7,12,17,22);
columns[3] = new Array(3,8,13,18,23);
columns[4] = new Array(4,9,14,19,24);

var rows = new Array();
rows[0] = new Array(0,1,2,3,4);
rows[1] = new Array(5,6,7,8,9);
rows[2] = new Array(10,11,12,13,14);
rows[3] = new Array(15,16,17,18,19);
rows[4] = new Array(20,21,22,23,24);

var bgPositions = new Array(0,-19,-38,-58,-77,-96,-115,-135,-154,-174);

function startGame() {
	kInterval = setInterval("fadeGameLogo()",20)
}


function fadeGameLogo() {
	opacity-=document.all?5:0.05;
	document.all?document.getElementById("gameLogo").style.filter="alpha(opacity="+opacity+")":document.getElementById("gameLogo").style.opacity = opacity;
	if(opacity<=0) {
		clearInterval(kInterval);
		document.getElementById("gameLogo").style.display = "none";
		document.getElementById("mTop").style.display = "block";	
		document.getElementById("mLeft").style.display = "block";
		document.getElementById("info").style.display = "block";

		opacity = document.all?100:1.0;
		tInterval = setInterval("countdown()",1000);
		sInterval = setInterval("animateArrow()",20);
		checkMatch();
	}
}

function init() {
	createGameElements();
	createCountDownTimer();
	createScoreBox();
	pickRandomSquares();
	setSquareColors();
}

function countdown() {
	nGameTime--;
	if(nGameTime<0) {
		clearInterval(tInterval);
		clearInterval(sInterval);
		document.getElementById("gameOver").style.display = "block";
		opacity=0;
		kInterval = setInterval("fadeGameOver()",10);
	}
	r = nGameTime.toString();
	if(nGameTime<100 && nGameTime>=10) {
		r = "0" + r;
	} else if(nGameTime<10) {
		r = "00" + r;
	}
	setImages(r,"mNum");
}

function fadeGameOver() {
	opacity+=document.all?5:0.05;
	document.all?document.getElementById("gameOver").style.filter="alpha(opacity="+opacity+")":document.getElementById("gameOver").style.opacity = opacity;
	maxOp = document.all?90:0.9;
	if(opacity>=maxOp)clearInterval(kInterval);
}

function createCountDownTimer() {
	x = 0;
	for(i=0;i<gameTime.length;i++) {
		z = document.getElementById("mTime").appendChild(document.createElement("div"));
		z.className = "number";
		z.id = "mNum" + i;
		z.style.left = x + "px";
		x+=18;
		n = parseInt(gameTime.charAt(i));
		z.style.backgroundPosition = "0px " + bgPositions[n] + "px";
	}
}

function createScoreBox() {
	x = 0;
	for(i=0;i<3;i++) {
		z = document.getElementById("mScore").appendChild(document.createElement("div"));
		z.className = "number";
		z.id = "nScore" + i;
		z.style.backgroundImage = "url(http://slayeroffice.com/arcade/diamond_slide/numbers_black.gif)";
		z.style.left = x + "px";
		x+=18;
	}
}

function incrementScore() {
	score++;
	zScore = score.toString();
	if(score<10) {
		zScore = "00" + zScore;
	} else if(score<100 && score>=10) {
		zScore = "0" + zScore;
	}

	setImages(zScore,"nScore");
}

function setImages(curNum,objName) {
	for(i=0;i<curNum.length;i++) {
		try {
			n = parseInt(curNum.charAt(i));
			if(nGameTime<=10 && objName == "mNum") document.getElementById(objName + i).style.backgroundImage = "url(http://slayeroffice.com/arcade/diamond_slide/numbers_red.gif)";
			document.getElementById(objName + i).style.backgroundPosition = "0px " + bgPositions[n]  +"px";
		} catch(err) { return; }
	}
}

function animateArrow() {
	if(!curSelected) {
		if(isMoving[0]) slideArrow(1,globalDirection,globalOrigin);
		y = parseInt(document.getElementById("mTop").offsetTop);
		arrowDirection[0]?y++:y--;
		document.getElementById("mTop").style.top = y + "px";
		if(y == -50) {
			arrowDirection[0] = 1;
		}else if (y == -36) {
			arrowDirection[0]=0;
		}
	} else {
		if(isMoving[1]) slideArrow(0,globalDirection,globalOrigin);
		x = parseInt(document.getElementById("mLeft").offsetLeft);
		arrowDirection[1]?x++:x--;
		document.getElementById("mLeft").style.left = x + "px";
		if(x == -50) {
			arrowDirection[1] = 1;
		}else if (x == -36) {
			arrowDirection[1]=0;
		}
	}
}

function slideArrow(arrow,direction,origin) {
	destination = direction?origin+36:origin-36;
	if(!arrow) {
		y = document.getElementById("mLeft").offsetTop;
		y+=globalDirection?5:-5;
		document.getElementById("mLeft").style.top = y + "px";
		document.getElementById("horizontalSelector").style.top = y + "px";
		if((direction && y>=destination) || (!direction && y<=destination)) {
			document.getElementById("mLeft").style.top = destination + "px";
			document.getElementById("horizontalSelector").style.top = destination + "px";
			isMoving[1] = 0;
		}
	} else {
		x = document.getElementById("mTop").offsetLeft;
		x+=globalDirection?5:-5;
		document.getElementById("mTop").style.left = x + "px";
		document.getElementById("verticalSelector").style.left = x + "px";
		if((direction && x>=destination) || (!direction && x<=destination)) {
			document.getElementById("mTop").style.left = destination + "px";
			document.getElementById("verticalSelector").style.left = destination + "px";
			isMoving[0] = 0;
		}
	}
}

function createGameElements() {
	x=1; y=1; mHTML = "";


	for(i=0;i<25;i++) {
		mHTML+="<div id=\"mSqr\" name=\"mSqr\" class=\"sqr\" style=\"top:" + y + "px;left:" + x + "px;\" onclick=\"handleClick(this.id)\"></div>";
		x+=36;
		if(x>=(36*5)) {
			x=1; y+=36;
		}
	}
	document.getElementById("mContainer").innerHTML = mHTML;

	z = document.getElementById("mContainer").appendChild(document.createElement("div"));
	z.id = "gameLogo";
	z.onclick = startGame;

	z = document.getElementById("mContainer").appendChild(document.createElement("div"));
	z.style.left = "-40px";
	z.style.top = "1px"
	z.id = "mLeft";

	z = document.getElementById("mContainer").appendChild(document.createElement("div"));
	z.style.left = "1px";
	z.style.top = "-40px";
	z.id = "mTop";

	z = document.getElementById("mContainer").appendChild(document.createElement("div"));
	z.id = "info";
	zHTML = "<div class=\"nfo\" id=\"mTime\"></div>";
	zHTML += "<div class=\"nfo\" id=\"mScore\"></div>";
	document.getElementById("info").innerHTML = zHTML;

	z = document.getElementById("mContainer").appendChild(document.createElement("div"));
	z.id = "gameOver";
	z.onclick = newGame;

	z = document.getElementById("mContainer").appendChild(document.createElement("div"));
	z.id = "verticalSelector";

	z = document.getElementById("mContainer").appendChild(document.createElement("div"));
	z.id = "horizontalSelector";

	if(navigator.userAgent.indexOf("Opera")>-1 || navigator.userAgent.indexOf("KHTML")>-1) {
		document.getElementById("verticalSelector").style.backgroundColor = "transparent";
		document.getElementById("horizontalSelector").style.backgroundColor = "transparent";
	}
}

function newGame() {
	location.reload();
}

function pickRandomSquares() {
	sqrData[0] = new Array();
	for(i=0;i<20;i++) {
		do {
			ran = Math.floor(Math.random()*25);
		} while(!isUnique(ran,sqrData[0]));
		sqrData[0][i] = ran;
	}
}

function setSquareColors() {
	sqrData[1] = new Array();
	for(i=0;i<sqrData[0].length;i++) {
		color = Math.floor(Math.random()*2)?-35:-70;
		document.getElementsByName("mSqr")[sqrData[0][i]].style.backgroundPosition = color + "px";
		sqrData[1][sqrData[0][i]] = color;
	}
	for(d=0;d<sqrData[1].length;d++)if(!sqrData[1][d])sqrData[1][d]=-1;
}

function isUnique(num,arrayName) {
	for(p=0;p<arrayName.length;p++) {
		if(arrayName[p] == num)return false;
	}
	return true;
}

function toggleSelected() {
	curSelected = curSelected?0:1;
	if(curSelected) {
		document.getElementById("mTop").style.top = "-40px";
		document.getElementById("verticalSelector").style.display = "none";
		document.getElementById("horizontalSelector").style.display = "block";
	} else {
		document.getElementById("mLeft").style.left = "-40px";
		document.getElementById("verticalSelector").style.display = "block";
		document.getElementById("horizontalSelector").style.display = "none";
	}
}

function moveTopSelector(direction) {
	if(isMoving[0])return;
	switch(direction) {
		case 0:
			curCol++;
			if(curCol>4) {
				curCol=4;
				return;
			}
			x = parseInt(document.getElementById("mTop").style.left);
			isMoving[0] = 1;
			globalDirection=1; globalOrigin=x;
			break;
		case 1:
			curCol--;
			if(curCol<0) {
				curCol=0;
				return;
			}
			x = parseInt(document.getElementById("mTop").style.left);
			isMoving[0] = 1;
			globalDirection=0; globalOrigin=x;
			break;
	}
	//document.getElementById("mTop").style.left = x + "px";
}

function moveLeftSelector(direction) {
	if(isMoving[1])return;
	switch(direction) {
		case 0:
			curRow++;
			if(curRow>4) {
				curRow=4;
				return;
			}
			y = parseInt(document.getElementById("mLeft").style.top);
			isMoving[1] = 1;
			globalDirection = 1; globalOrigin = y;
			break;
		case 1:
			curRow--;
			if(curRow<0) {
				curRow=0;
				return;
			}
			y = parseInt(document.getElementById("mLeft").style.top);
			isMoving[1] = 1;
			globalDirection = 0; globalOrigin = y;
			break;
	}
}

function doShift(direction) {
	// 0: down; 1: up; 2: right; 3:left
	// check if the column can be shifted:
	if(!canShift(direction))return;

	switch(direction) {
		case 0:
			for(i=4;i>0;i--) {
				if(sqrData[1][columns[curCol][i]] == -1) {
					k = i;
					do {
						k--;
					} while(sqrData[1][columns[curCol][k]] == -1);
					if(k<0)break;
					sqrData[1][columns[curCol][i]] = sqrData[1][columns[curCol][k]];
					try {
					document.getElementsByName("mSqr")[columns[curCol][i]].style.backgroundPosition = sqrData[1][columns[curCol][k]] + "px";
					document.getElementsByName("mSqr")[columns[curCol][k]].style.backgroundPosition = "0px";
					} catch(err) { sqrData[1][columns[curCol][k]] = -1;break; }
					sqrData[1][columns[curCol][k]] = -1;
				}
			}
			break;
		case 1:
			for(i=0;i<4;i++) {
				if(sqrData[1][columns[curCol][i]] == -1) {
					k = i;
					do {
						k++;
					} while(sqrData[1][columns[curCol][k]] == -1)
					if(k>4)break;
					sqrData[1][columns[curCol][i]] = sqrData[1][columns[curCol][k]];
					try {
					document.getElementsByName("mSqr")[columns[curCol][i]].style.backgroundPosition = sqrData[1][columns[curCol][k]] + "px";
					document.getElementsByName("mSqr")[columns[curCol][k]].style.backgroundPosition = "0px";
					} catch(err) { sqrData[1][columns[curCol][k]] = -1;break; }
					sqrData[1][columns[curCol][k]] = -1;
				}
			}
			break;
		case 2:
			for(i=4;i>0;i--) {
				if(sqrData[1][rows[curRow][i]] == -1) {
					k = i;
					do {
						k--;
					} while(sqrData[1][rows[curRow][k]] == -1)
					if(k<0)break;
					sqrData[1][rows[curRow][i]] = sqrData[1][rows[curRow][k]];
					try {
					document.getElementsByName("mSqr")[rows[curRow][i]].style.backgroundPosition = sqrData[1][rows[curRow][k]] + "px";
					document.getElementsByName("mSqr")[rows[curRow][k]].style.backgroundPosition = "0px";
					} catch(err) { sqrData[1][columns[curCol][k]] = -1;break; }
					sqrData[1][rows[curRow][k]] = -1;
				}
			}
			break;
		case 3:
			for(i=0;i<4;i++) {
				if(sqrData[1][rows[curRow][i]] == -1) {
					k = i;
					do {
						k++;
					} while(sqrData[1][rows[curRow][k]] == -1)
					if(k>4)break;
					sqrData[1][rows[curRow][i]] = sqrData[1][rows[curRow][k]];
					try {
					document.getElementsByName("mSqr")[rows[curRow][i]].style.backgroundPosition = sqrData[1][rows[curRow][k]] + "px";
					document.getElementsByName("mSqr")[rows[curRow][k]].style.backgroundPosition = "0px";
					} catch (err) { sqrData[1][columns[curCol][k]] = -1;break; }
					sqrData[1][rows[curRow][k]] = -1;
				}
			}
			break;

	}
	checkMatch();
}

function checkMatch() {
	// check rows:
	m=0;
	for(i=0;i<rows.length;i++) {
		matchColor = sqrData[1][rows[i][0]];
		for(z=1;z<rows[i].length;z++) {
			if(matchColor == -1 || matchColor != sqrData[1][rows[i][z]]) {
				m=0;
				break;
			} else {
				m++;
			}
		}
		if(m==4) {
			clearLine(i,0);
			break;
		}
	}
	if(m)return;
	//columns:
	m=0;
	for(i=0;i<columns.length;i++) {
		matchColor = sqrData[1][columns[i][0]];
		for(z=1;z<columns[i].length;z++) {
			if(matchColor == -1 || matchColor != sqrData[1][columns[i][z]]) {
				m=0;
				break;
			} else {
				m++;
			}
		}
		if(m==4) { 
			clearLine(i,1);
			break;
		}
	}
}

function clearLine(index,type) {
	if(!type) {
		for(p=0;p<rows[index].length;p++) {
			sqrData[1][rows[p]] = -1
			document.getElementsByName("mSqr")[rows[index][p]].style.backgroundPosition = "-105px";
		}
	} else {
		for(p=0;p<columns[index].length;p++) {
			sqrData[1][rows[p]] = -1;
			document.getElementsByName("mSqr")[columns[index][p]].style.backgroundPosition = "-105px";
		}
	}
	clearType = type; clearIndex = index; clearIncrement = 0;
	zInterval = setInterval("animateClear()",50);
}

function animateClear() {
	opacity-=document.all?5:0.05;
	if(!clearType) {
		for(e=0;e<rows[clearIndex].length;e++) {
			if(document.all) {
				document.getElementsByName("mSqr")[rows[clearIndex][e]].style.filter="alpha(opacity="+opacity+")";
			} else {
				document.getElementsByName("mSqr")[rows[clearIndex][e]].style.opacity = opacity;
			}
		}
	} else {
		for(e=0;e<columns[clearIndex].length;e++) {
			if(document.all) {
				document.getElementsByName("mSqr")[columns[clearIndex][e]].style.filter="alpha(opacity="+opacity+")";
			} else {
				document.getElementsByName("mSqr")[columns[clearIndex][e]].style.opacity = opacity;
			}
		}
	}
	if(opacity<=0) {
		clearInterval(zInterval);
		cleanUp(clearType);
	}
}

function cleanUp(type) {
	if(!type) {
		for(r=0;r<rows[clearIndex].length;r++) {
			sqrData[1][rows[clearIndex][r]] = -1;
			document.getElementsByName("mSqr")[rows[clearIndex][r]].style.backgroundPosition = "0px";
		}
	} else {
		for(r=0;r<columns[clearIndex].length;r++) {
			sqrData[1][columns[clearIndex][r]] = -1;
			document.getElementsByName("mSqr")[columns[clearIndex][r]].style.backgroundPosition = "0px";
		}
	}
	incrementScore();
	addMore();
}

function addMore() {
	additionals = new Array();
	for(i=0;i<5;i++) {
		do {
			ran = Math.floor(Math.random() * 24);
		} while(sqrData[1][ran] != -1);// && !isUnique(ran,additionals));
		additionals[i] = ran;
		sqrData[1][ran] = Math.floor(Math.random()*2)?-35:-70;
		document.getElementsByName("mSqr")[ran].style.backgroundPosition = sqrData[1][ran] + "px";
		document.all?document.getElementsByName("mSqr")[ran].style.filter="alpha(opacity=0)":document.getElementsByName("mSqr")[ran].style.opacity = 0.0;
	}
	opacity=0;
	aInterval = setInterval("animateAdditions()",20);
}

function animateAdditions() {
	opacity+=document.all?5:0.05;
	for(w=0;w<additionals.length;w++) {
		if(document.all) {
			document.getElementsByName("mSqr")[additionals[w]].style.filter="alpha(opacity=" + opacity + ")";
		} else {
			document.getElementsByName("mSqr")[additionals[w]].style.opacity = opacity;
		}
	}
	maxOp = document.all?100:1.0;
	if(opacity>=maxOp) {
		clearInterval(aInterval);
		resetAllOpacity();
		checkMatch();
	}
}


function resetAllOpacity() {
	for(i=0;i<document.getElementsByName("mSqr").length;i++) {
		if(document.all) {
			document.getElementsByName("mSqr")[i].style.filter="alpha(opacity=100)";
		} else {
			document.getElementsByName("mSqr")[i].style.opacity = 1.0;
		}
	}
}

function canShift(direction) {
	resetNulls();
	// 0: down; 1: up; 2: right; 3:left
	switch(direction) {
		case 0:
			for(i=1;i<columns[curCol].length;i++) {
				if(!sqrData[1][columns[curCol][i]])sqrData[1][columns[curCol][i]] = -1;
				if(sqrData[1][columns[curCol][i]] == -1 || !sqrData[1][columns[curCol][i]])return true;
			}
			return false;
			break;
		case 1:
			for(i=3;i>-1;i--) {
				if(!sqrData[1][columns[curCol][i]])sqrData[1][columns[curCol][i]] = -1;
				if(sqrData[1][columns[curCol][i]] == -1 || !sqrData[1][columns[curCol][i]])return true;

			}
			return false;
			break;
		case 2:
			for(i=1;i<rows[curRow].length;i++){
				if(!sqrData[1][rows[curRow][i]])sqrData[1][rows[curRow][i]] = -1;
				if(sqrData[1][rows[curRow][i]] == -1 || !sqrData[1][rows[curRow][i]])return true;
			}
			return false;
			break;
		case 3:
			for(i=3;i>-1;i--){
				if(!sqrData[1][rows[curRow][i]])sqrData[1][rows[curRow][i]] = -1;
				if(sqrData[1][rows[curRow][i]] == -1 || !sqrData[1][rows[curRow][i]])return true;
			}
			return false;
			break;

	}
}

function captureKeyEvent(e) {
	keyCode = document.all?window.event.keyCode:e.keyCode;
	switch(keyCode) {
		case 32:
			toggleSelected();
			break;
		case 39:
			if(curSelected) {
				doShift(2);
			} else {
				moveTopSelector(0);
			}
			break;
		case 37:
			if(curSelected) {
				doShift(3);
			} else {
				moveTopSelector(1);
			}
			break;
		case 40:
			if(curSelected) {
				moveLeftSelector(0);
			} else {
				doShift(0);
			}
			break;
		case 38:
			if(curSelected) {
				moveLeftSelector(1);
			} else {
				doShift(1);
			}
			break;
	}
}

function resetNulls() {
	//stupid hack to fix IE setting these index values to null somewhere
	for(j=0;j<sqrData[1].length;j++)if(!sqrData[1][j])sqrData[1][j] == -1;
}
