$(function() {
  loadTweets();
});

var days = [ ]
var months = [ 'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sept', 'Oct', 'Nov', 'Dec' ];

function loadTweets() {
  $.getJSON('http://search.twitter.com/search.json?callback=?&q=from%3Aunderclouds&rpp=10', function(data) {
    if (data.results) {
      var results = '';      
      $.each(data.results, function(key, item) {
        if (!item.to_user_id) {          
          var tweet = $.encode(item.text).replace(/[A-Za-z]+:\/\/[A-Za-z0-9-_]+\.[A-Za-z0-9-_:%&\?\/.=]+/, function(link) {
            return link.link(link);
          });          
          tweet = tweet.replace(/(^|\s)@([a-zA-Z0-9\_\-]+)/g, '$1@<a href="http://www.twitter.com/$2" rel="nofollow">$2</a>');
          tweet = tweet.replace(/(^|\s)#([a-zA-Z0-9\_\-]+)/g, '$1#<a href="http://search.twitter.com/search?q=%23$2" rel="nofollow">$2</a>');
          
          var date = new Date(item.created_at);
          
          var time = date.getHours();
          if (time < 12)
            time = (time == 0 ? 12 : time) + ":" + date.getMinutes() + " AM";
          else
            time = (time == 12 ? 12 : time - 12) + ":" + date.getMinutes() + " PM";
            
          var date = time + " " + months[date.getMonth()] + " " + date.getDate();
                    
          results += '<li><p>' + tweet + '</p><p class="when">' + date + '</p></li>';
        }
      });
      
      $(".tweets ul").html(results);
    }
  });  
}