Event.observe(document, "dom:loaded", function()
{

  // After DOM has loaded query Twitter for the timeline, and provide
  // a callback method.

  twitterlib.timeline('PanoLogic', {}, function(feed, options){

    // Loop through then timeline and extract HTML of tweets.

    var tweets = [];
    for(x in feed)
    {
      if(feed.hasOwnProperty(x))
      {
        tweets[x] = twitterlib.ify.clean(feed[x].text);
      }
    }

    // Animate in HTML of first tweet, then begin recursion.

    i = 0;
    $('tweet_text').insert(tweets[i]); // Add tweet HTML to DOM.

    new Effect.Appear('tweet_text',
    {
      "x": 0,
      "y": 0,
      "duration": 2.0,
      "mode": "absolute",
      "afterFinish": function() {
        setTimeout(clearTweet.curry(i, tweets), 5000); // Delay 5 sec
      }
    });
  });

  function clearTweet(i, tweets)
  {

    // Hide tweet and the move in next

    Effect.Fade('tweet_text',
    {
      "duration": 1.0,
      "afterFinish": moveInTweet.curry(i, tweets)
    });
  }

  function moveInTweet(i, tweets)
  {

    // Increment to next tweet, if at end of timeline, loop back

    i = i + 1;
    if(i == tweets.length)
    {
      i = 0;
    }

    $('tweet_text').innerHTML = ''; // Clear previous tweet

    $('tweet_text').insert(tweets[i]); // Add HTML of next tweet to DOM

    new Effect.Appear('tweet_text', {
      "x": 0,
      "y": 0,
      "duration": 3.0,
      "mode": "absolute",
      "afterFinish": function() {
        setTimeout(clearTweet.curry(i, tweets), 5000); // Delay 5 sec
      }
    });

  }
});

