//Adds twitter statuses to html
function twitterCallback2(twitters)
{
  var statusHTML = [];
  var displayTweets = 0;

  for (var i=0; i<twitters.length; i++)
  {
    //var username = twitters[i].user.screen_name;

    var status = twitters[i].text.replace(/((https?|s?ftp|ssh)\:\/\/[^"\s\<\>]*[^.,;'">\:\s\<\>\)\]\!])/g, function(url) {
      return '<a href="'+url+'">'+url+'</a>';
    }).replace(/\B@([_a-z0-9]+)/ig, function(reply) {
      return  reply.charAt(0)+'<a href="http://twitter.com/'+reply.substring(1)+'">'+reply.substring(1)+'</a>';
    });

    var linkEscape = new RegExp("\\[]"); // JR writes '[]' in a tweet when the tweet has
					// a link and nonetheless should appear.
    var hasEscape = linkEscape.test(status); // check whether to override showing link
    var aLink = new RegExp("t.co"); //all links contain this expression
    var hasLink = aLink.test(status)  //check if the tweet contains a link
    if (hasEscape || !hasLink)
    {
     //Then we don't have a link, or it's over-ridden.
        statusHTML.push('<span class="tweet"><i>' +status+ '</i><div class="twitter_when">'+get_time_elapsed(twitters[i].created_at)+'</div></span>');

        displayTweets++;
        if(displayTweets > 2) //when three tweets are displayed on the site
            break;
    }
  }

  if (document.getElementById('twitter_update_list'))
   document.getElementById('twitter_update_list').innerHTML = statusHTML.join('');
}


function get_time_elapsed(time_value)
{
  var d = new Date(time_value);
  var n = new Date();
  n = n.getTime();
  d = d.getTime();
  d = n - d;
  n = Math.floor( d / 3600000 );
  if (n > 0)
   return "" + n + " hours ago";
  n = Math.floor( d / 60000 );
  if (n > 0)
   return "" + n + " minutes ago";
  return "" + Math.floor (d / 1000) + " seconds ago";
}

$(document).ready(function() {
    $.getScript("http://twitter.com/statuses/user_timeline/jancisrobinson.json?callback=twitterCallback2&count=15");
});


