
Date.prototype.setISO8601 = function(dString) {

    var regexp = /(\d\d\d\d)(-)?(\d\d)(-)?(\d\d)(T)?(\d\d)(:)?(\d\d)(:)?(\d\d)(\.\d+)?(Z|([+-])(\d\d)(:)?(\d\d))/;

    if (dString.toString().match(new RegExp(regexp))) {
        var d = dString.match(new RegExp(regexp));
        var offset = 0;

        this.setUTCDate(1);
        this.setUTCFullYear(parseInt(d[1], 10));
        this.setUTCMonth(parseInt(d[3], 10) - 1);
        this.setUTCDate(parseInt(d[5], 10));
        this.setUTCHours(parseInt(d[7], 10));
        this.setUTCMinutes(parseInt(d[9], 10));
        this.setUTCSeconds(parseInt(d[11], 10));
        if (d[12])
            this.setUTCMilliseconds(parseFloat(d[12]) * 1000);
        else
            this.setUTCMilliseconds(0);
        if (d[13] != 'Z') {
            offset = (d[15] * 60) + parseInt(d[17], 10);
            offset *= ((d[14] == '-') ? -1 : 1);
            this.setTime(this.getTime() - offset * 60 * 1000);
        }
    }
    else {
        this.setTime(Date.parse(dString));
    }
    return this;
};

(function($) {
    function extract_template_data(item) {
        var today = new Date();
        today.setISO8601(item.created_time);

        var posttext = item.message != null ? item.message : (item.story != null ? item.story : item.caption + " in the album " + item.name);
        
        if (posttext.length > 90) {
            posttext = posttext.substring(0, 90) + "..."
        }
        var o = {};
        if ((item.from.id == "114293308656023") && (gFBcount < 3)) {
            o.item = item;
            o.message = posttext;
            o.link = item.actions[0].link;
            o.created_time_format = dateFormat(today, "mmmm d") + " at " + dateFormat(today, "h:MMTT");
            gFBcount++;
            return o;
        }
        else
            return null;
    }

    var gFBcount = 0;

    $.fn.facebookfeed = function(options) {
        var settings = {
            id: '145975662131224', //id of the facebook entity
            template: '<div style="padding: 10px; padding-left: 20px; padding-right: 15px; text-align: left;">${message} <a href="${link}" target="_blank" style="white-space: nowrap;">${created_time_format}</a></div><img src="/WebSiteGraphics/NewDesign/home/separator.png" />', //template for formatting each feed entry
            query: {},
            access_token: ''
        };
        if (options)
            $.extend(settings, options);
        var container = this;
        var requestURL = 'https://graph.facebook.com/' + settings.id + '/feed?access_token=' + settings.access_token + '&' + $.param(settings.query) + '&callback=?'; //calback=? is required to get JSONP behaviour
        var template = $.template(null, settings.template);

        $.getJSON(requestURL, function(json) {
            container.empty();
            gFBcount = 0;
            var posts = $.map(json.data || json, extract_template_data);
            var messages = $.tmpl(template, posts).appendTo(container);
        });
        return this;
    };
})(jQuery);
