/*****************************************
how to use:

<script src="/htm-webaxy/select/findWords.js"></script>
<script>
self.setTimeout("findWords('')",1000);
</script>

OR use to find word in file
<script src="/htm-webaxy/select/findWords.js"></script>
<script>
....
findWords(word);
...
</script>
*******************************************/

var colorSpanName = "foundWordSpan";
if (typeof foundBgColor == "undefined") {
	foundBgColor = new Array("#ffff66","#ff66ff","#66ffff");
}

//===================================================================
//  
//  findWords: find words in file (comma separated)
//  
//===================================================================
function findWords(wordsString) {
	
	if (location.href.indexOf("search-site.pl") != -1) {
		return;
	}
	if (wordsString.search(/\S/) == -1) {	// no word- find from url
		if (!location.search) {
			wordsString = "";
		}
		else {
			if (location.search.search(/findWords=([^&]+)/) != -1) {
				wordsString = RegExp.$1;
			}
		}
	}
	
	
	if (wordsString.search(/\S/) == -1) {
		return;
	}
	
	wordsString = decodeURIComponent(wordsString);
	
	var words = wordsString.split(",");
	findWordsIn(words,document.body);
	var foundSpans = document.getElementsByTagName("span");
	if (foundSpans) {
		for (var s=0; s< foundSpans.length; foundSpans++) {
			if (foundSpans[s].name == colorSpanName) {
				foundSpans[s].scrollIntoView(true);
				break;
			}
		}
	}
}

//===================================================================
//  
//  findWordsIn: color words in object (recursive!!)
//  
//===================================================================
function findWordsIn(wordsString,obj) {
	if (obj == undefined) {
		return;
	}
	if (obj.nodeType == 3) {	// obj is text node- stop and look for words
		cutWordsFrom(wordsString,obj);
		return;
	}
	if (!obj.childNodes) {
		return;
	}
	if (obj.nodeType != 1) {
		return;
	}
	if (obj.form) {		// like select box etc
		return;
	}
	// go recursive!
	
	for (var i = 0; i < obj.childNodes.length; i++) {
		findWordsIn(wordsString,obj.childNodes[i]);		
	}
}
//===================================================================
//  
//  cutWordsFrom: cut the search words from text node and place each word in its own node 
//  
//===================================================================
function cutWordsFrom(wordsString,obj) {
	var txt = obj.data;
	if (txt.search(/\S/) == -1) {
		return;
	}
	
	var foundBgColorIndex = 0;
	for (var w = 0; w < wordsString.length; w++) {		// loop on words
		var word = wordsString[w];
		
		if (word.search(/\S/) == -1) {	// empty string
			continue;
		}
		
		var fcolorIndex = w % foundBgColor.length;
		var fcolor = foundBgColor[fcolorIndex];		// color for the word
		
		var indexOfWord = txt.indexOf(word);
		
		
		if (indexOfWord == -1) { // no match
			continue;
		}
		
		if (txt == word) {		// now we really color the word
			replaceWordNode(obj,fcolor,fcolorIndex);
			return;
		}
		if (indexOfWord + word.length < txt.length) {		// cut node after word if text continue
			var newTextNode = obj.splitText(indexOfWord + word.length);
			txt = obj.data;
			if (obj.nextSibling) {
				obj.parentNode.insertBefore(newTextNode,obj.nextSibling);
			}
			else {
				obj.parentNode.appendChild(newTextNode);
			}
			if (indexOfWord == 0) {		// text starts with the word so now obj.data==word
				replaceWordNode(obj,fcolor,fcolorIndex);
				return;
			}
		}
		if (indexOfWord > 0) {		// cut node before word
			var newTextNode = obj.splitText(indexOfWord);
			txt = obj.data;
			if (obj.nextSibling) {
				obj.parentNode.insertBefore(newTextNode,obj.nextSibling);
			}
			else {
				obj.parentNode.appendChild(newTextNode);
			}
		}
	}	// loop on words
}
//===================================================================
//  
//  replaceWordNode: replace word textNode with span and color (fcolor) 
//  
//===================================================================
function replaceWordNode(obj,fcolor,fcolorIndex) {
	//alert("replace "+obj.data+" class="+fcolorIndex);
	var span = document.createElement("span");
	span.name = colorSpanName;
	//span.style.color = "#000000";
	span.style.backgroundColor = fcolor;
	span.className = "wordFound wordFound"+fcolorIndex;
	span.appendChild(document.createTextNode(obj.data));
	obj.parentNode.replaceChild(span, obj);
}



