function addLoadEvent(func) {
/* Add functions to window.onload
* Thank you, Simon Willison (See http://simon.incutio.com/archive/2004/05/26/addLoadEvent)
*/
var oldonload = window.onload;
if (typeof window.onload != 'function') {
	window.onload = func;
} else {
	window.onload = function() {
		if (oldonload) {
			oldonload();
        		}
		func();
		}
	}
}
addLoadEvent(dealCards);
addLoadEvent(setEventhandlers);

var allCards = new Array('card01','card02','card03','card04','card05','card06','card07','card08','card09','card10','card11','card12','card13','card14','card15','card16');
var allItems = new Array('A','A','B','B','C','C','D','D','E','E','F','F','G','G','H','H');

function randomize() {
	return (Math.round(Math.random())-0.5);
}

function dealCards() {
if (!document.getElementById) return false;
// Randomly distribute cards over board
	allCards.sort(randomize);
	for (i=0;i<allCards.length;i++) {
		var card = document.getElementById(allCards[i]);
		var para = document.createElement("p");
		// if board is already full, replace cards
		if (card.firstChild) {
			card.replaceChild(para,card.firstChild);
		} else {
		// else just deal
			card.appendChild(para);
		}
	 	var item = document.createTextNode(allItems[i]);
		para.appendChild(item);
	}
setEventhandlers();
}

function setEventhandlers() {
if (!document.getElementById) return false;
	// reset button
	var reset = document.getElementById("reset");
	reset.onclick = function() {
		dealCards();
		return false;
	}
	// kaarten
	for (i=0;i<allCards.length;i++) {
		var placeholder = document.getElementById(allCards[i]);
		placeholder.onclick = function() {
			var content = this.firstChild;
			var value = content.firstChild.nodeValue;
			content.className = "show";
			compareValues();
			return false;
		}
	}
}

function compareValues() {
if (!document.getElementsByTagName) return false;
/* This function is the core of the game. It checks whether two cards are the same, and 
* permanently unhides them if they are
*/
	var values = document.getElementsByTagName("p");
	var clicked = new Array;
//	alert(values.length);
	for (i=0;i<values.length;i++) {
		if (values[i].className == "show") {
			clicked.push(values[i]);
		}
	}
	if (clicked.length == 2) {
		// check occurs only if two cards have been turned
		var value1 = clicked[0].firstChild.nodeValue;
		var value2 = clicked[1].firstChild.nodeValue;
		if (value1 == value2) {
			// if cards are the same, make them permanently visible
			// and make them unclickable
			clicked[1].parentNode.onclick = null;
			clicked[1].className = "show_perm";
			clicked[0].parentNode.onclick = null;
			clicked[0].className = "show_perm";
			// and reset the turned cards count
			clicked = null;
		} else {
			setTimeout("hideCards()", 500);
			// if they're different, hide them again
			// and reset the turned cards count
			clicked = null;
		}
	}
}

function hideCards() {
if (!document.getElementsByTagName) return false;
	test = document.getElementsByTagName("p");
	for (i=0;i<test.length;i++) {
		if (test[i].className == "show") {
			test[i].className = null;
		}
	}
}

