var projects, cards, cardsFlipped, startTime;
var matches=0;
var timerRunning=false;

cardsFlipped=new Array();

cards = [
	[
		{"projID": "", "status": "unmatched"},
		{"projID": "", "status": "unmatched"},
		{"projID": "", "status": "unmatched"},
		{"projID": "", "status": "unmatched"},
		{"projID": "", "status": "unmatched"},
		{"projID": "", "status": "unmatched"}
	],
	[
		{"projID": "", "status": "unmatched"},
		{"projID": "", "status": "unmatched"},
		{"projID": "", "status": "unmatched"},
		{"projID": "", "status": "unmatched"},
		{"projID": "", "status": "unmatched"},
		{"projID": "", "status": "unmatched"}
	],
	[
		{"projID": "", "status": "unmatched"},
		{"projID": "", "status": "unmatched"},
		{"projID": "", "status": "unmatched"},
		{"projID": "", "status": "unmatched"},
		{"projID": "", "status": "unmatched"},
		{"projID": "", "status": "unmatched"}
	]
];

$(document).ready(function() {
	//Preload Images
	imageBase="images/portfolio/";
	for(i=0; i < projects.length; i++) {
		$.preloadImages(imageBase+projects[i].id+"/0.gif", imageBase+projects[i].id+"/30.gif", imageBase+projects[i].id+"/60.gif");
	}
	$.preloadImages(imageBase+"0.gif",imageBase+"30.gif",imageBase+"60.gif",imageBase+"90.gif");
	
	//Attach popup thing to cheat list links
	$("#cheat_list > a").colorbox({'width':'850px', 'height':'650px', 'iframe':true, 'close':'Back to game'});
	
	//Cheat Menu Things: hide and click action
	$("#cheat_list").hide();
	$("#cheat_link").click(function() {
		if($("#cheat_list").is(":hidden")) {
			$("#cheat_list").slideDown("normal");
		} else {
			$("#cheat_list").slideUp("normal");
		}
	});
	
	//Populate the game
	for(i=0; i < 3 ; i++) {
		for(j=0; j < 6 ; j++) {
			setCard(i, j);
		}
	}
	$(".portfolio_card").click(function() {
		if(!timerRunning) {
			startTimer();
		}
		flipCard($(this).attr("id"), 0, "back-to-front");
	});
});

function flipCard(cardID, stage, direction) {
	cardParts=cardID.split("-");
	x=cardParts[1];
	y=cardParts[2];
	projID=projIDFromCoords(x, y);
	
	//alert(projID + stage);
	
	if(stage==0) {
		if(direction=="back-to-front") {
			//Can't click while flipping
			$("#"+cardID).unbind("click");
		} else {
			//Make clickable after its reset
			$("#"+cardID).click(function() {
				flipCard($(this).attr("id"), 0, "back-to-front");
			});
		}
		newSrc="0.gif";
	} else if(stage==1) {
		newSrc="30.gif";
	} else if(stage==2) {
		newSrc="60.gif";
	} else if(stage==3) {
		newSrc="90.gif";
	} else if(stage==4) {
		newSrc=projID+"/60.gif";
	} else if(stage==5) {
		newSrc=projID+"/30.gif";
	} else if(stage==6) {
		newSrc=projID+"/0.gif";
	}
	
	//alert(stage);
	
	newSrc="images/portfolio/"+newSrc;
	$("#"+cardID+" > img").attr("src", newSrc);
	
	if(direction=="back-to-front") {
		stage++;
	} else {
		stage--;
	}
	
	//alert(stage);
	
	if(stage <= 6 && stage >=0) {
		setTimeout("flipCard('"+cardID+"', "+stage+", '"+direction+"')", 100);
	} else {
		//alert(projID);
		if(direction=="back-to-front") {
			cardFlipped(cardID, x, y);
		}
	}
}

function cardFlipped(cardID, x, y) {
	cardsFlipped.push(new Array(x, y));
	if(cardsFlipped.length==2) {
		//Check for win
		x1=cardsFlipped[0][0];
		y1=cardsFlipped[0][1];
		x2=cardsFlipped[1][0];
		y2=cardsFlipped[1][1];
		projID1=projIDFromCoords(x1, y1);
		projID2=projIDFromCoords(x2, y2);
		if(projID1==projID2) {
			$("#"+projID1+"-link").click();
			cards[x1][y1].status="matched";
			cards[x2][y2].status="matched";
			matches++;
			if(matches==9) {
				$(".portfolio_card").click(function() {
					window.location="portfolio.php";
				});
				stopTimer();
			}
		} else {
			$(".portfolio_card").unbind("click");
			setTimeout("resetCards("+x1+","+y1+","+x2+","+y2+")", 1000);
			setTimeout("reapplyClick()", 1700);
		}
		cardsFlipped=null;
		cardsFlipped=new Array();
	}
	$("#"+cardID).click(function() { });
}

function resetCards(x1, y1, x2, y2) {
	flipCard("card-"+x1+"-"+y1, 6, "front-to-back");
	flipCard("card-"+x2+"-"+y2, 6, "front-to-back");
}

function reapplyClick() {
	for(i=0; i < 3 ; i++) {
		for(j=0 ; j < 6 ; j++) {
			if(cards[i][j].status=="unmatched") {
				$("#card-"+i+"-"+j).click(function() {
					flipCard($(this).attr("id"), 0, "back-to-front");
			   });
			}
		}
	}
}

function randomNum() {
	return Math.floor(Math.random()*9);
}

function setCard(x, y) {
	id=randomNum();
	if(projects[id].times_used < 2) {
		cards[x][y].projID=id;
		projects[id].times_used++;
	} else {
		setCard(x, y);
	}
}

function projIDFromCoords(x, y) {
	projID=cards[x][y].projID;
	projID=projects[projID].id;
	return projID;
}

function startTimer() {
	startDate=new Date();
	startTime=startDate.getTime();
}

function stopTimer() {
	endDate=new Date();
	endTime=endDate.getTime();
	timeDiff=(endTime-startTime) / 1000;
	
	readableSeconds=Math.round(timeDiff % 60);
	if(readableSeconds < 10) readableSeconds="0"+readableSeconds;
	
	readableMinutes=Math.floor(timeDiff / 60);
	
	readableTime=readableMinutes + ":"+readableSeconds;
	alert("It took you "+readableTime+" to match every pair");
}

function dump(arr,level) {
	var dumped_text = "";
	if(!level) level = 0;
	
	//The padding given at the beginning of the line.
	var level_padding = "";
	for(var j=0;j<level+1;j++) level_padding += "    ";
	
	if(typeof(arr) == 'object') { //Array/Hashes/Objects 
		for(var item in arr) {
			var value = arr[item];
			
			if(typeof(value) == 'object') { //If it is an array,
				dumped_text += level_padding + "'" + item + "' ...\n";
				dumped_text += dump(value,level+1);
			} else {
				dumped_text += level_padding + "'" + item + "' => \"" + value + "\"\n";
			}
		}
	} else { //Stings/Chars/Numbers etc.
		dumped_text = "===>"+arr+"<===("+typeof(arr)+")";
	}
	return dumped_text;
}