/* http://www.kryogenix.org/code/browser/searchhi/ */
/* Modified 20021006 to fix query string parsing and add case insensitivity */
/* Modified 20070316 to stop highlighting inside nosearchhi nodes */
/* Modified by Milko 20080428 */


var _searchhi_highlightOff = false;
var _searchhi_elementId = "";


function highlightWord( node, word, index )
{
	// 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, index );
		}
	}
	
	// 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.indexOf("searchword") == -1 && pn.className.indexOf("intext") == -1)
			{
				HighlightToggleText();

				// 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" + (index ? index+"" : "");
				hiword.appendChild(hiwordtext);

				hiword_a = document.createElement("a");
				hiword_a.name = word;
				hiword_a.id = word;
				hiword_a.appendChild(hiword);

				pn.insertBefore(before,node);
				pn.insertBefore(hiword_a,node);
				pn.insertBefore(after,node);
				pn.removeChild(node);
			}
			else if( _searchhi_highlightOff == true )	// Milko
			{
				HighlightToggleText();
				pn.className = "";

				pn.style.color = "#000000";
				pn.style.backgroundColor = "#FFFFFF";
			}
		}
	}
}


function HighlightToggle()
{
	_searchhi_highlightOff = ! _searchhi_highlightOff;
	googleSearchHighlight();
}


function HighlightToggleText()
{
	document.getElementById("lblMessage").innerHTML = "<span class='searchword'>marcando las palabras buscadas</span>: " + _searchhi_words + " - <a href='javascript:HighlightToggle();'><img border='0' src='images/icon_" + (!_searchhi_highlightOff ? "close_14x14.png' title='click para desactivar marcado' alt='click para desactivar marcado'/>" : "new_16x16.png' title='click para marcar' alt='click para marcar'/>") + "</a></span><br/>";
}


var _searchhi_words = "";


function googleSearchHighlight()
{
	_searchhi_words = "";
	//alert(document.referrer);
	if (!document.createElement) return;
	ref = document.referrer;
	if (ref.indexOf('?') == -1) return;
	qs = ref.substr(ref.indexOf('?')+1);
	qs = decodeURIComponent(qs);	// Milko
	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') { // q= for Google, p= for Yahoo
			words = unescape(qsip[1].replace(/\+/g,' ')).split(/\s+/);
			for (w=0;w<words.length;w++)
			{
				if( words[w].length < 3 )	// Milko
					continue;

				_searchhi_words += "<a href='#" + words[w] + "' title='click para localizar este termino en la pagina'><span class='searchword" + w + "' style='text-decoration:underline'>" + words[w] + "</span></a> ";

				if( _searchhi_elementId == "" )	// Milko
					highlightWord( document.getElementsByTagName("body")[0], words[w], w );
				else
					highlightWord( document.getElementById(_searchhi_elementId), words[w], w );	// Milko
			}
	        }
	}
}

//window.onload = googleSearchHighlight;
LoadEventAdd( googleSearchHighlight );	// Milko
