$(document).ready(function() {
	if ($("#notifications .notification").length>1) {
		thengo();
	}
	
	//Grab the JSON data, no callback needed. Alter COUNT querystring as needed.
	//$.getJSON('http://twitter.com/statuses/user_timeline/SunshinePolicy.json?count=2&callback=?',
	$.getJSON('http://api.twitter.com/1/statuses/user_timeline.json?screen_name=SunshinePolicy&count=30&callback=?',
		function(json){
			//Loop through each record
			$.each(json, function(i, item){
				
				if (i==2)
					return false;
				
				//Grab required variables
				var id 			= item.id_str;
				var username 	= item.user.screen_name;
				var url 		= 'http://twitter.com/#!/'+username+'/status/'+id;
				var status 		= item.text.replace(/((https?|s?ftp|ssh)\:\/\/[^"\s\<\>]*[^.,;'">\:\s\<\>\)\]\!])/g, function(url) {
											return '<a href="'+url+'" target="_blank">'+url+'</a>';
										}).replace(/\B@([_a-z0-9]+)/ig, function(reply) {
											return reply.charAt(0)+'<a href="http://twitter.com/#!/'+reply.substring(1)+'" target="_blank">'+reply.substring(1)+'</a>';
										}).replace(/\B#([_a-z0-9]+)/ig, function(reply) {
											return reply.charAt(0)+'<a href="http://twitter.com/#!/search/'+reply.substring(1)+'" target="_blank">'+reply.substring(1)+'</a>';
										});
				//var status 		= item.text;
				
				
				//Append records to container
				$('#twitter-list').append('<li><p><strong><a href="' + url + '">' + username + '</a>: </strong>' + status + ' ' + relative_time(item.created_at) + '</p></li>');
				
			});
			
			$('#twitter-list').slideDown('2');
	});
});

function thengo() {
	setInterval("slideSwitch()", 8000);
}

var loop = 1;
function slideSwitch() {
	if ($("#notifications .notification").length>1) {
		$("#notifications .notification").each(function(i) {
			if (loop==i) {
				$(this).css({"z-index": "2"}).fadeIn("slow");
			} else {
				$(this).css({"z-index": "1"}).fadeOut("slow");
			}
		});
		
		loop = loop + 1;
		
		if (loop == $("#notifications .notification").length)
			loop = 0;
	}
}

function relative_time(time_value) {
	var values = time_value.split(" ");
	time_value = values[1] + " " + values[2] + " " + values[5] + " " + values[3];
	var parsed_date = new Date();
	parsed_date.setTime(Date.parse(time_value));
	var months = new Array('Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec');
	var m = parsed_date.getMonth();
	var postedAt = '';
	postedAt = months[m];
	postedAt += " "+ parsed_date.getDate();
	postedAt += ","
	postedAt += " "+ parsed_date.getFullYear();
	return postedAt;
} 
