var monthAbbrs = new Array("Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec");

/**
 * Returns a compact date representation of the given UNIX timestamp.
 *
 * @param {String} a UNIX timestamp.
 */
function formatTimestamp(timestamp) {
    var dt = new Date(timestamp*1000);
    return monthAbbrs[dt.getMonth()] + " " + dt.getDate() + ", " + dt.getFullYear();
}

/**
 * Writes the given Tumblr posts in a table.
 *
 * @param {Object} tumblrObj a JSON object corresponding to the output of the Tumblr read API.
 */
function writeTumblrPosts(tumblrObj) {
    document.write('<table>');
    
    var nbPosts = Math.min(tumblrObj.posts.length, 3);
    var i;
    for(i=0; i<nbPosts; i++) {
        document.write('    <tr>');
        document.write('        <td nowrap="" valign="top" class="strong">'+formatTimestamp(tumblrObj.posts[i]["unix-timestamp"])+'&nbsp;</td>');
        document.write('        <td>'+tumblrObj.posts[i]["regular-body"]+'</td>');
        document.write('    </tr>');
        document.write('    <tr>');
        document.write('        <td valign="top" class="strong">&nbsp;</td>');
        document.write('        <td>&nbsp;</td>');
        document.write('    </tr>');
    }

        document.write('    <tr>');
//        document.write('        <td valign="top" class="strong">&nbsp;</td>');
//        document.write('        <td><a href="http://mucommander.tumblr.com/">More news &raquo;</a></td>');
        document.write('        <td nowrap="" valign="top" class="strong"><a href="http://mucommander.tumblr.com/">More news &raquo;</a></td>');
        document.write('        <td>&nbsp;</td>');
        document.write('    </tr>');

    document.write('</table>');
}