var answerGenie = (function(window, document) {
	var callUrl = '/question';
	var isSending = false;
	var display = document.getElementById('display');
	var message = document.getElementById('message');
	var sendButton = document.getElementById('sendButton');
	var restartButton = document.getElementById('restartButton');

	function buildResponse(response) {
		display.innerHTML = response;
		message.style.display = 'none';
		message.value = '';
		sendButton.style.display = 'none';
		restartButton.style.display = 'block';
	}
	return {
		sendAjax: function() {
			if (!isSending) {
				var xhr = new XMLHttpRequest();
				xhr.open('GET', callUrl);
				xhr.onload = function() {
					if (xhr.status === 200) {
						buildResponse(xhr.responseText);
						isSending = false;
					} else {
						console.error('Request failed.  Returned status of ' +
						xhr.status);
						buildResponse('You\'re such a shit,' +
							' the server won\'t even respond');
					}
				};
				isSending = true;
				xhr.send();
			}
		},
		restart: function() {
			display.innerHTML = 'Answer Genie 9001';
			message.style.display = 'inline';
			sendButton.style.display = 'inline';
			restartButton.style.display = 'none';
		}
	};
})(window, document);
