/**
 * Fetches the public tweets for the twitter account.
 * Requires a <div id="twitter_feed"> somewhere on the page where you want the feeds embedded.
 *
 * @author Ben Barber (bbarber@i2rd.com)
 * @require i2rd-util.js
 */
(function(){

var twitterAccount = 'jollytimepops'; 
var maxTweets = 2;
var maxLength = 60; // characters. set to -1 to show full tweet.

function renderFeed(tweets) {
  var container = document.getElementById('twitter_feed');
  if (!container) return;

  var output = '<div class="articlecontainer"><ol class="ac_style_list">';
  for (var i = 0, mx = tweets.length; i < mx; i++) {
    var tweet = tweets[i];
    output += '<li class="ac_item">';
    output += '<a target="_blank" title="'+tweet.text+'" href="http://twitter.com/' + twitterAccount + '">';
   //output += '<a target="_blank" title="'+tweet.text+'" href="http://twitter.com/' + twitterAccount + '/status/'+ tweet.id +'">';
    output += truncate(tweet.text);
    output += '</a>';
    output += '</li>';
  }
  output += '</ol></div>';
  output += '<div class="read_more"><a href="http://twitter.com/' + twitterAccount + '" title="Go to Twitter Account" >Read More</a></div>';
  container.innerHTML = output;
}

i2rd.addEvent(window, 'load', function(){
  var script = document.createElement('script');
  script.type="text/javascript";
  script.src = 'https://twitter.com/statuses/user_timeline.json?callback=renderFeed&count='+maxTweets+'&screen_name='+twitterAccount;
  document.body.appendChild(script);
});

function truncate(text) {if ((maxLength && maxLength > 0) && text.length > maxLength) return text.substr(0, maxLength-1) + "..."; else return text;}

// Expose callback
window.renderFeed = renderFeed;

})();
