/*

Customized version of jQuery Highlight plugin for RealDecoy.com

http://www.unwrongest.com/projects/highlight/

*/

(function($){ 
    $.fn.extend({
        highlight: function(strings) {
             
             function findText(node, string) {
                  if (node.nodeType == 3)
                        return searchText(node, string);        
                  else if (node.nodeType == 1 && node.childNodes && !(/(script|style)/i.test(node.tagName))) {
                       for (var i = 0; i < node.childNodes.length; ++i) {
                        i += findText(node.childNodes[i], string);
                       }
                  }
                  return 0;
      
             }
 
               function searchText(node, string){
                  var position = node.data.toLowerCase().indexOf(string);
                   if (position >= 0)
                    return highlight(node, position, string);
                else
                    return 0;
              }
      
               function highlight(node, position, string){
                string = string.split(' ').join('-');
                var anchornode = document.createElement('a');
                anchornode.className = 'highlight';
                anchornode.href = '#' + string;
                var middlebit = node.splitText(position);
                var endbit = middlebit.splitText(string.length);
                var middleclone = middlebit.cloneNode(true);
                anchornode.appendChild(middleclone);
                middlebit.parentNode.replaceChild(anchornode, middlebit);
                 return 1;
             }
 
             return this.each(function() {
                 if(typeof strings == 'string')
                     findText(this, strings.toLowerCase());    
                 else
                     for (var i = 0; i < strings.length; ++i) findText(this, strings[i].toLowerCase());    
             });
        }
    }); 
})(jQuery);