// search
function search_focus(){
	var textbox = document.getElementById("seek");
	if( textbox.value == textbox.title ){
		textbox.value = '';
		textbox.style.color = '';
	}
}

var callbackcookie = function(){
	if (document.getElementById("seek") != null) {
		document.getElementById("seek").onfocus = function(){
			search_focus();
		}
	}
	
	if (document.getElementById("SearchWordEnter") != null) {
		document.getElementById("SearchWordEnter").addEventListener("keydown", function(){
			if (event.keyCode === 13) {
				SearchWord();
				return false;
			}
		})
	}
	
	if (document.getElementById("SearchWord") != null) {
		document.getElementById("SearchWord").onclick = function(){
			SearchWord();
		}
	}
};

if (
    document.readyState === "complete" ||
    (document.readyState !== "loading" && !document.documentElement.doScroll)
) {
  callbackcookie();
} else {
  document.addEventListener("DOMContentLoaded", callbackcookie);
}

function SearchWord() {
	var seek = document.getElementById("seek").value;
	var b = location.pathname;
	var bookname = b.split(/[\\\/]/).slice(-3)[0];
	var pagename = b.split(/[\\\/]/).slice(-1)[0];
	
	document.getElementById("seek").blur();
	
	if( document.getElementById("seek").style.color == "gray" ){
		document.getElementById("seek").value = document.getElementById("seek").title;
		return;
	}
	
	if( seek == "" ){
		document.getElementById("seek").style.color = "gray";
		document.getElementById("seek").value = document.getElementById("seek").title;
		return;
	}
	
	/*if( seek.substring(0,1) == "+" ){
		if( seek.length > 5 ){
			var booknumber = seek.substring(2,4);
			var titlename = seek.substring(4) + ".htm";
			if( isFinite(booknumber) ) {
				booknumber = parseInt(booknumber, 10);
				for( i = 0; i < pages.length; i++){
					if( booknumber != pages[i].bookId ){
						continue;
					}
					if( titlename != pages[i].fileName ){
						continue;
					}
					location.href = "../../" + books[pages[i].bookId].bookFolderName + "/int/" + titlename;
					return;
				}
			}
		}
	}*/

	if( seek == "#0000" ){
	}else{
		if( seek.substring(0,1) == "#" ){
			if( seek.length == 5 ){
				var directnum = seek.substring(1);
				if( isFinite(directnum) ) {
					directnum = parseInt(directnum, 10);
					for( i = 0; i < pages.length; i++){
						if( directnum != parseInt(pages[i].direct,10) ){
							continue;
						}
						
						var linkData = pages[i].fileName;
						splitedAncer = linkData.split("#");
						strAncer = "";
						if (splitedAncer.length == 1){
							linkUrl = linkData;
						} else {
							linkUrl = splitedAncer[0];
							strAncer = "#" + splitedAncer[1];
						}
						
						location.href = "../../" + books[pages[i].bookId].bookFolderName + "/int/" + linkUrl + strAncer;
						return;
					}
				}
			}
		}
	}
	
	location.href = 'search_results.htm' + '?seek=' + encodeURIComponent(seek) + '&bookname=' + encodeURIComponent(bookname) + '&bread=#1';
}

function back_to_index() {
	location.href = 'index.htm';
}

var type='type';
var nam='nam';
var num='num';
var current='current';
var child='child';

if (!window.Node) {
	var Node = {
	ELEMENT_NODE : 1,
	ATTRIBUTE_NODE : 2,
	TEXT_NODE : 3,
	CDATA_SECTION_NODE : 4,
	ENTITY_REFERENCE_NODE : 5,
	ENTITY_NODE : 6,
	PROCESSING_INSTRUCTIONS_NODE : 7,
	COMMENT_NODE : 8,
	DOCUMENT_NODE : 9,
	DOCUMENT_TYPE_NODE : 10,
	DOCUMENT_FRAGMENT_NODE : 11,
	NOTATION_NODE : 12
	}
}

function get_key_from_url() {
	var qsParm = new Array();
	var query = window.location.search.substring(1);
	// 11Aデザイン変換スクリプトによって自動生成した処理(全角文字で検索できるよう対応)
	windowsizeFlg = 0;
	if (window.parent.screen.width == 1024 && window.parent.screen.height == 478) {
		windowsizeFlg = 1;
	}
	if (window.parent.screen.width == 1024 && window.parent.screen.height == 600) {
		windowsizeFlg = 1;
	}
	mfpFlg = 0;
	if (navigator.userAgent.indexOf("ricoh_mfpapnl") > -1) {
		mfpFlg = 1;
	}
	if (window.navigator.platform.indexOf("Linux") > -1 && windowsizeFlg) {
		mfpFlg = 1;
	}

	if (mfpFlg) {
		var start = query.indexOf("seek=") + "seek=".length;
		var end = query.indexOf("&bookname=");
		if (end == -1) {
			end = query.indexOf("&bread=");
			if (end == -1) {
				end = query.length;
			}
		}
		var before = query.substring(start,end);
		var after = before.replace(/&/g,"＆");
		query = query.replace(before,after);
		
		// パンくず内の&のエスケープ
		start = query.indexOf("bread=") + "bread=".length;
		end = query.length;
		before = query.substring(start,end);
		after = before.replace(/&/g,"＆");
		query = query.replace(before,after);
	}
	var parms = query.split('&');
	for (var i=0; i<parms.length; i++) {
		var pos = parms[i].indexOf('=');
		if (pos > 0) {
			// 11Aデザイン変換スクリプトによって自動生成した処理(全角文字で検索できるよう対応)
			if (mfpFlg) {
				if(parms[i].indexOf("＆") > 0) {
					parms[i] = parms[i].replace(/＆/g,"&");
				}
			}
			var key = parms[i].substring(0,pos);
			var val = parms[i].substring(pos+1);
			qsParm[key] = decodeURIComponent(val);
		}
	}
	return qsParm;
}

function get_keywords( query ) {
//	query = query.replace( /([\/\\\.\*\+\?\|\(\)\[\]\{\}\$\^])/g, "\\$1" );
	query = query.replace( /([^A-Za-z0-9])/g, "\\$1" );
	query = query.replace( /( +|　+)/, ' ' );
	query = query.replace( /( |　)$/, '' );
	var keywords = query.split(/ |　/);
	return keywords;
}


function change() {
	var sh_id = window.location.hash;
	if (sh_id != ""){
		sh_dom = document.getElementById(sh_id.substring(1));
		if (sh_dom != null){
			sh_dom.classList.toggle("accordion-open");
//			if(('sessionStorage' in window) && (window.sessionStorage !== null) && (typeof sessionStorage !== 'undefined')) {
//				window.sessionStorage.setItem(sh_dom.id, true);
//			}
		}
	}

	var key = get_key_from_url();
	var w = key['seek'] == undefined ? key['word'] : key['seek'];

	if ( w == null || w == '' || w == undefined) {
		return;
	}

	w = w.replace(/　/g," ");
	w = w.replace(/^\s+|\s+$/g, "").replace(/\s+/g, " ");
	
	var d = document.getElementById("article");

	var a = new Array();
	traverseDom(d, a);
	var n;
	while(n = a.pop()){
		rep(n, w);
	}
}

function traverseDom(root, a) {
	var c = root, n = null;
	var it = 0;
	do {
		n = c.firstChild;

		//if (n == null || (c.nodeType == Node.ELEMENT_NODE && c.className == "relation") || (c.id != null && c.id.match("^bookname"))) {
		if (n == null || (c.id != null && c.id.match("^bookname"))) {
			// visit c
			if (c.nodeType == Node.TEXT_NODE) {
			a.push(c);
			}
			// done visit c
			n = c.nextSibling;
		}

		if (n == null) {
			var tmp = c;
			do {
				n = tmp.parentNode;
				if (n == root){
					break;
				}
				try {
					// visit n
					if (n.nodeType == Node.TEXT_NODE) {
						a.push(n);
					}
				} catch (e) {
					n = root;
					break;
				}
				// done visit n
				tmp = n;
				n = n.nextSibling;
			} while (n == null)
		}
		c = n;
	} while (c != root);
	return;
}

function rep(n, w) {

	var b = w.split(' ');
	for (var i=0; i < b.length; i++) {
		b[i] = quotemeta(b[i]);
	}
	
	var ww = b.join('|');
	
	var a = n.nodeValue.split(new RegExp("(" + ww + ")","i"));

	var f = document.createDocumentFragment();

	for(var i = 0; i < a.length; i++) {
	var e;
	if (a[i].match(new RegExp(ww,"i"))) {
		e = document.createElement('span');
		
		//use browser sniffing to determine if IE or Opera (ugly, but required)
		var isOpera, isIE = false;
		if(typeof(window.opera) != 'undefined') {
			isOpera = true;
		}
		if(!isOpera && navigator.userAgent.indexOf('MSIE') != -1) {
			isIE = true;
		}
		
		var styleData; 
		if (!findParent(n, 'div', 'important')) {
			styleData = "background-color: #B4EBFA;";
		} else {
			styleData = "color: black; background-color: #B4EBFA;";
		}
		
		if (!isIE) {
			e.setAttribute('style', styleData);
		} else {
			e.style.setAttribute('cssText', styleData);
		}
		
		e.appendChild(document.createTextNode(a[i]));
	} else {
		e = document.createTextNode(a[i]);
	}
	f.appendChild(e);
	}
	n.parentNode.replaceChild(f, n);
}

function findParent(node, tag, classname) {
	 var n = node;
	 while (n != null) {
		var p = n.parentNode;
		if (p != null && p.nodeType == Node.ELEMENT_NODE &&
			p.nodeName.match(new RegExp(tag, "i")) && p.className == classname) {
			return true;
		}
		n = p;
	 }
	 return false;
}

function escapeHTML(w) {
	w = w.replace(/&/g, "&amp;");
	w = w.replace(/'/g, "&apos;");
	w = w.replace(/"/g, "&quot;");
	w = w.replace(/</g, "&lt;");
	w = w.replace(/>/g, "&gt;");
	return w;
}

function unescapeHTML(w) {
	w = w.replace(/&gt;/g, ">");
	w = w.replace(/&lt;/g, "<");
	w = w.replace(/&quot;/g, "\"");
	w = w.replace(/&apos;/g, "'");
	w = w.replace(/&amp;/g, "&");
	return w;
}

function quotemeta(w) {
	w = w.replace(/\W/g, "\\$&");
	return w;
}

