/* http://www.kryogenix.org/code/browser/searchhi/ */
/* Modified  by webdesign.weisshart.de/  28.03.06*/

var ref = "";

function highlightWord(node,word) {
	// Iterate into this nodes childNodes
	if (node.hasChildNodes) {
		var hi_cn;
		for (hi_cn=0;hi_cn<node.childNodes.length;hi_cn++) {
			highlightWord(node.childNodes[hi_cn],word);
		}
	}

	// And do this node itself
	if (node.nodeType == 3) { // text node
		tempNodeVal = node.nodeValue.toLowerCase();
		tempWordVal = word.toLowerCase();
		if (tempNodeVal.indexOf(tempWordVal) != -1) {
			pn = node.parentNode;
			klasse = "searchword"+w; // different colors for differnt searchterms

			if (pn.className != klasse) {
				// word has not already been highlighted!
				nv = node.nodeValue;
				ni = tempNodeVal.indexOf(tempWordVal);
				// Create a load of replacement nodes
				before = document.createTextNode(nv.substr(0,ni));
				docWordVal = nv.substr(ni,word.length);
				after = document.createTextNode(nv.substr(ni+word.length));
				hiwordtext = document.createTextNode(docWordVal);
				hiword = document.createElement("em"); // modified from span to em

				hiword.className = klasse;
				hiword.appendChild(hiwordtext);
				pn.insertBefore(before,node);
				pn.insertBefore(hiword,node);
				pn.insertBefore(after,node);
				pn.removeChild(node);
			}
		}
	}
}

function Highlight() {
	if (!document.createElement) return;

	// added for site search
	if (window.location.search) ref = unescape(window.location.search);

	if (document.referrer.search(/google.+/) != -1 || document.referrer.search(/lycos.+/) != -1 || document.referrer.search(/yahoo.+/) != -1 || document.referrer.search(/fireball.+/) != -1 ||document.referrer.search(/search\.msn.+/) != -1 ) {
		ref = decodeURIComponent(document.referrer);
		if (ref.indexOf('?') == -1) return;
	}

	// for site search:
	// if your search file is not called suchen.*, you must modify the following line 2x!
	if (document.referrer.search(/results.+/) != -1 && document.URL.indexOf("results") == -1 ) {
		ref = unescape(document.referrer);
		if (window.location.search) ref = unescape(window.location.search);
	}

	//if (ref.indexOf('?') == -1) return;

	qs = ref.substr(ref.indexOf('?')+1);
	qsa = qs.split('&');

	for (i=0;i<qsa.length;i++) {
		qsip = qsa[i].split('=');
	        if (qsip.length == 1 || qsip.length == 5) continue;
        	if (qsip[0] == 'q' || qsip[0] == 'query' ||qsip[0] == 'p' || qsip[0] == 's') { // q= for Google, p= for Yahoo, query= Fireball, Lycos etc., s= for wordpress
	        if (qsip[1].length < 3 ) continue;


			// words not to be highlighted:
			qsip[1] = qsip[1].replace(/\"|\'|\*|;|\bbei\s*\b|\bfur\s*\b|\b.n\s*\b|\bvon\s*\b|\bnicht\s*\b|\bde.\s*\b|\bdie\s*\b|\bdas\s*\b|\bauf\s*\b|\ein\b|\bund\s*\b|\bwie\s*\b|\bkann\s*\b|\bich\s*\b|\bman\s*\b|\bmit\s*\b|\ist\s*\b|Suchbegriff/gi,'');

			qsip[1] = qsip[1].replace(/Suchbegriff/gi,'');
                        if (document.URL.indexOf('blog') >= 1) qsip[1] = qsip[1].replace(/\d\d|\d/g,''); // 1/2-digit number in blogs

			// remove all blanks and '+' before and after searchterm (bugfix: crashes FF & Op)
			qsip[1] = qsip[1].replace(/^(\s+|\++)/,'').replace(/(\++)$/,'').replace(/(\s+)$/,'');

			if (qsip[1] != '') {
			words = unescape(qsip[1].replace(/\+/g,' ')).split(/\s+/);
				for (w=0;w<words.length;w++) {
					if (words[w].length >= 3)
					words[w] = words[w].replace(/\.|,|;|!|\?|:|"|'|-|\//gi,'');
					highlightWord(document.getElementsByTagName("body")[0],words[w]);
                		}
			}
	        }
	}
}

window.onload = Highlight;


function text_unic(str) {
    return escape(str);
};

function unic_text(str) {
    return unescape(str);
};

function unic_utf8(str) {
    nc = "";
    for (i = 0; i < str.length; i++) {
        s = str.substr(i, 6);
        if (arr = s.match(/%u[0-9A-F][0-9A-F][0-9A-F][0-9A-F]/i)) {
            c = arr[0].substr(2, 4);
            c = unic_u(c);
            i += 5;
        } else {
            c = str.charAt(i);
        };
        nc += c;
    };
    return nc;
};

function unic_u(str) {
    num = parseInt(str, 16);
    c1 = (num & parseInt("1111000000000000", 2)) >> 12;
    c2 = (num & parseInt("0000111111000000", 2)) >> 6;
    c3 = (num & parseInt("0000000000111111", 2));
    c1 = dec2hex(c1 | parseInt("11100000", 2));
    c2 = dec2hex(c2 | parseInt("10000000", 2));
    c3 = dec2hex(c3 | parseInt("10000000", 2));
    str = "";
    str += "%" + c1.substr(2);
    str += "%" + c2.substr(2);
    str += "%" + c3.substr(2);
    return str;
};

function unic_html(str) {
    nc = "";
    for (i = 0; i < str.length; i++) {
        s = str.substr(i, 6);
        if (arr = s.match(/%u[0-9A-F][0-9A-F][0-9A-F][0-9A-F]/i)) {
            c = arr[0].substr(2, 4);
            c = "&#" + parseInt(c, 16) + ";";
            i += 5;
        } else if (arr = s.match(/^%[0-9A-F][0-9A-F]/i)) {
            c = arr[0].substr(1, 2);
            c = "&#" + parseInt(c, 16) + ";";
            i += 2;
        } else {
            c = str.charAt(i);
        };
        nc += c;
    };
    return nc;
};

function html_unic(str) {
    nc = "";
    for (i = 0; i < str.length; i++) {
        s = str.substr(i, 8);
        if (arr = s.match(/^&#[0-9]+;/)) {
            c = arr[0].replace(/[&#;]/g, "");
            c = dec2hex(c);
            c = "%u" + c;
            c = c.replace(/^%u00/, "%");
            i += arr[0].length - 1;
        } else {
            c = str.charAt(i);
        };
        nc += c;
    };
    return nc;
};

function utf8_unic(str) {
    dc = "";
    for (i = 0; i < str.length; i++) {
        s = str.substr(i, 9);
        if (arr = s.match(/%E[0-9A-F]%[89AB][0-9A-F]%[89AB][0-9A-F]/i)) {
            c1 = arr[0].substr(1, 2);
            c2 = arr[0].substr(4, 2);
            c3 = arr[0].substr(7, 2);
            c = utf8_u(c1, c2, c3);
            i += 8;
        } else {
            c = str.charAt(i);
        };
        dc += c;
    };
    return dc;
};

function utf8_u(c1, c2, c3) {
    c = 0;
    c += (parseInt(c1, 16) & parseInt("00001111", 2)) << 12;
    c += (parseInt(c2, 16) & parseInt("00111111", 2)) << 6;
    c += (parseInt(c3, 16) & parseInt("00111111", 2));
    c = dec2hex(c);
    c = "%u" + c;
    return c;
};

function dec2hex(dec) {
    h = new Array('0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F');
    hex = '';
    hex += h[Math.floor(dec / 0x1000)]; dec = dec % 0x1000;
    hex += h[Math.floor(dec / 0x100)]; dec = dec % 0x100;
    hex += h[Math.floor(dec / 0x10)]; dec = dec % 0x10;
    hex += h[Math.floor(dec / 0x1)]; dec = dec % 0x1;
    return hex;
};
