// Comics Alt Text
// version 0.3.1
// 11 Apr 2006
// Copyright (c) 2005, Adam Vandenberg
// Released under the GPL license
// http://www.gnu.org/copyleft/gpl.html
//
// ==UserScript==
// @name		Comics Alt Text
// @namespace	http://adamv.com/greases/
// @description 	Shows the "hover text" for some comics on the page
// @include	http://achewood.com/*
// @include	http://*.achewood.com/*
// @include	http://qwantz.com/*
// @include	http://*.qwantz.com/*
// @include	http://asofterworld.com/*
// @include	http://*.asofterworld.com/*
// @include http://drmcninja.com/*
// @include http://xkcd.com/*
// @include http://*.xkcd.com/*
// ==/UserScript==

(function(){
	function selectNodes(xpath, elem){
		var results = document.evaluate(
			xpath, elem || document, null,
			XPathResult.UNORDERED_NODE_ITERATOR_TYPE, null)

		var nodes = new Array();
		var result = null;
		while(result = results.iterateNext()) nodes.push(result);

		return nodes;
	}

	var tComic = {
		"achewood": '//img[starts-with(@src, "/comic.php?date=")]',
		"qwantz": '//img[starts-with(@src, "http://www.qwantz.com/comics/")]',
		"asofterworld": '//td/center/img',
		"drmcninja": '//img[starts-with(@src, "/issue")]',
		"xkcd": '//img[starts-with(@src, "http://imgs.xkcd.com/comics")]',
	}


	var whichSite;
	if (location.host.indexOf("achewood") > -1)
		whichSite = "achewood"
	else if (location.host.indexOf("qwantz") > -1)
		whichSite = "qwantz"
	else if (location.host.indexOf("asofterworld") > -1)
		whichSite = "asofterworld"
	else if (location.host.indexOf("drmcninja") > -1)
		whichSite = "drmcninja"
	else if (location.host.indexOf("xkcd") > -1){
		whichSite = "xkcd"
	}
	else return;

	var comic = selectNodes(tComic[whichSite])[0];
	if (!comic){
		 return;
	}
	if (comic.title){
		div = document.createElement("div")
		div.className ="msg"
		div.innerHTML = "(" + comic.title + ")"
		comic.parentNode.insertBefore(div, comic.nextSibling)
	}
})();

