//
// 2006 by bitart; Author: Alexander Graf
//
// Copyright by bitart - Furch & Graf GbR, Gartenstr. 5 56220 St. Sebastian,
// Germany. All rights reserved.
// Copyright in this software is owned by bitart, unless otherwise indicated.
//
// You are not allowed to distribute this software in any form or use it as
// part of another project without asking the owner for permission to use.
//

var highlight_chars = 'a-z0-9äöüÄÖÜß#$%@^_¡¢£¤¥¦§¨©ª«¬­®¯°±²³´µ¶¸¹º»¼½¾¿ÀÁÂÄÅÆÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÖ×ØÙÚÛÜÝÞßàáâãäåæçèéêëìíîïðñòóôõö÷øùúûüýþ';

var highlight_regex = null;
var highlight_split = null;
var highlight_clean = null;
var highlight_areas = Array('Tree','List','Result','Entry');
var highlight_fgcol = Array('#000000','#000000','#ffffff','#ffffff','#ffffff','#ffffff','#000000','#000000');
var highlight_bgcol = Array('#99ff99','#ff66ff','#00aa00','#886800','#004699','#990099','#ffff66','#a0ffff');

function setHighlight(regex_string)
{
	try {
		if (document.createDocumentFragment){
			if (regex_string){
				highlight_regex = eval('/^'+regex_string+'$/i');
			}else{
				highlight_regex = null;
			}
		}
	} catch(e) {
		highlight_regex = null;
	}
}

function initHighlight()
{
	try {
		if (highlight_split === null){
			highlight_split = eval('/[^'+highlight_chars+']+/i');
			highlight_clean = eval('/[^'+highlight_chars+']+/ig');
		}
		if (top.highlight_regex !== null){
			var nodes = Array();
			for (var cnt=0; cnt<highlight_areas.length; cnt++){
				var node = document.getElementById(highlight_areas[cnt]);
				if (node) nodes.push(node);
			}
			while (nodes.length){
				var node = nodes.pop();
				if (node.nodeType == 1){
					for (var t=node.childNodes, tc=t.length, c=0; c<tc; c++) nodes.push(t[c]);
				}else if (node.nodeType == 3 && node.data.replace(highlight_clean,'')){
					var res  = document.createDocumentFragment();
					var text = highlightSplitter(node.data);
					var hit  = null;
					var repl = false;
					var add  = '';
					for (var cnt=0; cnt<text.length; cnt+=2){
						if (hit = top.highlight_regex.exec(text[cnt])){
							repl = true;
							if (add.length){
								res.appendChild(document.createTextNode(add));
								add = '';
							}
							for (var col=2; col<hit.length; col++) if (hit[col]) break;
							var mark = document.createElement('span');
							mark.style.color = highlight_fgcol[(col-2)%highlight_fgcol.length];
							mark.style.background = highlight_bgcol[(col-2)%highlight_bgcol.length];
							mark.appendChild(document.createTextNode(text[cnt]));
							res.appendChild(mark);
							add += text[cnt+1];
						}else{
							add += text[cnt]+text[cnt+1];
						}
					}
					if (add.length) res.appendChild(document.createTextNode(add));
					if (repl) node.parentNode.replaceChild(res, node);
				}
			}
		}
	} catch (e) {}
}

function highlightSplitter(text){
	var res = new Array();
	if (highlight_split){
		var hit = null;
		while (hit = highlight_split.exec(text)){
			res.push(text.substr(0,hit.index));
			res.push(hit[0]);
			text = text.substr(hit.index + hit[0].length);
		}
		if (text.length){
			res.push(text);
			res.push('');
		}
	}
	return res;
};

