$(document).ready(function() {
  // Declare variables to hold twitter API url and user name
  var twitter_api_url = 'http://www.twitter.com/statuses/user_timeline/23750868.json?callback=?&count=10';
  var twitter_user = 'intentive';

  // Enable caching
  $.ajaxSetup({ cache: true });

  // Send JSON request
  // The returned JSON object will have a property called "results" where we find
  // a list of the tweets matching our request query

  $.getJSON(
    twitter_api_url,
    function(data) {
	  var tweet_html = '<div class="tweet"><ul>';
      $.each(data, function(i, tweet) {
        // Uncomment line below to show tweet data in Fire Bug console
        // Very helpful to find out what is available in the tweet objects
        //console.log(tweet);

        // Before we continue we check that we got data
        if(tweet.text !== undefined) {
          // Calculate how many hours ago was the tweet posted
          var date_tweet = new Date(Date.parse(tweet.created_at.replace(/(\+\S+) (.*)/, '$2 $1')));
          var date_now   = new Date();
          var date_diff  = date_now - date_tweet;
          var hours      = Math.round(date_diff/(1000*60*60));

          // Build the html string for the current tweet
		  tweet.text = tweet.text.replace(new RegExp("http://+[A-Za-z0-9\.\/]{13,20}", "gmi"), function(matched) {return "<a target=\"_blank\" href=\"" + matched + "\">" + matched + "</a>";});
          tweet_html    += '<li>';
          tweet_html    += tweet.text;
          //tweet_html    += ' <span class="tweet-date">' + date_tweet.toLocaleString() + '</span>';
          tweet_html    += '<\/li>';

          // Append html string to tweet_container div
        }
      });
      tweet_html    += '<\/ul><\/div>';
      $('#tweet_container').append(tweet_html);
	  $('.tweet').vTicker({
		   speed: 800,
		   pause: 6000,
		   showItems: 3,
		   animation: 'fade',
		   mousePause: false,
		   height: 0,
		   direction: 'up'
	  });
    }
  );
});
