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();
  //alert(tempNodeVal);
	    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\ig/)) { return; }
			    checkn = checkn.parentNode;
		    }
		    if (pn.className != "hl") {
			    // 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 = "hl";
			    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();
		    }
	    }
    }
  },

  googleSearchHighlight: function() {
    var ref = parent.window.location.search;
	if (ref.indexOf('?') == -1) ref = parent.window.location.zoom_query;
    if (ref.indexOf('?') == -1) return;
    var qs = ref.substr(ref.indexOf('?')+1);
    var qsa = qs.split('&');
    for (var i=0;i<qsa.length;i++) {
	    var qsip = qsa[i].split('=');
      if (qsip.length == 1) continue;
      if (qsip[0] == 'Search' || qsip[0] == 'zoom_query') { // q= for Google, p= for Yahoo
		    var wordstring = unescape(qsip[1].replace(/\+/g,' '));
		    searchhi.process(wordstring);
      }
    }
  },
  
  process: function(wordstring) {
    if (wordstring.length>=3)
    {
    searchhi.found = 0;
    var words = wordstring.split(/\s+/);
    //alert(document.getElementById("content"));
    for (w=0;w<words.length;w++) {
	    searchhi.highlightWord(document.getElementById("content"),words[w]);
    }
    if (searchhi.found === 0) {
      searchhi.nohits();
    }
    }
  },
  
  nohits: function() {
  },
  
  init: function() {
    if (!document.createElement || !document.getElementsByTagName) return;
    // hook up forms of type searchhi
    var frms = document.getElementsByTagName("form");
    for (var i=0; i<frms.length; i++) {
      if (frms[i].className.match(/\bsearchhi\b\ig/)) {
        frms[i].onsubmit = function() {
          var inps = this.getElementsByTagName("input");
          for (var j=0; j<inps.length; j++) {
            if (inps[j].type == "text") {
	       if (inps[i].value.length >=3)
		{
              searchhi.process(inps[j].value);
		}
              return false;
            }
          }
        };
      }
    }
    // highlight search engine referrer results
    searchhi.googleSearchHighlight();
  }
};

(function(i) {
var u =navigator.userAgent;
var e=/*@cc_on!@*/false; 
var st = setTimeout;
if(/webkit/i.test(u))
{
    st(function(){
        var dr=document.readyState;
        if(dr=="loaded"||dr=="complete")
        {
            i()
        }
        else
        {
            st(arguments.callee,10);
         }
    },10);}
else if((/mozilla/i.test(u)&&!/(compati)/.test(u)) || (/opera/i.test(u)))
{
    document.addEventListener("DOMContentLoaded",i,false); 
} 
else if(e)
{     
    (function(){
        var t=document.createElement('doc:rdy');
        try{
            t.doScroll('left');
            i();t=null;
        }
        catch(e){
            st(arguments.callee,0);
        }
     })();
}
else
{
    window.onload=i;
}
})(searchhi.init);

