function topsy_callback(json) {
    res = json.response;
    if ( !res.total ) {
        return false;
    }
    var html = '';
    for ( var i = 0; i < res.list.length; i++ ) {
        tweet     = res.list[i];
        thumb     = tweet.author.photo_url.replace(/(normal)\.([a-z]{3,4})$/i,'mini.$2');
        author_id = tweet.author.url.replace('http://twitter.com/','');
        if ( i < 5 ) { attr = ''; } else { attr = 'style = "display: none;"'; }
        indent = '			';
        html += indent + '<li ' + attr + '>' + "\n"
              + indent + '    <p>' + tweet.content.replace(/(\r\n|\r|\n)/g,'') + '</p>' + "\n"
              + indent + '    <p class="twitter_name">' + "\n"
              + indent + '        <img src="' + thumb + '" width="24" height="24" alt="' + author_id + '" />' + "\n"
              + indent + '        <a href="' + tweet.author.url + '" target="_blank">' + author_id + '</a>' + "\n"
              + indent + '        | <a href="' + tweet.permalink_url + '" target="_blank">' + tweet.date_alpha + '</a>' + "\n"
              + indent + '    </p>' + "\n"
              + indent + '</li>';
    }

    html += '</div>';
    document.getElementById('topsy_tweets_list').innerHTML = html;

    twttr.anywhere(function (T) {
        T("#twResult").hovercards();
        T("#topsy_tweets_list").hovercards();
        T("#topsy_tweets_list img").hovercards({
            username: function(node) { return node.alt; }
        });
        T("#topsy_tweets_list a").hovercards({
            username: function(node) { return node.textContent; }
        });
    });
}

function show_topsy_tweets(url) {
    if ($('#topsy_tweets').length) {
        script      = document.createElement('script');
        script.type = 'text/javascript';
        script.src  = 'http://otter.topsy.com/trackbacks.js?callback=topsy_callback&perpage=50&url='+encodeURIComponent(url);
        document.getElementsByTagName('head')[0].appendChild(script);
        $('#topsy_tweets').show();
    }
}

