var buzzwords = new Array ("Ajax",		// put first here so count = line#
	"Aperture update", 
	"Apple TV", 
	"AT&amp;T",
	"&quot;Available starting today&quot;",
	"Bento",
	"Blu-Ray",
	"&quot;Boom!&quot;",
	"A demo fails",
	"Digital Hub",
	"Dockable Mac",
	"Fake Steve Jobs",
	"HSPA",
	"H.264",
	"iPhone gets cut/paste",
	"iPhone gets Flash",
	"iPhone gets GPS",
	"iPhone gets iChat",
	"iPhone gets Java",
	"iPhone SDK available",
	"iPhone SDK shown",
	"iPhone 2.0",
	"iPhone 3G",
	"iPod sales",
	"iPod socks",
	"iSight HD",
	"Leopard",
	"Mac market share",
	"MacBook Air",
	"MacBook Nano",
	"MacTouch",
	"Mid-range tower",
	"MS Office mentioned",
	"MS Office demoed",
	"MS rep onstage",
	"Multi-touch Mac",
	"Musical act performs",
	"Native 3rd-party iPhone app",
	"New displays",
	"New MacBook case",
	"New MacBook Pro case",
	"OS X",
	"&quot;One more thing&quot;",
	"Phil Schiller demo",
	"Rentals on iTunes",
	"Schiller runs Vista, loses",
	"QuickTime",
	"Safari",
	"Sexbots",
	"Stock price mentioned",
	"Stock split announced",
	"Sub-notebook",
	"Tablet Mac",
	"Touchscreen Mac",
	"Twitter",
	"Vista",
	"Web 2.0",
	"&quot;Wouldn't it be great?&quot;",
	"xMac",
	"YouTube",
	"WiMAX",
	"10.5.2",
	"10.6 cat name announced",
	"5 or more &quot;Booms!&quot;",
	".Mac"
);

var usedWords = new Array(buzzwords.length);
window.onload = initAll;

function initAll() {
	if (document.getElementById) {
		document.getElementById("reload").onclick = newCard;
		newCard();
	}
	else {
		alert("Sorry, your browser doesn't support this script");
	}
}

function newCard() {
	for (i=0;i<buzzwords.length;i++) {
		usedWords[i] = false;
	}
	
	for (i=0;i<24;i++) {
		setBox(i);
	}
	document.getElementById("free").className = "pickedBack";
	document.getElementById("header").className = "";
	return false;
}

function setBox(thisSquare) {
	do {
		var randomWord = Math.floor((Math.random() * buzzwords.length));
	}
	while (usedWords[randomWord]);

	usedWords[randomWord] = true;
	var currSquare = "square" + thisSquare;
	document.getElementById(currSquare).innerHTML = buzzwords[randomWord];
	document.getElementById(currSquare).className = "";
	document.getElementById(currSquare).onmousedown = toggleColor;
}

function toggleColor(evt) {
	evt.target.className = (evt.target.className=="") ? "pickedBack" : "";
	checkWin();
}

function checkWin() {
	var winningOption = -1;
	winOptions = new Array();
	blinkSquare = new Array();
	for (var i=0; i<12; i++) {
		winOptions[i] = setWinConditions(i);
	}

	for (var i=0; i<winOptions.length; i++) {
		for (var j=0; j<winOptions[i].squares.length; j++) {
			var currSquare = "square" + winOptions[i].squares[j];
			if (document.getElementById(currSquare).className == "") {
				winOptions[i].winner = false;
			}
		}
		if (winOptions[i].winner) {
			winningOption = i;
		}
	}
	
	if (winningOption > -1) {
		for (var i=0; i<winOptions[winningOption].squares.length; i++) {
			blinkSquare[i] = "square" + winOptions[winningOption].squares[i];
		}

		if (winOptions[winningOption].squares.length==4) {
			blinkSquare[4] = "free";
		}		
		blinkAllSquares("winningBack");
	}
	
	function blinkAllSquares(thisClass) {
		for (var i=0; i<blinkSquare.length; i++) {
			document.getElementById(blinkSquare[i]).className = thisClass;
		}
		document.getElementById("header").className = thisClass;
		
		var nextClass = (thisClass=="winningBack") ? "" : "winningBack";
		// setTimeout(blinkAllSquares(nextClass), 2 * 1000);
	}

	function setWinConditions(condNo) {
		var thisObj = new Object();
		thisObj.winner = true;
	
		switch(condNo) {
			case 0:
				thisObj.squares = new Array(0,1,2,3,4);
				break;
			case 1:
				thisObj.squares = new Array(5,6,7,8,9);
				break;
			case 2:
				thisObj.squares = new Array(10,11,12,13);
				break;
			case 3:
				thisObj.squares = new Array(14,15,16,17,18);
				break;
			case 4:
				thisObj.squares = new Array(19,20,21,22,23);
				break;
			case 5:
				thisObj.squares = new Array(0,5,10,14,19);
				break;
			case 6:
				thisObj.squares = new Array(1,6,11,15,20);
				break;
			case 7:
				thisObj.squares = new Array(2,7,16,21);
				break;
			case 8:
				thisObj.squares = new Array(3,8,12,17,22);
				break;
			case 9:
				thisObj.squares = new Array(4,9,13,18,23);
				break;
			case 10:
				thisObj.squares = new Array(0,6,17,23);
				break;
			case 11:
				thisObj.squares = new Array(19,15,8,4);
				break;
			default:
		}
		return thisObj;
	} 
}