
function PageQuery(q) 
{
	if(q.length > 1) this.q = q.substring(1, q.length);
	else this.q = null;
	this.keyValuePairs = new Array();
	if(q) 
	{
		for(var i=0; i < this.q.split("&").length; i++) 
		{
			this.keyValuePairs[i] = this.q.split("&")[i];
		}
	}
	this.getKeyValuePairs = function() { return this.keyValuePairs; }
	this.getValue = function(s) 
	{
		for(var j=0; j < this.keyValuePairs.length; j++) 
		{
			if(this.keyValuePairs[j].split("=")[0] == s)
			return this.keyValuePairs[j].split("=")[1];
		}
		return false;
	}
	this.getParameters = function() 
	{
		var a = new Array(this.getLength());
		for(var j=0; j < this.keyValuePairs.length; j++) 
		{
			a[j] = this.keyValuePairs[j].split("=")[0];
		}
		return a;
	}
	this.getLength = function() { return this.keyValuePairs.length; } 
}

function queryString(key)
{
	var page = new PageQuery(window.location.search); 
	return unescape(page.getValue(key)); 
}

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 && tempWordVal.length > 2) {
			pn = node.parentNode;
			if (pn.className == "" || pn.className == "content" || pn.className=="description" || node.className=="description" || node.className == "content" || node.className=="rowdescription" || node.className == "rowtitle") {
				// 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() {
	if (queryString("q")!='false')
	{
        	
	words = unescape(queryString("q").replace(/\+/g,' ')).split(/\s+/);
	words.sort(sortit);                                                    // we want to list the larger word matches first
	    for (w=0;w<words.length;w++) 
	    {
			highlightWord(document.getElementsByTagName("body")[0],words[w]);
        }
	        
	}
}

function sortit(a,b)                                // sort an array in descending order by string length
{
	return (b.length - a.length);
}

window.onload = googleSearchHighlight;