var tempval = -1
var clicknum = 1
var selected1 = 0
var selected2 = 0
var firsttime = 1
var moves = 0
var win = false
var randval = new Array(30)
var sortval = new Array(30)
var imgarray = new Array("a","a","b","b","c","c","d","d","e","e","f","f","g","g","h","h","i","i","j","j","k","k","l","l","m","m","n","n","o","o")
var avail = new Array(1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1)
var imglist = new Array(30)

while(tempval==-1)
{

	for (var i = 0; i < 30; i++)
	{
		imglist[i] = new Image(100,100)
		tempval = Math.round(Math.random() * 29385) + 1
		randval[i] = tempval
		sortval[i] = tempval
	}
	
	sortval.sort()
	for (var u = 0; u < 29; u++)
	{
		if (sortval[u] == sortval[u+1])
		{
			tempval = -1
			break
		}
	}


}
	
for (var t = 0; t < 30 ; t++)
{
	for (var s = 0; s < 30; s++)
	{
		if (randval[t] == sortval[s])
		{
			imglist[t].src = "images/" + imgarray[s] + ".gif"
			break
		}
		
	}
}




//---------------------------------------------------------
//---------------------------------------------------------

function showtile(picnum, backpath)
{

if (avail[picnum] == 0) {return}

if (clicknum==1)
{
	if (firsttime)
	{
		firsttime = 0
	}
	else
	{

		if (document.getElementById("pexeso_"+selected1).src != document.getElementById("pexeso_"+selected2).src)
		{

			document.getElementById("pexeso_"+selected1).src=backpath
			document.getElementById("pexeso_"+selected2).src=backpath

		}
	}
	selected1 = picnum

	document.getElementById("pexeso_"+selected1).src=imglist[selected1 -1].src

	clicknum=2
}

if (clicknum==2)
{
	selected2=picnum
	if (selected1==selected2) {return}

	document.getElementById("pexeso_"+selected2).src=imglist[selected2 -1].src

	if (document.getElementById("pexeso_"+selected1).src == document.getElementById("pexeso_"+selected2).src)
	{
		avail[selected1] = 0
		avail[selected2] = 0
	}
	moves++
	win = checkwin()
	if (win)
		{
			alert("Gratuluji! Pexeso si vyřešil na " + moves + " pokusů.")
		}
	clicknum=1
	document.getElementById("pexeso_cnt").value=moves

}


}


//----------------------------------------------------
//----------------------------------------------------


function checkwin()
{

var tmpsrc


for (var s=1; s <= 30; s++)
{

        tmpsrc = document.getElementById("pexeso_"+s).src.charAt(document.getElementById("pexeso_"+s).src.length - 6) + document.getElementById("pexeso_"+s).src.charAt(document.getElementById("pexeso_"+s).src.length - 5)
	if (tmpsrc == "ck")
	{
		return false
	}
}
return true

}


//-------------------------------------------------------
//-------------------------------------------------------


function rules()
{

var rules = window.open("","rules","height=400,width=400,scrollbars,status,resizable,screenX=30,screenY=30")
rules.document.writeln("<html><head><title>Rules & FAQ</title></head><body>")
rules.document.writeln("<center><h1>Rules</h1></center>")
rules.document.writeln("<p>The point of this game is to reveal all the tiles. This is accomplished by finding two and two tiles that match. You play the game like this:")
rules.document.writeln("<ul><li>Click on two different tiles with your mouse.</li><li>If the two tile match, you hav found a pair, an these tiles will stay revealed for the rest of the game</li><li>If they don't match, the tiles will revert as soon as you click on a new tile.</li><li>The point is to remember where the different tiles are placed in the grid, so when you reveal a tile, you might already have seen it's match earlier in the game.</li><li>When all the tiles are revealed by finding the matching pairs, you have won.</li><li>The counter below the grid shows you how many moves you have used. Each time you reveal 2 tiles, the counter increases by one</li></ul>")
rules.document.writeln("<h1 align='center'>FAQ</h1>")
rules.document.writeln("<p align='left'><strong>Q</strong>: The counter doesn't seem to reset to zero when I reload the page.<br><big>A</big>: Don't worry. Even if the counter is not reset after a reload, you will see that after you have made your first move, it will change to show the value 1.</p>")
rules.document.writeln("<p align='left'><strong>Q:</strong> After I click a tile, it revelas itself, but my pointer changes into a 'busy pointer'. I have tried waiting for hours, but it seems to never return to normal.<br><strong>A:</strong> Some browsers seem to show a busy pointer after each time a javascript has been run. Just ignore this. As soon as you move your mouse over another tile, the pointer will revert to it's normal state. Even when it shows 'busy' you can use it as a normal pointer. </p>")
rules.document.writeln("</body></html>")
rules.document.close()

}
