// ==UserScript==
// @name          Incremental Tag Search for Hatena B!
// @namespace     http://nomadscafe.jp/
// @description	  helper for searching tags
// @include       http://b.hatena.ne.jp/*
// ==/UserScript==

(function() {

    var tags = document.evaluate(
        '(/descendant::A[attribute::class = "currenttag"]|/descendant::A[attribute::class = "tag-latest"]|/descendant::A[attribute::class = "tag-later"]|/descendant::A[attribute::class = "tag-earlier"]|/descendant::A[attribute::class = "tag-earliest"])',
	document, null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null
    );
    
    var taglistUL = document.evaluate(
        '/descendant::UL[attribute::class = "taglist"]',
	document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null
    ).singleNodeValue;
    
    if(taglistUL && tags.snapshotLength){
	var div = document.createElement('p');
	div.className = "taglist";

	var span = document.createElement('span');
	span.style.fontSize='8pt';
	span.appendChild(document.createTextNode('Incremental Tag Search'));
	div.appendChild(span);

	var igrep = document.createElement('input');
	igrep.type="text";
	igrep.style.width="100%";
	igrep.addEventListener('keyup',function(){
	    var str = igrep.value;
	    str = str.replace(/(\W)/,"\\$1");
	    var regex = new RegExp(str, "i");
	    for (var i = 0; i < tags.snapshotLength; i++) {
	        if (tags.snapshotItem(i).innerHTML.match(regex)) {
		    tags.snapshotItem(i).style.display="";
	        } else {
		    tags.snapshotItem(i).style.display="none";
	        }
	    }
	},true);

	div.appendChild(igrep);
	taglistUL.parentNode.insertBefore(div,taglistUL);
    }

})();
