$().ready(function() {
    var tweet_list = $('#tweet_list');
    var lastfm_list = $('#lastfm_list');
    var delicious_list = $('#delicious_list');

    var regexp = /((ftp|http|https):\/\/(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?)/gi;
    
    var lastfm_api_key = 'f163aa933c55b21404811114ac7f9b3f';
    
    jQuery.ajaxSetup({type: 'GET'});
    jQuery.get('http://twitter.com/statuses/user_timeline.json', {
        user_id: 9013922,
        count: 6
    }, function(data, status) {
        tweet_list.html('');
        if (data.length > 3) {
            limit = 3;
        } else {
            limit = data.length;
        }
        for (var i=0;i<limit;i++) {
            if (data[i].text[0] != "@") {
                html = data[i].text.replace(regexp, "<a href=\"$1\">Link.</a>");
                li = $('<li><p>'+html+'</p></li>');
                tweet_list.append(li);            

            }
        }
    }, 'jsonp');
    
    jQuery.get('http://ws.audioscrobbler.com/2.0/', {
        format: 'json',
        method: 'user.getlovedtracks',
        user: 'marcocavi',
        limit: 3,
        api_key: lastfm_api_key
    }, function(data, status) {
        songs = data.lovedtracks.track;
        lastfm_list.html('');
        for (var i=0;i<3;i++) {
            name = songs[i].name;
            artist_name = songs[i].artist.name;
            html = name + ' &mdash; '+artist_name;
            url = songs[i].url;
            li = $('<li><p><a href="'+url+'">'+html+'</a></p></li>');
            lastfm_list.append(li);            
        }        
    }, 'jsonp');
    
    jQuery.get('http://feeds.delicious.com/v2/json/cavicavicavi', {
        count: 3
    }, function(data, status) {
        delicious_list.html('');
        for (var i=0;i<data.length;i++) {
            url = data[i].u;
            name = data[i].d;
            li = $('<li><p><a href="'+url+'">'+name+'</a></p></li>');
            delicious_list.append(li);
        }
    }, 'jsonp');
});
