/**
* @author Scott Yang <http://scott.yang.id.au/>
 * @version 1.1
 */

// This script will detect all areas inside the HTML marked with <cite>...</cite> tags 
// and highlight keywords with a hyperlink. 

// Configuration:
Hilite = {
    /**
     * Whether to automatically hilite a section of the HTML document, by
     * binding the "Hilite.hilite()" to window.onload() event. If this
     * attribute is set to false, you can still manually trigger the hilite by
     * calling Hilite.hilite() in Javascript after document has been fully
     * loaded.
     */
    onload: true,

    debug_referrer: ''
};

/**
 * Decode the referrer string and return a list of search keywords.
 */
Hilite.decodeReferrer = function(referrer) {
    referrer = decodeURIComponent(referrer);

	if (keyword) {
	keyword = keyword.replace(/\'|"/, '');
	keyword = keyword.split(/[\s,]+/);
    }
	return keyword;

};

/**
 * Highlight a HTML string with a list of keywords.
 */

Hilite.hiliteHTML = function(html, keyword) {
	var re = new Array();
	for (var i = 0; i < keywords.length; i++) {
		if (keywords[i].keyword) {
			re.push([new RegExp('('+keywords[i].keyword+')', "i")]);
		}
		if(keywords[i].url!=self.location){
			html = html.replace(re[i][0], '<a href=\"'+keywords[i].url+'\" title=\"'+keywords[i].tooltip+'\"">$1</a>');
		}
	}

	return html;
};

/**
 * Highlight a DOM element with a list of keywords.
 */
Hilite.hiliteElement = function(elm, keyword) {
	if ( typeof( window['keywords'] ) == "undefined" ){
		return;
	}
	elm.innerHTML = Hilite.hiliteHTML(elm.innerHTML, keyword);
};

Hilite.hilite = function() {
	// If 'debug_referrer' then we will use that as our referrer string
	// instead.
	var q = Hilite.debug_referrer ? Hilite.debug_referrer : document.referrer;
	var e = null;
	q = Hilite.decodeReferrer(q);
	if (e = document.getElementsByTagName('p')){
		for (var i=0;i<e.length ;i++ )
		{
			Hilite.hiliteElement(e[i], q);
		}
	}
}

// Assign this script to the body onload event
window.onload = Hilite.hilite;
