// ==UserScript==
// @namespace     http://xkcd.org/
// @name          xkcd titles - hidden so you need to hover to see it
// @author				riddle, improved by sesse
// @include       http://xkcd.*/*
// @include       http://www.xkcd.*/*
// ==/UserScript==

function insertAfter(newElement, targetElement) {
	var parent = targetElement.parentNode;
	if (parent.lastChild == targetElement) {
		parent.appendChild(newElement);
	} else {
		parent.insertBefore(newElement, targetElement.nextSibling);
	}
}

var mc = document.getElementById("middleContent");
if (mc) {
	var img = mc.getElementsByTagName("img")[0];
	if (img && img.title) {
		var style = document.createElement("style");
		style.appendChild(document.createTextNode("a.secret { color: white; font-variant: normal; font-weight: normal; text-decoration: none; } a.secret:hover { color: black; }"));
		insertAfter(style, img);

		var desc = document.createElement("div");
		var a = document.createElement("a");
		a.setAttribute("class", "secret");
		a.appendChild(document.createTextNode(img.title));
		img.title = null;
		desc.appendChild(a);

		insertAfter(desc, style);
	}
}
