// ==UserScript==
// @name           hbBox.user.js
// @namespace      http://furyu.tea-nifty.com/
// @description    HatenaBookmark::Box ver.0.01i
// @include        http://*
// ==/UserScript==

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

// === Options
var	hbFontPixelMin=24;
var	hbFontPixelMax=48;
var	hbStrLengthMax=100;
var	hbOffsetX=116,hbOffsetY=16;
var	hbFontFamily='"MS PGothic", "Hiragino Kaku Gothic Pro", Osaka, sans-serif';
var	hbImageIcon=true;
var	hbShowId=true;
var	hbFilterOpacity=75;
var	hbFilterColor='black';
//var	hbChangeWait=0;
var	hbChangeWait=100;
var	hbSwitchWheelUpDown=false;
var	hbTopOffsetRate=1/5;

// ===
var	isIE=(function(){
	if (w.navigator.userAgent.match(/msie\s+(\d+)/i)) {
		var	iever=Number(RegExp.$1);
		if (iever<7) return true;
		if (d.compatMode!='CSS1Compat') return true;
	}
	return false;
})();

var	setTimeout,clearTimeout,setInterval,clearInterval;
(function(){
var	resolution=10;

var	origSetTimeout=w.setTimeout;
var	origClearTimeout=w.clearTimeout;
var	origSetInterval=w.setInterval;
var	origClearInterval=w.clearInterval;

var	comId=undefined,numId=0,cntId=0,timerInfos=[];
var	fncTout=function(){
	var now=(new Date()).getTime();
	for (var tid in timerInfos) {
		var	timerInfo=timerInfos[tid];
		if (!timerInfo) continue;
		var	handler=timerInfo.handler;
		if (handler&&timerInfo.tout<=now) {
			handler();
			if (timerInfo.resetflg) timerInfo.tout+=timerInfo.tval;
			else delete timerInfos[tid];
		}
	}
};
setTimeout=function(handler,tval,resetflg) {
	var now=(new Date()).getTime();
	if (typeof handler=='string') handler=new Function(handler);
	var	tid='s'+(numId++);
	if (!cntId++) comId=origSetInterval(fncTout,resolution);
	timerInfos[tid]={handler:handler,tval:tval,tout:now+tval,resetflg:resetflg};
	return tid;
};
clearTimeout=function(tid) {
	if (timerInfos[tid]) {
		if (!--cntId) {
			origClearInterval(comId);
			comId=undefined;
		}
		delete timerInfos[tid];
		return undefined;
	}
	else {
		return origClearTimeout(tid);
	}
};
setInterval=function(handler,tval){return setTimeout(handler,tval,true)};
clearInterval=clearTimeout;
})();

var	stripTags=function(txt){return txt.replace(/<\/?[^>]+>/gi,'')};

var	getClientWidth=(function(){
	if (typeof w.innerWidth!='undefined') {
		return function() {return w.innerWidth};
	}
	else if (d.all) {
		if (d.compatMode=='CSS1Compat') {
			return function() {return d.documentElement.clientWidth};
		}
		else {
			return function() {return d.body.clientWidth};
		}
	}
	else {
		return function() {return 0};
	}
})();	//	end of getClientWidth()

var	getClientHeight=(function(){
	if (typeof w.innerHeight!='undefined') {
		return function() {return w.innerHeight};
	}
	else if (d.all) {
		if (d.compatMode=='CSS1Compat') {
			return function() {return d.documentElement.clientHeight};
		}
		else {
			return function() {return d.body.clientHeight};
		}
	}
	else {
		return function() {return 0};
	}
})();	//	end of getClientHeight()

var	getClientTop=(function(){
	if ( typeof w.pageYOffset != 'undefined' ) {
		return function() {return w.pageYOffset};
	}
	else if (d.all) {
		if (d.compatMode=='CSS1Compat') {
			return function() {return d.documentElement.scrollTop};
		}
		else {
			return function() {return d.body.scrollTop};
		}
	}
	else {
		return function() {return 0};
	}
})();	//	end of getClientTop()

//	===
var	getClientLeft=(function(){
	if ( typeof w.pageXOffset != 'undefined' ) {
		return function() {return w.pageXOffset};
	}
	else if (d.all) {
		if (d.compatMode=='CSS1Compat') {
			return function() {return d.documentElement.scrollLeft};
		}
		else {
			return function() {return d.body.scrollLeft};
		}
	}
	else {
		return function() {return 0};
	}
})();	//	end of getClientLeft()

var	setEventHandler=(function(){
	var	handlerList=[],firstcall=true,completed=false;
	var	execFunctions=function(){
		for (var ci=0,len=handlerList.length; ci<len; ci++) handlerList[ci]();
		handlerList=[];
		completed=true;
	};
	if (w.attachEvent && w.navigator.userAgent.match(/msie/i)) {
		var	setHandler=function(obj,evt,handler){
			if (evt=='keypress') evt='keydown';
			if (evt=='load'&&obj==w) {
				if (completed||d.readyState=='complete') {
					handler();
				}
				else {
					handlerList[handlerList.length]=handler;
					if (firstcall) {
						d.write('<script type="text/javascript" defer src="javascript:void(0)"><\/script>');
						var	scripts=d.getElementsByTagName('script'),script=scripts[scripts.length-1];
						script.onreadystatechange=function(){
							if (script.readyState=='complete') execFunctions();
						};
						firstcall=false;
					}
				}
			}
			else {
				obj.attachEvent('on'+evt,handler)
			}
		};
		setHandler(w,'load',function(){});	//	dummy
		return setHandler;
	}
	else if (w.addEventListener) {
		if (w.navigator.userAgent.indexOf('AppleWebKit/') > -1) {
			return function(obj,evt,handler){
				if (evt=='load'&&obj==w) {
					var	stat=d.readyState;
					if (completed||stat=='loaded'||stat=='complete') {
						handler();
					}
					else {
						handlerList[handlerList.length]=handler;
						if (firstcall) {
							(function(){
								var	stat=d.readyState;
								if (stat=='loaded'||stat=='complete') execFunctions(); else setTimeout(arguments.callee,50);
							})();
							firstcall=false;
						}
					}
				}
				else {
					obj.addEventListener(evt,handler,false)
				}
			}
		}
		else {
			return function(obj,evt,handler){
				if (evt=='load'&&obj==w) {
					try {d.addEventListener("DOMContentLoaded",handler,false)} catch(e){obj.addEventListener(evt,handler,false)}
				}
				else {
					if (evt=='mousewheel'&&w.navigator.userAgent.match(/Firefox/i)) evt='DOMMouseScroll';
					obj.addEventListener(evt,handler,false);
				}
			};
		}
	}
	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	rmvEventHandler=(function(){
	if (w.attachEvent && w.navigator.userAgent.match(/msie/i)) {
		return function(obj,evt,handler){
			if (evt=='load'&&obj==w) return;
			if (evt=='keypress') evt='keydown';
			obj.detachEvent('on'+evt,handler);
		};
	}
	else if (w.addEventListener) {
		return function(obj,evt,handler){
			if (evt=='load'&&obj==w) return;
			if (evt=='mousewheel'&&w.navigator.userAgent.match(/Firefox/i)) evt='DOMMouseScroll';
			obj.removeEventListener(evt,handler,false);
		};
	}
})();

var	getFontPixel=function(textLen){
	var	fontPixel=hbFontPixelMin;
	if (hbFontPixelMin<hbFontPixelMax) {
		var	fontColorNum=0,chkLen=0;
		var	lenStep=~~(hbStrLengthMax/(hbFontPixelMax-hbFontPixelMin))*4;
		for (fontPixel=hbFontPixelMax;hbFontPixelMin<fontPixel;fontPixel-=4,chkLen+=lenStep,fontColorNum++) {
			if (textLen<chkLen+lenStep) break;
		}
	}
	return fontPixel;
};

var	setMouseHandler=function(obj,evt,handler) {
	var	mouseHandler=function(curEvent,e) {
		if (!e) e=w.event;
		if (!e) return;
		var	prop=obj._MouseProp_;
		var	curStatus=prop.status;
		if (curStatus==curEvent) return;
		if (curStatus=='out') {	//	outer to inner
			var	fncArrayOver=prop.fncArrayOver;
			for (var ci=0,len=fncArrayOver.length; ci<len; ci++) {
				fncArrayOver[ci].apply(obj,[e]);
			}
		}
		else {					//	inner to outer
			var	chkNode=e.toElement||e.relatedTarget;
			while (chkNode) {
				if (chkNode==obj) return;
				chkNode=chkNode.parentNode;
			}
			var	fncArrayOut=prop.fncArrayOut;
			for (var ci=0,len=fncArrayOut.length; ci<len; ci++) {
				fncArrayOut[ci].apply(obj,[e]);
			}
		}
		prop.status=curEvent;
	};
	var	prop=obj._MouseProp_;
	if (!prop) {
		prop=obj._MouseProp_={status:'out',fncArrayOver:[],fncArrayOut:[]};
		setEventHandler(obj,'mouseover',function(e){mouseHandler('over',e)});
		setEventHandler(obj,'mouseout',function(e){mouseHandler('out',e)});
	}
	if (evt=='mouseover') {
		prop.fncArrayOver[prop.fncArrayOver.length]=handler;
	}
	else if (evt=='mouseout') {
		prop.fncArrayOut[prop.fncArrayOut.length]=handler;
	}
	else {
		setEventHandler(obj,evt,handler);
	}
};
var	rmvMouseHandler=function(obj,evt,handler) {
	var	prop=obj._MouseProp_;
	if (!prop) {
		rmvEventHandler(obj,evt,handler);
		return;
	}
	if (evt=='mouseover') {
		var	fncArrayOver=prop.fncArrayOver;
		for (var ci=0,len=fncArrayOver.length; ci<len; ci++) {
			if (fncArrayOver[ci]==handler) {
				fncArrayOver.splice(ci,1);
				break;
			}
		}
	}
	else if (evt=='mouseout') {
		var	fncArrayOut=prop.fncArrayOut;
		for (var ci=0,len=fncArrayOut.length; ci<len; ci++) {
			if (fncArrayOut[ci]==handler) {
				fncArrayOut.splice(ci,1);
				break;
			}
		}
	}
	else {
		rmvEventHandler(obj,evt,handler);
	}
};

var	addWordBreak=(function(){
	if (w.navigator.userAgent.match(/msie/i)) {
		return function(html) {return html};
	}
	else {
		var	wb='<wbr />';
		var	reg1=/[:\-\.\/\?=;_,\d]/g, fnc1=function(s){return s+wb};
		var	reg2=/%/g, fnc2=function(s){return wb+s};
		return function(html) {
			return html.replace(reg1,fnc1).replace(reg2,fnc2);
		};
	}
})();	//	end of addWordBreak()

var	cssAddRule=(function(){
	var	d=document;
	var	styleElm=d.createElement('style');
	styleElm.type='text/css';
	try {
		d.getElementsByTagName('head')[0].appendChild(styleElm);
	}
	catch(e){
		return function(){};
	}
	var	s=d.styleSheets[d.styleSheets.length-1];
	if (s.addRule) {			// for IE
		return function(selector,property){
			s.addRule(selector,'{'+property+'}');
		};
	}
	else if (s.insertRule) {	// for others
		return function(selector,property){
			s.insertRule(selector+'{'+property+'}',s.cssRules.length);
		};
	}
})();	//	end of cssAddRule()

var	getCurrentStyle=function(obj){
	return obj.currentStyle||d.defaultView.getComputedStyle(obj,'');
}	//	end of getObjectTop()

cssAddRule('.hbBoxHopper a','text-decoration:none;');
cssAddRule('.hbBoxHopper a','border:none;');
cssAddRule('a.hbBoxLink','text-decoration:none;');
cssAddRule('a.hbBoxLink','border:none;');
cssAddRule('a.hbBoxLink:link','color:#006666');
cssAddRule('a.hbBoxLink:visited','color:#666666');

var	objectResume,objectSuspend;
(function(){
	var	tgts={
		selects	:	d.getElementsByTagName('select')
	,	objects	:	d.getElementsByTagName('object')
	,	iframes	:	d.getElementsByTagName('iframe')
	,	embeds	:	d.getElementsByTagName('embed')
	};
	for (var mem in tgts) {
		var	tgt=tgts[mem];
		for (var ci=0,len=tgt.length; ci<len; ci++) {
			try{tgt[ci].ovisibility=tgt[ci].style.visibility;}catch(e){};
		}
	}
	objectResume=function(){
		for (var mem in tgts) {
			var	tgt=tgts[mem];
			for (var ci=0,len=tgt.length; ci<len; ci++) {
				try{tgt[ci].style.visibility=tgt[ci].ovisibility;}catch(e){};
			}
		}
	};	//	end of objectResume()
	objectSuspend=function(){
		for (var mem in tgts) {
			var	tgt=tgts[mem];
			for (var ci=0,len=tgt.length; ci<len; ci++) {
				try{tgt[ci].style.visibility='hidden';}catch(e){};
			}
		}
	};	//	end of objectSuspend()
})();

var	curHopperId=0;
var	hoppers=[];
var	keyenable=false;
var	cancelBubble=function(e){
	if (!e) e=w.event;
	if (!e) return;
	if (e.stopPropagation) {
		e.preventDefault();
		e.stopPropagation();
	}
	else {
		e.returnValue=false;
		e.cancelBubble=true;
	};
	return false;
};

var	createHopper=function(entry,hbContainer,total,hbox,filter){
	var	hid=entry.id,text=entry.content,link=entry.link,tags=entry.tags;;
	var	textLen=text.length;
	if (!textLen||textLen<=0) return;
	
	var	cWidth=getClientWidth(),cHeight=getClientHeight(),cLeft=getClientLeft(),cTop=getClientTop();
	var	maxFontPixel=getFontPixel(textLen);
	
	var	hopper=d.createElement('div');
	hopper.className='hbBoxHopper';
	var	style=hopper.style;
	style.textAlign='left';
	style.position='absolute';
	style.margin=0;
	style.padding='4px';
	style.overflow='hidden';
	style.cursor='pointer';
	style.fontFamily=hbFontFamily;
	style.fontWeight='bolder';
	style.zIndex=1000000;
	style.display='none';
	style.height='auto';
	style.border='orange double 3px';
	style.color='navy';
	style.background='lightyellow';
	style.left='4px';
	style.top=(cTop+~~(cHeight*hbTopOffsetRate))+'px';
	
	setEventHandler(hopper,'click',function(e){
		if (!e) e=w.event;
		if (e&&e.shiftKey) w.open(entry.link,'_blank');
	});
	setMouseHandler(hopper,'mouseover',function(e){
		style.zIndex=1;
		filter.style.zIndex=1;
	});
	setMouseHandler(hopper,'mouseout',function(e){
	});
	hopper.innerHTML=[
		'<div style="color:brown; font-size:14px; line-height:120%; padding:4px 0; margin:0;">'
	,	'['+(total-entry.no)+'/'+total+'] '
	,	entry.date
	,	'<\/div>'
	].join('');
	if (hbImageIcon) {
		var	img=new Image();
		img.title=img.alt='id:'+hid;
		img.style.borderWidth='0px';
		img.style.cursor='pointer';
		img.style.width=img.style.height='64px';
		img.src='http://www.hatena.ne.jp/users/'+hid.slice(0,2)+'/'+hid+'/profile.gif';
		setEventHandler(img,'click',function(e){
			w.open(entry.link,'_blank');
		});
		hopper.appendChild(img);
	}
	if (!hbImageIcon||hbShowId) {
		hopper.innerHTML+=[
			'<span style="line-height:140%; font-size:'+hbFontPixelMax+'px;">&nbsp;'
		,	'<a href="'+entry.link+'" target="_blank" class="hbBoxLink">'
		,	hid
		,	'<\/a>'
		,	'<\/span>'
		].join('');
	}
	var	tagStrs=[];
	for (var ci=0,len=tags.length; ci<len; ci++) {
		var	tag=tags[ci];
		tagStrs[tagStrs.length]='<a href="http://b.hatena.ne.jp/t/'+encodeURIComponent(tag)+'" class="hbBoxLink" target="_blank">'+tag+'<\/a>';
	}
	hopper.innerHTML+=[
		'<div style="line-height:120%; font-size:16px; margin:0 18px; font-family:verdana">'
	,	tagStrs.join(' ')
	,	'<\/div>'
	,	'<div style="line-height:120%; font-size:'+maxFontPixel+'px; margin:0 18px;">'
	,	addWordBreak(text)
	,	'<\/div>'
	].join('');
	d.body.appendChild(hopper);
	
	var	imgSmall=new Image();
	imgSmall.src='http://www.hatena.ne.jp/users/'+hid.slice(0,2)+'/'+hid+'/profile_s.gif';
	imgSmall.title=img.alt='id:'+hid;
	imgSmall.border='0';
	imgSmall.hspace='0';
	imgSmall.vspace='0';
	var	ss=imgSmall.style;
	ss.cursor='pointer';
	ss.zIndex=1000000;
	ss.styleFloat=ss.cssFloat='right';
	ss.margin=0;
	ss.padding=0;
	ss.background='transparent';
	ss.border='solid 2px';
	ss.width='16px';
	ss.height='16px';
	ss.borderColor='transparent';
	
	setEventHandler(imgSmall,'click',function(e){
		w.open(entry.link,'_blank');
	});
	setEventHandler(imgSmall,'click',function(e){
		w.open(entry.link,'_blank');
	});
	
	var	selfId=hoppers.length;
	
	var	onmouseover=function(e){
		style.top=(getClientTop()+~~(getClientHeight()*hbTopOffsetRate))+'px';
		style.width=(getClientWidth()-32)+'px';
		style.display='block';
		style.zIndex=1000000;
		
		var	hstarButton=entry.hstarButton;
		if (!hstarButton) {
			var	spans=w.Ten.DOM.getElementsByTagAndClassName('span','hatena-star-star-container',entry.hstar);
			var	container=spans[0];
			if (container) {
				var	basecontainer=hopper.getElementsByTagName('div')[0];
				
				for (var ci=0,len=spans.length; ci<len; ci++) spans[ci].parentNode.removeChild(spans[ci]);
				
				spans=w.Ten.DOM.getElementsByTagAndClassName('span','hatena-star-comment-container',entry.hstar);
				var	cmtcontainer=spans[0];
				for (var ci=0,len=spans.length; ci<len; ci++) spans[ci].parentNode.removeChild(spans[ci]);
				if (cmtcontainer) basecontainer.appendChild(cmtcontainer);
				
				var	bimg=container.getElementsByTagName('img')[0];
				bimg.parentNode.removeChild(bimg);
				basecontainer.appendChild(container);
				
				var	hstarButton=d.createElement('span');
				hstarButton.appendChild(bimg);
				
				setEventHandler(hstarButton,'click',cancelBubble);
				
				hstarButton.className='hstar_bframe';
				
				entry.hstarButton=hstarButton;
			}
		}
		if (hstarButton) {
			var	hcounter=hbox.getElementsByTagName('div')[0];
			hcounter.appendChild(hstarButton);
		}
		ss.background='red';
		ss.borderColor='red';
		if (curHopperId!=selfId) hoppers[curHopperId].onmouseout(e);
		curHopperId=selfId;
	};
	var	onmouseout=function(e){
		var	hstarButton=entry.hstarButton;
		if (hstarButton) {
			var	pNode=hstarButton.parentNode;
			if (pNode) pNode.removeChild(hstarButton);
		}
		style.display='none';
		ss.background='transparent';
		ss.borderColor='transparent';
	}
	setEventHandler(imgSmall,'mouseover',function(e){
		if (!e) e=w.event;
		if (!e||e.shiftKey) return;
		onmouseover(e);
	});
	setEventHandler(imgSmall,'mouseout',function(e){
		if (!e) e=w.event;
		if (!e||e.shiftKey) return;
		onmouseout(e);
	});
	hbContainer.appendChild(imgSmall);
	
	hoppers[selfId]={
		id			:	selfId
	,	hopper		:	hopper
	,	onmouseover	:	onmouseover
	,	onmouseout	:	onmouseout
	};
};

var	createHbox=function(cmtEntries,hbURL,errorInfo){
	var	hbar=d.createElement('div');
	var	style=hbar.style;
	style.background='transparent';
	style.position=(isIE)?'absolute':'fixed';
	style.left=0;
	style.margin=0;
	style.padding='0';
	style.zIndex=1000000;
	
	var	hbContainer=d.createElement('div');
	var	style=hbContainer.style;
	style.margin=0;
	style.padding='4px 0px';
	style.textAlign='left';
	style.styleFloat=style.cssFloat='left';
	style.visibility='hidden';
	
	var	hbox=d.createElement('div');
	var	style=hbox.style;
//	style.margin='auto 0 0 auto';
	style.padding=0;
	style.width='82px';
	style.height='40px';
	style.border='solid 2px navy';
	style.background='#1841CE url("http://b.hatena.ne.jp/images/hatenade.gif") center no-repeat';
	style.cursor='pointer';
	style.styleFloat=style.cssFloat='right';
	style.textAlign='left';
	style.wordSpacing='0';
	style.letterSpacing='0';
	style.position='relative';
	hbox.unselectable='on'; // IE only
	
	var	hcounter=d.createElement('div');
	var	style=hcounter.style;
	style.margin=0;
	style.padding='4px';
	style.color='lime';
	style.fontSize='16px';
	style.fontWeight='bolder';
	style.fontFamily=hbFontFamily;
	style.background='transparent';
	style.textAlign='left';
	hcounter.unselectable='on'; // IE only

	var	updateNumber=function(number){
		while (hcounter.firstChild) hcounter.removeChild(hcounter.firstChild);
		if (isNaN(number)) {
			hcounter.style.color='red';
			numStr=number;
		}
		else {
			hcounter.style.color='lime';
			var	numStr=number.toString();
			if (numStr.length<3) numStr=(1000+number).toString().replace(/^./,'');
		}
		hcounter.appendChild(d.createTextNode(numStr));
	};
	if (cmtEntries) {
		updateNumber(cmtEntries.length);
		hbox.title=cmtEntries.length+' comment'+((1<cmtEntries.length)?'s':'');
	}
	else {
		updateNumber('Error');
		hbox.title=errorInfo;
	}
	hbox.appendChild(hcounter);
	
	
	hbar.appendChild(hbContainer);
	hbar.appendChild(hbox);
	
	d.body.appendChild(hbar);
	
	var	filter=d.createElement('div');
	var	style=filter.style;
	style.position=(isIE)?'absolute':'fixed';
	style.top=0;
	style.left=0;
	style.background=hbFilterColor;
	style.filter='alpha(opacity='+hbFilterOpacity+')';
	style.MozOpacity=hbFilterOpacity/100;
	style.opacity=hbFilterOpacity/100;
	style.zIndex=999999;
	style.display='none';
	d.body.appendChild(filter);
	setEventHandler(hbox,'click',function(e){
		if (!e) e=w.event;
		w.open(hbURL,'_blank');
	});
	var	lastcall=new Date().getTime();
	var	changeHopper=function(getdelta,e){
		if (!e) e=w.event;
		if (!e) return false;
		cancelBubble(e);
		if (!keyenable) return false;
		var	curtime=new Date().getTime();
		if (curtime-lastcall<hbChangeWait) return false;
		lastcall=curtime;
		if (hoppers.length<=0) return false;
		var	tgtHopper=curHopperId+getdelta(e);
		if (hoppers.length<=tgtHopper) tgtHopper=0;
		else if (tgtHopper<0) tgtHopper=hoppers.length-1;
		hoppers[tgtHopper].onmouseover(e);
		return false;
	};
	var	fncKeyPress=function(e){
		return changeHopper(function(e){
			if (e.keyCode==0x25) return 1;			//	left
			else if (e.keyCode==0x27) return -1;	//	right
			else return 0;
		},e);
	};
	var	fncMouseWheel=function(e){
		return changeHopper(function(e){
			var	delta=0;
			if (e.wheelDelta) {
				delta=e.wheelDelta/120;
//				if (w.opera) delta=-delta;
			}
			else if (e.detail) {
				delta=-e.detail/3;
			}
			if (hbSwitchWheelUpDown) delta=-delta;
			if (delta<0) return 1;					//	down
			else if (0<delta) return -1;			//	up
			else return 0;
		},e);
	};
	var	firstcall=true,loadcomplete=false;
	setMouseHandler(hbox,'mouseover',function(e){
		setEventHandler(d,'keypress',fncKeyPress);
		setEventHandler(d,'mousewheel',fncMouseWheel);
		if (firstcall) {
			updateNumber('Loading..');
			setTimeout(function(){
				if (!cmtEntries) return;
				for (var ci=0,len=cmtEntries.length; ci<len; ci++) {
					new createHopper(cmtEntries[ci],hbContainer,cmtEntries.length,hbox,filter);
				}
				refreshHbar();
				updateNumber(cmtEntries.length);
				hoppers[curHopperId].onmouseover(e);
				loadcomplete=true;
				keyenable=true;
			},20);
			firstcall=false;
		}
		else if (loadcomplete) {
			hoppers[curHopperId].onmouseover(e);
			refreshHbar();
			keyenable=true;
		}
		filter.style.display='block';
		filter.style.zIndex=999999;
		hbContainer.style.visibility='visible';
		objectSuspend();
	});
	setMouseHandler(hbar,'mouseout',function(e){
		rmvEventHandler(d,'keypress',fncKeyPress);
		rmvEventHandler(d,'mousewheel',fncMouseWheel);
		keyenable=false;
		if (e.shiftKey||hoppers.length<=0) {
			//	Keep Screen
		}
		else {
			hoppers[curHopperId].onmouseout(e);
			filter.style.display='none';
			hbContainer.style.visibility='hidden';
			objectResume();
		}
		refreshHbar();
	});
	if (isIE) {
		var	refreshHbar=function(){
			var	left=getClientLeft(),top=getClientTop(),width=getClientWidth(),height=getClientHeight();
			
			hbContainer.style.width=(width-hbOffsetX)+'px';
			
			var	s=hbar.style;
			s.width=(width-16)+'px';
			s.top=(top+height-hbOffsetY-1-hbar.offsetHeight)+'px';
			
			hbox.style.top=(hbar.offsetHeight-hbox.offsetHeight)+'px';
			
			var	s=filter.style;
			s.left=left+'px';
			s.top=top+'px';
			s.width=width+'px';
			s.height=height+'px';
		};
		setEventHandler(w,'scroll',refreshHbar);
	}
	else {
		var	refreshHbar=function(){
			var	width=getClientWidth(),height=getClientHeight();
			
			hbContainer.style.width=(width-hbOffsetX)+'px';
			
			var	s=hbar.style;
			s.width=(width-16)+'px';
			s.top=(height-hbOffsetY-1-hbar.offsetHeight)+'px';
			
			hbox.style.top=(hbar.offsetHeight-hbox.offsetHeight)+'px';
			
			var	s=filter.style;
			s.left=0;
			s.top=0;
			s.width=width+'px';
			s.height=height+'px';
		};
	}
	setEventHandler(w,'resize',refreshHbar);

	refreshHbar();
	
	return hcounter;
};

var	baseURL='http://b.hatena.ne.jp/entry/',baseRSS=baseURL+'rss/';
//var	targetURL=w.location.href.replace(/#/g,function(s){return encodeURIComponent(s)});
var	targetURL=w.location.href;

var	hstarwin=d.createElement('div');
hstarwin.style.display='none';
d.body.appendChild(hstarwin);

w.hbBoxCallback=function(result){
	if (result&&0<result.count) {
		var	entries=result.bookmarks,cmtEntries=[],eid=result.eid;
		for (ci=0,len=entries.length; ci<len; ci++) {
			var	entry=entries[ci], content=stripTags(entry.comment).replace(/(https?:\/\/[-_.!~*'()a-zA-Z0-9;/?:@&=+$,%#]+)/mgi,'<a href="$1">$1<\/a>');
			if (0<content.replace(/(\s|[\r\n])/mg,'').length) {
				var	date=entry.timestamp;
				var	no=cmtEntries.length,tags=entry.tags;
				if (!tags) {
					tags=[];
				}
				else if (tags.constructor==String) {
					tags=[tags];
				}
				var	hstar=d.createElement('h3');
				hstar.className='HatenaStar_hbBox';
				hstar.innerHTML='<a href="'+entry.link+'">'+entry.title+'<\/a>';
				hstarwin.appendChild(hstar);
				cmtEntries[no]={
					id		:	entry.user
				,	content	:	content
				,	link	:	'http://b.hatena.ne.jp/'+entry.user+'/'+date.replace(/^(\d+)\/(\d+)\/(\d+)\s.*$/,'$1$2$3')+'#bookmark-'+eid
				,	date	:	date
				,	no		:	no
				,	tags	:	tags
				,	hstar	:	hstar
				};
			}
		}
		if (0<cmtEntries.length) {
			(function(){
				if (!w.Hatena||!w.Hatena.Star) {
					setTimeout(arguments.callee,10);
					return;
				}
				w.Hatena.Star.EntryLoader.headerTagAndClassName=['h3','HatenaStar_hbBox'];
				new w.Hatena.Star.EntryLoader();
				createHbox(cmtEntries,baseURL+targetURL);
			})();
		}
	}
};

(function(){
	if (!w.Hatena||!w.Hatena.Star) {
		var	s=d.createElement('script');
		s.type='text/javascript';
		s.src='http://s.hatena.ne.jp/js/HatenaStar.js';
		d.body.appendChild(s);
	}
	var	s=d.createElement('script');
	s.type='text/javascript';
	s.src='http://b.hatena.ne.jp/entry/json/?callback=hbBoxCallback&url='+encodeURIComponent(targetURL);
	d.body.appendChild(s);
})();

})();

