// Source: http://stackoverflow.com/q/2090551/435253
function getQueryVariable(variable) {
	var query = window.location.search.substring(1);
	var vars = query.split("&");
	for (var i = 0; i < vars.length; i++) {
		var pair = vars[i].split("=");
		if (pair[0] == variable) {
			return unescape(pair[1].replace(/\+/g, " "));
		}
	}
	return undefined;
}

function entityDecode(str) {
	/* First, make the HTML safe */
	var translationMap = { "<": "&lt;", ">": "&gt;" };
	str = str.replace(/<>/g, function(match) {
		return translationMap[match];
	});

	/* Now let the browser decode entities */
	return $("<div/>").html(str).text();
}

function jqe(myid) { return myid.replace(/(:|\.)/g,'\\$1'); }

$(document).ready(function(){
	// Implement purely client-side surrogates for vffa-bulk.ficfan.org
	// TODO: Figure out a way to make this work for author pages too.
	var target = getQueryVariable('q');
	if (target) {
		/* Manually translate from the gzipped to gunzipped filenames */
		if (!document.getElementById(target)) {
			target = target.replace(/\.gz$/,"");
		}

		if (document.getElementById(target)) {
			if (window.history.replaceState) {
				window.history.replaceState({}, "", window.location.pathname);
			}
			window.location.hash = target;
		} else {
			$("div.body").eq(0).prepend("<div class='alert'>The direct link " +
				"you've followed is temporarily broken. However, you may " +
				"still manually navigate to the fic referenced. Sorry for " +
				"the inconvenience.</div>");
		}
	}

	// Highlight whatever the above code links to
	window.onhashchange = function() {
		$(".selected").removeClass("selected");
		if (location.hash) {
			$(jqe(location.hash) + '.fic').addClass("selected");
		}
	};
	window.onhashchange();

	$('a.extlink').click(function() {
		// Workaround for WebKit bug #54783
		if (jQuery.browser.webkit) {
			$(this).attr('target', '_blank');
		}
		try {
			window.urchinTracker('/external/' + $(this).attr('href'));
		} catch (e) {}
	});
	$('a.ficlink').click(function() {
		// Workaround for WebKit bug #54783
		if (jQuery.browser.webkit) {
			$(this).attr('target', '_blank');
		}
		try {
			window.urchinTracker('/fics/' + $(this).attr('title'));
		} catch (e) {}
	}).filter('a[href^="http://dl.dropbox.com/"][href$=".gz"]').click(
		function(evt) {
			alert("At the moment, you'll get gibberish if you visit this " +
				"link.\nYou'll have to right-click, choose 'Save Link As" +
				"...', and then decompress the file on your hard drive.\n" +
				"Windows users will need an archive tool like 7-zip or " +
				"WinRAR.\nSorry for the inconvenience.");
			evt.preventDefault();
			evt.stopImmediatePropagation();
	});

	$('a.nsfw, a[href^="http://stfan.free.fr/"]').click(function() {
		return confirm("The link you have clicked is Not Safe For Work. Continue?");
	});

	$('span.date').prettyDate();
	setInterval(function(){ $("span.date").prettyDate(); }, 1 * 60 * 1000);


	// Keep opera from rendering my zero-width spaces as boxes.
	if ($.browser.opera) { $('span.wbr').hide(); }

	var subject = getQueryVariable('subject');
	if (subject) {
		$('#subject').val(entityDecode(subject));
	}
});
// vi: noexpandtab

