var accounts=['sybasematchplay'];//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>60)status=status.substring(0,59)+'...';
    //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 class="row"><a href="http://twitter.com/'+username+'/statuses/'+twitters[i].id+'">@'+username+'\'s Tweet: <span>'+status+'</span><br /> '+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_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);
  }
  jQuery("#twitter_update_list li").each(function(index){
	jQuery(this).addClass('row'+index%2);
  });
}

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=5&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();
    }
  });
}

//onload statement
jQuery(function(){
  //run the get feed function for the first accounts (index=0)
  twitter_init(0);
});
