$(function(){
  
  
  loadNewTweets()
  
});


function loadNewTweets()
{
  var recent = $($(".tweet-item")[0]).attr("id").split("tw-").join("");
  $.post("ajax.php", {last: recent}, onNewTweetsLoaded, "json");
}

function onNewTweetsLoaded( data )
{
  if(data.results.length)
  {
    var alltweets = $("#AllTweetsContainer");  
    for(var a = 0; a < data.results.length; a++)  
    {
      var e = $(data.results[a]["item_html"]);
      alltweets.prepend(e.hide());
      var time = a*250;
    	var fnc = Delegate.create(e, deployNewTweet);
    	setTimeout(fnc, time);
    }
  }
  
  //update elapsedtime
  $("div.tweet-item").each(function(){
    var ts = distance_of_time_in_words(getClassVar(this, "ts"))
    $("span.tweet-elapsed", $(this)).html(ts+" ago");
  });
  
  setTimeout(loadNewTweets, 5000);
}

function deployNewTweet()
{
  $(this).slideDown(500, "easeOutSine");
}