// ==UserScript==
// @name          NicoNicoSampleInserter
// @namespace     http://libelabo.jp/
// @include       http://*
// @exclude       http://www.nicovideo.jp/*
// @exclude       http://www.smilevideo.jp/*
// @exclude       http://ichiba.nicovideo.jp/*
// @exclude       http://www.nicochart.jp/*
// @exclude       http://ext.nicovideo.jp/*
// @version       0.2
// ==/UserScript==

(function() {
	
	var videopath = './/a[contains(@href,"www.nicovideo.jp/watch")]';
	var interval = 1500;
	
	function niconico_insert(urlvideo){
		if(urlvideo==null) return;
		var url=urlvideo.getAttribute("href");
		if( url.match(/^http:\/\/www.nicovideo.jp\/watch\/(.*)/) ){
			var vid = RegExp.$1;
			var niconico = document.createElement('div');
			var style = niconico.style;
			style.width = "318px";
			style.border = "solid 1px #ccc";
			niconico.innerHTML = '<iframe src="http://www.nicovideo.jp/thumb?v=' + vid + '" width="100%" height="198" scrolling="no" border="0" frameborder="0"></iframe>';
			urlvideo.parentNode.insertBefore(niconico,urlvideo.nextSibling);
		}
	}
	
	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 len != 0 ? ret : null;
			}
		}
		return null;
	}
	
	// Thanks to os0x
	// http://d.hatena.ne.jp/os0x/20071018/1192663237
	var eachLater = function(arr,ms,call,callback,ob) {
		var self = arr;
		if(!arr) return;
		var obj = ob || typeof(callback) != 'function' ? callback : null;
		var res = {
			complete: false,
			cancel: function(){
				clearInterval(PID);
			},
			notify: function(){
				clearInterval(PID);
				if (endCallback && typeof(endCallback) == 'function') callback.call(obj,self);
			}
		};
		var len = self.length;
		var i = 0;
		var PID = setInterval(function(){
			try{
				call.call(obj,self[i],i);
			} catch (e) {
			} finally {
				i++;
				if (i >= len) {
					clearInterval(PID);
					res.complete = true;
					if(callback && typeof(callback) == 'function') callback.call(obj,self);
				}
			}
		},ms);
		return res;
	}
	
	eachLater($x(videopath,document),interval,function(a){niconico_insert(a)});
	
	if(window.AutoPagerize && window.AutoPagerize.addFilter) {
		window.AutoPagerize.addFilter(
		function(docs) {
			if(!docs) return;
			// console.log(docs);
			(function(c){
				var _self = arguments.callee;
				if(!docs[c]) return
				eachLater($x(videopath,docs[c]),interval,function(a){niconico_insert(a)},function(){_self(++c);});
			})(0);
		}
	);
	}
})();
