//Format the JSON returned by the Twitter API 

function TBW_twitterformat(twitters,maxtwits) {
  var statusHTML = [];
  for (var i=0; i<twitters.length & i<maxtwits; 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>';
    }).replace(/[#]+[A-Za-z0-9-_]+/g, function(t) {
		var tag = t.replace("#","%23")
		return t.link("http://search.twitter.com/search?q="+tag);
	});
    statusHTML.push('<li><span class="TBW_Tweets">'+status+'</span> <span class="TBW_Time"><a href="http://twitter.com/'+username+'/statuses/'+twitters[i].id_str+'">'+Loc_relative_time(twitters[i].created_at)+'</span></a></li>');
  }
  return (statusHTML.join(''));
}

function TBW_ProcessWidgets()
{
    var jTB = jQuery.noConflict()
    jTB(".TBW_Data" ).each(function( intIndex,Element ){
       jTB("#"+Element.id).html("<span class=TBW_Error>Timeout or "+jTB("#"+Element.id).attr("data-TBWtwitterid")+" is invalid</span>");
       jTB.getJSON(jTB("#"+Element.id).attr("data-TBWjsonurl"),function(data) {
                jTB("#"+Element.id).html(TBW_twitterformat(data,jTB("#"+Element.id).attr("data-TBWcount")));
            });
        });        
}

TBW_ProcessWidgets();

//http://dev.twitter.com/pages/follow_button
//Now load the widgets script for the follow button
(function(){
    var twitterWidgets = document.createElement('script');
    twitterWidgets.type = 'text/javascript';
    twitterWidgets.async = true;
    twitterWidgets.src = 'http://platform.twitter.com/widgets.js';
    document.getElementsByTagName('head')[0].appendChild(twitterWidgets);
  })();

 
