KeyWords = {
}
KeyWords.walk = function(node, depth, textproc) {
	var skipre = /^(script|style|textarea|a|select)/i
	var count = 0;
	while (node && depth > 0) {
		count++;
		if (count >= 1000) {
			var handler = function() {
				KeyWords.walk(node, depth, textproc);
				setTimeout(handler, 50);
				return;
			}
		}

		if (node.nodeType == 1) { // ELEMENT_NODE
			if (!skipre.test(node.tagName) && node.childNodes.length > 0) {
				node = node.childNodes[0];
				depth++;
				continue;
			}
		} else if (node.nodeType == 3) { // TEXT_NODE
			node = textproc(node, 0);
		}

		if (node.nextSibling) {
			node = node.nextSibling;
		} else {
			while (depth > 0) {
				node = node.parentNode;
				depth--;
				if (node.nextSibling) {
					node = node.nextSibling;
					break;
				}
			}
		}
	}
}
KeyWords.hilite = function(lang, id) {
	var el = $(id);
	if (!el) {
		return;
	}

	new Ajax.Request('/' + lang + '/keywords', {
			method: 'get',
			onSuccess: function(transport) {
				var resp = transport.responseText;
				if (!resp) {
					return;
				}
				var keywords = new Array();
				var data = new Array();
				var rows = resp.split('\n');
				for (var i=0; i<rows.length; i++) {
					var parts = rows[i].split('||');
					if (parts.length != 4) {
						continue;
					}
					if (!parts[0]) {
						continue;
					}
					keywords.push(parts[0]);
					data[parts[0].toLowerCase()] = [parts[1], parts[2], parts[3]];
				}
				KeyWords.show(el, keywords, data);
			},
			onFailure: function() {
			}
		});
}
KeyWords.show = function(el, keywords, data) {
	var charre = '[_0-9A-Za-z\u00c0-\u00de\u00df-\u00ff\u0150\u0170\u0151\u0171\u0100-\u017e\u0410-\u044f\u0401\u0451]';
	var charne = '[^_0-9A-Za-z\u00c0-\u00de\u00df-\u00ff\u0150\u0170\u0151\u0171\u0100-\u017e\u0410-\u044f\u0401\u0451]';
	// \\-
	var qre = new Array();
	for (var i=0; i<keywords.length; i++) {
		//qre.push('\\b' + keywords[i] + '\\b');
		//qre.push('\\b' + keywords[i] + '(?=$|' + charne + ')');
		qre.push(keywords[i] + '(?=$|' + charne + ')');
	}

	qre = new RegExp(qre.join('|'), 'im');
	cre = new RegExp(charre, 'im');

	var textproc = function(node, offset) {
		var text = node.data.substring(offset);
		//var match = qre.exec(node.data);
		var match = qre.exec(text);
		if (match) {
			var index = match.index;
			var idx = offset + index - 1;
			if (idx > 0 && cre.exec(node.data.substring(idx, idx+1))) {
				return textproc(node, offset + index + 1);
			}
			var val = match[0];
			var key = val.toLowerCase();
			var k = '';
			var node2 = node.splitText(offset + index);
			var node3 = node2.splitText(val.length);
			var span = node.ownerDocument.createElement('SPAN');
			node.parentNode.replaceChild(span, node2);
			span.className = 'keyword';
			span.onmouseover = function() {
				var url = data[key][0];
				var title = data[key][1];
				var txt = data[key][2];
				var text = '';
				if (url) {
					if (!title) {
						title = url;
					}
					text += '<a href="' + url + '">' + title + '</a><br />';
				} else if (title) {
					text += '<b>'  + title + '</b><br />';
				}
				if (txt) {
					text += '<span>' + txt + '</span>';
				}
				Tip(text, STICKY, true, DURATION, 3000, CLICKCLOSE, true, ABOVE, true, FADEIN, 300, FADEOUT, 300);
			}
			span.appendChild(node2);
			return span;
		} else {
			return node;
		}
	}
	KeyWords.walk(el.childNodes[0], 1, textproc);
}