var query = 'onsightnl';
$(function() {
  $.getJSON("http://search.twitter.com/search.json?callback=?&q=" + query, 
    function (r) {
      setResults(r);
  });
});

var results = [];
var index = 0;
function setResults(data) {
  results = data.results;
  update();
}

function setTweet(result) {
  var text = result.text.replace(/(https?:\/\/[^\s]+)/g,'<a target="_blank" href="$1">$1</a>');
  text = text.replace(/@([a-z0-9]*)/g,'<a target="_blank" href="http://twitter.com/$1">@$1</a>');
  text = "&quot;" + text + "&quot;"
  $("#tweetBody").html(text);
}

function update() {
  setTweet(results[index]);
  index += 1;
  if(index == results.length) {
    index = 0;
  }
  setTimeout(update,8000);
}
