/*
 *	Name: Highlight.
 *	Description: Highlights a keyword on the page.
 */
function highlight(keywords, container, fgColour, bgColour)
{
	if (!keywords) return;

	var keyword_split = keywords.split(',');
	for (var i = 0; i < keyword_split.length; i++)
		highlightKeyword(keyword_split[i], container, fgColour, bgColour);
}

function highlightKeyword(keyword, container, fgColour, bgColour)
{
	if (!keyword) return;
	
	var keyword_low = keyword.toLowerCase();
	for (var i = 0; i < container.childNodes.length; i++)
	{
		var node = container.childNodes[i];
		if (node.nodeType == 3)
		{
			var data = node.data;
			var data_low = data.toLowerCase();
			if (data_low.indexOf(keyword_low) != -1)
			{
				// Item found.
				var fontSize = node.parentNode.style.fontSize;
				var new_node = document.createElement('SPAN');
				new_node.name = "highlight";
				new_node.style.fontSize = fontSize;
				node.parentNode.replaceChild(new_node, node);
				var result;
				while ((result = data_low.indexOf(keyword_low)) != -1)
				{
					var child_node = document.createElement('SPAN');
					child_node.name = "highlight";
					child_node.style.backgroundColor = bgColour;
					child_node.style.color = fgColour;
					child_node.style.fontSize = fontSize;
					child_node.appendChild(document.createTextNode(data.substr(result,keyword.length)));
					
					new_node.appendChild(document.createTextNode(data.substr(0, result)));
					new_node.appendChild(child_node);
					data = data.substr(result + keyword.length);
					data_low = data_low.substr(result + keyword.length);
				}
				
				new_node.appendChild(document.createTextNode(data));
			}
		}
		else
		{
			// Recurse
			highlight(keyword, node, fgColour, bgColour);
		}
	}
}

function updateHighlight(container)
{
	var fontSize = container.style.fontSize;
	var nodes = document.getElementsByTagName("SPAN");
	for (var i = 0; i < nodes.length; i++)
		if (nodes[i].name = 'highlight')
			nodes[i].style.fontSize = fontSize;
}

