$(document).ready(function(){
  
  getTweets();
  setInterval(showTweet, 7000);

  $(window).resize(function(){
    var ww = $(window).width();
    if (ww < 1440) {
      $('.header-left').css('margin-left',~~((1440-ww)/2)*-1)
    } else {
      $('.header-left').css('margin-left',0)
    }
  })

  $(window).resize();
  
})

var tweets = [];
var current_tweet = 0;
function getTweets() {  
  var twitter_username = $('#twitter_username').text().replace('@', '');
  var twitter_username_width = $('#twitter_username').outerWidth();
  $('.tweet-text','#last-tweet').width(813-twitter_username_width);
  $('#last-tweet .more').attr('href', 'http://twitter.com/#!/' + twitter_username);
  $.getJSON("http://twitter.com/statuses/user_timeline/" + twitter_username + ".json?callback=?", function(data) {
    tweets = data;
    showTweet();
  });
}


function showTweet() {
  if (current_tweet + 1 > tweets.length) current_tweet = 0;
  
  var tweet = tweets[current_tweet].text.replace(/(@([^\s:]+))/g,' <a href="http://twitter.com/$2" target="_blank">$1</a>');
  
  tweet = tweet.replace(/(http:\/\/t\.co[^\s]+)/g,'<a href="$1" target="_blank">$1</a>');
  
  $('.tweet-text','#last-tweet').fadeOut(function() {
    $(this).html(tweet).fadeIn();
  });
  
  current_tweet++;
}
