(function (latest_id) {
  var parse, prettyDateUpdater, template;

  template = '<p class="post"><a href="http://twitter.com/{{screen_name}}">{{screen_name}}</a> {{{html}}} <a href="http://twitter.com/{{screen_name}}/status/{{id}}" class="updated" title="{{{time_iso}}}">{{{time}}}</a></p>';

  parse = function (tweets) {
    var i, length, box, old_height, old_margin, new_height, tweet;

    box = jQuery('#main');
    old_height = box.height();
    old_margin = parseInt(box.css('margin-top'), 10);
    new_height = 0;
    length = tweets.length;

    if (length) {
      latest_id = tweets[0].event_id;

      for (i = length - 1; i >= 0; i--) {
        tweet = jQuery(Mustache.to_html(template, tweets[i])).prependTo(box).find('.updated').prettyDate();

        new_height += tweet.outerHeight();
        box.css('margin-top', '-' + (new_height - old_margin) + 'px');
      }

      new_height =  box.height() - old_height;
      box.css('margin-top', '-' + (new_height - old_margin) + 'px');

      box.stop(true).animate({
        marginTop : 0
      }, 500);
      box.children(':gt(20)').animate({height:0,paddingBottom:0}, 600, 'linear', function () {
        jQuery(this).remove();
      });
    }

    jQuery.getJSON('/wait?since_id=' + latest_id, parse);
  };
  jQuery.getJSON('/wait?since_id=' + latest_id, parse);

  prettyDateUpdater = function () {
    jQuery('.post .updated').prettyDate();
    setTimeout(prettyDateUpdater, 5000);
  };
  prettyDateUpdater();
})(importClientLatestId ? importClientLatestId : 0);