// Arekore-Link-List. last-update: 2005.01.02.
	// (c) snow-materia "http://sm.useyan.com/"


//	-	-	-	-	-	-	-	-	-	-	-	-	-	-	-	-	-	list-setting
var lin = {
	// リストブロック (必須)
	tag  : 'DL',	// タグ
	id   : 'ArekoreLink',	// ID

	// リストブロックに見出しをつけるなら (なくてもよい)
	head_tag  : 'DT', // タグ
	head_id   : 'link-head',	// ID
	head_text : 'LinkArekore', // テキスト

	// リスト (なくてもよい)
	list_tag  : 'DD',	// タグ
	list_id   : 'link-list',	// ID

	// リストの項目に、そのリストの項目名のCLASS属性をつける？
	item_class : 0,	// [0:false, 1:true]

	// ホバーでリストを開閉する？ (するなら最初は閉じた状態で表示)
	hide : 1,	// [0:false, 1:true]

	// グループ名をホバーしてもリストアイテムを閉じる？
	over : 1,	// [0:false, 1:true]

	// クリックでリストアイテムを開閉したら、ホバーでは開閉しなくする？ (全閉じでリセット)
	fix  : 1,	// [0:false, 1:true]

	// リストを閉じるまでの間隔 [ms]
	interval : 1500
};




//	-	-	-	-	-	-	-	-	-	-	-	-	-	-	-	-	-	arekore-setting
function lin_tag_object() {
	return [
	//	-	-	-	-	-	-	-	-	-	-	-	-	-	-	-	-	-	-
	// リストはいくらでも追加・変更して設定できます.
		// ただし、各設定の左辺 ('tag'とか'skip'とか) は変えないでください.
			// (*) とマークしてあるのは必須の項目です.

	// group-condition
		// group : グループ名 (*)
		// clas  : グループのCLASS
		// id    : グループのリストのID

	// element-condition ( | で区切ると複数の候補で指定できます)
		// tag   : そのグループでピックアップするタグ.  (*)
		// href  : リンクのアドレスとする属性. (*)
		// term  : 項目名とする属性. (空白可) (空白でなく属性がない要素はスキップ)
		// title : 項目とする属性. (属性がないときは href の値になる)
		// skip  : この属性と属性値のどれかがあったら、その項目はスキップ. (or)

	// Section
	{
		// group-condition
		group : 'Section-Link',	// name
		clas  : 'list-group',	// class
		id    : 'section-link',	// list-id

		// element-condition
		tag   : 'BODY|DIV',	// tag-name
		href  : 'id',	// for link-address
		term  : '|class',	// for item-name
		title : 'title',	// for item

		// attributes for skip
		skip : {
			'class' : ['hidden'],
			'' : ''
		 }
	},
	// LINK
	{
		// group-condition
		group : 'Link-Link',	// name
		clas  : 'list-group',	// class
		id    : 'link-link',	// list-id

		// element-condition
		tag   : 'LINK',	// tag-name
		href  : 'href',	// for link-address
		term  : 'rel',	// for item-name
		title : 'title',	// for item

		// attributes for skip
		skip  : {
			'type' : ['text/css', 'application/rss+xml'],
			'rel'  : ['shortcut icon', 'alternate'],
			'' : ''
		 }
	},
	// A
	{
		// group-condition
		group : 'A-Link',	// name
		clas  : 'list-group',	// class
		id    : 'a-link',	// list-id

		// element-condition
		tag   : 'A',	// tag-name
		href  : 'href',	// for link-address
		term  : '',	// for item-name
		title : 'title',	// for item

		// attributes for skip
		skip  : {
			'class' : ['hidden'],
			'title' : ''
		 }
	},

	//	-	-	-	-	-	-	-	-	-	-	-	-	-	-	-	-	-	-
	0];
}




//	-	-	-	-	-	-	-	-	-	-	-	-	-	-	-	-	-	-	-	script
// IE ?
lin['ie'] = (!window.opera && document.all) ? 1 : 0;
// fake-standard-script forIE ?
lin['fake'] = 0;
if (lin['ie'] && window.addElementMethod && window._ie) {
	if (_ie['listener'] || _ie['setclass']) lin['fake'] = 1;
	if (_ie['listener'] && _ie['setclass']) lin['ie'] = 0;
}




//	-	-	-	-	-	-	-	-	-	-	-	-	-	-	-	-	-	-	create list
// text-transform
function lin_create_list() {
	var bef = ( new Date() ).getTime();

	var d = document;
	if (!d.body || !lin['id']) return;

	// lin_tag_object
	var tag_list = lin_tag_object();
	if (!tag_list || typeof(tag_list) != 'object') return;

	// object-exist ?
	var ch = lin_object_list(tag_list);
	if (!ch) return;

	// div-elements
	var div = lin_create_element(d.body, lin['tag'], lin['id']);
	if (lin['hide']) lin_addEvent('mouseover', lin_list_open, false, div);
	if (lin['hide']) lin_addEvent('mouseout',  lin_close_interval, false, div);

	// headline
	if (lin['head_tag']) {
		var head = lin_create_element(div, lin['head_tag'], lin['head_id']);
		lin_addEvent('click', lin_list_fix, false, head);
		if (lin['head_text']) head.appendChild( d.createTextNode(lin['head_text']) );
	}

	// list
	var list = 0;
	if (lin['list_tag']) {
		list = lin_create_element(div, lin['list_tag'], lin['list_id']);
		if (lin['hide'] && list.style) list.style['display'] = 'none';	// style-display
	}

	// first-list
	var dl;
	if (lin['list_tag'] && list) {
		dl = lin_create_element(list, 'DL');
	} else {
		dl = lin_create_element(div, 'DL');
		if (lin['hide'] && dl.style) dl.style['display'] = 'none';	// style-display
	}

	// tag-list
	for (var l = 0; l < tag_list.length; l++) {
		var o = tag_list[l];
		if (o && typeof(o) != 'object' || !o['length']) continue;

		// elements
		var els = o['object'];

		// DT
		var dt = lin_create_element(dl, 'DT', null, o['clas']);
		dt.appendChild( d.createTextNode( o['group'] ) );
		lin_addEvent('click', lin_list_item_fix, false, dt);
		if (lin['hide']) lin_addEvent('mouseover', lin_list_item_open, false, dt);

		// DD
		var dd = lin_create_element(dl, 'DD', null, o['clas']);
		if (lin['hide'] && dd.style) dd.style['display'] = 'none';	// style-display

		// list-item
		var dlol = (o['term']) ? 'DL' : 'OL';
		var ddli = (o['term']) ? 'DD' : 'LI';
		var item = lin_create_element(dd, dlol, o['id']);

		// list-items for elements
		for (var i = 0; i < o['length']; i++) {
			var el = els[i];
			if (!el) continue;

			// class
			var clas = null;

			// href
			var href = lin_get_attribute(el, o['href']);	// get-attribute
			if (!href) continue;

			// term
			var term = '';
			if (o['term']) {
				term = lin_get_attribute(el, o['term']);	// get-attribute
				if (!term) continue;
				if (term == -1) term = el.nodeName.toLowerCase();
				if (term && lin['item_class']) clas = term;
				var item_term = lin_create_element(item, 'DT', null, clas);
				item_term.appendChild( d.createTextNode(term) );
			}

			// item
			var item_title = lin_create_element(item, ddli, null, clas);

			// title
			var title = '';
			if (o['title']) title = lin_get_attribute(el, o['title']);	// get-attribute
			if (!title) title = href;

			// a
			var item_a = lin_create_element(item_title, 'A');
			if (o['href'].toLowerCase() == 'id') href = '#' + href;
			item_a.setAttribute('href', href);
			item_a.setAttribute('title', title);
			item_a.appendChild( d.createTextNode(title) );
		}
	}

	// format
	tag_list = null;

	// close list on other-place-click
	if (lin['hide']) lin_addEvent('click', lin_close_other, false, window.document);

	// format
	lin['tag'] = null;
	lin['head_tag'] = null;
	lin['head_tag'] = null;
//	lin['head_id'] = null;
	lin['head_text'] = null;
	lin['timeout'] = null;
	lin['hide'] = null;
	if (lin['fix']) lin['fixed'] = new Array();

//	document.write('<xmp>' + div.innerHTML + '</xmp>');
}


// create-element
function lin_create_element(to, tag, id, clas) {
	var obj = document.createElement(tag);
	// append
	to.appendChild(obj);
	// element-method
	if (lin['fake']) addElementMethod(obj);
	// id
	if (id) obj.setAttribute('id', id);
	// class
	if (clas) {
		obj.setAttribute('class', clas);
		if (lin['ie']) obj.className = clas;
	}
	// return object
	return obj;
}


// get-attribute
function lin_get_attribute(obj, ato) {
	var attr = '';
	var attrs
		= (typeof(ato) == 'string') ? ato.split(/\|/)
		: (typeof(ato) == 'object' && ato.length) ? ato
		: new Array();
	if (!attrs.length) return attr;

	// attributes
	for (var j = 0; j < attrs.length; j++) {
		var at = attrs[j];
		if (!at) continue;
		attr = obj.getAttribute(at);
		if (lin['ie'] && at.toLowerCase() == 'class') attr = obj.className;	// ie-class
		if (attr) break;
	}

	// nothing ok
	if (!attr && typeof(ato) == 'string' && ato.match(/(^\||\|$|\|\|)/)) attr = -1;

	return attr;
}


// object-exist ?
function lin_object_list(tag_list) {
	var d = document;
	var oln = 0;

	for (var l = 0; l < tag_list.length; l++) {
		var o = tag_list[l];

		// initialize
		if (!o || typeof(o) != 'object' || !o['group']) continue;
		o['object'] = null;
		o['length'] = 0;
		if (!o['tag'] || !o['href']) continue;

		// elements
		var els = new Array();
		var tags = o['tag'].split(/\|/);
		for (var i = 0; i < tags.length; i++) {
			if (!tags[i]) continue;
			var es = d.getElementsByTagName(tags[i]);
			if (!es || !es.length) continue;
			for (var j = 0; j < es.length; j++) els[ els.length ] = es[j];
		}
		if (!els || !els.length) continue;

		// skip-object
		var skip = o['skip'];
		if (!skip || typeof(skip) != 'object') skip = 0;
		if (skip) {
			var ch = 0;
			for (var k in skip) {
				if (k && skip[k] && skip[k].length) ch++;
			}
			if (!ch) skip = null;
		}

		// list-items for elements
		var news = new Array();
		for (var i = 0; i < els.length; i++) {
			var el = els[i];
			if (!el) continue;

			// href
			var href = lin_get_attribute(el, o['href']);	// get-attribute
			if (!href) continue;

			// skip-attribute
			if (skip) {
				var ch = 1;
				for (var k in skip) {
					if (!k || typeof(el[k]) == 'undefined') continue;
					var attr = el.getAttribute(k) || '';
					var sk = skip[k];
					if (typeof(sk) == 'string') {
						if (attr == sk) { ch = 0; break; }
					} else if (typeof(sk) == 'object' && sk.length) {
						for (var j = 0; j < sk.length; j++) {
							if (attr == sk[j]) { ch = 0; break; }
						}
						if (!ch) break;
					}
				}
				if (!ch) continue;
			}
			news[ news.length ] = el;
		}
		if (!news || !news.length) continue;

		o['object'] = news;
		o['length'] = news.length;
		oln++;
	}
	return oln;
}




//	-	-	-	-	-	-	-	-	-	-	-	-	-	-	-	-	-	-	open list
// list_open
function lin_list_open(e) {
	var d = document;

	// object
	var obj;
	if (lin['list_id']) obj = d.getElementById(lin['list_id']);
	else {
		var div = d.getElementById(lin['id']);
		if (div && div.lastChild && div.nodeType == 1) obj = div.lastChild;
	}
	if (!obj || !obj.style) return;

	// object-display
	obj.style['display'] = 'block';

	// clear-timeout
	lin_clear_timeout();
}


// list_fix on mouse-click
function lin_list_fix() {
	var d = document;

	// object
	var obj;
	if (lin['list_id']) obj = d.getElementById(lin['list_id']);
	else {
		var div = d.getElementById(lin['id']);
		if (div && div.lastChild && div.nodeType == 1) obj = div.lastChild;
	}
	if (!obj || !obj.style) return;

	// object-display
	if (obj.style['display'] == 'none') obj.style['display'] = 'block';

	// list_tag ?
	if (lin['list_tag']) obj = obj.firstChild;
	if (obj.nodeType != 1 || obj.nodeName.toUpperCase() != 'DL') return;

	// none ?
	var ch = 0;

	// dt, dd
	if (!obj.childNodes || !obj.childNodes.length) return;
	for (var i = 0; i < obj.childNodes.length; i++) {
		var o = obj.childNodes[i];
		if (o.nodeType != 1 || o.nodeName.toUpperCase() != 'DD' || !o.style) continue;
		if (o.style['display'] == 'none') ch++;
		o.style['display'] = 'block';
		if (lin['fix']) lin['fixed'][i] = true;
	}

	// close ?
	if (!ch) lin_close(1);

	// clear-timeout
	lin_clear_timeout();
}




//	-	-	-	-	-	-	-	-	-	-	-	-	-	-	-	-	-	-	close list
// list-close
function lin_close(n) {
	// clear-timeout
	lin_clear_timeout();
	if (!n && lin['fix']) lin['fixed'] = new Array();

	var d = document;

	// object
	var obj;
	if (lin['list_id']) obj = d.getElementById(lin['list_id']);
	else {
		var div = d.getElementById(lin['id']);
		if (div && div.lastChild && div.nodeType == 1) obj = div.lastChild;
	}
	if (!obj || !obj.style) return;

	// object-display
	if (!n) obj.style['display'] = 'none';

	// list_tag ?
	if (lin['list_tag']) obj = obj.firstChild;
	if (obj.nodeType != 1 || obj.nodeName.toUpperCase() != 'DL') return;

	// dd
	if (!obj.childNodes || !obj.childNodes.length) return;
	for (var i = 0; i < obj.childNodes.length; i++) {
		var o = obj.childNodes[i];
		if (o.nodeType != 1 || o.nodeName.toUpperCase() != 'DD' || !o.style) continue;
		o.style['display'] = 'none';
	}
}


// list-close-interval
function lin_close_interval() {
	if (lin['timeout']) return;

	// to lin_close
	if (lin['interval']) {	// interval
		lin['timeout'] = setTimeout('lin_close()', lin['interval']);
	} else {	// quick
		lin_close();
	}
	return;
}


// lin-close-other
function lin_close_other() {
	if (lin['timeout']) lin_close();
}




//	-	-	-	-	-	-	-	-	-	-	-	-	-	-	-	-	-	open list-item
// check this-object
function lin_this_object(obj, e) {
	if (!obj.nodeType) {	// IE
		if (!e || typeof(e) != 'object') e = window.event || null;
		if (e && e.srcElement) obj = e.target || e.srcElement;
	}
	return (typeof(obj) == 'object') ? obj : null;
}


// get next-object-check
function lin_next_object(obj) {
	if (!obj || !obj.parentNode) return;

	// parent
	var children = obj.parentNode.childNodes;
	if (!children || !children.length) return;

	// next
	var ch = 0;
	var next = 0;
	for (var i = 0; i < children.length; i++) {
		if (children[i] == obj) { ch = 1; continue; }
		if (!ch || children[i].nodeType != 1) continue;
		next = children[i];
		ch = i;
		break;
	}
	return new Array(next, ch);
}


// list_item_open
function lin_list_item_open(e) {
	// object
	var obj = lin_this_object(this, e);
	if (!obj) return;

	// next-object
	var nexts = lin_next_object(obj);
	var next = nexts[0];
	if (!next || !next.style || next.nodeName.toUpperCase() != 'DD') return;

	// fixed ?
	if (lin['fix'] && lin['fixed'][ nexts[1] ]) return;

	// display
	next.style['display']
		= (!lin['over']) ? 'block'
		: (next.style['display'] == 'none') ? 'block'
		: 'none';

	// clear-timeout
	lin_clear_timeout();
}


// list_item_fix on mouse-click
function lin_list_item_fix(e) {
	// object
	var obj = lin_this_object(this, e);
	if (!obj) return;

	// next-object
	var nexts = lin_next_object(obj);
	var next = nexts[0];
	if (!next || !next.style || next.nodeName.toUpperCase() != 'DD') return;

	// fixed ?
	if (lin['fix']) lin['fixed'][ nexts[1] ] = true;

	// display
	next.style['display']
		= (next.style['display'] == 'none') ? 'block'
		: 'none';

	// clear-timeout
	lin_clear_timeout();
}




//	-	-	-	-	-	-	-	-	-	-	-	-	-	-	-	-	-	-	-	tips
// clear timeout
function lin_clear_timeout() {
	if (lin['timeout']) {
		clearTimeout(lin['timeout']);
		lin['timeout'] = null;
	}
}


// addEvent ?
function lin_addEvent(type, func, capt, obj) {
	if (obj.addEventListener) obj.addEventListener(type, func, false);	// DOM2 ?
	else if (obj.attachEvent) obj.attachEvent('on'+type, func);	// IE ?
	else {	// Other
		var prev = obj['on'+type];
		obj['on'+type] = (prev) ? function (e) { prev(e); func(e); } : func;
	}
}




// window-onload
lin_addEvent('load', lin_create_list, false, window);
