// Highlight TdF 2005
// $Id $
// Copyright (c) 2005, Michael Strasser
// Released under the GPL license
// http://www.gnu.org/copyleft/gpl.html
//
// --------------------------------------------------------------------
//
// This is a Greasemonkey user script.
//
// To install, you need Greasemonkey: http://greasemonkey.mozdev.org/
// Then restart Firefox and revisit this script.
// Under Tools, there will be a new menu item to "Install User Script".
// Accept the default configuration and install.
//
// To uninstall, go to Tools/Manage User Scripts,
// select "HighlightCountries", and click Uninstall.
//
// --------------------------------------------------------------------
//
// ==UserScript==
// @name          Highlight TdF 2005
// @namespace     http://relativelyignorant.net
// @description   Highlight teams and countries of riders in reports from the
//                Tour de France 2005 on cyclingnews.com
// @include       http://www.cyclingnews.com/road/2005/tour05/*
// ==/UserScript==
//
// --------------------------------------------------------------------
//
(function() {

    var prenodes, i, node, key, html;
    
    // Specify the REs to select and the CSS to highlight them with.
    var styles = {
        // Teams.
        "Team CSC" : "background-color: #CC3333; color: #FFFFFF;",
        "Discovery Channel" : "background-color: #0099FF; color: #FFFFFF;",
        "T-Mobile Team" : "background-color: #FF33FF; color: #FFFFFF;",
        "Credit Agricole" : "background-color: #339933; color: #FFFFFF;",
        "Phonak Hearing Systems" : "background-color: #FFFF00; color: #339933;",
        "Fassa Bortolo" : "background-color: #CCCCCC; color: #0000CC;",
        "Bouygues Telecom" : "background-color: #66CCCC; color: #FFFFFF;",
        "Liberty Seguros-W.rth" : "background-color: #000066; color: #66CCCC;",
        "Gerolsteiner" : "background-color: #99FFFF; color: #000000;",
        "Fran.aise Des Jeux" : "background-color: #EEEEEE; color: #000066;",
        "Illes Balears-Caisse d'Epargne" : "background-color: #FFCC66; color: #339933;",
        "Davitamon-Lotto" : "background-color: #FF9933; color: #FFFFFF;",
        "Liquigas-Bianchi" : "background-color: #CCFF33; color: #000099;",
        "Domina Vacanze" : "background-color: #FFCC66; color: #0000CC;",
        "Quick.Step" : "background-color: #0000FF; color: #FFFFFF;",
        "Ag2r-Prevoyance" : "background-color: #0000FF; color: #FFFF00;",
        "Rabobank" : "background-color: #FF9933; color: #0000CC;",
        "Saunier Duval-Prodir" : "background-color: #FFFF00; color: #CC3333;",
        "Euskaltel-Euskadi" : "background-color: #FF9933; color: #000000;",
        "Cofidis, Le Credit Par Telephone" : "background-color: #FFCC66; color: #CC3333;",
        "Lampre-Caffita" : "background-color: #6666FF; color: #FFFFFF;",
        // Riders names and their countries.
        "[^\\n]+\\(Aus\\)" : "background-color: #006600; color: #FFFFAA; font-weight: bold;"
    };
    // Create regular expressions for the same countries.
    // They search for whole lines that contain the country abbreviation
    // in parentheses, excluding the surrounding newline characters.
    // On Windows the lines are surrounded by \r\n sequences, so exclude both.
    var regex = {};
    for (key in styles) {
//        regex[key] = new RegExp("[^\\n]+\\(" + key + "\\)[^\\r\\n]+", "ig");
        regex[key] = new RegExp(key, "ig");
    }
    
    // Result lines are all in <pre> nodes.
    prenodes = document.evaluate(
        "//pre",
        document,
        null,
        XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE,
        null
    );

    for (i = 0; i < prenodes.snapshotLength; ++i) {
        node = prenodes.snapshotItem(i);
        html = node.innerHTML;
        // Replace the searched-for line with a highlighted version of it.
        for (key in styles) {
            html = html.replace(
                regex[key],
                "<span style='" + styles[key] + "'>$&</span>"
            );
        }
        node.innerHTML = html;
    }

})();


