/*
 hilite v1.0
 @ Based on the orinial idea: highlight v3 by Johann Burkard
 @ autor: Ubaldo Cotta
 @ madrid, 24 november 2.009
 @ Licence: GPL 3 <http://www.gnu.org/licenses/gpl.html>

	This program is free software: you can redistribute it and/or modify
	it under the terms of the GNU General Public License as published by
	the Free Software Foundation, either version 3 of the License, or
	(at your option) any later version.

	This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.

	You should have received a copy of the GNU General Public License
	along with this program.  If not, see <http://www.gnu.org/licenses/>.


 Lastest versions: http://www.codigoabierto.info/plugins.html
 <mailto: ukamata@gmail.com>

 How to use this class?


Script example:
	jQuery.noConflict();
	jQuery(document).ready(function() {
		// Set our colors.
		jQuery('.myDiv').hiliteColors(Array('#faa', '#afa', '#aff'));
		// Remark the words
		jQuery('.myDiv').hilite("yellow");
		jQuery('.myDiv').hilite("cab");
	}

HTML Example:
	<div class='mydiv'>There is a cat in a yellow cab wating for a fish</div>

 */

jQuery.hilite = {
	posicion : -1,
	ultimo : null,
	colors : ['gold', 'chartreuse', 'skyblue', 'violet', 'darkorange', 'aquamarine', 'dodgerblue', 'fuchsia']
};

jQuery.fn.hilite = function(pat) {
	function cleanNode(node) {
		// Replace spanish characters
		node = node.replace('\xC1', 'A');
		node = node.replace('\xC9', 'E');
		node = node.replace('\xCD', 'I');
		node = node.replace('\xD3', 'O');
		node = node.replace('\xDA', 'U').replace('\xDC', 'U');
		return node;
	}
	function innerHilite(node, pat) {
		var skip = 0;
		if (node.nodeType == 3) {
			var pos = cleanNode(node.data.toUpperCase()).indexOf(pat);
			if (pos >= 0) {
				var spannode = document.createElement('span');
				spannode.style.background = jQuery.hilite.colors[jQuery.hilite.posicion];
				spannode.className = 'hiliteMark';
				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 += innerHilite(node.childNodes[i], pat);
			}
		}
		return skip;
	}

	return this.each(function() {
		if (jQuery.hilite.ultimo != pat) {
			jQuery.hilite.ultimo = pat;
			jQuery.hilite.posicion++;
			// Restart counter if we already used all colors in the array.
			if (jQuery.hilite.posicion > jQuery.hilite.colors.length)
				jQuery.hilite.posicion = 0;
		}
		innerHilite(this, cleanNode(pat.toUpperCase()));
	});
};

jQuery.fn.hiliteColors = function(colors) {
	jQuery.hilite.colors = colors;
}

jQuery.fn.removeHilite = function() {
	return this.find("span.highlight").each(function() {
		this.parentNode.firstChild.nodeName;
		with (this.parentNode) {
			replaceChild(this.firstChild, this);
			normalize();
		}
	}).end();
};
