
var gLastWordHighlighted = "";	// global to save the last word that was highlighted, so we can easily un-highlight it
var cleanContentInnerHTML = "";

function highlightOnClick(tagCloudWord) {
	var textContainerNode = document.getElementById("content");

	// First un-highlight any previously highlighted terms
	if (gLastWordHighlighted != "") {
		unHighlightTextNodes(textContainerNode, gLastWordHighlighted);
	}

	// only highlight the newly selected word if it's not the same as the old word
	if (gLastWordHighlighted == tagCloudWord) {
		gLastWordHighlighted = "";
	} else {
		gLastWordHighlighted = tagCloudWord;
		cleanContentInnerHTML = textContainerNode.innerHTML;
		searchhi.highlightWord(textContainerNode,tagCloudWord);
	}
}

function unHighlightTextNodes(element, word) {
  element.innerHTML = cleanContentInnerHTML;
  cleanContentInnerHTML = "";
  /*
  OLD METHOD WHICH FAILED ON IE
  var tempinnerHTML = element.innerHTML;
  // Do regex replace
  var regex = new RegExp("<span class=\"highlighted\">("+word+")</span>", "ig");

  // Remove any <span> tags with class of 'highlighted term0'
  element.innerHTML = tempinnerHTML.replace(regex,'$1');
  */
}

/*************************************************************************/
/* COW modified the following code by only keeping the highlightWord function. The
 * rest of the functions weren't needed for our purposes.
 *
 * Kudos go to the following person: 
 */
/* http://www.kryogenix.org/code/browser/searchhi/ */
/* Modified 20021006 to fix query string parsing and add case insensitivity */
/* Modified 20070316 to stop highlighting inside nosearchhi nodes */
/* Modified 20081217 to do in-page searching and wrap up in an object */
/* Modified 20081218 to scroll to first hit like 
   http://www.woolyss.free.fr/js/searchhi_Woolyss.js and say when not found */

searchhi = {
  highlightWord: function(node,word) {
    // Iterate into this nodes childNodes
    if (node.hasChildNodes) {
	    for (var hi_cn=0;hi_cn<node.childNodes.length;hi_cn++) {
		    searchhi.highlightWord(node.childNodes[hi_cn],word);
	    }
    }

    // And do this node itself
    if (node.nodeType == 3) { // text node
	    tempNodeVal = node.nodeValue.toLowerCase();
	    tempWordVal = word.toLowerCase();
	    if (tempNodeVal.indexOf(tempWordVal) != -1) {
		    var pn = node.parentNode;
		    // check if we're inside a "nosearchhi" zone
		    var checkn = pn;
		    while (checkn.nodeType != 9 && 
		    checkn.nodeName.toLowerCase() != 'body') { 
		    // 9 = top of doc
			    if (checkn.className.match(/\bnosearchhi\b/)) { return; }
			    checkn = checkn.parentNode;
		    }
		    if (pn.className != "highlighted") {
			    // word has not already been highlighted!
			    var nv = node.nodeValue;
			    var ni = tempNodeVal.indexOf(tempWordVal);
			    // Create a load of replacement nodes
			    var before = document.createTextNode(nv.substr(0,ni));
			    var docWordVal = nv.substr(ni,word.length);
			    var after = document.createTextNode(nv.substr(ni+word.length));
			    var hiwordtext = document.createTextNode(docWordVal);
			    var hiword = document.createElement("span");
			    hiword.className = "highlighted";
			    hiword.appendChild(hiwordtext);
			    pn.insertBefore(before,node);
			    pn.insertBefore(hiword,node);
			    pn.insertBefore(after,node);
			    pn.removeChild(node);
			    searchhi.found += 1;
			    if (searchhi.found == 1) pn.scrollIntoView();
		    }
	    }
    }
  }
};

/*************************************************************************/
/* end of searchhi code */
