
function shuffle(a) {
	for (var i=0; i<200; i++) {
		for (j in a) {
			var r = Math.floor(Math.random() * a.length);
			var t = a[j];
			a[j] = a[r];
			a[r] = t;
		}
	}
}


var STATE_OPEN = 0;
var STATE_LOCKED = 9;

var links = new Array();
links['[sediment]'] = ['http://sediment.semifat.net/', 'Semifat Sediment (blog)'];
links['[twitter]'] = ['http://twitter.com/joshleejosh', 'Twitter'];
links['[delicious]'] = ['http://del.icio.us/joshleejosh', 'Del.icio.us'];
links['[flickr]'] = ['http://flickr.com/photos/joshleejosh/', 'Flickr'];
links['[tumblr]'] = ['http://joshleejosh.tumblr.com/', 'Tumblr'];
links['[facebook]'] = ['http://www.facebook.com/joshleejosh', 'Facebook'];
links['[last.fm]'] = ['http://www.last.fm/user/joshleejosh/', 'Last.fm'];
links['[linkedin]'] = ['http://www.linkedin.com/in/joshleejosh', 'LinkedIn'];
links['[friendfeed]'] = ['http://friendfeed.com/joshleejosh', 'FriendFeed'];
links['[sifteo]'] = ['http://sifteo.com', 'Sifteo (day job)'];
links['[portfolio]'] = ['http://joshlee.semifat.net/', 'Game design portfolio'];

var pieces = new Array();
var states = new Array();
var pieceA;
var pieceB;
var clickingAllowed = true;

function getIndex(p) {
	for (i in pieces)
		if (pieces[i].attr('id') == p.attr('id'))
			break;
	return i;
}

function piecesMatch(a, b) {
	var alta = a.find('img:first').attr('alt');
	var altb = b.find('img:first').attr('alt');
	return (alta==altb);
}

function canFlip(p) {
	var i = getIndex(p);
	return (states[i] == STATE_OPEN);
}

function faceDown(p) {
	p.find('img:first').css('display', 'none');
}
function faceUp(p) {
	p.find('img:first').css('display', 'inline');
}
function isUp(p) {
	return (p.find('img:first').css('display') == 'inline');
}
function lockUp(p) {
	var i = getIndex(p);
	states[i] = STATE_LOCKED;
	faceUp(p);
	var img = p.find('img:first');
	var alt = img.attr('alt');
	var href = links[alt][0];
	img.wrap('<a href="' + href + '"></a>');
}

function revealLink(p) {
	var alt = p.find('img:first').attr('alt');
	var href = links[alt][0];
	var title = links[alt][1];
	$('#links').append('<a href="' + href + '">' + title + '</a><br/> ');
}
function revealAllLinks() {
	$('#links').empty();
	for (i in links)
		$('#links').append('<a href="' + links[i][0] + '">' + links[i][1] + '</a><br/> ');
	for (i in pieces)
		lockUp(pieces[i]);
	checkDone();
}

function resetPair() {
	faceDown(pieceA);
	faceDown(pieceB);
	clickingAllowed = true;
	pieceA = null;
	pieceB = null;
}

function checkDone() {
	var allUp = true;
	for (i in states)
		if (states[i] != STATE_LOCKED)
			allUp = false;
	if (allUp)
		$('#killjoy').css('display','none');
}

function flip(p) {
	if (isUp(p)) {
		faceDown(p);
		if (pieceA && p.attr('id') == pieceA.attr('id'))
			pieceA = null;
	} else {
		faceUp(p);
		if (pieceA && p.attr('id') != pieceA.attr('id')) {
			pieceB = p;
			if (isUp(pieceA) && isUp(pieceB) && piecesMatch(pieceA, pieceB)) {
				lockUp(pieceA);
				lockUp(pieceB);
				revealLink(pieceA);
				pieceA = null;
				pieceB = null;
				checkDone();
			} else {
				clickingAllowed = false;
				$.timer(500, resetPair, resetPair);
			}
		} else {
			pieceA = p;
		}
	}
}

function onMouseUp(evt) {
	if (clickingAllowed && canFlip($(this)))
		flip($(this));
}

function makePuzzle() {
	var puzzle = $('#puzzle:first');
	$('.piece').each(function() {
		var dupe = $(this).clone();
		puzzle.append(dupe);
		pieces.push($(this));
		pieces.push(dupe);
	});

	shuffle(pieces);
	for (i in pieces) {
		states[i] = STATE_OPEN;
	}

	var baseY = 0;
	var baseX = 0;
	var pad = 5;
	var width = 100;
	var height = 100;
	var perRow = 5;
	var row = 0;
	var col = 0;

	for (i in pieces) {
		pieces[i].css('top', '' + (baseY + height * row + (pad * (row-1))) + 'px');
		pieces[i].css('left', '' + (baseX + width * col + (pad * (col-1))) + 'px');
		col++;
		if (col >= perRow) {
			col = 0;
			row++;
		}
		pieces[i].attr('id', 'piece'+i);
		faceDown(pieces[i]);
		pieces[i].mouseup(onMouseUp);
	}
}

