document.tweets = $A()

function add_tweets(tw) {
  $A(tw).each(function(t) {
    document.tweets.push(t)
  })
}

function sort_tweets() {
  document.tweets = document.tweets.sortBy(function(tweet) { return new Date(tweet.created_at).toJSON() })
}

function setup_tweets() {
  sort_tweets()
  var container = $('tweets')
  for(var i = 0; i < 5; i++) {
    if(i < document.tweets.length) {
      var tweet = document.tweets[i]
      var li = new Element('li')
      li.insert(new Element('img', {'src': tweet.user.profile_image_url}))
      var h4 = new Element('h4')
      h4.update(new Element('a', {'href': 'http://twitter.com/' + tweet.user.name, 'target': '_blank'}).update(tweet.user.name))
      li.insert(h4)
      li.insert(new Element('p').update(tweet.text))
      container.insert(li)
    }
  }
}
