//	Slot Puzzle Game
//	written by Stephen Battey - August 1997
//	modified July 1999
//	 for DEBUGGED @ http://www.truplex.com




//------------------------------------------------
//		Global Game Variables
//------------------------------------------------


PlayMode=0

HoleCol=1
HoleRow=1
InPosition=0

Moves=0
Seconds=0
Minutes=0

ShuffleDepthUsed=0
ShufflesLeft=0
ShuffleDelay=10
NoNextMov=0

PuzNo=0
PuzName = new Array("Chur","Lake","Dog")
PuzTitle = new Array("Church","Boat House","Jack")
PuzMovesTableId = new Array(11,13,15)
PuzTimesTableId = new Array(12,14,16)


ImageTags = new Array()
for (i=1; i<6; i++)
	ImageTags[i] = new Array()

if ( navigator.userAgent.indexOf("SunOS")>-1 )
	PicOffset=21
else
	PicOffset=-3





//------------------------------------------------
//		Low level functions
//------------------------------------------------



function NumText(v,width,use) {
//			returns the number 'v' in a text string of minimum
//			length 'width', preceeding spaces being filled with
//			the character specified in 'use'

	post=v.toString()
	pre=""
	uselen=width-post.length
	for (i=1; i<=uselen; i++)
		pre+=use

	return pre+post
}



function mMODn(m,n) {
//			Function returns the least
//			residue of :   m (mod n)

	while ( m>=n )
		m=m-n
	return m
}



now4Random = new Date()
mRandom=1024
cRandom=55
aRandom=13
rRandom=((now4Random.getSeconds()-1)/60)*mRandom

function Random(minv,maxv) {
//			Function returns a random number in the range
//			minv - maxv

	rRandom=mMODn(((aRandom*rRandom)+cRandom),mRandom)
	return ( ((rRandom/1024)*(maxv-minv))+minv )
}






//------------------------------------------------
//		Sub-Level Functions
//------------------------------------------------


function RefreshSlot(r,c) {
//			Refresh row 'r' col 'c' slot in puzzle

	ImNo=(r*5)+c+PicOffset
	document.images[ImNo].src=PuzName[PuzNo]+"Tile"+ImageTags[r][c]+".jpg"
}


function ResetSlate() {
//			set up a completed puzzle

	for (row=1; row<6; row++) {
		for (col=1; col<6; col++) {
			ImageTags[row][col]=row.toString()+col.toString()
			RefreshSlot(row,col)
		}
	}

	HoleRow=1
	HoleCol=1
	InPosition=25

}



function ShowTime() {
//			shows the time on the screen

	ToShow=NumText(Minutes,2," ")+":"+NumText(Seconds,2,"0")
	document.Disp.time.value=ToShow
}



function ShowMoves() {
//			show the number of moves taken on the screen

	document.Disp.moves.value=NumText(Moves,5," ")
}



function MovePiece(d) {
//			move the piece in the direction denoted by 'd'
//			into the current empty slot
//			d :  1-up , 2-left , 3-right , 4-down

	if ( ( d==1 ) & ( HoleRow==1 ) )
		return -1
	if ( ( d==4 ) & ( HoleRow==5 ) )
		return -1
	if ( ( d==2 ) & ( HoleCol==1 ) )
		return -1
	if ( ( d==3 ) & ( HoleCol==5 ) )
		return -1

	if ( ( d==1 ) | ( d==4 ) ) {
		FromCol=HoleCol
		if ( d<2 )
			FromRow=HoleRow-1
		else
			FromRow=HoleRow+1
	} else {
		FromRow=HoleRow
		if ( d>2 )
			FromCol=HoleCol+1
		else
			FromCol=HoleCol-1
	}

	InPic=ImageTags[FromRow][FromCol]
	IntoRow=eval(InPic.substring(0,1))
	IntoCol=eval(InPic.substring(1,2))

	if ( ( IntoRow==FromRow ) & ( IntoCol==FromCol ) )
		InPosition--

	if ( ( HoleRow==1 ) & ( HoleCol==1 ) )
		InPosition--

	ImageTags[FromRow][FromCol]="11"
	ImageTags[HoleRow][HoleCol]=InPic
	RefreshSlot(HoleRow,HoleCol)
	RefreshSlot(FromRow,FromCol)

	if ( ( IntoRow==HoleRow ) & ( IntoCol==HoleCol ) )
		InPosition++

	if ( ( FromRow==1 ) & ( FromCol==1 ) )
		InPosition++

	HoleRow=FromRow
	HoleCol=FromCol

	return 0
}



function EndShuffle() {
//			end shuffling procedure

	PlayMode=2
	Moves=0
	ShowMoves()
	Minutes=0
	Seconds=0
	ShowTime()

	window.status="GO ! ! !"

	TimeTimer=window.setTimeout("UpdateTime()",1000)
}



function Shuffle() {
//			shuffle a piece in the puzzle

	donemove=false

	while ( !donemove ) {
		movedir=Math.floor(Random(1,4.99))
		if ( movedir!=NoNextMov ) {
			if ( MovePiece(movedir)==0 )
				donemove=true
		}
	}

	NoNextMov=5-movedir

	ShufflesLeft--
	if ( ShufflesLeft==0 )
		ShuffleTimer=window.setTimeout("EndShuffle()",ShuffleDelay)
	else
		ShuffleTimer=window.setTimeout("Shuffle()",ShuffleDelay)

	if ( ShufflesLeft==40 )
		window.status="READY..."
	if ( ShufflesLeft<20 )
		window.status="STEADY.."
}



function UpdateTime() {
//			move the clock counter on one second

	Seconds++
	if ( Seconds==60 ) {
		Seconds=0
		Minutes++
	}
	ShowTime()

	if ( ( Minutes==0 ) & ( Seconds==5 ) )
		window.status=""

	TimeTimer=window.setTimeout("UpdateTime()",1000)
}





//------------------------------------------------
//		Top-Level Functions
//------------------------------------------------




function StartShuffle() {
//			begin the shuffling of the puzzle

	if ( PlayMode==1 )
		clearTimeout(ShuffleTimer)

	if ( PlayMode==2 )
		clearTimeout(TimeTimer)

	ResetSlate()
	PlayMode=1

	ShuffleDepthUsed=200;
	ShufflesLeft=ShuffleDepthUsed

	NoNextMov=0

	ShuffleTimer=window.setTimeout("Shuffle()",ShuffleDelay)

}



function Move(ClickRow,ClickCol) {
//			handles move requests from the user

	if ( PlayMode<2 )
		return
	if ( ( ClickRow==HoleRow ) & ( ClickCol==HoleCol ) )
		return
	if ( ( ClickRow!=HoleRow ) & ( ClickCol!=HoleCol ) )
		return


	if ( ClickRow==HoleRow ) {
		if ( ClickCol<HoleCol ) {
			dir=2
			ToMove=HoleCol-ClickCol
		} else {
			dir=3
			ToMove=ClickCol-HoleCol
		}
	} else {
		if ( ClickRow<HoleRow ) {
			dir=1
			ToMove=HoleRow-ClickRow
		} else {
			dir=4
			ToMove=ClickRow-HoleRow
		}
	}

	for (m=1; m<=ToMove; m++)
		MovePiece(dir)


	Moves++
	ShowMoves()

	if ( InPosition==25) {
		clearTimeout(TimeTimer)
		PlayMode=0

	
		if (promptSubmitScore)
		{
			var scores = new Array();
			
			// submit num of moves for this puzzle and all puzzles
			scores[0] = new HiScore(  9, Moves, PuzTitle[PuzNo] );
			scores[1] = new HiScore( PuzMovesTableId[PuzNo], Moves, "" );
			
			// submit time taken for this puzzle and all puzzles
			var totalSeconds = (Minutes*60) + Seconds;
			scores[2] = new HiScore( 10, totalSeconds, PuzTitle[PuzNo] );
			scores[3] = new HiScore( PuzTimesTableId[PuzNo], totalSeconds, "" );

			promptSubmitScore( scores );
		}
	}
}



function ChangePuzzle() {
//			Changes the selected puzzle

	if ( PlayMode==1 )
		clearTimeout(ShuffleTimer)

	if ( PlayMode==2 )
		clearTimeout(TimeTimer)

	PlayMode=0
	PuzNo = window.document.GameType.puzzle.selectedIndex

	window.document.compPic.src=PuzName[PuzNo]+"Pic.jpg"
	ResetSlate()
}
