// annopop.js - 注釈表示スクリプト v2.0.7       (C)2007-2009 T.MURACHI
// ※あらゆる利用 (改変、再配布、商利用、etc...) において自由。一切の責任を負いません。ご利用は自己責任で。

var annopop_is_loaded;		// 重複読み込み回避用
if (!annopop_is_loaded){


var ANNOPOP_CLASS_ANCHOR		= "annopop_a";
var ANNOPOP_CLASS_TARGET		= "annopop_t";
var ANNOPOP_STYLE_ANCHOR		= "color: black; text-decoration: none; border-bottom: dashed 1px black;";	// アンカー用の追加のスタイル
var ANNOPOP_STYLE_TARGET		= "display: none; top: 0px; left: 0px;" +		// ← ターゲット用の必須のスタイル
								  " border: solid 1px #444; padding: 4px;" +					// ← これ以下はお好みで
								  " background-color: white; color: black; font-size: 11px;";
// IE 6.0 以前では position: fixed が使えない為、position: absolute にして茶を濁す
if (typeof ScriptEngine == 'function' && ScriptEngineMajorVersion() * 10 + ScriptEngineMinorVersion() <= 56)
	ANNOPOP_STYLE_TARGET += " position: absolute;";
else
	ANNOPOP_STYLE_TARGET += " position: fixed;";

if (typeof ScriptEngine == 'function'){	// IE の場合、DOM の Node オブジェクトなんて存在しないらしい (^_^; 。
	var Node = new Object;
	Node.TEXT_NODE = 3;
}

var annopop_view_rect = {
	"left"		: 0,
	"top"		: 0,
	"right"		: window.innerWidth,
	"bottom"	: window.innerHeight
};
annopop_view_rect.width = function () { return this.right - this.left; };

// IE 6.0 以前の場合に参照する body 要素
var annopop_ie_body_elem = (document.compatMode && document.compatMode.match(/CSS1/)) ? document.documentElement : document.body;

function annopopOnLoad() {
	//for (var i = 0; i < document.styleSheets.length; i++){
		//var stylesheet = document.styleSheets.item(i);
		var stylesheet = document.styleSheets[document.styleSheets.length - 1];
		if (stylesheet.addRule) {	// IE
			stylesheet.addRule("." + ANNOPOP_CLASS_ANCHOR, ANNOPOP_STYLE_ANCHOR);
			stylesheet.addRule("." + ANNOPOP_CLASS_TARGET, ANNOPOP_STYLE_TARGET);
		}
		else if (stylesheet.insertRule) {	// DOM
			stylesheet.insertRule("." + ANNOPOP_CLASS_ANCHOR + "{" + ANNOPOP_STYLE_ANCHOR + "}", 0);
			stylesheet.insertRule("." + ANNOPOP_CLASS_TARGET + "{" + ANNOPOP_STYLE_TARGET + "}", 0);
		}
		else	return;
	//}
	var anchors = document.getElementsByTagName("a");
	var target_elem;
	// IE 7.0 以前では、href 属性はフルパスの URI になる。
	var reg_anchor = typeof ScriptEngine == 'function' && navigator.userAgent.match(/MSIE 8/) == null ? new RegExp("^" + location.href + "#annopop-") : /^#annopop-/;
	for (var i = 0; i < anchors.length; i++){
		var anchor = anchors[i];
		// 注釈アンカーかどうかを判定し、注釈文の要素 ID を決定する
		var target_id;
		if (anchor.getAttribute("href") && anchor.getAttribute("href").match(reg_anchor)){		// href="#annopop-..." の場合
			target_id = anchor.getAttribute("href").replace(reg_anchor, "annopop-");
		}
		else if (anchor.id.match(/^annopop-/)){					// ID="annopop-..." の場合
			target_id = anchor.id.replace(/^annopop-/, "");
		}
		else	continue;
		if (!(target_elem = document.getElementById(target_id)))	continue;
		
		if (target_id.match(/^annopop-/)){	// 以下の操作は注釈アンカーが href="#annopop-..." の場合のみ実行する
			// 注釈文が <dl> 要素の場合、<dd> 要素の中身のみを <div> 要素にまとめなおして、他は削除する
			if (target_elem.tagName.match(/^dl$/i)){
				var dd_list = target_elem.getElementsByTagName("dd");
				if (!dd_list.length)	continue;
				var new_elem = document.createElement("div");
				new_elem.innerHTML = dd_list[0].innerHTML;
				target_elem.parentNode.appendChild(new_elem);
				target_elem.parentNode.removeChild(target_elem);
				target_elem = new_elem;
			}
			// 注釈文が <span> 要素の場合、両端が丸カッコならば、取り除く
			else if (target_elem.tagName.match(/^span$/i)){
				var span_childs = target_elem.childNodes;
				for (var i = 0; i < span_childs.length; i++){
					if (span_childs[i].nodeType != Node.TEXT_NODE)	continue;
					span_childs[i].nodeValue = span_childs[i].nodeValue.replace(/^\(|^（/, "");
					break;
				}
				for (var i = span_childs.length - 1; i >= 0; i--){
					if (span_childs[i].nodeType != Node.TEXT_NODE)	continue;
					span_childs[i].nodeValue = span_childs[i].nodeValue.replace(/\)$|）$/, "");
					break;
				}
			}
		}
		
		anchor.className = ANNOPOP_CLASS_ANCHOR;
		anchor.setAttribute("href", "javascript: void(0);");
		anchor.onclick = (function (target_elem_){ return function (e) {
			if (document.all)	e = event;
			target_elem_.style.display = "block";
			if (target_elem_.offsetWidth > annopop_view_rect.width())
				target_elem_.style.width = annopop_view_rect.width() + "px";
			var x = e.clientX - target_elem_.offsetWidth / 2;
			x = x < annopop_view_rect.left								? annopop_view_rect.left :
				x > annopop_view_rect.right - target_elem_.offsetWidth	? annopop_view_rect.right - target_elem_.offsetWidth :
																		  x;
			var y = e.clientY - target_elem_.offsetHeight / 2;
			y = y < annopop_view_rect.top									? annopop_view_rect.top :
				y > annopop_view_rect.bottom - target_elem_.offsetHeight	? annopop_view_rect.bottom - target_elem_.offsetHeight :
																			  y;
			if (typeof ScriptEngine == 'function' && ScriptEngineMajorVersion() * 10 + ScriptEngineMinorVersion() <= 56){
				// IE 6.0 以前では、position: absolute にしている為、スクロール位置に応じて座標値をずらす
				x += annopop_ie_body_elem.scrollLeft;
				y += annopop_ie_body_elem.scrollTop;
			}
			with (target_elem_.style){
				left = x + "px";
				top = y + "px";
			}
		}; })(target_elem);
		target_elem.className = ANNOPOP_CLASS_TARGET;
		target_elem.onclick = (function(target_elem_){ return function () {
			target_elem_.style.display = "none";
		}; })(target_elem);
	}
}

var annopop_temp_onload_function = window.onload;
window.onload = function() {
	if (annopop_temp_onload_function)	annopop_temp_onload_function();
	annopopOnLoad();
};


}
annopop_is_loaded = 1;
