/**
 * 
 * jQuery 1.4.2 or greater is required for this
 * 
 */

jQuery.fn.highlight = function(term, className, removeOld) {			
			
	// remove any old highlights
	if(removeOld) {
		$('span.'+className).each(function() {
			$(this).after($(this).html()).remove();
		});
	}
			
	// highlight new terms
	var regex = new RegExp(term, 'gi');
	return this.each(
		function() {
			$(this).contents().filter(
					function() {
						return this.nodeType == 3 && regex.test(this.nodeValue);
					}).replaceWith(
						function() { 
							return (this.nodeValue || "").replace(regex, 
								function(match) {
									return '<span class="'+className+'">'+match+'</span>'; 
								});
						});
		});
};



