/**
 * jQuery Highlight - A jQuery highlighting plugin
 * 
 * @version 1.0
 * @date    2008-11-15
 * 
 * Copyright (c) 2008 Trey Shugart (shugartweb.com/jquery/)
 * 
 * Dual licensed under: 
 *  MIT - (http://www.opensource.org/licenses/mit-license.php) 
 *  GPL - (http://www.gnu.org/licenses/gpl.txt)
 */
;(function($) {
	
	$.highlight = {};
	$.highlight.dataKey = 'highlight';
	$.highlight.options = {
			className: 'jquery-highlight',
			wrapper: 'span'
		};
	
	$.fn.highlight = function(str, options) {
		if ($.trim(str) === '')
			return false;
		var options = $.extend($.highlight.options, options);
		var style = ' class="' + options.className + '"';
		return $(this).each(function() {
			var $this = $(this);
			$this.data($.highlight.dataKey + '.options', options);
			$this.html($this.html().replace(new RegExp('(?![^<]+>)(' + str + ')(?![^<]+>)', 'gi'), '<' + options.wrapper + style + '>$1</' + options.wrapper + '>'));
		});
	}
	
	$.fn.unhighlight = function() {
		return $(this).each(function() {
			var $parent = $(this);
			var options = $parent.data($.highlight.dataKey + '.options') || $.highlight.options;
			var $children = $parent.find('.' + options.className);
			$children.each(function() {
				var $child = $(this);
				var txt = $child.text();
				$child.replaceWith(txt);
			});
		});
	}
	
})(jQuery);
