// --- Klasse Marker ---

function _markup() {
	var result = 0;
	var bodynode = this.doc.getElementsByTagName('body')[0];
	for (var c=0, mc=this.wordArr.length; c<mc; c++) {
		if (this.wordArr[c].length){
		  var hits = this.markTraverse(bodynode, eval('/\\b('+this.wordArr[c]+')\\b/i'))
			result += hits;
			if (hits) this.cnt++;
		}
	}
	return result;
}


function _unmark() {
	var spans = new Array();
	for (var s=this.doc.getElementsByTagName('span'), sc=s.length, c=0; c<sc; c++) {
		if (s[c].id == this.tag) spans.push(s[c]);
	}

	while (spans.length){
		var span = spans.pop();
		var text = span.firstChild.data;
		if (span.previousSibling && span.previousSibling.nodeType == 3) {
			text = span.previousSibling.data + text;
			span.parentNode.removeChild(span.previousSibling);
		}
		if (span.nextSibling && span.nextSibling.nodeType == 3){
			text += span.nextSibling.data;
			span.parentNode.removeChild(span.nextSibling);
		}
		span.parentNode.replaceChild(this.doc.createTextNode(text), span);
	}

	this.cnt=0;
	return false;
}


function _showquery() {
	this.message = this.doc.createElement('p');
	this.message.myMarker=this;
	this.message.onclick = this.hideQuery;
	this.message.style.border = '1px solid #287dc3';
	this.message.style.cursor = 'pointer';
	this.message.style.background = '#eeeeee';
	this.message.style.padding = '4px';
	this.message.style.paddingLeft = '93px';
	this.message.style.textIndent = '-88px';
	this.message.style.marginBottom = '4px';
	var msgspan = this.doc.createElement('span');
	msgspan.style.fontWeight = 'bold';
	msgspan.appendChild(this.doc.createTextNode(this.msg));

	var delspan = this.doc.createElement('div');
	delspan.style.color = '#287dc3';
	delspan.style.fontSize = 'small';
	delspan.style.textAlign = 'right';
	delspan.appendChild(this.doc.createTextNode(this.del));
	
	this.message.appendChild(msgspan);
	this.message.appendChild(this.doc.createTextNode(this.wordArr.join('  ')));
	this.message.appendChild(delspan);

	this.insertpos = this.doc.getElementById('markquery');
	this.parentpos = this.insertpos.parentNode;
	this.parentpos.replaceChild(this.message,this.insertpos);
}


function _hidequery() {
	var o=this.myMarker;	// im <p> hinterlegt
	o.parentpos.replaceChild(o.insertpos,o.message);
	o.unMark();
}


function _traverse(obj, regex){
	var childs = new Array();
	for (var t=obj.childNodes, tc=t.length, c=0; c<tc; c++) { childs.push(t[c]) }
	var hits=0;
	while (childs.length){
		var child = childs.pop();
    	if (child.nodeType == 1 && child.id != this.tag) { hits += this.markTraverse(child, regex) }
    	if (child.nodeType == 3) { hits += this.markColorize(child, regex) }
	}
	return hits;
}


function _colorize(obj, regex) {
	var hits=0;
   var text=obj.data;
	if (regex.test(text)) {
		var fragment = this.doc.createDocumentFragment();
		var chunks   = this.splitter(text, regex);
		var clength  = chunks.length;
		for (var count=0; count<clength; count++) {
			var chunk = chunks[count];
			if (chunk.length) {
				if (count % 2){
					hits++;
					var col = this.doc.createElement('span');
					col.id = this.tag;
					col.style.color = this.fg[this.cnt % this.fg.length];
					col.style.background = this.bg[this.cnt % this.bg.length];
					col.appendChild(this.doc.createTextNode(chunk));
				 	fragment.appendChild(col);
				} else{
					fragment.appendChild(this.doc.createTextNode(chunk))
				}
			}
		}
	  	obj.parentNode.replaceChild(fragment, obj);
	}
	return hits;
}


function _splitterMOZ(text, regex){
	return text.split(regex);
}


function _splitterMSIE(text, regex){
	var result = new Array();
	while (1) {
		var pos = text.search(regex);
		if (pos >= 0){
			var match = RegExp.$1;
			result.push(text.substr(0,pos));
			result.push(match);
			text = text.substr(pos+match.length);
		} else { 
			if (text.length) result.push(text);
			break;
		}
	}
	return result;
}


function _markiere() {
	if (this.wordArr.length) {
		this.showQuery();
		this.markUp();
	}
}

// Constructor

function Marker(docu,words) {
	this.fg  			= ['black','black','white','white','white','white','black','black'];
	this.bg  			= ['#99ff99','#ff66ff','#00aa00','#886800','#004699','#990099','#ffff66','#A0FFFF'];
	this.par 			= 'mark';
	this.tag 			= 'marked';
	this.msg 			= 'Suchbegriffe: ';
	this.del 			= 'Markierung aufheben.';
	this.cnt 			= 0;
	this.message		= 0;
	this.insertpos		= 0;
	this.parentpos		= 0;
	this.moz 			= (navigator.userAgent.indexOf('Gecko')>0);
	this.wordArr		= new Array(); this.wordArr=unescape(words).split(';');
	this.doc    		= docu;
	
	this.markUp   		= _markup;
	this.unMark   		= _unmark;
	this.showQuery		= _showquery;
	this.hideQuery		= _hidequery;
	this.markTraverse	= _traverse;
	this.markColorize	= _colorize;
	this.splitter		= (this.moz) ? _splitterMOZ : _splitterMSIE;
	this.markiere     = _markiere;
}

// --- Ende der Klasse Marker ---


function InitMarkWords(query) {
	if ( ((!document.createDocumentFragment) || (navigator.userAgent.indexOf('MSIE 5.0')>=0)) || 
  	     ((navigator.userAgent.indexOf('Mozilla/5')>=0) && (navigator.userAgent.indexOf('Win')>=0)) ) return "";
	var quotes  = /("[^"]*")/;
	var splitre = /[\[\]()*\\\/& ]+(\.[0-9]+)?/i;
	var chunks = (navigator.userAgent.indexOf('Gecko')>0) ? query.split(quotes) : _splitterMSIE(query,quotes);
	var marks = new Array();
	var clength = chunks.length;
	for (var count=0; count < clength; count++) {
		var chunk = chunks[count];
		if (chunk.length) {
			if (count % 2) {
				marks.push(chunk.substring(1,chunk.length-1));
   		} else {
      		var splits = chunk.split(splitre);
	  			while (splits.length) {
         		var chunk = splits.pop();
         		var ctest = chunk.toLowerCase();
         		if ((chunk.length && ctest.substr(0,3) != 'and') && (ctest.substr(0,2) != 'or') && 
						 (ctest.substr(0,3) != 'not') && (ctest.substr(0,4) != 'near'))  marks.push(chunk);
				}
			}
		}

	  	marks.reverse();
	  	return escape(marks.join(';'));
	}
}


function InitMarkup() {
	var p=location.search.indexOf('markup=');
	if (p>0) {
		var m=new Marker(self.document,InitMarkWords(unescape(location.search.substr(p+7,256))));
		m.markiere();
	}
}

