//
//	■「予言詩メーカー」(http://pha22.net/yogen/)をブログパーツ化するスクリプト
//	 ※「予言詩メーカー」は pha さん(http://pha22.net/)が公開されている Web サービスです。
//	
//	file	:	showPropheticVerser.js
//	version	:	0.01c
//	url		:	http://furyu.tea-nifty.com/script/showPropheticVerser.js
//				※別途、http://furyu.tea-nifty.com/script/gae_xhrequest.js が必要
//	update	:	2009/02/27
//	author	:	風柳(furyu-tei)
//	site	:	http://furyu.tea-nifty.com/annex/
//				http://d.hatena.ne.jp/furyu-tei/
//	license	:	フリーソフトウェアとして公開します。配布・改変等は自由です。
//				This program is free software. You can redistribute it and/or modify it.
//

(function(){
var	w=window,d=w.document;
if (!w.UserExtensions||!w.UserExtensions.HatenaUserID) {
	var	script=d.createElement('script');
	script.type='text/javascript';
	script.charset='utf-8';
	script.src='http://furyu-tei.sakura.ne.jp/script/HatenaUserID.js';
	var	pnode=d.getElementsByTagName('head')[0]||d.body;
	pnode.insertBefore(script,pnode.firstChild);
}
w.showPropheticVerse_Hatena=function(urlopts,fncopts){
	if (!w.UserExtensions||!w.UserExtensions.HatenaUserID) {
		setTimeout(function(){w.showPropheticVerse_Hatena(urlopts,fncopts)},100);
		return;
	}
	w.UserExtensions.HatenaUserID.getCurrentID({
		callback	:	function(infos){
			fncopts.hatena_checked=true;
			if (!infos.error) {
				fncopts.hatenaid_found=true;
				urlopts.name=infos.id;
			}
			showPropheticVerse(urlopts,fncopts)
		}
	});
};
})();

function	showPropheticVerse(urlopts,fncopts){
	var	w=window,d=w.document;
	if (!urlopts) urlopts={};
	if (!fncopts) fncopts={};
	
	var	cdate=new Date(),cyear=cdate.getFullYear(),cmonth=1+cdate.getMonth(),maxmonth=3+cmonth;
	var	normDate=function(){
		var	min=cdate.getTime();
		var	tdate=new Date();
		tdate.setYear(cyear);tdate.setMonth(-1+maxmonth);
		var	max=tdate.getTime();
		tdate.setYear(urlopts.year);tdate.setMonth(-1+urlopts.month);
		var	chk=tdate.getTime();
		if (chk<min||max<chk) {
			urlopts.year=cyear;
			urlopts.month=cmonth;
		}
		else {
			urlopts.year=tdate.getFullYear();
			urlopts.month=1+tdate.getMonth();
		}
	};
	if (!fncopts.hatena_checked) {
		w.showPropheticVerse_Hatena(urlopts,fncopts);
		return;
	}
	if (!fncopts.hatenaid_found) {
		var	check_keys=['typepadauthor','author','auth','name'];
		for (var ci=0,len=check_keys.length; ci<len; ci++) {
			if (d.cookie.match(new RegExp(check_keys[ci]+'=([^;]*)'))) {
				urlopts.name=unescape(RegExp.$1).replace(/[>＞→⇒&].*/,'');
				break;
			}
		}
	}
	if (!urlopts.name) urlopts.name='';
	urlopts.name=urlopts.name.replace(/^\s*/,'').replace(/\s*$/,'');
	if (!urlopts.name) urlopts.name='訪問者';
	
	if (!urlopts.byear) urlopts.byear='';
	if (!urlopts.bmonth) urlopts.bmonth='';
	if (!urlopts.bday) urlopts.bday='';
	if (!urlopts.year) urlopts.year=cyear;
	if (!urlopts.month) urlopts.month=cmonth;
	normDate();
	
	if (typeof fncopts.repeat==undefined) fncopts.repeat=true;
	if (typeof fncopts.maxline==undefined) fncopts.maxline=0;
	if (typeof fncopts.laptime==undefined) fncopts.laptime=3;
	
	var	optstrs=[];
	for (var mem in urlopts) {
		optstrs[optstrs.length]=mem+'='+encodeURIComponent(urlopts[mem]);
	}
	var	requrl='http://pha22.net/yogen/u.php?'+optstrs.join('&');
	
	GAE_xmlhttpRequest({
		method	:	'GET'
	,	url		:	requrl
	,	onload	:	function(xh){
			var	pvframe=d.getElementById('PropheticVerseFrame'),pvtitle=d.getElementById('PropheticVerseTitle'),pvline=d.getElementById('PropheticVerseLines');
			if (!pvframe||!pvtitle||!pvline) return;
			
			var	rslt=xh.responseText;
			if (!rslt.match(/<textarea.*?>([\s\S]*?)<\/textarea/mi)) return;
			
			var	PropheticVerse=RegExp.$1.replace(/[\r\n]+/g,'\n').replace(/^\s*\n+/mg,'').replace(/^[\s　]/mg,'');
			var	lines=PropheticVerse.split(/[\r\n]+/);
			if (lines.length<2) return;
			
			var	title=lines.shift(),url=lines.shift();
			if (!url.match(/^http/)) return;
			title=title.replace(/\s*\|.*/,'');
			
			var	link=d.createElement('a'),tNode=d.createTextNode(title);
			link.appendChild(tNode);
			link.href=url;
			link.target='_blank';
			with (link.style) {
				textDecoration='none';
				color='white';
			}
			pvtitle.appendChild(link);
			
			if (!d.getElementById('PropheticVerseToolCredit')) {
				var	credit=d.createElement('a'),tNode=d.createTextNode('パーツ提供／');
				credit.id='PropheticVerseToolCredit';
				credit.appendChild(tNode);
				credit.href='http://furyu.tea-nifty.com/annex/2009/02/post-8ca1.html';
				credit.target='_blank';
				with (credit.style) {
					fontSize='12px';
					fontWeight='normal';
					textDecoration='none';
					color='gray';
					display='none';
				}
				credit.title='by 風柳';
				var	pvcredit=d.getElementById('PropheticVerseCredit'),inspnt=pvcredit.lastChild;
				while (inspnt) {
					if (inspnt.nodeType==1&&inspnt.nodeName.toUpperCase()=='A') break;
					inspnt=inspnt.previousSibling;
				}
				if (inspnt) inspnt.target='_blank';	// 設置フォームバグへのパッチ
				inspnt.title='サービス提供 by pha';
				pvcredit.insertBefore(credit,inspnt);
				pvcredit.onmouseover=function(){
					credit.style.display='inline';
				}
				pvcredit.style.color='gray';
			}
			var	tlines=[].concat(lines),maxline=fncopts.maxline,repeat=fncopts.repeat,laptime=1000*fncopts.laptime,tid=null;
			pvline.title='クリックで月の切替';
			pvline.style.cursor='pointer';
			pvline.onclick=function(){
				pvline.onclick=function(){};
				if (tid) clearInterval(tid);
				tid=null;
				while (pvtitle.firstChild) pvtitle.removeChild(pvtitle.firstChild);
				while (pvline.firstChild) pvline.removeChild(pvline.firstChild);
				urlopts.month++;
				normDate();
				setTimeout(function(){
					showPropheticVerse(urlopts,fncopts);
				},10);
			};
			var	makeColorCode=function(cnum){
				var	c=Number(cnum).toString(16);
				return '#'+c+c+c+c+c+c;
			};
			var	setNodeColor=function(tgtNode,cnum){
				var	cc=makeColorCode(cnum);
				if (tgtNode==pvline) {
					var	tgt=pvline.firstChild;
					while (tgt) {
						if (tgt.nodeType==1) tgt.style.color=cc;
						tgt=tgt.nextSibling;
					}
				}
				else {
					tgtNode.style.color=cc;
				}
			};
			var	fadeInOut=function(opts){
				var	flg=opts.fadein, tgtNode=opts.tgtNode, tm=opts.timeout, cb=opts.ontimeout;
				var	cnt=15,intv=~~(tm/cnt);
				var	cnum=(flg)?0x0:0xf;
				setNodeColor(tgtNode,cnum);
				tid=setInterval(function(){
					cnum=(flg)?cnum+1:cnum-1;
					setNodeColor(tgtNode,cnum);
					if (!--cnt) {
						if (tid) clearInterval(tid);
						tid=null;
						if (typeof cb=='function') cb();
						return;
					}
				},intv);
			};
			var	multiFadeout=function(){
				fadeInOut({
					fadein		:	false
				,	tgtNode		:	pvline
				,	timeout		:	laptime
				,	ontimeout	:	function(){
						while (pvline.firstChild) pvline.removeChild(pvline.firstChild);
						show();
					}
				});
			};
			var	show=function(){
				if (!tlines.length) {
					if (repeat) {
						tlines=[].concat(lines);
						multiFadeout();
					}
					return;
				}
				var	tgtNode=d.createElement('div'),tNode=d.createTextNode(tlines[0]);
				tgtNode.appendChild(tNode);
				pvline.appendChild(tgtNode);
				
				fadeInOut({
					fadein		:	true
				,	tgtNode		:	tgtNode
				,	timeout		:	laptime
				,	ontimeout	:	function(){
						tlines.shift();
						if (maxline==0) {
							show();
							return;
						}
						curline=lines.length-tlines.length;
						if (curline%maxline==0) {
							multiFadeout();
							return;
						}
						show();
					}
				});
			};
			show();
		}
	});
}	//	end of showPropheticVerse()

