 	var arrayWidth = 8;                       
 	var arrayHeight = 6;                      
 	var differentPicturesAvailable = 20;
	var numberOfIdenticalPairs = 2;

	var nElements = arrayWidth * arrayHeight;
	var i, j, X1, Y1, X2, Y2, tmp;
	var timesShuffle = arrayWidth * arrayHeight * 2;
	var picSelected = -1;
	var iSelected = -1;
	var jSelected = -1;
	var movesMade = 0;
	var gameField;

function prepareGameField( width, height )
{
	arrayWidth = width;
	arrayHeight = height;
	nElements = arrayWidth * arrayHeight;
	numberOfIdenticalPairs = Math.ceil( nElements / differentPicturesAvailable / 2 );
	createArray();
}

function createArray()
{
	var i, j;
	gameField = new Array( arrayWidth );

	for( i = 0; i < arrayWidth; i++ )
	{
		gameField[i] = new Array(arrayHeight);
	}
	
	
// Fill up the array
	for( i = 0; i < arrayWidth; i++ )
	 for( j = 0; j < arrayHeight; j++ )
	  gameField[i][j] = (arrayWidth * j + i) % (nElements / 2 / numberOfIdenticalPairs)

// Shuffle the array
	for( i = 0; i < timesShuffle; i++ )
	{
		X1 = Math.ceil( Math.random() * arrayWidth - 1 );
		Y1 = Math.ceil( Math.random() * arrayHeight - 1 );
		X2 = Math.ceil( Math.random() * arrayWidth - 1 );
		Y2 = Math.ceil( Math.random() * arrayHeight - 1 );

		tmp = gameField[X1][Y1];
		gameField[X1][Y1] = gameField[X2][Y2];
		gameField[X2][Y2] = tmp;
			
	}
}

function startTheGame( width, height )
{
	if( width * height / 2 != Math.floor( width * height / 2 ) )		//if width*height is odd
	{
		alert( "The field can not contain an odd number of cells." );
		return;
	}
	var picNumber = 0;		//Number of pictures in the file before the table

// Display shuffled results
	document.write( "\n<table align='center' border = 1 cellpadding=0 cellspacing=0>\n<tr><td>\n" );

	for( j = 0; j < height; j++ )
	{
	 document.write( "<tr>" );
	 for( i = 0; i < width; i++ )
	 {
		picName = "pic" + i + j;
		document.write( "<td>\n     " );
		document.write( "<a href = \"javascript:clicked( " + picNumber + ", " + i + ", " + j + " );\">\n" );
		document.write( "<img name = \"" + picName + "\" src = \"logo.jpg\" border = \"0\">\n" );
		document.write( "</a>\n" );
		picNumber++;
	 }			
	}

	document.write( "</table>" );
}

function Finish( picN, picSelected )
{
				document.images[picSelected].src = "logo.jpg";
				document.images[picN].src = "logo.jpg";
}

function clicked( picN, i, j )
{
	if( document.images[picN].src.indexOf( "logo.jpg" ) >= 0 )
	{
		document.images[picN].src = gameField[i][j] + ".jpg";
		if( picSelected == -1 )		// Nothing yet selected
		{	
			picSelected = picN;
			iSelected = i;
			jSelected = j;
		}
		else if( picSelected != -1 )	// Something selected
		{
			if( gameField[i][j] != gameField[iSelected][jSelected] )
			{
				window.setTimeout( 'Finish( ' + picN + ', ' + picSelected + ')', 500 );
			}
			picSelected = -1;
			iSelected = -1;
			jSelected = -1;
		}
	}
	movesMade++;
	if( wonTheGame() == true )
	{
		 endOfGame( movesMade );	
	}
}

function wonTheGame()
{
	for( q = 0; q < arrayWidth * arrayHeight; q++ )
		if( document.images[q].src.indexOf( "logo.jpg" ) >= 0 ) return false;
	return true;
}

function endOfGame( movesMade )
{

}
