/*
なんだかなぁ
*/

var MESSAGE = null;
var TREE = null;
var CSSNAME = null;

function GetDataPath(str) {
	var i, c=0, n=0;
	var limit = str.length;
	
	for(i=0; i<limit; i++) {
		if((c = str.indexOf('/', c)) != -1) {
			n++;
			c++;
		} else break;
	}
	str='';
	if(n) for(i=0; i<n; i++)
		str += '../';

	return str;
}
if(location.hostname.length) {
	TREE = GetDataPath(location.pathname.substring(1))+'~wokonomono/';
} else {
	TREE = GetDataPath(location.pathname.substring('file:///F:/program/HTML/wokonomono'.length));
}

function CSS(css) {
	if(css == null) {
		CSSNAME = 'normal';
	} else {
		CSSNAME = css;
	}
	document.write('<link rel="stylesheet" media="screen" href="' + TREE + 'data/' + CSSNAME + '/css.css" />');
}

function Date2String(date, sep) {
	if(date.length != 3) throw new String("日付の配列は3つ。");
	if(sep == null) sep='';
	return Plus0(date[0]) +sep+ Plus0(date[1]) +sep+ Plus0(date[2]);
}

function MessageLine(PageOnly) {
	if(PageOnly != null) {
		if(PageOnly.length) {
			document.write('<span>' + PageOnly + '</span>');
		} else {
			document.write('<span>ERR:PageOnly</span>');
		}
	}
	if(MESSAGE != null) {
		if(MESSAGE.length) {
			document.write('<span>' + MESSAGE + '</span>');
		} else {
			document.write('<span>ERR:Message</span>');
		}
	}
}

function Jikoman(xhtml, css, list) {
	if(!location.hostname.length) return;
	var ListTag = function(URI, banner, alt, w, h) {
		if(alt == null) alt = URI;

		if(w == null) w = 88;
		if(h == null) h = 31;
		
		document.write('<li>');
		document.write('<a href="' + URI + '">');
		document.write('<img src="' + banner + '" width="' + w + '" height="' + h + '" alt="' + alt + '" />');
		document.write('</a>');
		document.write('</li>');
	}
	
	document.write('<ul id="jikoman">')
	
	if((xhtml == null) || (xhtml == true)) {
		ListTag(
			'http://validator.w3.org/check?uri=referer',
			'http://www.w3.org/Icons/valid-xhtml11-blue',
			'Valid XHTML 1.1'
		);
	}
	if((css == null) || (css == true)) {
		var flag;
		switch(CSSNAME) {
			case 'normal':
				flag = true;
				break;
			default:
				flag = false;
		}
		if(flag) {
			ListTag(
				'http://jigsaw.w3.org/css-validator/validator?uri=http://www.usamimi.info/~wokonomono/data/' + CSSNAME  + '/css.css',
				'http://jigsaw.w3.org/css-validator/images/vcss-blue',
				'Valid CSS!'
			);
		}
	}
	if(list != null) {
		for(var i in list) {
			ListTag(
				list[i][0],
				list[i][1],
				list[i][2],
				list.length > 3? list[i][3]: null,
				list.length > 4? list[i][4]: null
			);
		}
	}
	
	document.write('</ul>');
}


function CreatePageMenu() {

	document.write('<div id="PageMenu" onmouseover="PageMenu_onMouseOver()" onmouseout="PageMenu_onMouseOut()">');
	document.write('<span id="MenuView" onclick="MenuView_onClick()" class="off">MENU</span>');

	// meta 取得
	var obj = document.getElementsByTagName('meta');
	var temp;
	
	//link を Javascript で適当に読む方法を見つけられなかったので meta で代用
	var MetaInfo = function(item) {
		try {
			temp = obj.namedItem(item).getAttributeNode("content").value;
		} catch(e) {
			temp = '';
		}
		
		return temp;
	}

	// メニュー
	document.write('<ul id="List" style="display: none;">');
	
	document.write('<li id="PageTitle" onmouseover="PageInfo_onMouseOver()" onmouseout="PageInfo_onMouseOut()" onclick="PageInfo_onClick()" class="off">' + document.title + '</li>');

	var Link = document.getElementsByTagName('link');
	var total = new Array();
	for(var i=0, j=0; i<Link.length; i++) {
		var use=true;
		var list;
		switch(Link[i].getAttribute('rel').toLowerCase()) {
		case 'contents':
			list = new Array(0x0, '≪', '目次');
			break;
		case 'index':
			list = new Array(0x1, '⇔', '索引');
			break;
		case 'parent':
			list = new Array(0x2, '↑', '上位の文書');
			break;
		case 'prev':
			list = new Array(0x3, '←', '前の文書');
			break;
		case 'next':
			list = new Array(0x4, '→', '次の文書');
			break;
		case 'glossay':
			list = new Array(0x5, '≫', '用語集');
			break;
		case 'help':
			list = new Array(0x6, '？', 'ヘルプ');
			break;
		case 'bookmark':
			list = new Array(0x7, '※', 'ブックマーク');
			break;
		case 'fontdef':
			list = new Array(0x8, '字', 'フォント定義ファイル');
			break;
		case 'copyright':
			list = new Array(0x9, '&copy;', '著作権情報');
			break;
		default:
			use = false;
		}
		if(use && Link[i].getAttribute('href'))
			total[j++] = new Array(list[0], list[1], list[2], Link[i].getAttribute('href'));
	}
	total.sort();
	for(var i in total) {
		document.write('<li>');
		document.write('<a href="' + total[i][3] + '" title="' + total[i][2] + '">');
		document.write(total[i][1]);
		document.write('</a>');
		document.write('</li>');
	}
	
	/*
	document.write('<li>BBS-T</li>');
	document.write('<li>BBS-P</li>');
	document.write('<li>Mail</li>');
	*/
	document.write('</ul>');
	
	document.write('</div>');
	
	var last = new Date(document.lastModified);
	var date = new Array(
		last.getFullYear(),
		last.getMonth() + 1,
		last.getDate(),
		last.getHours(),
		last.getMinutes()
	);
	for(var i=3; i<date.length; i++) {
		date[i] = Plus0(date[i]);
	}
	var update = Date2String(new Array(date[0],date[1],date[2]), '/') + '&nbsp;' + date[3] + ':' +  date[4];
	
	document.write('<dl id="PageInfo" style="display: none;">');
	document.write('<dt>著者</dt>');
	document.write('<dd>' + MetaInfo('Author') + '</dd>');
	document.write('<dt>概要</dt>');
	document.write('<dd>' + MetaInfo('Description') + '</dd>');
	document.write('<dt>最終更新日</dt>');
	document.write('<dd>' + update + '</dd>');
	document.write('<dt>URL</dt>');
	document.write('<dd>' + document.URL + '</dd>');
	document.write('</dl>');
}

var PageMenu_Timer = null;
var MenuView_Check = false;
function PageMenu_onMouseOver() {
	if(!MenuView_Check) {
		if(PageMenu_Timer != null) {
			clearTimeout(PageMenu_Timer);
			return;
		}
		document.getElementById('List').style.display = "inline";
		document.getElementById('PageMenu').style.width="auto";
	}
}
function PageMenu_onMouseOut() {
	if(!MenuView_Check) {
		PageMenu_Timer = setTimeout('onTimer()', 500);
	}
}
function onTimer() {
	document.getElementById('List').style.display = "none";
	document.getElementById('PageMenu').style.width="auto";
	PageMenu_Timer = null;
}

function MenuView_onClick() {
	MenuView_Check = !MenuView_Check;
	var obj = document.getElementById('MenuView');
	
	if(MenuView_Check) {
		obj.className = 'on';
	} else {
		obj.className = 'off';
	}
}

var PageInfo_Check = false;
function PageInfo_onMouseOver() {
	if(!PageInfo_Check) {
		document.getElementById('PageInfo').style.display = "block";
	}
}
function PageInfo_onMouseOut() {
	if(!PageInfo_Check) {
		document.getElementById('PageInfo').style.display = "none";
	}
}
function PageInfo_onClick() {
	PageInfo_Check = !PageInfo_Check;
	
	var obj = document.getElementById('PageTitle');
	
	if(PageInfo_Check) {
		obj.className = 'on';
	} else {
		obj.className = 'off';
	}
}

function Plus0(number) {
	if(number < 10)
		return '0' + number;

	return number.toString();
}


