var	cocoCatTree = {
//	---
	Version						:	"0.01a"
//	---
,	reverse						:	false
,	onload						:	null
,	showNum						:	2		//	同一エントリ内の表示数(最大5)
,	newDay						:	3		//	class="new"が付く日数(0は無効)
,	showDate					:	false
//	---
,	catId						:	'archive-category'
//	---
,	entryList					:	[]

//	---
,	getXmlHttpObj				:	function() {
		var	xmlHttpObj = null ;
		for (;;) {
			if ( typeof ActiveXObject != "undefined" ) {
				var	msXml = [ 'Msxml2.XMLHTTP', 'Microsoft.XMLHTTP' ] ;
				for ( var ci=0; ci < msXml.length; ci++ ) {
					xmlHttpObj = new ActiveXObject( msXml[ci] ) ;
					if ( xmlHttpObj ) {
						break ;
					}
				}
			}
			else if ( typeof XMLHttpRequest != "undefined" ) {
				try {
					xmlHttpObj = new XMLHttpRequest() ;
				}
				catch ( e ) {
					xmlHttpObj = null ;
				}
			}
			break ;
		}
		return xmlHttpObj ;
	}	//	end of getXmlHttpObj()
//	---
,	getPageRequest				:	function( requestid, path, callback ) {
		var	ret = false ;
		for (;;) {
			if ( !path ) break ;
			var	xmlHttpObj = cocoCatTree.getXmlHttpObj() ;
			if ( !xmlHttpObj ) break ;
			
			xmlHttpObj.onreadystatechange = function() {
				for (;;) {
					if ( xmlHttpObj.readyState != 4 ) break ;
					if ( xmlHttpObj.status == 200 ) {
						if ( callback ) {
							callback( requestid, path, xmlHttpObj ) ;
						}
					}
					break ;
				}
			} ;
			xmlHttpObj.open( 'GET', path ) ;
			xmlHttpObj.send( null ) ;
			ret = true ;
			break ;
		}
		return ret ;
	}	//	end of getPageRequest()
//	---
,	getNodeText					:	(
		function ( node ) {
			return ( ( node.hasChildNodes() ) ? node.childNodes[0].nodeValue : "" ) ;
		}
	)	//	end of getNodeText()
//	---
,	makeTree					:	(
		function ( entryList ) {
			var	catList = document.getElementById(this.catId) ;
			if ( !catList ) return ;
			
			var	now = (new Date()).getTime() ;
			var	checknew = function (info, fixclass) {
				var	ret = fixclass ;
				for (;;) {
					if ( cocoCatTree.newDay <= 0 ) break  ;
					var	atime = Date.parse(info.update.replace(/\./g,'/')) ;
					if ( isNaN(atime) || ( (cocoCatTree.newDay*24*60*60*1000) <= (now-atime) ) ) break ;
					ret += ' new latest' ;
					break ;
				}
				return ret ;
			}
			var	makelist = function(info, fixclass) {
				var	showtitle = info.title ;
				if ( cocoCatTree.showDate ) {
					showtitle = '[' + info.update + ']&nbsp;' + showtitle ;
				}
				return [
					'<li class="' + checknew(info, fixclass) + '">'
				,	'<a href="' + info.link +'" title="DATE:' + info.update + '">'
				,	showtitle
				,	'</a>'
				,	'</li>'
				].join('');
			}
			var	listObjs = catList.getElementsByTagName('li') ;
			var	lists = new Array() ;
			for ( var ci=0; ci < listObjs.length; ci++ ) {
				lists[ci] = listObjs[ci] ;
			}
			for ( var ci=0; ci < lists.length; ci++ ) {
				var	links = lists[ci].getElementsByTagName('a') ;
				if ( links.length != 1 ) continue ;
				var	cat = links[0].innerHTML ;
				var	entryInfos = entryList[cat] ;
				if ( !entryInfos || entryInfos.length < 1 ) continue ;
				var	listHtmls = new Array() ;
				listHtmls[listHtmls.length] = '<ul class="tree">' ;
				var	cj ;
				if ( this.reverse ) {
					for ( cj=entryInfos.length-1; 0 < cj; cj-- ) {
						listHtmls[listHtmls.length] = makelist( entryInfos[cj], 'lst' ) ;
					}
				}
				else {
					for ( cj=0; cj < entryInfos.length-1; cj++ ) {
						listHtmls[listHtmls.length] = makelist( entryInfos[cj], 'lst' ) ;
					}
				}
				listHtmls[listHtmls.length] = makelist( entryInfos[cj], 'end' ) ;
				listHtmls[listHtmls.length] = '</ul>' ;
				lists[ci].innerHTML += listHtmls.join('\n') ;
			}
			if ( typeof this.onload == 'function' ) {
				this.onload() ;
			}
		}
	)	//	end of makeTree()
//	---
,	checkList					:	(
		function	( rsp ) {
			var	htmlFrame = document.createElement('div') ;
			htmlFrame.innerHTML = 'dummy' + rsp.responseText ;
			
			var	entryList = this.entryList ;
			var	divs = htmlFrame.getElementsByTagName('div') ;
			var	daycheck = function(a,b) { return(eval(b.update.replace(/\./g,''))-eval(a.update.replace(/\./g,''))); } ;
			for ( var ci=0; ci < divs.length; ci++ ) {
				var	div = divs[ci] ;
				var	h2s = div.getElementsByTagName('h2') ;
				if ( h2s.length <= 0 ) continue ;
				if ( !h2s[0].innerHTML.match( /<a href="(.*?)">(.*?)<\/a>/i ) ) continue ;
				var	caturl = RegExp.$1 ;
				var	cat = RegExp.$2 ;
				entryList[cat] = new Array() ;
				entryList[cat].caturl = caturl ;
				var	lists = div.getElementsByTagName('li') ;
				for ( var cj=0; cj < lists.length; cj++ ) {
					if ( !lists[cj].innerHTML.match( /<a href="(.*?)">(.*?)<\/a>.*?\(([0-9\.]+?)\)/i ) ) continue ;
					var	num = entryList[cat].length ;
					entryList[cat][num] = new Array() ;
					entryList[cat][num].link = RegExp.$1 ;
					entryList[cat][num].title = RegExp.$2 ;
					entryList[cat][num].update = RegExp.$3 ;
				}
				entryList[cat].sort(daycheck) ;
				if ( 0 < cocoCatTree.showNum ) {
					while ( cocoCatTree.showNum < entryList[cat].length ) entryList[cat].pop() ;
				}
			}
			this.makeTree( entryList ) ;
		}
	)	//	end of checkList()
//	---
,	loadListRequest				:	(
		function	() {
			var	h1s = document.getElementsByTagName('h1') ;
			var	ci ;
			var	listurl = '' ;
			for ( ci=0; ci < h1s.length; ci++ ) {
				var	links = h1s[ci].getElementsByTagName('a') ;
				if ( 0 < links.length ) {
					listurl = links[0].href + 'include/catrecent.html?reload=' + (new Date).getTime() ;
					break ;
				}
			}
			if ( listurl ) {
//				new Ajax.Request( listurl, {method: 'get', onComplete: function(rsp){ cocoCatTree.checkList(rsp); } } ) ;
				cocoCatTree.getPageRequest( 0, listurl, function(requestid,path,xmlHttpObj){ cocoCatTree.checkList(xmlHttpObj); } ) ;
			}
			else {
				alert( "cocoCatTreeR.js: カテゴリーリストが見付かりません" ) ;
			}
		}
	)	//	end of loadListRequest()
//	---
,	init						:	(
		function	()	{
			for (;;) {
				this.loadListRequest() ;
				break ;
			}
		}
	)	//	end of init()
	
}	//	end of cocoCatTree object

//	cocoCatTree.init() ;
