/**
 * @fileoverview
 * The script that drives the network status page. The overall structure has
 * been adapted from the old Web site, but the code is about 95% new -- partly
 * to make it use jQuery, but also to tidy up a lot of the cruft that had crept
 * into the old file.
 */

/**
 * The "states" that can have faults associated with them. Obviously this is a
 * misnomer, given the existance of "National". (And the territories, if you
 * want to be pedantic.)
 */
var states = {
    au:  "National",
    wa:  "West Australia",
    nsw: "New South Wales",
    sa:  "South Australia",
    nt:  "Northern Territory",
    qld: "Queensland",
    vic: "Victoria",
    tas: "Tasmania",
    act: "ACT"
};

/** Possible fault levels and their mappings to CSS classes. */
var levels = {
    "1": "major",
    "2": "serious",
    "3": "minor"
};

/**
 * Generates the tables listing the current public faults. This depends on the
 * existence of the serviceevents variable, which should be populated within
 * /status/au-data.html.
 */
function generateFaultTables() {
    $.each(serviceevents, function() {
        /* Service events are broken down by "state". We need to create a table
         * for each one. */
		var str = "<table class=\"status\">" +
                    "<tr class=\"always\">" +
                        "<th>Service</th>" +
                        "<th>Upgrades</th>" +
                        "<th>Faults</th>" +
                        "<th>Start</th>" +
                        "<th>End / ETA</th>" +
						"<th>Location</th>" +
                    "</tr>" +
                  "</table>";
        var table = $(str);

        if (this.eventlist.length > 0) {
            /* Iterate through the events listed and create a row for each one.
             * Simple, right? Right? Bueller? */
            $.each(this.eventlist, function() {
                if (this && this.eventType) {
                    var link = "<a href='/status/faultinfo/" + this.faultId + ".html' target='_blank'>" + this.faultId + "</a>";
                    var locations = this.location.sort();
                    var locElem = "<td>" + locations.join(", ") + "</td>";

                    /* This is broadly similar to the behaviour on the old
                     * site, but not quite identical. The use of the jQuery
                     * click function is a bit of a hack, admittedly. */
                    if (this.location.length > 5) {
                        locElem = $("<td><span>" + locations[0] + " <a href='#'>and " + (locations.length - 1) + " others</a></span></td>");
                        $("a", locElem).click(function() { $(this).replaceWith(", " + locations.slice(1).join(", ")); return false; });
                    }

                    var upgrade = "";
                    var fault = "";
                    if (this.eventType == "Outage") {
                        this.eventType = "Upgrade";
                        upgrade = link;
                    }
                    else {
                        fault = link;
                    }

                    var row = $("<tr class='service" + this.sid + "'>" +
                                   "<td>" + this.service + "</td>" +
                                   "<td>" + upgrade + "</td>" +
                                   "<td>" + fault + "</td>" +
                                   "<td>" + this.startTime + "</td>" +
                                   "<td>" + this.endTime + "</td>" +
                                "</tr>");
                    row.append(locElem);
                    table.append(row);
                }
            });
        }
        else {
            table.append("<tr><td colspan='6'>There are no outstanding events for this region.</td></tr>");
        }

        $("#" + this.state).append(table);
    });
}

/**
 * Generates the divs that display current fault and upgrade totals for each
 * region on the map. Depends on the activefaults and netstatus variables that
 * come in from /status/au-data.html.
 */
function showFaultOutages() {
    $.each(states, function(abbr, name) {
        var layer = $("#layer_" + abbr);
        var status = netstatus[abbr];
        var level = activefaults.locations ? activefaults.locations[abbr] : null;

        // Update the state layer text.
        var lines = [];
        $.each(netstatus[abbr], function(type, number) {
            lines.push("<span>" + number + "</span>" + type + ":");
        });
        $("#layer_" + abbr).html("<p class='meta'>" + lines.join("<br />") + "</p>");

        // Set the highest priority state colour class.
        var cls;
        if (level) {
            $.each(levels, function(idx, name) {
                if (level[idx] && !cls) {
                    cls = name;
                }
            });
        }

        if (cls) {
            $("#base_" + abbr).addClass(cls);
        }
    });
}

/**
 * Shows the fault table for a particular region and hides the others.
 *
 * @param {String} region The region that needs to be shown.
 */
function showHide(region) {
    // Hide all fault tables.
    $("#serviceevents > div").css("display", "none");

    /* Update the sIFR heading to the correct region name, assuming sIFR is
     * active. */
    var flash = $("#service_heading .sIFR-flash");
    var title = $("#" + region + " h4").text();
    if (flash) {
        flash.each(function() { if (this.replaceText) this.replaceText(title); });
    }
    else {
        $("#service_heading").text(title);
    }
    
    // Show the table we're interested in.
    $("#" + region).css("display", "block");

    // Force a reflow in inferior browsers.
    if ($.browser.msie) {
        $("#footer").css("display", "none").css("display", "block");
    }
}

/* Basic initialisation: generate the fault tables, show the national one, and
 * update the map. */
$(function() {
    showFaultOutages();
    showHide("au");
    generateFaultTables();
});

// vim: set et ts=4 sw=4 cin nopaste:
