$(document).ready(function() {
	$("#no_script").css("display", "none");
	$("#script").css("display", "block");
	
	var secret_trigger = false,
		secret_answer = "",
		short_question = "Hey, Ace Quest",
		long_question = "Hey, Ace Quest Adventunare",
		ignore_keyCode = {
			8  : 'backspace',
			9  : 'tab',
			35 : 'end', 	
			36 : 'home',
			37 : 'left',
			38 : 'up',
			39 : 'right',
			40 : 'down',
			46 : 'delete'},
		no_messages = [
			"Ace assures me he has thought about your question before. Unfortunately, he doesn't like you. Sorry.",
			"That's an interesting question...Try googling it.",
			"Really? Ace refuses to answer that.",
			"Ace thinks that there are stupid questions and stupid people. You connect the dots...On second thought, we should probably connect those dots. You're stupid.",
			"I wish Ace wasn't shy, otherwise he might answer that.",
			"Ace refuses to answer anymore questions phrased that way.",
			"Excuse me? You think that question is worth Ace Quest's time?",
			"It's not that Ace doesn't like you...actually it is."],
		yes_messages = [
			"Ace thought about the question, and he thinks the answer is...",
			"That's a tough one, but after much thought Ace thinks the answer is...",
			"Ace thought you might ask that. He says the answer is...",
			"Trying to pull a fast one on us, huh? Try this answer on for size...",
			"Ace says..."];
		
	//Ask ace a question
	$("#question")
		.keypress(function(event) {
			var keyCode = event.keyCode || event.which,
				key = String.fromCharCode(keyCode),
				$element = $(this),
				value = $element.attr("value"),
				value_length = value.length;
				
			if(ignore_keyCode[keyCode]) {return;}
			
			if(value.length === 0 && key === ";" || secret_trigger) {
				secret_trigger = true;
				var replace_character;
				
				if(value_length < short_question.length) {
					replace_character = short_question.charAt(value_length);
				} else if(value_length < long_question.length) {	
					replace_character = long_question.charAt(value_length);
				}
				$element.attr("value", value + replace_character);
				
				if(value.length > 0 && key === ";" || value.length + 1 === long_question.length) {
					secret_trigger = false;
				}
				
				secret_answer = key !== ";" 
					? secret_answer + key
					: secret_answer;
				return false;
			}
		});

	$("#askace")
		.click(function(event) {
			var answer = $("#question").val() + "<br/><br/>";
			$("#question").val("");
			
			var random = 0;
			
			while(random === 0) {
				random = Math.random()
			}
			if(secret_answer.length > 0 ) {
				random = Math.ceil(random * yes_messages.length) - 1;
				$("#answer").html(answer + yes_messages[random] + secret_answer);
			} else {
				random = Math.ceil(random * no_messages.length) - 1;
				$("#answer").html(answer + no_messages[random]);
			}
			secret_answer = "";
		});
});
