/* http://www.kryogenix.org/code/browser/searchhi/ */
/* Modified 20021006 to fix query string parsing and add case insensitivity */



    function deunicode(str)
    {
	var sb = '';
	for(var i = 1; i < str.length; i++)
	{
	    if (str.charAt(i - 1) == '&' && str.charAt(i) == '#')
	    {
	       var index = str.indexOf(';', i + 1);
	       if (index != -1)
	       {
		  var ch = str.substring(i + 1, index);
		  sb += String.fromCharCode( ch );
		  i = index + 1;
		  continue;
	       }
	    }
	    sb += str.charAt(i - 1);
	    if (i == str.length - 1)
		sb += str.charAt(i); // doklejamy ostatni znak
	}
	
	return sb;
    }


//    alert( deunicode('&#1103;') );


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('granic') != -1)
	//	{
	//		alert( tempNodeVal + " : " + tempWordVal );
	//	}
		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("span");
				hiword.className = "searchword";
				hiword.appendChild(hiwordtext);
				pn.insertBefore(before,node);
				pn.insertBefore(hiword,node);
				pn.insertBefore(after,node);
				pn.removeChild(node);
			}
		}
	}
}

function googleSearchHighlight() {
	

	link = document.URL
	if(link.search(/search_string/gi) != -1) return;
	ref = document.URL;
	if (!document.createElement) return;
	ref = document.referrer;
	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) continue;
        	if (qsip[0] == 'q' || qsip[0] == 'p' || qsip[0] == 'query' || qsip[0] == 'search_string') { // q= for Google, p= for Yahoo
			newstr = qsip[1];
			newstr = newstr.replace(/%B1/gi, '±'); 
			newstr = newstr.replace(/%B6/gi, '¶'); 
			newstr = newstr.replace(/%E6/gi, 'æ'); 
			newstr = newstr.replace(/%BF/gi, '¿'); 
			newstr = newstr.replace(/%BC/gi, '¼'); 
			newstr = newstr.replace(/%B3/gi, '³'); 
			newstr = newstr.replace(/%F1/gi, 'ñ'); 
			newstr = newstr.replace(/%F3/gi, 'ó'); 
			newstr = newstr.replace(/%EA/gi, 'ê'); 
			newstr = newstr.replace(/%A6/gi, '¦'); 
			newstr = newstr.replace(/%A1/gi, '¡'); 
			newstr = newstr.replace(/%C6/gi, 'Æ'); 
			newstr = newstr.replace(/%CA/gi, 'Ê'); 
			newstr = newstr.replace(/%A3/gi, '£'); 
			newstr = newstr.replace(/%D1/gi, 'Ñ'); 
			newstr = newstr.replace(/%D3/gi, 'Ó'); 
			newstr = newstr.replace(/%AC/gi, '¬'); 
			newstr = newstr.replace(/%AF/gi, '¯'); 
		
//rosyjskie znaczki
		newstr = newstr.replace(/%3B/gi, ';'); 
		newstr = newstr.replace(/%23/gi, '#'); 
		newstr = newstr.replace(/%26/gi, '&'); 
//alert("str " + newstr);
			words = unescape(newstr.replace(/\+/g,' ')).split(/\s+/);
//alert("word" + words[0]);	
		for (w=0;w<words.length;w++) 
		{
//alert('Highlight ' + words[w]);
		    var hword = deunicode( words[w] );

if(hword.charAt(0) == '+' ){
hword = hword.substring(1)
//	alert('hword' + hword);
}
////////////////////////////////////
if(hword.charAt(0) == '"' || hword.charAt(0) == '\''){
hword = hword.substring(1)
//	alert('cudzyslow1 ' + hword);
}
if(hword.indexOf('"') != -1){
	var index = hword.indexOf('"');
	hword =	hword.substring(0,index) 
}
if(hword.indexOf('^') != -1){
	var index = hword.indexOf('^');
	hword =	hword.substring(0,index) 
}
if(hword.indexOf('\'') != -1){
	var index = hword.indexOf('\'');
	hword =	hword.substring(0,index) 
}

//////////////////////////////////////
highlightWord(document.getElementsByTagName("body")[0], hword);
                }
		//	alert('End');
	        }
	}
}

window.onload = googleSearchHighlight;
