if (window.location.href.substring(0, 5) == "http:") {
	GeoTest = window.GeoTest || {};

	GeoTest.BUG_LINK, GeoTest.BUG_POPUP, GeoTest.BUG_FIELD, GeoTest.BUG_CLICK, GeoTest.BUG_PROMPT;

	GeoTest.state = GeoTest.state || {};

	GeoTest.bugs = {
		MESSAGE:
			"Use this for glitches, bugs, or whenever " +
			"I messed up someone's capital. I'll probably " +
			"fix it by tomorrow.",
	
		showBugs: function () {
			GeoTest.BUG_LINK.addClass("enabled");
		
			GeoTest.BUG_PROMPT.text(GeoTest.bugs.MESSAGE);
		
			$("body").append(GeoTest.BUG_POPUP);
		},
		hideBugs: function () {
			GeoTest.BUG_LINK.removeClass("enabled");
			GeoTest.BUG_POPUP.remove();
		},
		submitBug: function (text, oncomplete) {
			$.post("/bug", {"text": text}, function () {
				oncomplete();
			});
		}
	};

	$(function () {
		GeoTest.BUG_LINK = $('<a>problems?</a>').addClass("bug");
	
		GeoTest.BUG_POPUP = $('<p class="bug_popup"></p>');

		GeoTest.BUG_PROMPT = $("<p/>").text(GeoTest.bugs.MESSAGE)
		GeoTest.BUG_POPUP.append(GeoTest.BUG_PROMPT);
	
		GeoTest.BUG_FIELD = $('<textarea></textarea>');
		GeoTest.BUG_CLICK = $('<input type="submit" value="Report"/>');
	
		GeoTest.BUG_CLICK.click(function () {
			GeoTest.BUG_CLICK.attr("disabled", "disabled").val("Reporting...");
		
			GeoTest.bugs.submitBug(GeoTest.BUG_FIELD.val(), function () {
				GeoTest.BUG_PROMPT.text("Thank you! I'll try to fix it as soon as I can.");
			
				GeoTest.BUG_FIELD.val("");
			
				setTimeout(function () {
					GeoTest.bugs.hideBugs();
				}, 10 * 1000);
			
				GeoTest.BUG_CLICK.removeAttr("disabled", "disabled").val("Report");
			});
		})
	
		GeoTest.BUG_POPUP.append(GeoTest.BUG_FIELD).append(GeoTest.BUG_CLICK);
	
		$("body").append(GeoTest.BUG_LINK);
	
		GeoTest.BUG_LINK.click(function() {
			if (!GeoTest.state.showingBugs) {
				GeoTest.bugs.showBugs();
			
				GeoTest.state.showingBugs = true;
			
			} else {
				GeoTest.bugs.hideBugs();
			
				GeoTest.state.showingBugs = false;
			}
		
			return false;
		})
	});

}