var showhighlightaction = false;

function level2highlight()
{
        var words = getsearchwords();

        if (!words) return;

        jQuery('.members a').each(function(x) {
                highlightlink(words, this, 1);
        });
        jQuery('.gab4 a').each(function(x) {
                highlightlink(words, this, 1);
        });

}

function level4highlight()
{
        var words = getsearchwords();

        if (!words) return;
        jQuery('.gab4 a').each(function(x) {
                highlightlink(words, this, 2);
        });
}

function highlightlink(words, link, minrequired)
{
                var matchcount = 0;
                var linktext = ' ' + link.innerHTML.toLowerCase() + ' ';
                for (w=0;w<words.length;w++) {
                        if (linktext.indexOf(' '+words[w].toLowerCase()+' ') != -1)
                                matchcount++;
                }

                if (matchcount >= minrequired)
                {
                        // two or more words match so highlight this row
                        jQuery(link).css('background-color', 'yellow');
                        if (!showhighlightaction)
                        {
                                jQuery('.content-container h1').after('<br /><center><h3 style="padding:10px;background-color:yellow;margin-bottom:10px;">' +
					'Your search terms &quot;' + words.join(' ').replace("'","") + '&quot; are highlighted below. ' +
                                        'Please scroll down to see your results.</h3></center>');
                                showhighlightaction = true;
                        }
                }


}

function getsearchwords()
{
	if (!document.createElement) return false;
	ref = document.referrer;
	if (!ref) ref = document.location.href;
	if (ref.indexOf('?') == -1 || ref.indexOf('beta.wikiworldbook.com') != -1 || ref.indexOf('http://wikiworldbook.com') != -1)
		return false;
	if (ref.indexOf('q=') == -1 && ref.indexOf('p=') == -1) 
		return false

	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') { // q= for Google, p= for Yahoo
			words = decodeURIComponent(qsip[1].replace(/\+/g,' ')).split(/\s+/);
			if (words.length == 0)
				return false;
			else
				return words;
	        }
	}

	return false;
}

