﻿/* 
 * mrshaunwilson.js
 * Copyright (c) Shaun Wilson
 *
 * introduces a few globals, borrowed helpers, etc
 */
var mrshaunwilson = {
    options: {
        linkify: [
            /* support for multiple regex and process method pair */
            { regex: '(#[a-zA-z]+)', process: function(match) { return match; } }
        ]
    }
};

String.prototype.format = function () {
    var args = arguments;
    return this.replace(/{(\d+)}/g, function (match, number) {
        return typeof args[number] != 'undefined' ? args[number] : match;
    });
};

String.prototype.formatAsLocalDate = function () {
    var date = new Date(Date.parse(this));
    return '{0}/{1}/{2} {3}:{4} {5}'.format(
            zeroPad(date.getMonth() + 1, 2),
            zeroPad(date.getDate(), 2),
            date.getFullYear(),
            zeroPad((hours % 12), 2),
            zeroPad(date.getMinutes(), 2),
            (hours >= 12)
                ? 'PM'
                : 'AM');
};

Date.prototype.formatAsLocalDate = function () {
    var hours = this.getHours();
    return '{0}/{1}/{2} {3}:{4} {5}'.format(
            zeroPad(this.getMonth() + 1, 2),
            zeroPad(this.getDate(), 2),
            this.getFullYear(),
            zeroPad((hours > 12) ? hours-12 : hours, 2),
            zeroPad(this.getMinutes(), 2),
            (hours >= 12) ? 'PM' : 'AM');
};

function zeroPad(num, places) {
    var zero = places - num.toString().length + 1;
    return Array(+(zero > 0 && zero)).join("0") + num;
}

Date.prototype.parseJSON = function (json) {
    try {
        if (json[0] == '\/' && json[json.length - 1] == '\/') {
            var org = parseInt(json.toString().substring(6));
            return new Date(org);
        }
    } catch (e) {
        console.log(e);
    }
    return Date.parse(json);
};

Date.prototype.adjustToUTC = function () {
    var formatted = "{0}/{1}/{2} {3}:{4}:{5}".format(
            this.getUTCMonth() + 1,
            this.getUTCDate(),
            this.getUTCFullYear(),
            this.getUTCHours(),
            this.getUTCMinutes(),
            this.getUTCSeconds())
    return new Date(formatted);
};

Date.prototype.adjustToLocal = function () {
    var formatted = "{0}/{1}/{2} {3}:{4}:{5}".format(
            this.getMonth() + 1,
            this.getDate(),
            this.getFullYear(),
            this.getHours(),
            this.getMinutes(),
            this.getSeconds());
    return new Date(formatted);
};

Date.prototype.toUTC = function (fromLocal) {
    try {
        var org = this.UTC();
        var offset = fromLocal
            ? this.getTimezoneOffset() * 60 * 1000
            : 0;
        return new Date(org + offset);
    } catch (e) {
        console.log(e);
    }
    return this;
};

if (jQuery) {
    $.fn.extend({
        htmlEncode: function (value) {
            return $('<div/>').text(value).html();
        },
        htmlDecode: function (value) {
            return $('<div/>').html(value).text();
        }
    });
}
else {
    String.prototype.htmlEncode = function (value) {
        return $('<div/>').text(value).html();
    };
    String.prototype.htmlDecode = function (value) {
        return $('<div/>').html(value).text();
    };
}

String.prototype.linkify = function (value) {
    return value;
}

