$(function() {
	if(typeof highlightWords == "undefined" || highlightWords.length == 0) return;
	var artNode = document.getElementById("article-block");
	if(!artNode) return;
	
	var className = "highlight";
	
	var HL = function(node, word) {
		if(
			node.className == className
			||
			node.tagName && node.tagName.toLowerCase() == 'h1'
			||
			node.tagName && node.className == 'subtitle'
		) return;
		if(node.hasChildNodes)
			for(var i=0; i<node.childNodes.length; i++)
				HL(node.childNodes[i], word);
		if(node.nodeType == 3) { // text node
			var val = node.nodeValue;
			//if(val.indexOf(word) == -1) return;
			var found = node.nodeValue.match(new RegExp(word, "g"));
			if(!found) return;
			var parent = node.parentNode;
			for(var i = 0; i < found.length; i++) {
				var hiword = document.createElement("span");
				hiword.className = className;
				hiword.appendChild(document.createTextNode(found[i]));
				var ni = val.indexOf(found[i]);
				parent.insertBefore(document.createTextNode(val.substr(0, ni)), node);
				parent.insertBefore(hiword, node);
				val = val.substr(ni + found[i].length);
			}
			parent.insertBefore(document.createTextNode(val), node);
			parent.removeChild(node);
		}
	};
	
	for(var i=0; i<highlightWords.length; i++) HL(artNode, highlightWords[i]);
	
});