var TweetThisLink = {
    shorten : function(e) {
        // this stops the click, which will later be handled in the  response method
        e.preventDefault();
        // find the link starting at the second 'http://'
        var url = this.href.substr(this.href.indexOf('http:',5));
        BitlyClient.shorten(url, 'TweetThisLink.response');
    },
    
    response : function(data) {
        var bitly_link = null;
        for (var r in data.results) {
            bitly_link = data.results[r]['shortUrl']; 
            break;
        }
        var tweet_text = document.title + ': ';
        
        window.open("http://twitter.com/home?status=" + encodeURIComponent(tweet_text + ' ' + bitly_link), 'twitter');
    }
}

$(document).ready(function(){
    jQuery('.tweetlink').bind('click', TweetThisLink.shorten);
    
    $('.facebooklink.onpage').click(function(e){
        e.preventDefault();
        window.open("http://facebook.com/sharer.php?u="+escape(document.location)+"&t="+escape(document.title), 'facebook');
    });

    $('.reddit_button').each(function(){
        $(this).find('a:last').text(
        $(this).find('a:last').text() == 'submit'
        ? 'Submit to Reddit'
        : $(this).find('a:last').text() 
        );
    });
    
    
    
    $('.reddit_button img').attr('src', '/images/my-reddit-alien.png');
    
});



