/** from: http://eriwen.com/javascript/highlight-search-results-with-js/ **/

function highlightOnLoad(location) {
  // Get search string
  if (/s\=/.test(window.location.search)) {
    var searchString = getSearchString();
    // Starting node, parent to all nodes you want to search
    var textContainerNode = document.getElementById(location);

    // Informational message for search
    var searchInfo = 'Search Results for: ';

    // Split search terms on '|' and iterate over resulting array
    var searchTerms = searchString.split('|');
    for (var i in searchTerms) 	{
      // The regex is the secret, it prevents text within tag declarations to be affected
      var regex = new RegExp(">([^<]*)?("+searchTerms[i]+")([^>]*)?<","ig");

      highlightTextNodes(textContainerNode, regex, i);
      // Add to info-string
      searchInfo += ' <span class="highlighted term'+i+'">'+searchTerms[i]+'</span> ';
    }

    // Create div describing the search
    //var searchTermDiv = document.createElement("H2");
    //searchTermDiv.className = 'searchterms';
    //searchTermDiv.innerHTML = searchInfo;

    // Insert as very first child in searched node
    //textContainerNode.insertBefore(searchTermDiv, textContainerNode.childNodes[0]);



  }
}

function replaceUmlauts(string, index){
	
	var anArray = new Array(2);
	anArray[0] = new Array("Ö", "ö", "Ä", "ä", "Ü", "ü","XSQoE","XSQaE","XSQuE");
	anArray[1] = new Array("&Ouml;", "&ouml;", "&Auml;", "&auml;", "&Uuml;", "&uuml;","&ouml;", "&auml;", "&uuml;");    
	for (var i=0; i<anArray[index].length; i++){
		myRegExp = new RegExp(anArray[index][i],"g");
		string = string.replace(myRegExp, anArray[(index==0?1:0)][i]);
	}

	if(string.match(/\u00e4/g)) {
		string = string.replace(/\u00e4/, "&auml;");
	} else if (string.match(/\u00fc/g)) {
		string = string.replace(/\u00fc/, "&uuml;");
	} else  if(string.match(/\u00f6/g)) {
		string = string.replace(/\u00f6/, "&ouml;");
	} 
	return string;
}



// Pull the search string out of the URL
function getSearchString() {
  // Return sanitized search string if it exists
  var rawSearchString = window.location.search.replace(/[a-zA-Z0-9\?\&\=\%\#]+s\=(\w+)(\&.*)?/,"$1");


  rawSearchString = replaceUmlauts(rawSearchString,0);
  //alert(rawSearchString);


  // Replace '+' with '|' for regex
  // Also replace '%20' if your cms/blog uses this instead (credit to erlando for adding this)
  


  return rawSearchString.replace(/\%20|\+/g,"\|");
}

  function highlightTextNodes(element, regex, termid) {
    var tempinnerHTML = element.innerHTML;
    // Do regex replace
    // Inject span with class of 'highlighted termX' for google style highlighting
    element.innerHTML = tempinnerHTML.replace(regex,'>$1<span class="highlighted term'+termid+'">$2</span>$3<');
  }



// Call this onload, I recommend using the function defined at: http://untruths.org/technology/javascript-windowonload/
//addOnLoad(highlightOnLoad());




