// Setup jquery
$(document).ready(function() {
	$().ajaxStart(function() {
		$('#hangmanGame').block({
			message: '<h1 style="padding: 20px; font-weight: bold; font-size: 16pt;">Processing...</h1>', 
			css: { border: '3px solid #000000' } 
		});
	});
	$().ajaxStop(function() {
		$('#hangmanGame').unblock();
	});

	var source = $.cookie("source");
	var workfriendly = $.cookie("workfriendly");
	var options = "";

	if ($.cookie("workfriendly") == "true") {
		$('#workfriendly').attr("checked","checked");
	} else {
		$('#workfriendly').attr("checked","");
	}
	if (source == null || source == "") {
		source = "wikipedia";
	}
	$('#source').attr("value", source);

	if (source != null && source != "") {
		options = source;
	}
	if (workfriendly != null && (workfriendly == "true" || workfriendly == true)) {
		if (options == "") {
			options = "work friendly";
		} else {
			options += "; work friendly";
		}
	}
	if (options != "") {
		$('#hSettings').text("Settings | " + options);
	} else {
		$('#hSettings').text("Settings");
	}

	$('#letter').focus();

	// Setup the location of the settings frame, for when it needs to be shown
	var pTop = $('#hangmanGame').position().top;
	var pLeft = $('#hangmanGame').position().left;
	var pWidth = $('#hangmanGame').width();

	$('#hangmanOptions').css({ position: "absolute", top: pTop - 6, left: pLeft + 8, width: pWidth - 18 });

	$.fn.getClue = function() {
		$.ajax({
			type: "GET",
			url: "/games/hangman/process.php",
			data: "getclue",
			success: function(data) {
				if (data == "") {
					$('#hangmanClue').hide();
				} else {
					$('#hangmanClue').empty().append("<b>Clue:</b> " + data).show();
				}
			}
		});
	}

	$.fn.resetLetters = function() {
		// Reset the hangman letters
		$.ajax({
			type: "GET",
			url: "/games/hangman/process.php",
			data: "getletters",
			success: function(data) {
				$('#hangmanLetters').empty().append(data);
			}
		});
	}

	$.fn.resetGame = function() {
		// Reset the current game
		$.ajax({
			type: "GET",
			url: "/games/hangman/process.php",
			data: "newgame",
			success: function(data) {
				$('#letter').removeAttr("disabled");

				$().processPage("");
				$().getClue();
				$().resetLetters();
			}
		});
	}

	$.fn.processPage = function(letter) {
		$.ajax({
			type: "GET",
			url: "/games/hangman/process.php",
			data: "letter=" + letter,
			success: function(data) {
				// Letter was submitted successfully, lets update the display
				var dataArray = data.split("|");

				$('#hangmanImage').empty().append(dataArray[0]);
				$('#hangmanGuesses').empty().append(dataArray[1]);
				$('#hangmanWord').empty().append(dataArray[2]);

				if (dataArray[3] == "DONE") {
					$('#letter').attr("disabled", true);
				} else {
					$('#letter').focus();
				}

				if (dataArray.length == 5) {
					$('#hangmanScores').empty().append(dataArray[4]);
				}
			}
		});
	}

	$().getClue();
	$().processPage("");

	$('#letter').keyup(function(e) {
		if(e.keyCode == 13) {
			var string = $('#letter').val();
			$('#letter').attr("value","");

			$('a[href="' + string.toUpperCase() + '"]').replaceWith(string.toUpperCase());
			$().processPage(string);
		}
	});

	$('#workfriendly').click(function() {
		var checked = $('#workfriendly').is(":checked");
		$.cookie("workfriendly", checked, { path: "/", expires: 365 });
		$().processPage("");
	});

	$('#source').change(function() {
		var value = $('#source').val();
		$.cookie("source", value, { path: "/", expires: 365 });
		$().resetGame();
	});

	$('#hangmanLetters a').live("click", function(e) {
		if ($('#letter').attr("disabled") == false) {
			$().processPage($(this).attr("href"));
			var string = $(this).attr("href");
			$(this).replaceWith(string);
		}
		e.preventDefault();
	});

	$('#letter').wTooltip({
		content: "Enter guesses here, one letter at a time",
		offsetY: -40,
		offsetX: 0
	});

	$('#workfriendly, #workfriendlyLabel').wTooltip({
		content: "When checked ON text will be displayed instead of a hangman image",
		offsetY: -40,
		offsetX: 0
	});

	$('#source').wTooltip({
		content: "Choose a source to retrieve Hangman data from",
		offsetY: -40,
		offsetX: 0
	});

	$('div.scoreRow:not(:first)').mouseover(function() {
		$(this).addClass("scoreHighlight");
	});
	
	$('div.scoreRow:not(:first)').mouseout(function() {
		$(this).removeClass("scoreHighlight");
	});

	$('#hSettings').click(function(e) {
		if ($('#hangmanOptions').is(":hidden")) {
			$('#hangmanOptions').slideDown("normal");
			$('#hSettings').text("Settings [ close ]");
		} else {
			//$('#hangmanOptions').hide();
			$('#hangmanOptions').slideUp("normal");

			var source = $.cookie("source");
			var workfriendly = $.cookie("workfriendly");
			var options = "";

			if (source != null && source != "") {
				options = source;
			}
			if (workfriendly != null && (workfriendly == "true" || workfriendly == true)) {
				if (options == "") {
					options = "work friendly";
				} else {
					options += "; work friendly";
				}
			}
			if (options != "") {
				$('#hSettings').text("Settings | " + options);
			} else {
				$('#hSettings').text("Settings");
			}
		}

		e.preventDefault();
	});
});
