/**
 * Search Engine Keyword Highlight.
 *
 * This module script was made by author Scott Yang scott.yang.id.au, 
 * its for search engine keywords, and then highlight those keywords on
 * the page, by wrapping them around <span class="hilite">...</span> tags.
 * Document can then define styles else where to provide visual feedbacks.
 * Styles are done with the CSS Code
 *
 * Karl Daniels SEO Expert and Owner of www.mkpitstop.co.uk/forum has modded this to work with VB
 * This mod is serported at VB org and at MK PitStop
 *  
 */

Hilite = {
    
    exact: true,
    onload: true,
    style_name: 'hilite',
    style_name_suffix: true,
    debug_referrer: ''
};

Hilite.decodeReferrer = function(referrer) {
    referrer = decodeURIComponent(referrer);
    var query = null;

    if (referrer.match(/^http:\/\/(www\.)?alltheweb.*/i)) {
	// AllTheWeb
	if (referrer.match(/q=/))
	    query = referrer.replace(/^.*q=([^&]+)&?.*$/i, '$1');
    } else if (referrer.match(/^http:\/\/(www)?\.?google.*/i)) {
	// Google
	if (referrer.match(/q=/))
	    query = referrer.replace(/^.*q=([^&]+)&?.*$/i, '$1');
    } else if (referrer.match(/^http:\/\/search\.lycos.*/i)) {
	// Lycos
	if (referrer.match(/query=/))
	    query = referrer.replace(/^.*query=([^&]+)&?.*$/i, '$1');
    } else if (referrer.match(/^http:\/\/search\.msn.*/i)) {
	// MSN
	if (referrer.match(/q=/))
	    query = referrer.replace(/^.*p=([^&]+)&?.*$/i, '$1');
    } else if (referrer.match(/^http:\/\/search\.yahoo.*/i)) {
	// Yahoo
	if (referrer.match(/p=/))
	    query = referrer.replace(/^.*p=([^&]+)&?.*$/i, '$1');
    }

    if (query) {
	query = query.replace(/\'|"/, '');
	query = query.split(/[\s,\+\.]+/);
    }

    return query;
};

Hilite.hiliteHTML = function(html, query) {
    var re = new Array();
    for (var i = 0; i < query.length; i ++) {
        query[i] = query[i].toLowerCase();
        if (Hilite.exact)
            re.push('\\b'+query[i]+'\\b');
        else
            re.push(query[i]);
    }

    re = new RegExp('('+re.join("|")+')', "gi");

    var subs;
    if (navigator.userAgent.search(/Safari/) >= 0 || 
        !Hilite.style_name_suffix) 
    {
        subs = '<span class="'+Hilite.style_name+
            (Hilite.style_name_suffix?'1':'')+'">$1</span>'
    } else {
        var stylemapper = {};
        for (var i = 0; i < query.length; i ++)
            stylemapper[query[i]] = Hilite.style_name+(i+1);
        subs = function(match) {
            return '<span class="'+stylemapper[match.toLowerCase()]+'">'+match+
                '</span>';
        };
    }

    var last = 0;
    var tag = '<';
    var skip = false;
    var skipre = new RegExp('^(script|style|textarea)', 'gi');
    var part = null;
    var result = '';

    while (last >= 0) {
        var pos = html.indexOf(tag, last);
        if (pos < 0) {
            part = html.substring(last);
	    last = -1;
        } else {
            part = html.substring(last, pos);
            last = pos+1;
        }

        if (tag == '<') {
            if (!skip)
                part = part.replace(re, subs);
            else
                skip = false;
        } else if (part.match(skipre)) {
            skip = true;
        }

        result += part + (pos < 0 ? '' : tag);
        tag = tag == '<' ? '>' : '<';
    }

    return result;
};

Hilite.hiliteElement = function(elm, query) {
    if (!query)
	return;

    var oldhtml = elm.innerHTML;
    var newhtml = Hilite.hiliteHTML(oldhtml, query);

    if (oldhtml != newhtml)
        elm.innerHTML = newhtml;
};

Hilite.hilite = function() {
    var q = Hilite.debug_referrer ? Hilite.debug_referrer : document.referrer;
    var e = null;
    q = Hilite.decodeReferrer(q);
    if (q && ((e = document.getElementById('content')) ||
              (e = document.getElementById('container')) ||
              (e = document.body)))
    {
	Hilite.hiliteElement(e, q);
    }
};

if (Hilite.onload) {
    if (window.onload) {
	Hilite._old_onload = window.onload;
	window.onload = function(ev) {
	    Hilite._old_onload(ev);
	    Hilite.hilite();
	};
    } else {
	window.onload = Hilite.hilite;
    }
}
