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

var hilite_words;

/**
 * Decode the referrer string and return a list of search keywords.
 */
Hilite.decodeReferrer = function(referrer) {
	return hilite_words;
};

/**
 * Highlight a HTML string with a list of keywords.
 */
Hilite.hiliteHTML = function(html, query) {
	var re = new Array();
    for (var i = 0; i < query.length; i ++) {
        query[i] = query[i].toLowerCase();
		//alert(query[i]);
        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;
};

/**
 * Highlight a DOM element with a list of keywords.
 */
Hilite.hiliteElement = function(elm, query) {
    if (!query)
	return;

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

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

/**
 * Highlight a HTML document using keywords extracted from document.referrer.
 * This is the main function to be called to perform search engine highlight
 * on a document.
 *
 * Currently it would check for DOM element 'content', element 'container' and
 * then document.body in that order, so it only highlights appropriate section
 * on WordPress and Movable Type pages.
 */
Hilite.hilite = function() {
    // If 'debug_referrer' then we will use that as our referrer string
    // instead.
//	alert("started");
    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.getElementById('resumecontent')) ||
			  (e = document.getElementById('frame1')) ||
              (e = document.body)))
    {
	Hilite.hiliteElement(e, q);
    }
};

// Trigger the highlight using the onload handler.
/*
if (Hilite.onload) {
    if (window.onload) {
	Hilite._old_onload = window.onload;
	window.onload = function(ev) {
	    Hilite._old_onload(ev);
	    startup();
	};
    } else {
	window.onload = startup;
    }
}
function startup() {
Hilite.hilite();
}
*/
