var accounts=['mstizzle','KNOWHOPETWEETS','willmcginniss'];//array with usernames
var accounts_last=accounts.length-1;//set last item index
var global_tweets=[];//global tweets array
var twitter_timer=null;//used for timed LIs switching
var twitter_current=0;
var twitter_count=0;

function twitter_parser(twitters){
  //loop through tweets from current 'twitters' variable
  for(var i=0;i<twitters.length;i++){
    //get the screen name
    var username=twitters[i].user.screen_name;
    //get the tweet text
    var status=twitters[i].text;
    //cut status if it's to long
    if(status.length>120)status=status.substring(0,119)+'...';
    //create the html structure, whole line will link to the tweet page
    global_tweets.push({
      //id for sort function by the latest entry
      id:twitters[i].id,
      status:'<li><a href="http://twitter.com/'+username+'/statuses/'+twitters[i].id+'">@'+username+'\'s Tweet: <span>'+status+'</span> '+twitter_relative_time(twitters[i].created_at)+'</a></li>'
    });
  }
}

//from twitter.com/javascripts/blogger.js - change the timestamp to text version
function twitter_relative_time(time_value) {
  var values=time_value.split(" ");
  time_value=values[1]+" "+values[2]+", "+values[5]+" "+values[3];
  var parsed_date=Date.parse(time_value);
  var relative_to=(arguments.length>1)?arguments[1]:new Date();
  var delta=parseInt((relative_to.getTime()-parsed_date)/1000);
  delta=delta+(relative_to.getTimezoneOffset()*60);
  if(delta<60){return 'less than a minute ago';}else if(delta<120){return 'about a minute ago';
  }else if(delta<(60*60)){return (parseInt(delta/60)).toString()+' minutes ago';
  }else if(delta<(120*60)){return 'about an hour ago';
  }else if(delta<(24*60*60)){return 'about '+(parseInt(delta/3600)).toString()+' hours ago';
  }else if(delta<(48*60*60)){return '1 day ago';
  }else{return(parseInt(delta/86400)).toString()+' days ago';}
}

/*function twitter_animate(){
  jQuery("#twitter_update_list li").eq(twitter_current).fadeOut(500,function(){
    jQuery(this).css({display:'none'});
    twitter_current++;
    if(twitter_current==twitter_count)twitter_current=0;
    jQuery("#twitter_update_list li").eq(twitter_current).fadeIn(500);
  });
}
*/
function twitter_render(){
  //sort global_tweets by 'id'
  global_tweets.sort(function(a,b){return(b.id-a.id)});
  twitter_count=global_tweets.length;
  //loop through the tweets and add them to #twitter_update_list
  for(var i=0;i<global_tweets.length;i++)jQuery("#twitter_update_list").append(global_tweets[i].status);
  //fade in the first tweet and after that set the timer to launch the animation
  jQuery("#twitter_update_list li").eq(0).fadeIn(100,function(){
    //twitter_timer=setInterval(twitter_animate,9000);
  });
  //hover state for tweets
  jQuery("#twitter_update_list a").hover(function(){
    //stop the animation
    clearInterval(twitter_timer);
    twitter_timer=null;
  },function(){
    //and start it again on mouse out
    //twitter_timer=setInterval(twitter_animate,5000);
  });
}

function twitter_init(current){
  //get the 15 tweets for current user feed with twitter_parser function as a callback
  jQuery.getScript('http://twitter.com/status/user_timeline/'+accounts[current]+'.json?count=15&callback=twitter_parser',function(){
    //if not last feed get next one else render the array
    if(current<accounts_last){
      //run this function (recursivly) to get all tweets for the next user
      twitter_init(++current);
    }else{
      //if all tweets are loaded into the global_tweets array and launch twitter_render function
      twitter_render();
    }
  });
}

/*Added in myself to display twitter avatar
function showAvatar(twitters) {
var statusHTML = [];
for (var i=0; i<twitters.length; i++){
var username = twitters[i].user.screen_name;
var followers_count = twitters[i].user.followers_count;
var profile_image_url = twitters[i].user.profile_image_url;
statusHTML.push('<a href="http://twitter.com/'+username+'"><img src="'+profile_image_url+'" style="border:none;"/></a><br /><b>@'+username+'</b>');
}
document.getElementById('twitter_avatar').innerHTML = statusHTML.join('');
}*/

//onload statement
jQuery(function(){
  //run the get feed function for the first accounts (index=0)
  twitter_init(0);
});
