function parseTwitterDate(text) {
	//running regex to grab everything after the time
	var newtext = text.replace(/(\d{1,2}[:]\d{2}[:]\d{2}) (.*)/, '$2 $1');
	//moving the time code to the end
	newtext = newtext.replace(/(\+\S+) (.*)/, '$2 $1')
	var date = new Date(Date.parse(newtext));
	return date;
}

$(document).ready(function() {
	$("#slideshow").cycle();
	
	var username='LeicsPA', format='json', tweets, i, source, li_class;
	var url='http://api.twitter.com/1/statuses/user_timeline/'+username+'.'+format+'?callback=?'; // make the url
	
	$.getJSON(url,function(tweets){ // get the tweets
		var exp = /(\b(https?|ftp|file):\/\/[-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|])/ig;
		$('div#tweet').html('');
		for(i in tweets) {
			if(i < 1) {
				var tweetDate = parseTwitterDate(tweets[i].created_at);
				var now = new Date();
				var elapsedInMS = now.getTime() - tweetDate.getTime();
				var time_since  = (elapsedInMS / 1000 / 60);
				var time_label = 'minute';
				if(Math.floor(time_since)<1) {
					time_since = Math.round(time_since * 60);
					time_label = 'seconds';
				}
				else{
					time_since = Math.floor(time_since);
					if(time_since > 1) time_label = 'minutes';
					
					if(time_since>60) {
						time_since = Math.floor(time_since / 60);
						if(time_since == 1) time_label = 'hour';
						else time_label = 'hours';
						
						if(time_since > 24) {
							time_since = Math.floor(time_since / 24);
							if(time_since == 1) time_label = 'day';
							else time_label = 'days';
						}
					}
				}
				li_class = '';
				if(i==0) li_class = ' first';
				source = tweets[i].source.replace('&lt;/a&gt;','');
				source = source.replace('&lt;', '<');
				source = source.replace('&gt;', '>');
				source = source.replace(/<.*?>/g, '');
				$("div#tweet").append('<p class="text">' + tweets[i].text.replace(exp,"<a href='$1'>$1</a>") + '<span class="date">' + time_since + ' ' + time_label + ' ago</span></p>');
			}
		}
	});
});
