/* http://hp.vector.co.jp/authors/VA022006/を元に改変 */

// User Agent判別
var UA = new function()
{
	this.uaStr = navigator.userAgent;
	this.Win = /Win/.test(navigator.platform);
	this.Mac = /Mac/.test(navigator.platform);
	this.Opera   = (/Opera/     .test(this.uaStr) && !!window.opera);
	this.Opera6  = (/Opera.6/   .test(this.uaStr) && !!window.opera);
	this.Opera7  = (/Opera.7/   .test(this.uaStr) && !!window.opera);
	this.Opera71 = (/Opera.7\.1/.test(this.uaStr) && !!window.opera);
	this.Opera8  = (/Opera.8/   .test(this.uaStr) && !!window.opera);
	this.Opera8  = (/Opera.8\.5/   .test(this.uaStr) && !!window.opera);
	this.Opera8  = (/Opera.8\.53/   .test(this.uaStr) && !!window.opera);
	this.MSIE    = (/MSIE/      .test(this.uaStr) && document.all && !this.Opera);
	this.MacIE   = (/MSIE 5/    .test(this.uaStr) && this.Mac && this.MSIE);
	this.WinIE   = (/MSIE [56]/ .test(this.uaStr) && this.Win && this.MSIE);
	this.WinIE   = (/MSIE 6\.0/ .test(this.uaStr) && this.Win && this.MSIE);
	this.WinIE55 = (/(5\.5|6)/  .test(this.uaStr) && this.WinIE);
	this.KHTML   = /(KHTML|like Gecko|AppleWebKit)/.test(this.uaStr);
	this.Gecko   = (navigator.product == 'Gecko') && !this.KHTML;
	this.NN4     = (!!document.layers && !/OmniWeb/.test(this.uaStr));
};
// URLパス
var URL = new function()
{
	this.root = 'file:///E:/テキスト/000/';
	if (location.host == "127.0.0.1") this.root = '/テキスト/000/';
	else if (location.protocol == "http:") this.root = '/';
};

// abbr要素を再生成する(for WinIE)
// 参考元：http://www.akatsukinishisu.net/itazuragaki/?WinIE_abbr
function regenerateAbbr()
{
	if (!UA.WinIE55) return;
	var abbrs = document.getElementsByTagName('abbr');
	for (var i=0; i<abbrs.length; i++)
	{
		if (abbrs.item(i).parentNode.nodeName.match(/^H1$/i) ) continue;
		var oldAbbr = abbrs.item(i);
		var newAbbr = document.createElement('abbr');
		if (oldAbbr.lang)
		{
			newAbbr.setAttribute('lang', oldAbbr.lang);
			newAbbr.setAttribute('xml:lang', oldAbbr.lang);
		}
		if (oldAbbr.title) newAbbr.setAttribute('title', oldAbbr.title);
		oldAbbr.parentNode.insertBefore(newAbbr, oldAbbr);
		while (oldAbbr.nextSibling.nodeName != '/ABBR')
		{
			newAbbr.appendChild(oldAbbr.nextSibling);
		}
		oldAbbr.parentNode.removeChild(oldAbbr.nextSibling);
		oldAbbr.parentNode.removeChild(oldAbbr);
		/*alert(newAbbr.parentNode.innerHTML);*/ // 確認用
	}
}

// q要素の内容を引用符で括る(for WinIE)
function appendQMarks()
{
	var qs = document.getElementsByTagName('q');
	for (var i=0; i<qs.length; i++)
	{
		var obj = qs.item(i);
		obj.style.cursor = (obj.title || obj.cite) ? 'help' : 'auto';
		if (UA.WinIE)
		{
			if ( /^en/.test( getLangAttr(obj) ) ) {
				op_quote = '\x300c;', cl_quote = '\x300d';
				obj.className += (obj.className ? ' ' : '') + 'en';
			}
			else {
				op_quote = '「', cl_quote = '」';
			}
			obj.insertBefore( document.createTextNode(op_quote), obj.firstChild );
			obj.appendChild ( document.createTextNode(cl_quote) );
		}
	}
	function getLangAttr(obj)
	{
		if ( lang = obj.getAttribute('lang') ) return lang;
		else while (obj.nodeName.toLowerCase() != 'html')
		{
			obj = obj.parentNode, lang = obj.lang;
			if (lang && lang != 'ja') break;
		}
		return lang;
	}
}

// 引用先を明示
function appendCitation()
{
	var appIDName = 'appendCitation';
	if ( document.getElementById(appIDName+'0') ) return;
	var blqs = document.getElementsByTagName('blockquote');
	for (var i=0, cnt=0; i<blqs.length; i++)
	{
		var blq = blqs.item(i);
		if (title = (blq.title || blq.cite) )
		{
			//blq.title = '';
			nP = document.createElement('p');
			nP.setAttribute("class", "cite");
			nC = document.createElement('cite');
/*			nC.setAttribute("class", "block");*/
			nC.className = 'block';
			nP.id = appIDName+cnt;
			nP.className = 'citation';
			if (blq.cite)
			{
				nA = document.createElement('a');
				nA.href = blq.cite;
				nA.title = blq.title;
				nA.appendChild( document.createTextNode(title) );
				nC.appendChild( document.createTextNode('「') );
				nC.appendChild(nA); nC.appendChild( document.createTextNode('」') );
			}
			else nC.appendChild( document.createTextNode('「'+title+'」') );
			nP.appendChild(nC); nP.appendChild( document.createTextNode('') );
/*			if (UA.Gecko && blq.nextSibling.nextSibling.nodeName.toLowerCase() == 'p')
			blq.nextSibling.nextSibling.style.marginTop = '-.6em !important';*/
			blq.style.paddingBottom = '';
			blq.parentNode.insertBefore(nP, blq.nextSibling); cnt++;
		}
	}
}

// ブラウザ別スタイル
function appendStylesheet()
{
	if ( !(document.createElement && document.appendChild) ) return;
	if (UA.Opera) {
		appendLinkElement(URL.root+'css/ruby_opera.css', 'screen, tv');
	}
	if (UA.Gecko) {
		appendLinkElement(URL.root+'css/ruby_gecko.css', 'screen, tv');
	}
	function appendLinkElement(_url, _media)
	{
		var
		nLink = document.createElement('link');
		nLink.setAttribute('href' , _url);
		nLink.setAttribute('media', _media);
		nLink.setAttribute('rel'  , 'stylesheet');
		nLink.setAttribute('type' , 'text/css');
		document.getElementsByTagName('head')[0].appendChild(nLink);
	}
}
appendStylesheet();

if (document.attachEvent) {
	window.attachEvent('onload', regenerateAbbr);
	window.attachEvent('onload', appendQMarks);
	window.attachEvent('onload', appendCitation);
}
else if (document.addEventListener) {
	window.addEventListener('load', appendQMarks, false);
	window.addEventListener('load', appendCitation, false);
}

if (self != top) {
	top.location.href = self.location.href;
}

// ターゲット設定

var defaultTarget="_top"
var checkedTarget="_blank"

function settarget(b){
for(var i=0;i<document.links.length;i++)
document.links[i].target=b?checkedTarget:defaultTarget
}

// 装飾角丸
//function addEvent(obj, evType, fn) {
//	if (obj.addEventListener) {
//		obj.addEventListener(evType, fn, true);
//		return true;
//	} else if (obj.attachEvent) {
//		var r = obj.attachEvent("on"+evType, fn);
//		return r;
//	} else {
//		return false;
//	}
//}

//function blockquote() {
//	if (!UA.WinIE55) return;
//	classTree     = new Array(3);
//	classTree[0]  = ["top","right","bottom","left","topright","bottomright","bottomleft","topleft"];
//	classTree[1]  = ["faux","north","south"];
//	classTree[2]  = ["north", "east", "west", "south"];
//	classNames    = [''];
	
//	tempdivs = [];
//	divs = document.getElementsByTagName('blockquote');
//	for (i=0;i<divs.length;i++) {
//		for (j=0; j<3; j++) {
//			cdiv = divs[i];
//			if (cdiv.className.indexOf(classNames[j]) > -1) {
//				tempinner = cdiv.innerHTML;
//				cdiv.innerHTML = "";
//				prevdiv = cdiv;
//				for (a=0; a<classTree[j].length; a++) {
//					tempdivs[a] = document.createElement('div');
//					tempdivs[a].className = classTree[j][a];
//					prevdiv.appendChild(tempdivs[a]);
//					prevdiv = tempdivs[a];
//				}
//				prevdiv.innerHTML = tempinner;
//			}
//		}
//	}
//}

//if (document.getElementById && document.createElement) { addEvent(window, 'load', blockquote); }
