function findNonReplyTweet(data) {
	
	if (! $.isArray(data)) return null;
	if (data.length == 0) return null;
	
    for (i = 0; i < data.length; i++) {
	  var tweet = data[i];
	  var message = tweet.text;
	  if (message.substr(0, 1) != '@') {
	    return tweet;
	  }
    }
}

function processTweets(selector, url) {
  
  var numTweetsToCheck = 10;
  var urlWithCount = url + "?count=" + numTweetsToCheck;
  
  $.ajax({
    dataType: "jsonp",
    url: urlWithCount,
    success: function(data) {
      var tweet = findNonReplyTweet(data);
      var date = new Date(tweet.created_at);
      $('#' + selector).append('<div class="date">' + date.format("stylishDate") + ' ' + date.format("shortTime") + '</div><p>' + tweet.text + '</p>');    
    }
  });
}

$(document).ready(function() {
  
  // Retrieve latest tweet from Aaron
  processTweets("aarontweet", "http://twitter.com/statuses/user_timeline/AaronHillegass.json");
  
  // Retrieve latest tweet from Big Nerd Ranch
  processTweets("bnrtweet", "http://twitter.com/statuses/user_timeline/BigNerdRanch.json");

});  

