﻿
function fbFetch() {
    var id = 222419014494490;
    var month=new Array(12);
    month[0]="jan";
    month[1]="feb";
    month[2]="mar";
    month[3]="apr";
    month[4]="maj";
    month[5]="jun";
    month[6]="jul";
    month[7]="aug";
    month[8]="sep";
    month[9]="okt";
    month[10]="nov";
    month[11]="dec";

    //Set Url of JSON data from the facebook graph api. make sure callback is set with a '?' to overcome the cross domain problems with JSON
    var url = "https://graph.facebook.com/" + id + "/feed?limit=10&access_token=123975213079|nqKWO89vVW4QH_bNmKH-Wiy3W0w&callback=?";
    
    //Use jQuery getJSON method to fetch the data from the url and then create our unordered list with the relevant data.
    $.getJSON(url, function (json) {
        var html = "<ul>";
        //loop through and within data array's retrieve the message variable.
        $.each(json.data, function (i, fb) {
            var link = "";
            var date = formatFBTime(fb.created_time);
            html += "<li><span class='fb_date'>[" + date.getDate() + "-" + month[(date.getMonth())] + " " + date.getFullYear() + "]</span>";

            html += "<span class='fb_link'>";
            if (fb.link) {
                link = fb.link;
            }
            else {
                link = "https://www.facebook.com/" + id + "/posts/" + fb.id.substring(fb.id.lastIndexOf("_") + 1);
            }
            html += "<a href='" + link + "' target='_blank'>";
            if (fb.name) html += "<b>" + fb.name + "</b>, ";
            if (fb.message) html += "" + fb.message + ". ";
            if (fb.description) html += "" + fb.description + ". ";

            html += "</a></span></li>";

        });
        html += "</ul>";


        //A little animation once fetched
        $('.facebookfeed').animate({ opacity: 0 }, 500, function () {

            $('.facebookfeed').html(html);

        });

        $('.facebookfeed').animate({ opacity: 1 }, 500);

    });


};
function formatFBTime(fbDate) {
    var arrDateTime = fbDate.split("T");
    var arrDateCode = arrDateTime[0].split("-");
    var strTimeCode = arrDateTime[1].substring(0, arrDateTime[1].indexOf("+"));
    var arrTimeCode = strTimeCode.split(":");
    var valid_date = new Date()
    valid_date.setUTCFullYear(arrDateCode[0]);
    valid_date.setUTCMonth(arrDateCode[1] - 1);
    valid_date.setUTCDate(arrDateCode[2]);
    valid_date.setUTCHours(arrTimeCode[0]);
    valid_date.setUTCMinutes(arrTimeCode[1]);
    valid_date.setUTCSeconds(arrTimeCode[2]);
    return valid_date;
}

fbFetch();

