// ==UserScript==
// @name        Youtube get_video
// @description 
// @namespace   http://lowreal.net/
// @include     http://*youtube.com/*
// ==/UserScript==


(function () {
alert(location.pathname);
	switch (location.pathname) {
		case "/watch": {
			var href = document.getElementById("interactDiv").innerHTML.match(RegExp('"/player2.swf([^"]+)'))[1];
			href = href.replace(/&amp;/g, "&");
			href = href.replace(/&l=[^&]+/, "");
			document.getElementById("actionsDiv").appendChild(
				$N("div", {"class":"actionRow"}, [
					$N("a", {href:"/get_video" + href, "class": "noul"}, [
						$N("img", {src:"/img/web_w_icon.gif",border:"0","class":"alignMid"}),
						$N("span", {"class":"eLink",style:"font-weight: bold"}, "Save Video")
					])
				])
			);
		}
		default: {
			$X("//a[starts-with(@href, '/watch?v=') and not(.//img)]").forEach(function (i){
				var e = $N("span", {}, [
					" ",
					$N("a", {href:"javascript:void(0);"}, "[Save]"),
					" "
					]);
				var video_id = i.href.match(/v=([^&]+)/)[1];
				i.parentNode.insertBefore(e, i);
				e.addEventListener("click", function (e) {
					GM_xmlhttpRequest({
						method : "GET",
						url : "/watch?v=" + video_id,

						headers : {
						},

						onload : function (req) {
							var m = req.responseText.match(RegExp('"/player2.swf([^"]+)'));
							open("http://youtube.com/get_video" + m[1]);
						},

						onerror : function (req) {
						}
					});
				}, 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;
	}

})();
