﻿function showrecentposts(json) {
    // start a loop
    // in this loop we get the entry from the feed and parse it
    for (var i = 0; i < numposts; i++) {
        // get entry i from feed
        var entry = json.feed.entry[i];
        // get the posttitle
        var posttitle = entry.title.$t;
        // get the post url
        // check all links for the link with rel = alternate
        var posturl;
        if (i == json.feed.entry.length) break;
        for (var k = 0; k < entry.link.length; k++) {
            if (entry.link[k].rel == 'alternate') {
                posturl = entry.link[k].href;
                break;
            }
        }
        // get the postdate, take only the first 10 characters
        var postdate = entry.published.$t.substring(0, 10);
        // get the post author
        var postauthor = entry.author[0].name.$t;
        // get the postcontent
        // if the Blogger-feed is set to FULL, then the content is in the content-field
        // if the Blogger-feed is set to SHORT, then the content is in the summary-field
        if ("content" in entry) {
            var postcontent = entry.content.$t;
        }
        else
            if ("summary" in entry) {
            var postcontent = entry.summary.$t;
        }
        else var postcontent = "";
        // strip off all html-tags
        var re = /<\S[^>]*>/g;
        postcontent = postcontent.replace(re, "");
        // reduce postcontent to numchar characters
        if (postcontent.length > numchars) postcontent = postcontent.substring(0, numchars);

        // format postdate
        postdate = postdate.substring(5, 7) + '/' + postdate.substring(8, 10) + '/' + postdate.substring(2, 4);
        
        // display the results
        document.write('<a href="' + posturl + '" title="read article"><h2>' + posttitle + '</h2></a>');
        document.write('<span class="content">' + postcontent + '...</span>');        
        document.write('<span class="date">Posted ' + postdate + '</span>');
        document.write('<div class="box_sep">&nbsp;</div>');
    }
}

function twitterCallback2(twitters) {
    var statusHTML = [];
    for (var i = 0; i < twitters.length; i++) {
        var username = twitters[i].user.screen_name;
        var status = twitters[i].text.replace(/((https?|s?ftp|ssh)\:\/\/[^"\s\<\>]*[^.,;'">\:\s\<\>\)\]\!])/g, function(url) {
            return '<a href="' + url + '">' + url + '</a>';
        }).replace(/\B@([_a-z0-9]+)/ig, function(reply) {
            return reply.charAt(0) + '<a href="http://twitter.com/' + reply.substring(1) + '">' + reply.substring(1) + '</a>';
        });
        statusHTML.push('<span class="content">Ms. VBA ' + status + '</span> <span class="date">Posted ' + relative_time(twitters[i].created_at) + '</span><div class="box_sep">&nbsp;</div>');
    }
    document.getElementById('twitter_update_list').innerHTML = statusHTML.join('');
}

function relative_time(time_value) {
    var values = time_value.split(" ");
    time_value = values[1] + " " + values[2] + ", " + values[5] + " " + values[3];
    var parsed_date = Date.parse(time_value);
    var relative_to = (arguments.length > 1) ? arguments[1] : new Date();
    var delta = parseInt((relative_to.getTime() - parsed_date) / 1000);
    delta = delta + (relative_to.getTimezoneOffset() * 60);

    if (delta < 60) {
        return 'less than a minute ago';
    } else if (delta < 120) {
        return 'about a minute ago';
    } else if (delta < (60 * 60)) {
        return (parseInt(delta / 60)).toString() + ' minutes ago';
    } else if (delta < (120 * 60)) {
        return 'about an hour ago';
    } else if (delta < (24 * 60 * 60)) {
        return 'about ' + (parseInt(delta / 3600)).toString() + ' hours ago';
    } else if (delta < (48 * 60 * 60)) {
        return '1 day ago';
    } else {
        return (parseInt(delta / 86400)).toString() + ' days ago';
    }
}