/* 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 != "searchword") {
			    // 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 = "searchword";
			    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 = document.referrer;
    var qs;
    if (ref.indexOf('?') == -1) {
	  qs = window.location.search.substring(1);
	  if (qs == null) {
	    return;
      }
    }
    else {
      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] == 'q' || qsip[0] == 'p' || qsip[0] == 'dig') { // q= for Google, p= for Yahoo, d=our search
		    var wordstring = unescape(qsip[1].replace(/\+/g,' '));
		    searchhi.process(wordstring);
      }
    }
  },
  
  process: function(wordstring) {
    searchhi.found = 0;
    var words = wordstring.split(/\s+/);
	var sorted = words.sort(sortWords); //Dee added
	/*
    for (w=0;w<words.length;w++) {
	    searchhi.highlightWord(document.getElementsByTagName("body")[0],words[w]);
    }
	*/
    for (w=0;w<sorted.length;w++) {
		if(sorted[w] == 'a') continue; //Don't highlight a
	    searchhi.highlightWord(document.getElementsByTagName("body")[0],
							   sorted[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/)) {
        frms[i].onsubmit = function() {
          var inps = this.getElementsByTagName("input");
          for (var j=0; j<inps.length; j++) {
            if (inps[j].type == "text") {
              searchhi.process(inps[j].value);
              return false;
            }
          }
        };
      }
    }
    // highlight search engine referrer results
    searchhi.googleSearchHighlight();
  }
};

function sortWords (a, b) {
  /*
  Highlight shorter words first, that way the whole word is highlighted.
  If you search for brother+he, and highlight brother and then highlight
  he, just the "he" in brother will be highlighted. If he was higlighted 
  first, then brother would be correctly highlighted. Note, highlight order
  does not appear to be the order in the search string.
  */
	if (a.length <= b.length) {
		return false;
	}
    else {
	  return true;
	}
}

(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);

