// JavaScript Document

var accounts=['GShock_UK'];//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>300)status=status.substring(0,)+'...';
//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_str,
status:'<li><a href="http://twitter.com/'+username+'/statuses/'+twitters[i].id_str+'"><span class="tweetStatus">'+status+'</span></a><br /><br /><span class="tweetTime"> '+twitter_relative_time(twitters[i].created_at)+'</span><br /></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_relative_time(time_value) {
var values=time_value.split(" ");
time_value=values[1]+" "+values[2]+", "+values[5]+" "+values[3];

//time_value = dateFormat(time_value, "dddd, mmmm dS, yyyy, h:MM:ss TT");
return time_value;
}


function twitter_animate(){
//$("#twitter_update_list li").eq(twitter_current).fadeOut(100,function(){
//$(this).css({display:'none'});
//twitter_current++;
//if(twitter_current==twitter_count)twitter_current=0;
//$("#twitter_update_list li").eq(twitter_current).fadeIn(100);
//});
}
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++)$("#twitter_update_list").append(global_tweets[i].status);
//fade in the first tweet and after that set the timer to launch the animation
$("#twitter_update_list li").eq(0).fadeIn(100,function(){
twitter_timer=setInterval(twitter_animate,5000);
});
//hover state for tweets
$("#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
$.getScript('http://twitter.com/status/user_timeline/'+accounts[current]+'.json?count=2&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
$(function(){
//run the get feed function for the first accounts (index=0)
twitter_init(0);
});

