/*

highlight v3

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>

**** modified by jah for PNC cable project to only highlight the first instance of a word...also made it 
integrate with the qtip jquery plugin...

*/

jQuery.fn.highlight = function(pat) {

    var found = false;
 
    function innerHighlight(node, pat) {
        var skip = 0;


  if (node.nodeType == 3) {
  
   var pos = node.data.toUpperCase().indexOf(pat);
   
   if (pos >= 0 && !found) {
   
        checkParentage(node);
   
        var applyTag = true;
   
        if (pos > 0) {
        
            var bitBefore = node.data.substring(pos - 1, pos);
            
            if (bitBefore != " ") {
                applyTag = false;
            }
            
        }
        
        var posAfter = pos + pat.length;
        
     //   alert("ends at: " + posAfter);
      //  alert("node length: " + node.data.length);
        
        var bitAfter = node.data.substring(posAfter, posAfter + 1);
        
        if (bitAfter != "" && bitAfter != " ") {
            applyTag = false;
        }
        
        if (checkParentage(node) != 0) {
            applyTag = false;
        }
        
        if (applyTag) {

            var spannode = document.createElement('span');
            spannode.className = 'glossaryHighlight';
            var middlebit = node.splitText(pos);
            
            var endbit = middlebit.splitText(pat.length);
            var middleclone = middlebit.cloneNode(true);
    
            //alert($(middleclone).text());
    
            spannode.appendChild(middleclone);
    
                $(spannode).qtip({
                    content: {
                    url: '/_displayTips/?',
                    data: { w: pat.toLowerCase() },
                    method: 'get',
                    title: {
                        text: pat.toLowerCase()
                    }
                },
                show: {solo:true,
                when: {
                    event:'mouseover'
                    },
                effect: {
                type:'fade'
                }                    
                },
                hide: {
                fixed: false,
                delay: 1500,
                event: 'inactive',
                effect: {
                type:'fade'
                } 
            },
            position: {
                target: 'mouse',
                adjust:{screen:true, resize:true}
            }            
        
        });
        
        middlebit.parentNode.replaceChild(spannode, middlebit);
        skip = 1;
        found = true;
    }
   }
  }
  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();
};

function checkParentage(thisNode) {

    var countBadMatches = 0;

    countBadMatches = $(thisNode).parents().filter('a').length;
    countBadMatches = countBadMatches + $(thisNode).parents().filter('h1').length;
    countBadMatches = countBadMatches + $(thisNode).parents().filter('h2').length;
    countBadMatches = countBadMatches + $(thisNode).parents().filter('h3').length;        
    
    return countBadMatches;

}