<!-- 
// Space Lights
// JavaScript copyright (c) 2000-2002 by Karen M. Glatt
// This game is not freeware nor is it in the public domain.
// If you would like to use this game on your web site, 
// contact KMG Associates at www.kmgassociates.com.


// global variables
var board = new Array(25);  // 1="on", 2="off"
var currentPattern = 0;		// for the start of the game

// Preload some of the images into global variables
var light0 = new Image();
light0.src = "lghtred.gif";	
var light1 = new Image();
light1.src = "lghtblue.gif";	
var light2 = new Image();

var msgblank = new Image();
msgblank.src = "msgbuy.gif";
var msgwin = new Image();
msgwin.src = "msgwin.gif";

// Display either the light "on" image or the
// light "off" image depending upon the values
// in the board[] array.
function ShowLight(pos)
{
    // Put the image on the screen
    document.images[eval('"iboard' + pos + '"')].src = board[pos] ? light1.src: light0.src;
}

// Initialize the game
function InitGame()
{
    var i;

	// Clear out any previous msgs.
	document.msg.src = msgblank.src;

	// Set up the board. Display the lights according to the
	// currently selected pattern.
    for (i = 0; i < 25; ++i)
        {
		board[i] = eval('pattern' + currentPattern + '[' + i + ']');
        ShowLight(i);
        }
}

// Checks to make see if all of the lights are on.
function CheckBoard()
{
	var i;

	for (i = 0; i < 25; ++i)
		{
		if (board[i] != 1)
			return;
		}
	
	// The user did it!
	document.msg.src = msgwin.src;
}

// The player has clicked on a light to select it. We have to
// reverse the on/off state of this light, and any lights
// directly above/below & to the left/right.
function Selected(pos)
{
	// Swap the state of this light
	board[pos] = board[pos] ? 0 : 1;
	ShowLight(pos);
	
	// If this isn't a light in the first row, reset the
	// light directly above it.
	if (pos > 4)
		{
		board[pos-5] = board[pos-5] ? 0 : 1;
		ShowLight(pos-5);
		}

	// If this isn't a light in the last row, reset the
	// light directly above it.
	if (pos < 20)
		{
		board[pos+5] = board[pos+5] ? 0 : 1;
		ShowLight(pos+5);
		}

	// If this isn't a light in the first column, reset the
	// light directly to the left of it.
	if ((pos % 5))
		{
		board[pos-1] = board[pos-1] ? 0 : 1;
		ShowLight(pos-1);
		}

	// If this isn't a light in the last column, reset the
	// light directly to the left of it.
	if ((pos % 5) != 4)
		{
		board[pos+1] = board[pos+1] ? 0 : 1;
		ShowLight(pos+1);
		}

	// Did the player win?
	CheckBoard();
}

// The user wants to change the pattern. Set up the next
// pattern and reinitialize the board.
function NewPattern()
{
	++currentPattern;
	if (currentPattern >= numPatterns)
		currentPattern = 0;

	InitGame();
}
// End JavaScript -->
