/*
** Discover C-Match
*/

if (typeof ourworld == "undefined") var ourworld = new Object();
if (typeof ourworld.cmatch == "undefined") ourworld.cmatch = new Object();

ourworld.cmatch.choice_map = [ " ", "r", "i", "a", "s", "e", "c" ];

ourworld.cmatch.state = "   ";

ourworld.cmatch.load = function () {
	var ourworld_cmatch_state = $.cookie("ourworld_cmatch_state");
	
	if (!ourworld_cmatch_state) {
		ourworld.cmatch.state = "   ";
	} else {
		ourworld.cmatch.state = ourworld_cmatch_state;
	}
	
	console.log("ourworld.cmatch.load()", ourworld.cmatch.state);
}

ourworld.cmatch.save = function (cmatch) {
	ourworld.cmatch.state = cmatch;
	
	$.cookie("ourworld_cmatch_state", ourworld.cmatch.state);

	console.log("ourworld.cmatch.save()", ourworld.cmatch.state);
}

ourworld.cmatch.check_click = function () {
	var id = $(this).attr("id");
	var checked = $(this).attr("checked");
	var which = parseInt(id.substr(5,1), 10);
	var val = parseInt(id.substr(6,1), 10);
	var i, j;

	for (i = 1; i <= 6; i++) {
		if (i != which) {
			$("#radio" + i.toString() + val.toString()).attr("checked", false);
		}
	}
	
	var choice1 = 0;
	var choice2 = 0;
	var choice3 = 0;

	$("input.radio").each(function (unused) {
		var id = $(this).attr("id");
		var checked = $(this).attr("checked");
		var which = parseInt(id.substr(5,1), 10);
		var val = parseInt(id.substr(6,1), 10);
		
		if (checked) {
			if (val == 1) {
				choice1 = which;
			} else if (val == 2) {
				has2 = true;
				choice2 = which;
			} else 	if (val == 3) {
				choice3 = which;
			}
		}
	});

	var cmatch = "";
	
	cmatch += ourworld.cmatch.choice_map[choice1];
	cmatch += ourworld.cmatch.choice_map[choice2];
	cmatch += ourworld.cmatch.choice_map[choice3];
	
	console.log("ourworld.cmatch.check_click()", id, which, cmatch);
	
	ourworld.cmatch.save(cmatch);
	
	ourworld.cmatch.check_submit();
	ourworld.cmatch.update_mymcatch();
}

ourworld.cmatch.check_submit = function () {
	var has1 = false;
	var has2 = false;
	var has3 = false;
	
	$("input.radio").each(function (unused) {
		var id = $(this).attr("id");
		var checked = $(this).attr("checked");
		var which = parseInt(id.substr(5,1), 10);
		var val = parseInt(id.substr(6,1), 10);
		
		if (checked) {
			if (val == 1) {
				has1 = true;
			} else if (val == 2) {
				has2 = true;
				choice2 = which;
			} else 	if (val == 3) {
				has3 = true;
			}
		}
	});

	console.log("ourworld.cmatch.check_click()", has1, has2, has3);

	$("div#submit button").css("visibility", has1 && has2 && has3 ? "visible" : "hidden");
}

ourworld.cmatch.update_mymcatch = function () {
	for (j = 1; j <= 3; j++) {
		var which = ourworld.cmatch.state.substr(j-1, 1);

		if (which == " ") {
			$("img#cmatch" + j).attr("src", "images/cmatch_" + j + ".png");
		} else {
			$("img#cmatch" + j).attr("src", "images/cmatch_" + which + ".png");
		}
	}
}

ourworld.init_discover = function () {
	ourworld.cmatch.load();

	console.log("ourworld.init_discover()", ourworld.cmatch.state);

	$("input.radio").attr("checked", false).bind("click", ourworld.cmatch.check_click);

	var i, j;

	for (j = 1; j <= 3; j++) {
		var which = ourworld.cmatch.state.substr(j-1, 1);
		for (i = 0; i <= 6; i++) {
			if (which == ourworld.cmatch.choice_map[i]) {
				$("#radio" + i.toString() + j.toString()).attr("checked", "checked");
			}
		}
		
	}
	
	$("#submit-button").bind("click", function () {
		var url = $("#explore-form").attr("action");
		window.location = url;
		return false;
	});
	
	ourworld.cmatch.update_mymcatch();
	ourworld.cmatch.check_submit();
}
