/*

highlight

Highlights arbitrary terms.

<http://johannburkard.de/blog/programming/javascript/highlight-javascript-text-higlighting-jquery-plugin.html>

MIT license.

Johann Burkard
<http://johannburkard.de>
<mailto:jb@eaio.com>

Function highlightSearchTerm written by JR (Software & Friends GmbH). Call
this function from the onload handler of a page or from the jQuery(function($) {...}
block. Modify it to highlight text in different parts of the page. Optimized
for simple Lucene queries, but does not support the full range of the Lucene
query syntax.
*/

jQuery.fn.highlight = function(pat) {
 function innerHighlight(node, pat) {
  var skip = 0;
  if (node.nodeType == 3) {
   var pos = node.data.toUpperCase().indexOf(pat);
   if (pos >= 0) {
    var spannode = document.createElement('span');
    spannode.className = 'highlight';
    var middlebit = node.splitText(pos);
    var endbit = middlebit.splitText(pat.length);
    var middleclone = middlebit.cloneNode(true);
    spannode.appendChild(middleclone);
    middlebit.parentNode.replaceChild(spannode, middlebit);
    skip = 1;
   }
  }
  else if (node.nodeType == 1 && node.childNodes && !/(script|style)/i.test(node.tagName)) {
   for (var i = 0; i < node.childNodes.length; ++i) {
    i += innerHighlight(node.childNodes[i], pat);
   }
  }
  return skip;
 }
 return this.each(function() {
  innerHighlight(this, pat.toUpperCase());
 });
};

jQuery.fn.removeHighlight = function() {
 return this.find("span.highlight").each(function() {
  this.parentNode.firstChild.nodeName;
  with (this.parentNode) {
   replaceChild(this.firstChild, this);
   normalize();
  }
 }).end();
};




/**
 * Highlights the given term in the article text or search results. The
 * function is optimized for a Lucene search query, but it does not support
 * the full range of the Lucene query syntax.
 * 
 * Supported:
 * - Lucene operator AND (as AND or as '+' in front of a word)
 * - Lucene operator OR (as OR or as spaces between words)
 * - Lucene operator NOT (as NOT or as '-' in front of a word)
 * 
 * Not Supported:
 * - grouping with '"..."'
 * - logical grouping with '(...)'
 * - masking of Lucene operators with '\'
 * - Lucene wildcards '*' and '?'
 * - Lucene fuzzy operator, word range etc.
 * 
 * Basically the function will highlight words given in the parameter
 * individually, without the spaces between them, even if they are not right
 * next to each other (= OR logic). It tries not to highlight Lucene operators
 * and won't highlight parts of the query which are marked as "NOT" (of
 * course, they shouldn't be on the page anyway). It does not support
 * grouping, fuzzy searches, wildcards etc. In these cases, the highlighting
 * will simply not work or might, in rare cases, highlight the wrong content.
 * 
 * param argSearchTerm The search term or search query.
 */
function highlightSearchTerm(argSearchTerm) {
  if (argSearchTerm && (argSearchTerm != '')) {
    argSearchTerm = argSearchTerm.replace(/\"/g, ""); // we don't support grouping with "..." yet
    var terms = argSearchTerm.split(" ");
    for (i = 0; i < terms.length; i++) {
      var term = terms[i];
      if (term == '') {
        continue;
      }
      if ((term == 'AND') || (term == 'OR')) {
        // Lucene operators, ignore
        continue;
      } else if (term == 'NOT') {
        // Lucene NOT operator, ignore term after this
        i++;
        continue;
      } else if ((term.length > 1) && (term.charAt(0) == '-')) {
        // Lucene NOT operator, ignore term
        continue;
      } else {
        if ((term.length > 1) && (term.charAt(0) == '+')) {
          // Lucene AND operator, remove operator
          term = term.substr(1);
        }
        term = term.toUpperCase(); // needed for the script to work
        $('.liz_SearchResultHit').highlight(term);
      }
    } /* end for terms */
  } /* end if search term */
}

