var UserIE8 = false;

//*************************************************************************************************
window.onload = function(){
//*************************************************************************************************
	try{
		if(UseIE){
			if(navigator.userAgent.indexOf("MSIE 8.0") != -1){
				UserIE8 = true;
			}
		}else{
		}

		// load resource
		fncLoadResource();

		fncSearchBox("hdr_srch_w_bg");

		var iHideLevel = 999;
		var bIsCurrentChapter = false;

		// ----------------------------------------------------------------------------------------
		// Search
		// ----------------------------------------------------------------------------------------
		if (strWindowType != "HOME") {

			// set click event handler to search button
			document.getElementById("id_search_button").onclick = function() {
				var strSearchText = document.getElementById("id_search").value;
				if (strSearchText == fncGetResourceByResourceId("enter_search_keyword")) {
					return;
				}
				strSearchHelpTocId = "../search/search";
				fncOpenSearchPage(1);
			}

			var strSearchText = document.location.search.split("?search=")[1];
			if (strSearchText != undefined) {
				strSearchText = decodeURIComponent(strSearchText);
				document.getElementById("id_search").value = strSearchText;
				document.getElementById("id_search").style.color = "#000000";

				fncSetCookie("SEARCH-RESULT-SETTING", "");
				fncDoSearch(1);
			}

			// visible/invisible search options (default:invisible)
			if (document.getElementById("id_search_options_label")) {
				document.getElementById("id_search_options_label").innerHTML = "<a href=\"#\" onclick=\"fncToggleSearchOptions();\"><img src=\"../frame_images/srch_opt_show.gif\" />" + fncGetResourceByResourceId("search_options_show") + "</a>";
			}
			if (document.getElementById("id_search_options_search_scope_all")) {
				document.getElementById("id_search_options_search_scope_all").onclick = function() {
					fncSelectChaptersFromAll();
				}
			}

			if (fncGetCookie("SEARCH-OPTIONS-CASE") == "TRUE") {
				document.getElementById("id_search_options_case").checked = true;
			}

			var strSavedSearchKeyword = fncGetCookie("SEARCH-KEYWORD");
			if (strSavedSearchKeyword) {
				document.getElementById("id_search").value = strSavedSearchKeyword;
			}
			fncSearchBox("hdr_srch_w_bg");
		}

		// resize window
		fncOnResize();

		// scroll to link
		if (	(document.location.hash != "")
			&&	(document.getElementById("id_toc_current"))
		) {			
			document.getElementById("id_toc_current").scrollIntoView(true);
			document.getElementById("id_toc").scrollLeft = 0;

		} else {

			if (document.getElementById("id_toc")) {
				if (strCurrentChapterId == "") {
					strCurrentChapterId = "ALL";
				}

				// NOTE: for IE7 and later
				if (document.all) {
					var ti = window.setTimeout(
						function() {
							document.getElementById("id_toc").scrollTop = fncGetCookie("TOC-SCROLL-POSITION-TOP-" + strCurrentChapterId);
							document.getElementById("id_toc").scrollLeft = fncGetCookie("TOC-SCROLL-POSITION-LEFT-" + strCurrentChapterId);
						},
						1
					);
				} else {
					document.getElementById("id_toc").scrollTop = fncGetCookie("TOC-SCROLL-POSITION-TOP-" + strCurrentChapterId);
					document.getElementById("id_toc").scrollLeft = fncGetCookie("TOC-SCROLL-POSITION-LEFT-" + strCurrentChapterId);
				}
			}
		}

		// ----------------------------------------------------------------------------------------
		// highlight
		// ----------------------------------------------------------------------------------------
		if (document.location.search) {
			if (strWindowType != "HOME") {
				var ti = window.setTimeout("fncMarkupSearch()", 26);
			}
		} else {
			// for IE6
			if (document.all) { // TODO: ignore IE8
				if (document.location.hash != "") {
					var strHash = document.location.hash.substring(1);
					if (document.all.item(strHash)) {
						document.all.item(strHash).scrollIntoView(true);
					}
				}
			}
		}

		// ----------------------------------------------------------------------------------------
		// for IE7: scroll bar position and height
		// ----------------------------------------------------------------------------------------
		if (	(document.all) // ignore FF/SF
			&&	(window.XMLHttpRequest)	// ignore IE6
		) {
			var strContents = document.getElementById("id_content").innerHTML;
			document.getElementById("id_content").innerHTML = strContents.replace(/<hr>/gi, "<div style=\"height:40px;vertical-align:Middle;\"><hr /></div>");
		}
	}catch(e){
	}
}

function fncMarkupSearch() {
	try {

		// get search keyword
		var strSearchText = document.location.search.split("?search=")[1];

		strSearchText = strSearchText.split("&word=yes")[0];
		if (strSearchText != "") {

			// decode
			strSearchText = decodeURIComponent(strSearchText);

			// trim
			strSearchText = strSearchText.trim();

			if (strSearchText == fncGetResourceByResourceId("enter_search_keyword")) {
				return;
			}

			var res = / |　/;
			var arrSearchText = strSearchText.split(res);

			var iLoopLength = arrSearchText.length;
			for (var i = 0; i < iLoopLength; i++) {
				arrSearchText[i] = arrSearchText[i].replace(/___SPACE___/g, " ");
			}

			if (document.getElementById("id_search_options_multibyte")) {
				if (document.getElementById("id_search_options_multibyte").checked) {
					for (var i = 0; i < iLoopLength; i++) {
						arrSearchText[i] = fncConvertSearchText(arrSearchText[i], false);
					}
				}
			}

			var strSearchOptionCaseSensitive = "i";
			if (fncGetCookie("SEARCH-OPTIONS-CASE") == "TRUE") {
				strSearchOptionCaseSensitive = "";
			}

			var bWordMarking = false;
			if (document.location.search.indexOf("&word=yes") != -1) {
				bWordMarking = true;
			}

			fncMarkupText(
				document.getElementById("id_content"),
				arrSearchText,
				strSearchOptionCaseSensitive,
				bWordMarking
			);

// VAIOではヒット位置にスクロールしない 2011-03-29
//			if (document.getElementById("id_hit")) {
//				document.getElementById("id_hit").scrollIntoView(true);
//			}
		}
	} catch (e) {
	}
}

// ------------------------------------------------------------------------------------------------
// highlight
// ------------------------------------------------------------------------------------------------
function fncMarkupText(element, arrSearchText, strSearchOptionCaseSensitive, bWordMarking) {
	try {

		var nElementChildLength = element.childNodes.length;
		for (var i = 0; i < nElementChildLength; i++) {
			var child = element.childNodes[i];

			if (child.nodeType == 3) { // #text
				var strNodeValue = child.nodeValue;

				var strNodeValueTemp = strNodeValue.replace(/\t| |\n/g, "");
				if (strNodeValueTemp == "") {
					continue;
				}

				var bIsMarkedup = false;

				var nMarkerColor = 0;
				var nSearchTextLength = arrSearchText.length;
				for (var j = 0; j < nSearchTextLength; j++) {

					switch (nMarkerColor) {
						case 0:
							strMarkerColor = String.fromCharCode(0x2080); // "₀";
							break;
						case 1:
							strMarkerColor = String.fromCharCode(0x2081); // "₁";
							break;
						case 2:
							strMarkerColor = String.fromCharCode(0x2082); // "₂";
							break;
						case 3:
							strMarkerColor = String.fromCharCode(0x2083); // "₃";
							break;
						case 4:
							strMarkerColor = String.fromCharCode(0x2084); // "₄";
							break;
						case 5:
							strMarkerColor = String.fromCharCode(0x2085); // "₅";
							break;
						case 6:
							strMarkerColor = String.fromCharCode(0x2086); // "₆";
							break;
						case 7:
							strMarkerColor = String.fromCharCode(0x2087); // "₇";
							break;
						case 8:
							strMarkerColor = String.fromCharCode(0x2088); // "₈";
							break;
						case 9:
							strMarkerColor = String.fromCharCode(0x2089); // "₉";
							break;
					}

					var strSearchText = arrSearchText[j];

					var strSearchTextParam = "(" + strSearchText + ")";
					if (bWordMarking) {
						strSearchTextParam = "\\b(" + strSearchText + ")\\b";
					}
					var re = new RegExp(strSearchTextParam, "g" + strSearchOptionCaseSensitive);
					if (re.exec(strNodeValue) != null) {
						var strNodeValue = strNodeValue.replace(re, String.fromCharCode(0x2045) + strMarkerColor + String.fromCharCode(0x2046) + "$1" + String.fromCharCode(0x208E));
						bIsMarkedup = true;
					}
					nMarkerColor++;
					if (nMarkerColor >= 10) {
						nMarkerColor = 0;
					}
				}
				if (bIsMarkedup) {
					strNodeValue = strNodeValue.replace(/</g, "&lt;");
					strNodeValue = strNodeValue.replace(/>/g, "&gt;");

					strNodeValue = strNodeValue.replace(/\u2045/g, "<span id=\"id_hit\" class=\"hit hit_"); // ⁅
					strNodeValue = strNodeValue.replace(/\u2046/g, "\">"); // ⁆
					strNodeValue = strNodeValue.replace(/\u2080/g, "0"); // ₀
					strNodeValue = strNodeValue.replace(/\u2081/g, "1"); // ₁
					strNodeValue = strNodeValue.replace(/\u2082/g, "2"); // ₂
					strNodeValue = strNodeValue.replace(/\u2083/g, "3"); // ₃
					strNodeValue = strNodeValue.replace(/\u2084/g, "4"); // ₄
					strNodeValue = strNodeValue.replace(/\u2085/g, "5"); // ₅
					strNodeValue = strNodeValue.replace(/\u2086/g, "6"); // ₆
					strNodeValue = strNodeValue.replace(/\u2087/g, "7"); // ₇
					strNodeValue = strNodeValue.replace(/\u2088/g, "8"); // ₈
					strNodeValue = strNodeValue.replace(/\u2089/g, "9"); // ₉
					strNodeValue = strNodeValue.replace(/\u208E/g, "</span>"); // ₎

					var newNode = document.createElement("span");
					newNode.innerHTML = strNodeValue;
					element.replaceChild(newNode, child);
				}
			} else {
				fncMarkupText(child, arrSearchText, strSearchOptionCaseSensitive, bWordMarking);
			}
		}
	} catch (e) {
	}
}

// resize
window.onresize = fncOnResize;
function fncOnResize() {
	try {

		var w = fncGetWindowWidth();
		var h = fncGetWindowHeight();

		if (document.location.search.indexOf("?sub=yes") == -1) {
			document.getElementById("id_panel").style.height = h - 93 + "px";
			document.getElementById("id_toc").style.height = h - 194 + "px";
			document.getElementById("id_content").style.height = h - 93 + "px";

			if (!document.getElementById("id_res_bar_icon_toggle_panel")) {
				document.getElementById("id_content").style.width = w - 300 + "px";
			} else if (document.getElementById("id_res_bar_icon_toggle_panel").src.indexOf("hide") != -1) {
				document.getElementById("id_content").style.width = w - 300 + "px";
			} else {
				document.getElementById("id_content").style.width = w - 0 + "px";
			}

			document.getElementById("id_footer").style.top = h - 26 + "px";
			document.getElementById("id_footer").style.width = w + "px";

			if (document.getElementById("id_search_options").style.display.toLowerCase() == "none") {
				document.getElementById("id_search_results").style.height = h - 191 + "px";
			} else {
				document.getElementById("id_search_results").style.height = h - 401 + "px";
			}

			if (!document.getElementById("id_chapter_select")) {
				document.getElementById("id_toc").style.top = "22px";
				document.getElementById("id_toc").style.height = h - 147 + "px";
			}

		} else {
			document.getElementById("id_content").style.height = h - 28 + "px";
			document.getElementById("id_close").style.top = h - 28 + "px";
		}
	} catch (e) {
	}
}

var strWindowType = "MAIN";

function fncOpenSearchPage(nPage) {
	try {
		var strFilePath = "";

		var search_texts = encodeURIComponent(document.getElementById("id_search").value);
		var htmlHref = document.location.href;
		var posContents = htmlHref.indexOf("/contents/");
		if (posContents == -1)
		{
			strFilePath = "contents/search/search" + ".html?search=" + search_texts;
		} else {
			var upperPath = "";
			var posPath = htmlHref.indexOf("/", posContents + 10);
			while (posPath > 0)
			{
				upperPath = upperPath + "../";
				posPath = htmlHref.indexOf("/", posPath + 1);
			}
			strFilePath = upperPath + "search/search" + ".html?search=" + search_texts;
		}

		fncSetSearchResultsInfo(nPage);

		window.open(strFilePath, "_self");
	} catch (e) {
	}
}