//------------------------------------------------------------------------------------
// UIWebViewで検索を実現する
// http://www.icab.de/blog/2010/01/12/search-and-highlight-text-in-uiwebview/
// 少しいじってる
// 実行上の注意
// (0)TARGETS-Build Phase-にこのファイルを登録する；極めて重要
// (1)スクリプトはHTMLの先頭に書いても良い
// (2)呼び出しはHTMLの最後に置かなければならない(bodyが確定しないから)
//------------------------------------------------------------------------------------

// We're using a global variable to store the number of occurrences
var MyApp_SearchResultCount = 0;

// helper function, recursively searches in elements and their child nodes
function MyApp_HighlightAllOccurencesOfStringForElement(element,keyword)
{
	if (element) {
		if (element.nodeType == 3) { // Text node
			while (true) {
				var value = element.nodeValue;  // Search for keyword in text node
				// .find()はエラーが出る
				var idx = value.toLowerCase().indexOf(keyword); // indexOf()が検索
				if (idx < 0) break; // 見つからないので終了(-1)
//				var span = document.createElement("div");	// こちらにすると１行まるごと反転になる
				var span = document.createElement("span");
				var text = document.createTextNode(value.substr(idx,keyword.length));
				span.appendChild(text);
				span.setAttribute("class","MyAppHighlight");
				span.style.backgroundColor="yellow";
				span.style.color="black";
				span.style.fontWeight="bold";
				text = document.createTextNode(value.substr(idx+keyword.length));
				element.deleteData(idx, value.length - idx);
				var next = element.nextSibling;
				element.parentNode.insertBefore(span, next);
				element.parentNode.insertBefore(text, next);
				element = text;
				MyApp_SearchResultCount++;	// update the counter
			}
		} else if (element.nodeType == 1) { // Element node
			if (element.style.display != "none" && element.nodeName.toLowerCase() != 'select') {
				for (var i=element.childNodes.length-1; i>=0; i--) {
					MyApp_HighlightAllOccurencesOfStringForElement(element.childNodes[i],keyword);
				}
			}
		}
	}
}

// the main entry point to start the search
function MyApp_HighlightAllOccurencesOfString(keyword)
{
	MyApp_RemoveAllHighlights();
	MyApp_HighlightAllOccurencesOfStringForElement(document.body, keyword.toLowerCase());
}

//------------------------------------------------------------------------------------

// helper function, recursively removes the highlights in elements and their childs
function MyApp_RemoveAllHighlightsForElement(element)
{
	if (element) {
		if (element.nodeType == 1) {
			if (element.getAttribute("class") == "MyAppHighlight") {
				var text = element.removeChild(element.firstChild);
				element.parentNode.insertBefore(text,element);
				element.parentNode.removeChild(element);
				return true;
			} else {
				var normalize = false;
				for (var i=element.childNodes.length-1; i>=0; i--) {
					if (MyApp_RemoveAllHighlightsForElement(element.childNodes[i])) {
						normalize = true;
					}
				}
				if (normalize) {
					element.normalize();
				}
			}
		}
	}
	return false;
}

// the main entry point to remove the highlights
function MyApp_RemoveAllHighlights()
{
	MyApp_SearchResultCount = 0;
	MyApp_RemoveAllHighlightsForElement(document.body);
}

//------------------------------------------------------------------------------------
