/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
Schlidey 1.0 ©2000 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.
*/

function initialise() {
	//set up variables and arrays
	playing = false;
	moveNumber = 0-howHard;
	boardSize = Math.pow(sideLength,2);
	boardArray = new Array(boardSize);
	boardArray[0]="!";
	for (n=1;n<boardSize;n++) {
		boardArray[n]=n;
	}
	boardArray[boardSize]="B";
	//mix up pieces
	whichSq=1;
	colPosition=1;
	n=0;
	lastDone = 0;
	mixUp()
}

function restart() {
	if (playing) {
		document.getElementById("coverID").style.visibility="visible";
		playing = false;
		document.getElementById("moveNumberID").innerHTML = "";
		initialise();
	}
}


function mixUp() {
	if (n<howHard) {
		setTimeout("doMixUp()",1);
	} else {
		document.getElementById("coverID").style.visibility="hidden";
		playing = true;
	}
}


function doMixUp() {
	whichSq = container('B')
	colPosition = whichSq%sideLength
	if (colPosition==0) {colPosition=sideLength};
	switch (Math.round(Math.random()*4)) {
		//case 1 is swap the piece with the one to the left
		case 1: 
			if ((colPosition!=1)&&(lastDone!=2)) {
				swap(whichSq,whichSq-1);
				n++;
				lastDone = 1;
				break;
			}
		//case 2 is swap the piece with the one to the right
		case 2: 
			if ((colPosition!=sideLength)&&(lastDone!=1)) {
				swap(whichSq,whichSq+1);
				n++;
				lastDone = 2;
				break;
			}
		//case 3 is swap the piece with the one below
		case 3: 
			if ((whichSq+sideLength<boardSize)&&(lastDone!=4)) {
				swap (whichSq,whichSq+sideLength);
				n++;
				lastDone = 3;
				break;
			}
		//case 4 is swap the piece with the one above
		case 4: 
			if ((whichSq-sideLength>0)&&(lastDone!=3)) {
				swap (whichSq,whichSq-sideLength);
				n++;
				lastDone = 4;
				break;
			}
	}
	mixUp();
}


function swap(sqA,sqB) {
	//swap two pieces
	valueA=boardArray[sqA];
	valueB=boardArray[sqB];
	boardArray[sqA]=valueB;
	boardArray[sqB]=valueA;
	//redraw screen
	rowPosition=0;
	for (i=1;i<=boardSize;i++) {
		colPosition =i%sideLength
		if (colPosition==0) {
			colPosition=sideLength;
			}
		if (colPosition==1) {
			rowPosition++;
			}
		document.getElementById('square'+boardArray[i]).style.top=(rowPosition*tileWidth)-tileWidth;
		document.getElementById('square'+boardArray[i]).style.left=(colPosition*tileWidth)-tileWidth;
	}
	//check to see if it's done yet
	numberDone=0;
	for (x=1;x<boardSize;x++) {
		if (boardArray[x]==x) {
			numberDone++;
		}
	}
	moveNumber ++;
	if (playing) {
		document.getElementById("moveNumberID").innerHTML = moveNumber;
	}
	if ((numberDone==boardSize-1)&&(moveNumber>0)) {
		youWon(moveNumber);
	} else {
	}
	if ((numberDone==boardSize-1)&&(moveNumber<0)) {
		initialise(); // the programme sometimes rearranges the pieces into the original order!
	}
}


function container(whichChar) {
	for (y=1;y<=boardSize;y++) {
		if (boardArray[y]==whichChar) {
			return y;
			break;
		}
	}
}


function moveMe(clickSq) {
	colPosition = container(clickSq)%sideLength;
	if (colPosition==0) {
		colPosition=sideLength;
	}
	blankSq = container('B');
	switch (blankSq-container(clickSq)) {
		//case 1 is blank is on right side of clicked square
		case 1:
			if (colPosition!=sideLength) {
				swap (container(clickSq),blankSq);
			}
			break;
		//case -1 is blank is on left side of clicked square
		case -1:
			if (colPosition!=1) {
				swap (container(clickSq),blankSq);
			}
			break;
		//case sideLength is blank square is below clicked square
		case sideLength:
			swap (container(clickSq),blankSq);
			break;
		//case 0-sideLength is blank square is above clicked square
		case (0-sideLength):
			swap (container(clickSq),blankSq);
			break;
	}
}


