﻿String.prototype.format = function ()
{
    var pattern = /\{\d+\}/g;
    var args = arguments;
    return this.replace(pattern, function (capture) { return args[capture.match(/\d+/)]; });
}

String.prototype.formatWith = function (obj, clean)
{
    return this.replace(/\{(.*?)\}/gim, function (all, match)
    {
        return (clean == true) ? HtmlEncode(obj[match]) : obj[match];
    });
}

function reduce(fn, a, init, limit)
{
    var s = init;
    var l = (limit == undefined) ? a.length : Math.min(a.length, limit);
    for (i = 0; i < l; i++)
        s = fn(s, a[i], i);
    return s;
}

function map(fn, a)
{
    var l = a.length;
    for (i = 0; i < l; i++)
        a[i] = fn(a[i]);
}

function HtmlEncode(value)
{
    return $('<div/>').text(value).html();
}

function HtmlDecode(value)
{
    return $('<div/>').html(value).text();
}

function MakeRelative()
{
    $("#mainContent a, #aside a, #bucketArea a").filter(function ()
    {
        return new RegExp('^http://((psnh|nusoctgolf)stage.nu.com|((new|www).psnh|www.nusoctgolf).com)').test($(this).attr('href'));
    }).each(function ()
    {
        var href = $(this).attr('href')
        $(this).attr('href', href.substring(href.indexOf('/', 7)));
    });
}

function FormatTables()
{
    $('.contentBlock tbody tr:even').addClass('altRow');
}

function LoadOutageData(s, e)
{
    $.ajax(
    {
        type: 'POST',
        url: '/outage/services/OutageData.asmx/AllOutageData',
        data: '{}',
        contentType: 'application/json; charset=utf-8',
        dataType: 'json',
        success: function (data)
        {
            s(data.d);
        },
        error: function (xhr, textStatus)
        {
            if (xhr.status != 0)
                e();
        }
    });
}

$(document).ready(function ()
{
    MakeRelative();
    FormatTables();
});
