/**
 *   Fonction qui enleve les accents du String "st" en entree
 */
function enleveAccents (st) {
        st = st.replace(/à/g,"a") ;
        st = st.replace(/â/g,"a") ;
        st = st.replace(/ä/g,"a") ;
        st = st.replace(/ç/g,"c") ;
        st = st.replace(/é/g,"e") ;
        st = st.replace(/è/g,"e") ;
        st = st.replace(/ê/g,"e") ;
        st = st.replace(/ë/g,"e") ;
        st = st.replace(/ï/g,"i") ;
        st = st.replace(/î/g,"i") ;
        st = st.replace(/ö/g,"o") ;
        st = st.replace(/ô/g,"o") ;
        st = st.replace(/ù/g,"u") ;
        st = st.replace(/ü/g,"u") ;
        st = st.replace(/û/g,"u") ;
        st = st.replace(/ÿ/g,"y") ;
        return st ;
}


/**
 * Highlight a DOM element with a list of keywords.
 */
function hiliteElement(elm, query, color) {
    if (!query || elm.childNodes.length == 0)
	return;
    var qre = new Array();
    for (var i = 0; i < query.length; i ++) {
        query[i] = query[i].toLowerCase();
        qre.push(query[i]);
    }

    qre = new RegExp(qre.join("|"), "i");

    var stylemapper = {};
    for (var i = 0; i < query.length; i ++)
        stylemapper[query[i]] = 'hilite'+(i+1+color);

    var textproc = function(node) {
        var valeurNode = node.data ;
        valeurNode = enleveAccents(valeurNode) ;

        var match = qre.exec(valeurNode);
        if (match) {
            var val = match[0];
            var k = '';
            var node2 = node.splitText(match.index);
            var node3 = node2.splitText(val.length);
            var span = node.ownerDocument.createElement('SPAN');
            node.parentNode.replaceChild(span, node2);
            span.className = stylemapper[val.toLowerCase()];
            span.appendChild(node2);
            return span;
        } else {
            return node;
        }
    };
    walkElements(elm.childNodes[0], 1, textproc);
}

/**
 * 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.
 */
function hilite(wordList,color) {
    // If 'debug_referrer' then we will use that as our referrer string
    // instead.
    var e = null;

    var q = wordList.split(' ');
    if (q && (((e = document.getElementById('contentDiv'))) || (e = document.getElementById('col2')) ||
              (e = document.body)))
    {
	hiliteElement(e, q, color);
    }
}

function walkElements(node, depth, textproc) {
    var skipre = /^(script|style|textarea)/i;
    var count = 0;
    while (node && depth > 0) {
        count ++;
        if (count >= 2000) {
            var handler = function() {
                walkElements(node, depth, textproc);
            };
            setTimeout(handler, 50);
            return;
        }

        if (node.nodeType == 1) { // ELEMENT_NODE
            if (!skipre.test(node.tagName) && node.childNodes.length > 0) {
                node = node.childNodes[0];
                depth ++;
                continue;
            }
        } else if (node.nodeType == 3) { // TEXT_NODE
            node = textproc(node);
        }

        if (node.nextSibling) {
            node = node.nextSibling;
        } else {
            while (depth > 0) {
                if (node) { node = node.parentNode; }
                depth --;
                if (node && node.nextSibling) {
                    node = node.nextSibling;
                    break;
                }
            }
        }
    }
}
