/* http://www.kryogenix.org/code/browser/searchhi/ */
/* Modified 20021006 to fix query string parsing and add case insensitivity */
/* Modified  by webdesign.weisshart.de:
   20050530: added for site search - where target-file is called with ?q=searchword 
             unescape -> decodeURI, to cope with german Umlauts
   20050611: remove all blanks and '+' before and after searchterm (bugfix: crashes FF & Op) */
/* Installationsanleitung: http://webdesign.weisshart.de/kryogenix.php */

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;
			if (pn.className != "searchword") {
				// 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 = "searchword";
				hiword.appendChild(hiwordtext);
				pn.insertBefore(before,node);
				pn.insertBefore(hiword,node);
				pn.insertBefore(after,node);
				pn.removeChild(node);
			}
		}
	}
}

//
function googleSearchHighlight () {
	if (!document.createElement) return;
	ref = document.referrer;

//index.php?site=patchdb&sort=search&q=browse

	// added for site search - where target-file is called with ?q=searchword
	if (window.location.search) {
		ref = unescape(window.location.search); 
	
		if (ref.indexOf('?') == -1)	return; 	// || qstr.indexOf('?') == -1 
	
		qs = ref.substr(ref.indexOf('?')+1); //site=patchdb&sort=search&q=browse
		qsa = qs.split('&');													 //site=patchdb , sort=search ,  q=browse gesplittet im Array qsa, qsa[0]= "site=patchdb", qsa[1]= "sort=search" ...
	
		for (i=0;i<qsa.length;i++) {
			qsip = qsa[i].split('=');									//im 1. Durchlauf hat qsip[0] = site u. qsip[1] = patchdb
			if (qsip.length == 1) continue;
			if (qsip[0] == 'q' || qsip[0] == 'p')  { // q= for Google, p= for Yahoo
	
				// remove all blanks and '+' before and after searchterm (bugfix: crashes FF & Op)
				qsip[1] = qsip[1].replace(/^(\s+|\++)/,'').replace(/(\++)$/,'');
				
				words = decodeURI(qsip[1].replace(/\+/g,' ')).split(/\s+/); //unescape -> decodeURI, to cope with german Umlauts
				for (w=0;w<words.length;w++) {
					highlightWord(document.getElementsByTagName("body")[0],words[w]);
				}
			}
		}
	}
	
else if (!window.location.search)	{
		ref = unescape(window.location.href); 
		
		if(ref.search(/pdbsearch/i) == -1) return;
		
		qs = ref.substr(ref.lastIndexOf('/')+1);		//pdbsearch-browse.html 
		qsa = qs.split('-');													 					//qsa[0] = pdbsearch , qsa[1] = browse.html
		qsip = qsa[1].split('.');																//qsip[0] = browse
		
		if (qsip.length >2) {
			for (x=1;x<qsip.length-1;x++) {
				qsip[0] = qsip[0] + '.' + qsip[x]
			}	
		} 
		
		// remove all blanks and '+' before and after searchterm (bugfix: crashes FF & Op)
		qsip[0] = qsip[0].replace(/^(\s+|\++)/,'').replace(/(\++)$/,'');
		
		words = decodeURI(qsip[0].replace(/\+/g,' ')).split(/\s+/); //unescape -> decodeURI, to cope with german Umlauts
		for (w=0;w<words.length;w++) {
			highlightWord(document.getElementsByTagName("body")[0],words[w]);
		}
	}
}

window.onload = googleSearchHighlight;

