/* Versione originale funzioni highlightWord e searchHighlight: */
/* http://www.kryogenix.org/code/browser/searchhi/ */

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;
			// check if we're inside a "nosearchhi" zone
			checkn = pn;
			while (checkn.nodeType != 9 && 
			checkn.nodeName.toLowerCase() != 'body') { 
			// 9 = top of doc
				if (checkn.className.match(/\bnosearchhi\b/)) { return; }
				checkn = checkn.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 getElementsByClassName(classname, node)  {
	if(!node) node = document.getElementsByTagName("body")[0];
	var a = [];
	var re = new RegExp('\\b' + classname + '\\b');
	var els = node.getElementsByTagName("*");
	for(var i=0,j=els.length; i<j; i++)
		if(re.test(els[i].className))a.push(els[i]);
	
	return a;
}

function searchHighlight() {
	
	
	if (!document.createElement) return;
	ref = readCookie("whitePageSearchTerms");
	if(ref == null) return;
	if(ref == "") return;
	ref = "?t=" + ref.replace(/ /g, "&t=");
	eraseCookie("whitePageSearchTerms");
	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] == 't') {
			words = unescape(qsip[1].replace(/\+/g,' ')).split(/\s+/);
	                for (w=0;w<words.length;w++) {
				highlightWord(document.getElementById("content"),words[w]);
                	}
                	for (w=0;w<words.length;w++) {
				highlightWord(document.getElementById("title"),words[w]);
                	}
	        }
	}
	

	

}

/* Rimuove la selezione dalle parole ricercate. */
function deHighlightWords() {
	var highlightedWords = getElementsByClassName("searchword");
	for(i=0; i< highlightedWords.length; i++)
		highlightedWords[i].className = "";
}


function setUpHighlight() {
	searchHighlight();
	
	/* Lo scrollbar viene spostato verticalmente in modo da visualizzare la prima 
	corrispondenza trovata. */
	for (i=0; i<document.getElementsByTagName('span').length; i++){
		if(document.getElementsByTagName('span')[i].className == "searchword") {
			//alert("->" + getY(document.getElementsByTagName('span')[i]));
				window.scrollBy(0,getY(document.getElementsByTagName('span')[i].parentNode)-200);
			break;
		}
	}
	
	setTimeout ( "deHighlightWords()", 4000 );
	
	
	
}


function getY(oElement)
{
var iReturnValue = 0;
while( oElement != null ) {
iReturnValue += oElement.offsetTop;
oElement = oElement.offsetParent;
}
return iReturnValue;
}


var fun = setUpHighlight;
if (window.addEventListener) window.addEventListener('load', fun, false); 
else if (window.attachEvent) window.attachEvent("onload", fun);  