// ==UserScript==
// @name           expandFootNote.user.js
// @namespace      http://furyu.tea-nifty.com/
// @description    Expand footnotes ver.0.01e
// @include        http://d.hatena.ne.jp/*
// @include        http://*.wikipedia.org/wiki/*
// @include        http://takagi-hiromitsu.jp/diary/*
// ==/UserScript==

(function(){
var	oWindow=window;
if (typeof unsafeWindow!='undefined') {
	window=unsafeWindow;
	document=window.document;
}
try {if (window!=top||(window.opener&&window.opener.location.href==window.location.href)) return}catch(e){};
var	w=window,d=w.document;

var	iswp=!!w.location.href.match(/^http:\/\/.+\.wikipedia\.org\/wiki/);

var	setEventHandler=(function(){
	if (w.addEventListener) {
		return function(obj,evt,handler){obj.addEventListener(evt,handler,false)};
	}
	else if (w.attachEvent) {
		return function(obj,evt,handler){obj.attachEvent('on'+evt,handler)};
	}
	else {
		return function(obj,evt,handler){var org=obj['on'+evt];obj['on'+evt]=function(){if(typeof org=='function')org();handler()}};
	}
})();	//	end of setEventHandler()

var	getElementsByTagAndClassName=function(tagName,className,parent) {
	if (!parent) parent=d;
	var children=parent.getElementsByTagName(tagName);
	var elements=[];
	if (className) {
		var	chkElms=(typeof className=='object')?className:className.split(' ');
		var	flgElms=[];
		for (var ci=0,len=chkElms.length; ci<len; ci++) flgElms[chkElms[ci]]=true;
		for (var ci=0,leni=children.length; ci<leni; ci++) {
			var child=children[ci];
			var cname=child.className;
			if (!cname) continue;
			var cnameElms = cname.split(' ');
			for (var cj=0,lenj=cnameElms.length; cj<lenj; cj++) {
				if (flgElms[cnameElms[cj]]) {
					elements[elements.length]=child;
					break;
				}
			}
		}
		return elements;
	}
	else {
//		return Array.prototype.slice.call(children,0);
		for (var ci=0,leni=children.length; ci<leni; ci++) elements[elements.length]=children[ci];
	}
	return elements;
}	//	end of getElementsByTagAndClassName()

function	addEfnSwitch(tgt) {
	var	switchFootnote=function(tgt,link,footnote,fontSize,e){
		if (!e) e=w.event;
		if (!e) return false;
		if (e.stopPropagation) {
			e.preventDefault();
			e.stopPropagation();
		}
		else {
			e.returnValue=false;
			e.cancelBubble=true;
		};
		var	tgtStyle=tgt.style, linkStyle=link.style, footnoteStyle=footnote.style;
		if (footnoteStyle.display=='block') {
			tgtStyle.fontSize=fontSize;
			linkStyle.fontSize='100%';
			footnoteStyle.display='none';
			link.title='open footnote';
		}
		else {
			tgtStyle.fontSize='100%';
			linkStyle.fontSize=fontSize;
			footnoteStyle.display='block';
			link.title='close footnote';
		}
		return false;
	};
	if (iswp) {
		var	srcs=getElementsByTagAndClassName('sup','reference',tgt);
		var	tgtlist=getElementsByTagAndClassName('ol','references',tgt)[0];
		if (!tgtlist) return;
		var	tgts=getElementsByTagAndClassName('li',null,tgtlist,tgt);
	}
	else {
		var	srcs=getElementsByTagAndClassName('span','footnote',tgt);
		var	tgts=getElementsByTagAndClassName('p','footnote',tgt);
	}
	for (var ci=srcs.length-1; 0<=ci; ci--) {
		var	src=srcs[ci];
		if (src.__flgefn__) break;
		src.__flgefn__=true;
		var	linksrc=src.getElementsByTagName('a')[0];
		if (linksrc&&linksrc.href.match(/#(.+)$/)) {
			var	check=RegExp.$1;
			for (var cj=tgts.length-1; 0<=cj; cj--) {
				var	tgt=tgts[cj];
				var	linktgt=tgt.getElementsByTagName('a')[0];
				if (linktgt) {
					if (linktgt.name==check||linktgt.id==check||tgt.id==check) {
						var	footnote=d.createElement('blockquote');
						var	footnoteStyle=footnote.style;
						footnoteStyle.display='none';
						if (iswp) {
							footnoteStyle.lineHeight='120%';
						}
						var	content=tgt.cloneNode(true);
						var	link=content.getElementsByTagName('a')[0];
						if (link) {
							link.removeAttribute('name');
							link.removeAttribute('id');
							link.href='#'+check;
							if (iswp) link.innerHTML=(cj+1)+'.';
						}
						if (iswp) {
							var	testNode=content.firstChild;
							if (testNode&&testNode.nodeType==3&&testNode.nodeValue.match(/^[\^\s]+$/)) content.removeChild(testNode);
							while (content.firstChild) footnote.appendChild(content.firstChild);
						}
						else {
							footnote.appendChild(content);
						}
						var	nextNode=linksrc.nextSibling;
						if (nextNode) {
							linksrc.parentNode.insertBefore(footnote,nextNode);
						}
						else {
							linksrc.parentNode.appendChild(footnote);
						}
						linksrc.title='open footnote';
						(function(src,footnote,linksrc){
							var	fontSize=(src.currentStyle||d.defaultView.getComputedStyle(src,'')).fontSize;
							setEventHandler(linksrc,'click',function(e){return switchFootnote(src,linksrc,footnote,fontSize,e)});
						})(src,footnote,linksrc);
//						tgts.splice(cj,1);
						break;
					}
				}
			}
		}
	}
}
addEfnSwitch(d);

(function(){
var	pagerBottom=d.getElementById('pager-bottom');
if (!pagerBottom) return;
var	link=pagerBottom.getElementsByTagName('a')[0],img=pagerBottom.getElementsByTagName('img')[0];
if (!link||!img) {
	setTimeout(arguments.callee,10);
	return;
}
var	checkHref=link.href;
var	isBusy=false;
function	checkPager(img) {
	setEventHandler(img,'click',function(){
		if (isBusy) return;
		isBusy=true;
		(function(){
			var	pagerBottom=d.getElementById('pager-bottom');
			var	link=pagerBottom.getElementsByTagName('a')[0];
			if (link) {
				var	newimg=pagerBottom.getElementsByTagName('img')[0];
				if (link.href==checkHref||img===newimg) {
					setTimeout(arguments.callee,10);
					return;
				}
				addEfnSwitch(d);
				checkhref=link.href;
				if (newimg) checkPager(newimg);
				isBusy=false;
			}
		})();
	});
}
checkPager(img);
})();

var	AutoPagerize=w.AutoPagerlike||oWindow.AutoPagerize;
if (AutoPagerize) {
	var	addFilter=(AutoPagerize.addElementFilter)?AutoPagerize.addElementFilter:AutoPagerize.addFilter;
	addFilter(function(nodes,url,siteinfo){
		for (var ci=0,len=nodes.length; ci<len; ci++) {
			addEfnSwitch(nodes[ci]);
		}
	});
}

})();

