var AfricanCountries = "Algeria, Angola, Benin, Botswana, Burkina Faso, Burundi, Cameroon, Cape Verde, Central African Republic, Chad, Comoros, Cote D'Ivoire, Democratic Republic of the Congo, Djibouti, Egypt, Equatorial Guinea, Eritrea, Ethiopia, Gabon, Ghana, Guinea, Guinea-Bissau, Kenya, Lesotho, Liberia, Libya, Madagascar, Malawi, Mali, Mauritania, Mauritius, Morocco, Mozambique, Namibia, Niger, Nigeria, Republic of the Congo, Rwanda, Sao Tome and Principe, Senegal, Seychelles, Sierra Leone, Somalia, South Africa, Sudan, Swaziland, Tanzania, The Gambia, Togo, Tunisia, Uganda, Zambia, Zimbabwe";

var AfricanCountriesShortenedNamesForMap = { "Democratic Republic of the Congo" : "DR Congo", "Central African Republic" : "CAE", "Republic of the Congo" : "R Congo", "Equatorial Guinea" : "E Guinea", "Sao Tome and Principe" : "STP" };
var AfricanCountriesLocs = { "Algeria" : [96, 80], "Angola" : [186, 327], 
	"Benin" : [118, 182], "Botswana" : [214, 399], "Burkina Faso" : [78, 172], "Burundi" : [267, 281], 
	"Cameroon" : [154, 229], "Cape Verde" : [-56, 159], "Central African Republic" : [204, 212], "Chad" : [194, 158], "Comoros" : [335, 326], "Cote D'Ivoire" : [73, 217],
	"Democratic Republic of the Congo" : [204, 269], "Djibouti" : [335, 181],
	"Egypt" : [246, 91], "Eritrea" : [306, 156], "Ethiopia" : [299, 205], "Equatorial Guinea" : [111, 242],
	"Gabon" : [132, 265],"Ghana" : [93, 205], "Guinea" : [29, 189],"Guinea-Bissau" : [-38, 183],
	"Kenya" : [295, 257], 
	"Lesotho" : [251, 439],"Liberia" : [20, 225],"Libya": [175, 82], 
	"Madagascar" : [348, 364],"Malawi" : [287, 339],"Mali" : [85, 146],"Mauritania" : [11, 140],"Mauritius" : [388, 382],"Morocco" : [49, 53],"Mozambique" : [300, 349],
	"Namibia" : [171, 378],"Niger" : [145, 145],"Nigeria" : [129, 207],
	"Republic of the Congo" : [187, 257], "Rwanda" : [268, 271], 
	"Sao Tome and Principe" : [132, 254],"Senegal" : [-2, 166],"Seychelles" : [395, 279], "Sierra Leone" : [-15, 207],"Somalia" : [337, 240],"South Africa" : [207, 448],"Sudan" : [252, 178],"Swaziland" : [270, 422], 
	"Tanzania" : [272, 297], "The Gambia" : [-38, 173], "Togo" : [109, 192], "Tunisia" : [153, 44],
	"Uganda" : [277, 243], 
	"Zambia" : [230, 344],"Zimbabwe" : [253, 375]};

var timeStart = 10 * 60;
var timeLeft = timeStart;
// start
var ca = AfricanCountries.split(", ");
var lo = AfricanCountriesLocs;
var foundCountries = new Array();
var totalCountries = ca.length;
var current = "";

$(function() {
  $("#country").keyup(function() {
	var index = $.inArrayi($(this).val(), ca);
	if (index > -1)
	{
		// list
		foundCountries.push(ca[index]);
		current = ca[index];
		foundCountries.sort();
		ca.splice(index, 1);
		$("#countryList").html(foundCountries.length+"/"+totalCountries+"<br/>"+foundCountries.join(", "));
		// map
		addToMap(current, true);
		// clear form
		$(this).val("");
		// check if there are no countries left
		if (ca.length == 0) {
			clearTimeout(timer);
			alert("Awesome: all done in "+formatTime(timeStart - timeLeft - 1));
		}
	}
  }).attr("disabled", false).focus();

  $("#finish").click(function(){
  	timeLeft = 0;
  	clearTimeout(timer);
  	updateTimer();
  });

  updateTimer();
});
// case insensitive match
jQuery.inArrayi = function (G, H) {
	for (var E = 0, F = H.length; E < F; E++) {
		if (H[E].toLowerCase() === G.toLowerCase()) {
			return E;
		}
	} return -1;
}

function timesUp() {
	$("#country").unbind().attr("disabled", true);
	alert("Time's up.");
	$("#missingList").text("Missing: ("+ca.length+") "+ca.join(", "));
	// add missing countries to map marked incorrect
	for (var i = 0; i < ca.length; i++)
		addToMap(ca[i], false); 
}

function addToMap(country, correct) {
	if (lo[country]) {
		var label = AfricanCountriesShortenedNamesForMap[country]?AfricanCountriesShortenedNamesForMap[country]:country;
		$("<div class=\"label\">"+label+"</div>").insertAfter("#map")
			.css({"left":lo[country][0],"top":lo[country][1],"color":(correct?"black":"red")});
	}
}

function formatTime(time) {
	var timeMins = parseInt(time/60);
	var timeSecs = time%60;
	return timeMins +":"+((timeSecs<10)?"0"+timeSecs:timeSecs);
}

function updateTimer() {
	$("#timerText").val(formatTime(timeLeft))
	if (timeLeft > 0)
	{
		timeLeft -= 1;
		timer = setTimeout("updateTimer()", 1000);
	}
	else
		timesUp();
}