<!--

//Constants
var iOpen = 0;
var iBlock = 1;
var iStart = 2;
var iEnd = 3;

var iLeft = 37;
var iUp = 38;
var iRight = 39;
var iDown = 40;

var iPosX;
var iPosY;

var bNewMaze = false;

var sSpaceColor = "";
var sBlockColor = "";

//Other declarations
var bCanMove = false;

var sNewMazeTitle = "";

var arPlayerImages = new Array();
arPlayerImages[1] = new Image;
arPlayerImages[1].src = "images/player1.gif";
arPlayerImages[2] = new Image;
arPlayerImages[2].src = "images/player2.gif";
arPlayerImages[3] = new Image;
arPlayerImages[3].src = "images/player3.gif";
arPlayerImages[4] = new Image;
arPlayerImages[4].src = "images/player4.gif";

//Displays the maze
function LoadMaze()
{
		
	bCanMove = true;
		
	sSpaceColor = document.frmSettings.SpaceColor.value;
	sBlockColor = document.frmSettings.BlockColor.value;
	
	if( sSpaceColor == "" ) { sSpaceColor = sMazeSpaceColor; }
	if( sBlockColor == "" ) { sBlockColor = sMazeBlockColor; }

	iPosX = iStartPosX;
	iPosY = iStartPosY;
	
	document.all.MazeLayer.style.clip = "rect(0," + iTotalMazeWidth + "," + iTotalMazeHeight + ",0)";

}

function SetWindowStatus(x, y)
{
	window.status = x + "," + y;
}

//Reset the maze
function ResetMaze()
{

	//Reset the player position
	iPosX = iStartPosX;
	iPosY = iStartPosY;

	document.all.Player.style.pixelLeft = (iPosX - 1) * iCellWidth;
	document.all.Player.style.pixelTop = (iPosY - 1) * iCellHeight;

	bCanMove = true;

}

//Key handler
function CheckKeys()
{

	if( bCanMove ) {
							
		switch( event.keyCode ) {
			case iLeft:
				if( iPosX != 1 ) {
					if( arMaze[iPosY][iPosX - 1] == iOpen || arMaze[iPosY][iPosX - 1] == iStart || arMaze[iPosY][iPosX - 1] == iEnd ) {
						iPosX--;
						document.all.Player.style.pixelLeft = (iPosX - 1) * iCellWidth;
					}
				}
				break;
			case iUp:
				if( iPosY != 1 ) {
					if( (arMaze[iPosY - 1][iPosX] == iOpen || arMaze[iPosY - 1][iPosX] == iStart || arMaze[iPosY - 1][iPosX] == iEnd) ) {
						iPosY--;
						document.all.Player.style.pixelTop = (iPosY - 1) * iCellHeight;
					}
				}
				break;
			case iRight:
				if( iPosX != iGridWidth ) {
					if( arMaze[iPosY][iPosX + 1] == iOpen || arMaze[iPosY][iPosX + 1] == iStart || arMaze[iPosY][iPosX + 1] == iEnd ) {
						iPosX++;
						document.all.Player.style.pixelLeft = (iPosX - 1) * iCellWidth;
					}
				}
				break;
			case iDown:
				if( iPosY != iGridHeight ) {
					if( arMaze[iPosY + 1][iPosX] == iOpen || arMaze[iPosY + 1][iPosX] == iStart || arMaze[iPosY + 1][iPosX] == iEnd ) {						
						iPosY++;
						document.all.Player.style.pixelTop = (iPosY - 1) * iCellHeight;
					}
				}
				break;
		}

		if( arMaze[iPosY][iPosX] == iEnd ) { 
			alert("Congratulations, you have solved the maze!     ");
			StopPlay();			
		}

	}

}

function VerifyNewGrid()
{

	var iHeight = document.frmNewMaze.iMazeHeight.value;
	var iWidth = document.frmNewMaze.iMazeWidth.value;
		
	if( iHeight < 5 || iHeight > 100 ) { 
		alert("Please enter a height between 5 and 100.     ");
		document.frmNewMaze.iMazeHeight.focus();
		document.frmNewMaze.iMazeHeight.select();
		return false;
	}

	if( iWidth < 5 || iWidth > 100 ) { 
		alert("Please enter a width between 5 and 100.     ");
		document.frmNewMaze.iMazeWidth.focus();
		document.frmNewMaze.iMazeWidth.select();
		return false;
	}
	
	document.frmNewMaze.submit();
	
}

function CreateMaze()
{

	bNewMaze = true;

	iPosX = iStartPosX;
	iPosY = iStartPosY;
		
	sSpaceColor = document.frmSettings.SpaceColor.value;
	sBlockColor = document.frmSettings.BlockColor.value;
	
	if( sSpaceColor == "" ) { sSpaceColor = sMazeSpaceColor; }
	if( sBlockColor == "" ) { sBlockColor = sMazeBlockColor; }
		
	document.all.Grid1_1.style.backgroundColor = "#009900";
	document.all["Grid" + iGridWidth + "_" + iGridHeight].style.backgroundColor = "#FF0000";
	
	arMaze[1][1] = iStart;
	arMaze[iGridHeight][iGridWidth] = iEnd;
	
	document.all.MazeLayer.style.clip = "rect(0," + iTotalMazeWidth + "," + iTotalMazeHeight + ",0)";
	
	//document.all.Save.style.visibility = "visible";	
	//document.all.MazeTitle.innerHTML = "<FORM name='frmMazeTitle'><INPUT type='textbox' maxlength=25 name='txtMazeTitle'>&nbsp;&nbsp;<INPUT type='button' value='Save Title' onClick='SaveTitle()'></FORM>";

}

function SaveTitle()
{
	
	sNewMazeTitle = document.frmMazeTitle.txtMazeTitle.value;
	document.all.MazeTitle.innerHTML = sNewMazeTitle;

}

function SaveMaze()
{

	var sTitle = prompt("Enter maze title:     ", "");
	if( sTitle == "" ) { 
	
		alert("You have failed to comply!     ");
	
	} else {

		document.frmSaveMaze.SpaceColor.value = sSpaceColor;
		document.frmSaveMaze.BlockColor.value = sBlockColor;

		document.frmSaveMaze.GridHeight.value = iGridHeight;
		document.frmSaveMaze.GridWidth.value = iGridWidth;

		document.frmSaveMaze.CellHeight.value = iCellHeight;
		document.frmSaveMaze.CellWidth.value = iCellWidth;

		document.frmSaveMaze.StartPosX.value = iStartPosX;
		document.frmSaveMaze.StartPosY.value = iStartPosY;

		document.frmSaveMaze.EndPosX.value = iEndPosX;
		document.frmSaveMaze.EndPosY.value = iEndPosY;
		
		document.frmSaveMaze.SavePlayer.value = iPlayerImage;

		var sGridData = new String();

		for( var y = 1; y < parseInt(iGridHeight) + 1; y++ ) {
			if( y != 1 ) { sGridData += "|"; }
			for( var x = 1; x < parseInt(iGridWidth) + 1; x++ ) {
				sGridData += arMaze[y][x];
			}
		}

		document.frmSaveMaze.GridData.value = sGridData;
		document.frmSaveMaze.Title.value = sTitle;
		document.frmSaveMaze.submit();

	}		
	
}

function RefreshSettings()
{

	iPlayerImage = document.frmSettings.PlayerImage.value;
	document.all.imgPlayer.src = arPlayerImages[iPlayerImage].src;

	sSpaceColor = document.frmSettings.SpaceColor.value;
	sBlockColor = document.frmSettings.BlockColor.value;
	
	if( sSpaceColor == "" ) { sSpaceColor = sMazeSpaceColor; }
	if( sBlockColor == "" ) { sBlockColor = sMazeBlockColor; }

	var bgColor;

	for( var y = 1; y < parseInt(iGridHeight) + 1; y++ ) {
		for( var x = 1; x < parseInt(iGridWidth) + 1; x++ ) {

			//Determine cell type and set background color
			switch( arMaze[y][x] ) {
				case iOpen:
					bgColor = sSpaceColor;
					break;
				case iBlock:
					bgColor = sBlockColor;
					break;
				case iStart:
					bgColor = "#009900";
					break;
				case iEnd:
					bgColor = "#FF0000";
					break;
			}
									
			document.all["Grid" + x + "_" + y].style.backgroundColor = bgColor;

		}
	}	

}

function StartPlay()
{
	
	bCanMove = true;
	document.all.Play.style.visibility = "hidden";
	document.all.Stop.style.visibility = "visible";			
	
}

function StopPlay()
{
					
	if( bNewMaze ) {
		ResetMaze();
		document.all.Play.style.visibility = "visible";
		document.all.Stop.style.visibility = "hidden";
	}
	
	bCanMove = false;
	
}

var bMouseDown = false;
var bCreateBlocks = false;

function SelectedCell(element, x, y)
{

	
	if( bCanMove == false ) {
		
		bMouseDown = true;
		
		if( arMaze[y][x] == iOpen ) {
			bCreateBlocks = true;
		} else {
			bCreateBlocks = false;
		}

		SelectCell(element, x, y);		
		
	}	
	
}

function CheckSelected(element, x, y)
{
		
	if( bCanMove == false ) {

		if( bMouseDown ) {	
			SelectCell(element, x, y);
		}
		
	}	

}

var bSetStart = false;
var bSetEnd = false;

function SetStart()
{
	if( document.all.chkStart.checked == true ) {
		bSetStart = true;
		bSetEnd = false;
		document.all.chkEnd.checked = false;
	} else {
		bSetStart = false;
	}
}

function SetEnd()
{
	if( document.all.chkEnd.checked == true ) {		
		bSetEnd = true;
		bSetStart = false;
		document.all.chkStart.checked = false;
	} else {
		bSetEnd = false;
	}
}

function SelectCell(element, x, y)
{

	if( arMaze[y][x] != iStart && arMaze[y][x] != iEnd ) {

		if( bSetStart ) {

			if( iStartPosY < iGridHeight ) {
				if( arMaze[iStartPosY + 1][iStartPosX] == iBlock ) {
					document.all["Grid" + iStartPosX + "_" + (parseInt(iStartPosY) + 1)].style.borderTop = "1px solid #00579F";
				}
			}

			if( iStartPosX < iGridWidth ) {
				if( arMaze[iStartPosY][iStartPosX + 1] == iBlock ) {
					document.all["Grid" + (parseInt(iStartPosX) + 1) + "_" + iStartPosY].style.borderLeft = "1px solid #00579F";
				}
			}
		
			arMaze[iStartPosY][iStartPosX] = iOpen;
			document.all["Grid" + iStartPosX + "_" + iStartPosY].style.backgroundColor = sSpaceColor;			
			element.style.backgroundColor = "#009900";
			document.all.Player.style.pixelLeft = (x - 1) * iCellWidth;
			document.all.Player.style.pixelTop = (y - 1) * iCellHeight;
			iStartPosX = x;
			iStartPosY = y;
			bSetStart = false;
			document.all.chkStart.checked = false;
			arMaze[y][x] = iStart;
			element.style.borderLeft = "none";
			element.style.borderRight = "none";
			element.style.borderTop = "none";
			element.style.borderBottom = "none";		

			iPosX = iStartPosX;
			iPosY = iStartPosY;
				
		} else if ( bSetEnd ) {

			if( iEndPosY < iGridHeight - 1 ) {
				if( arMaze[iEndPosY + 1][iEndPosX] == iBlock ) {
					document.all["Grid" + iEndPosX + "_" + (parseInt(iEndPosY) + 1)].style.borderTop = "1px solid #00579F";
				}
			}

			if( iEndPosX < iGridWidth - 1 ) {
				if( arMaze[iEndPosY][iEndPosX + 1] == iBlock ) {
					document.all["Grid" + (parseInt(iEndPosX) + 1) + "_" + iEndPosY].style.borderLeft = "1px solid #00579F";
				}
			}
			
			arMaze[iEndPosY][iEndPosX] = iOpen;
			document.all["Grid" + iEndPosX + "_" + iEndPosY].style.backgroundColor = sSpaceColor;
			element.style.backgroundColor = "#FF0000";
			iEndPosX = x;
			iEndPosY = y;
			bSetEnd = false;
			document.all.chkEnd.checked = false;
			arMaze[y][x] = iEnd;
			element.style.borderLeft = "none";
			element.style.borderRight = "none";
			element.style.borderTop = "none";
			element.style.borderBottom = "none";
			
		} else {		
			
			if( bCreateBlocks ) {
				element.style.backgroundColor = sBlockColor;

				//Left Border
				if( x > 1 ) {
					if( arMaze[y][x - 1] != iBlock ) {
						element.style.borderLeft = "1px solid #00579F";
					}
				}

				//Right Border
				if( x < iGridWidth ) {
					element.style.borderRight = "1px solid #00579F";
					if( arMaze[y][x + 1] == iBlock ) {
						document.all["Grid" + (parseInt(x) + 1) + "_" + y].style.borderLeft = "none";
					}
				}					

				//Top Border
				if( y > 1 ) {
					if( arMaze[y - 1][x] != iBlock ) {
						element.style.borderTop = "1px solid #00579F";
					}
				}

				//Bottom Border
				if( y < iGridHeight ) {
					element.style.borderBottom = "1px solid #00579F";
					if( arMaze[y + 1][x] == iBlock ) {
						document.all["Grid" + x + "_" + (parseInt(y) + 1)].style.borderTop = "none";
					}				
				}			

				arMaze[y][x] = iBlock;	
						
				
			} else {

				element.style.backgroundColor = sSpaceColor;
				element.style.borderLeft = "none";
				element.style.borderRight = "none";
				element.style.borderTop = "none";
				element.style.borderBottom = "none";

				if( y < iGridHeight ) {
					if( arMaze[y + 1][x] == iBlock ) {
						document.all["Grid" + x + "_" + (parseInt(y) + 1)].style.borderTop = "1px solid #00579F";
					}
				}

				if( x < iGridWidth ) {
					if( arMaze[y][x + 1] == iBlock ) {
						document.all["Grid" + (parseInt(x) + 1) + "_" + y].style.borderLeft = "1px solid #00579F";
					}
				}
			
				arMaze[y][x] = iOpen;
				
			}
			
		}
		
	}
	
}

function CaptureCursorLocation()
{

	var iPosX = window.event.x;
	var iPosY = window.event.y;

}

function ReleaseObjects()
{
	bMouseDown = false;
	bCreateBlocks = false;
}

document.onmousemove = CaptureCursorLocation;
document.onmouseup = ReleaseObjects;

//Maze Settings
function ShowSettings()
{
	var opts = "LOCATION=NO,TOOLBAR=YES,MENUBAR=YES,STATUS=YES,RESIZABLE=YES,SCROLLBARS=YES,HEIGHT=500,WIDTH=600";
	var xWindow = window.open("settings.php?color1=" + sSpaceColor + "&color2=" + sBlockColor + "&player=" + iPlayerImage, "", opts);	
}


//-->

