// ==UserScript==
// @name        Hatena Bookmark show SS and Comments
// @description Show screenshot and comments on click [\d users?] link
// @namespace   http://lowreal.net/
// @include     http://b.hatena.ne.jp/*
// ==/UserScript==

(function () {

	const STYLE = "font-size: 75%; border: 1px solid #ccc; -moz-border-radius: 1em; padding: 1em; margin: 1em; width: 70%; overflow: hidden; background: #f6f6f6";

	$X("//*[@class='bookmarklist' or @class='entry']//a[starts-with(@href, '/entry/') and contains(., 'user')]").forEach(function (i) {

		var parent = $X("ancestor::dl", i)[0];
		if (!parent) parent = $X("ancestor::div[@class='entry']", i)[0];
		var cache = null;

		parent.style.clear = "left";

		i.addEventListener("click", function (e) {
			e.preventDefault();
			e.stopPropagation();


			if (cache) {
				cache.style.display = (cache.style.display == "none") ? "block" : "none";
			} else {
				GM_xmlhttpRequest({
					method : "GET",
					url : i.href,

					onload : function (req) {
						var d = document.createElement("div");
						d.innerHTML = req.responseText;

						var ss, bl;

						var dd = $N("div", {
							"class":"entry-footer",
							style: STYLE
						}, [
							ss = $X(".//div[@class='screenshot']", d)[0],
							bl = $X(".//div[@class='bookmarklist']/ul", d)[0]
							]);


						with (ss.style) {
							cssFloat = "left";
							border = "1px solid #000";
							margin = "1em 2em 1em 0";
						}

						$X(".//li", bl).forEach(function (ee) {
							//
							if ($X("string(child::text()[last()])", ee).match(/^\s*$/)) {
								ee.style.display = "none";
							}
						});

						parent.appendChild(dd);
						cache = dd;
					},

					onerror : function (req) {
						alert(req.responseText);
					}
				});
			}
		}, false);
	});


	/* template functions  */
	function $N (name, attr, childs) {
		var ret = document.createElement(name);
		for (k in attr) {
			if (!attr.hasOwnProperty(k)) continue;
			v = attr[k];
			if (k == "class") {
				ret.className = v;
			} else {
				ret.setAttribute(k, v);
			}
		}
		switch (typeof childs) {
			case "string": {
				ret.appendChild(document.createTextNode(childs));
				break;
			}
			case "object": {
				for (var i = 0, len = childs.length; i < len; i++) {
					var child = childs[i];
					if (typeof child == "string") {
						ret.appendChild(document.createTextNode(child));
					} else {
						ret.appendChild(child);
					}
				}
				break;
			}
		}
		return ret;
	}

	function $X (exp, context) {
		if (!context) context = document;
		var resolver = function (prefix) {
			var o = document.createNSResolver(context)(prefix);
			return o ? o : (document.contentType == "text/html") ? "" : "http://www.w3.org/1999/xhtml";
		}
		var exp = document.createExpression(exp, resolver);

		var result = exp.evaluate(context, XPathResult.ANY_TYPE, null);
		switch (result.resultType) {
			case XPathResult.STRING_TYPE : return result.stringValue;
			case XPathResult.NUMBER_TYPE : return result.numberValue;
			case XPathResult.BOOLEAN_TYPE: return result.booleanValue;
			case XPathResult.UNORDERED_NODE_ITERATOR_TYPE: {
				result = exp.evaluate(context, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null);
				var ret = [];
				for (var i = 0, len = result.snapshotLength; i < len ; i++) {
					ret.push(result.snapshotItem(i));
				}
				return ret;
			}
		}
		return null;
	}


})();
