$(function(){
    $('div.aside').corner('5px');
});

function twitter(res) {
    if (res.responseStatus != 200) return;
    
    var data = res.responseData.feed.entries[0];
    $('#statustext').html(
        "<a href=http://twitter.com/tomita class=small>"
        + relative_time(data.publishedDate)
        + "</a>「" + data.content.replace(/^[^:]*: /, "") + "」"
    );
}

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 '1分くらい前'; //'about a minute ago';
    } else if(delta < (60*60)) {
        return (parseInt(delta / 60)).toString() + '分前'; //' minutes ago';
    } else if(delta < (120*60)) {
        return '1時間くらい前'; //'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日前'; //'1 day ago';
    } else {
        return (parseInt(delta / 86400)).toString() + '日前'; //' days ago';
    }
}

function delicious(res) {
    if (res.responseStatus != 200) return;
    
    $(res.responseData.feed.entries).each(function(k, v){
        $('#delicious-list').append(
            "<li><a href='" + v.link + "'>" + v.title + "</a> "
            + (v.content.match(/<p>(.*)<\/p>/) ? RegExp.$1 : "")
            + "</li>"
        );
    });
}

function hatenab(data) {
    if (!data || !data.count) return;
    
    $(data.bookmarks).each(function(k, v) {
        if (! v.comment ) return;
        if (v.tags.length) {
            v.comment = "[" + v.tags.join("][") + "] " + v.comment;
        }
        
        $('#bookmarks').prepend(
            $("<li></li>").append(
                $("<p></p>").text(v.comment),
                "<p class=silver>はてなブックマーク by "
                + "<a href='http://b.hatena.ne.jp/"  + v.user + "/'>" + v.user + "</a>&nbsp; "
                + "<span class=small>" 
                + v.timestamp.replace(/:[0-9]{2}$/, "")
                + "</span></p>"
            )
        );
    });

    $('#bookmarks').prepend(
        "<li>"
        + "<a href='" + data.entry_url + "'><img src='http://b.hatena.ne.jp/entry/image/" + data.url + "' class=icon /></a>&nbsp;"
        + "<a href='http://clip.livedoor.com/page/" + data.url + "'><img src='http://image.clip.livedoor.com/counter/" + data.url + "' class=icon /></a>&nbsp;"
        + "</li>"
    );
}