Interpeople.Twitter = function()
{
	var _self = this;
	
	function tweetSort( a, b )
	{
		var result = 0;
		
		if( a.id < b.id )
		{
			result = 1;
		}
		else if( a.id > b.id )
		{
			result = -1;
		}
		
		return result;
	}

	function init()
	{
		var allTweets = [];
		
		$.getJSON("http://api.twitter.com/1/statuses/user_timeline.json?screen_name=Interpeople1&callback=?",
			function( searchData )
			{
				$.each(searchData,
					function( i, searchResult )
					{
						allTweets.push(searchResult);
					}
				);
				
				$.getJSON("http://api.twitter.com/1/statuses/user_timeline.json?screen_name=ChrisBates12&callback=?",
						function( searchData )
						{
							$.each(searchData,
								function( i, searchResult )
								{
									allTweets.push(searchResult);
								}
							);
							
							$.getJSON("http://api.twitter.com/1/statuses/user_timeline.json?screen_name=ConorOBrien7&callback=?",
									function( searchData )
									{
										$.each(searchData,
											function( i, searchResult )
											{
												allTweets.push(searchResult);
											}
										);
										
										$.getJSON("http://api.twitter.com/1/statuses/user_timeline.json?screen_name=AlastairHaldane&callback=?",
												function( searchData )
												{
													$.each(searchData,
														function( i, searchResult )
														{
															allTweets.push(searchResult);
														}
													);
													
													allTweets.sort(tweetSort);
													
													displayTweets(allTweets);
												}
											);
										
									}
								);
							
						}
					);
				
			}
		);
	}

	function displayTweets( tweets )
	{
		$('#twitterModule .scrollable .items').empty();
		var pattern = /test/;
		var excludePattern = /^@/;

		$.each(tweets, function(index, value) {
			var text = value.text;
			
			
			var originalText = text;
			var text = text.substr(0, 111);
			if( text != originalText )
			{
				text = text + '...';
			}
			
			text = text.replace(/@(\w+)/g, function( $0, $1 )
			{
				return '<a href="http://twitter.com/' + $1.toString() + '" target="_blank">@' + $1.toString() + '</a>'
			});
			text = text.replace(/#(\w+)/g, function( $0, $1 )
			{
				return '<a href="http://twitter.com/search/%23' + $1.toString() + '" target="_blank">#' + $1.toString() + '</a>'
			});
			
			if( !value.from_user && value.user )
			{
				value.from_user = value.user.screen_name;
				value.from_user_id = value.user.id;
			}
					
			var tweetInfoDiv = $('<div class="tweetInfo"></div>');
			tweetInfoDiv.append('<div class="tweetSnippet"><a href="http://twitter.com/#!/@' + value.from_user + '/status/' + value.id_str + '/" target="_blank">' + value.user.name + '</a>: ' + text + '</div><span class="tweetTime">' + convert_twitter_date(value.created_at) + '</span>');

			$('#twitterModule .scrollable .items').append(tweetInfoDiv);
		});

		$('#twitterModule .scrollable').scrollable({ circular: true }).autoscroll({
			interval: 12000
		});
	}

	function convert_twitter_date( a )
	{
		var K = function ()
		{
		    var a = navigator.userAgent;
			return {
				ie: a.match(/MSIE\s([^;]*)/)
			}
		}();

		var H = function (a)
		{
			var b = new Date();
			var c = new Date(a);
			/*if (K.ie && !($.browser.msie && $.browser.version == '8.0')) {
				c = Date.parse(a.replace(/( \+)/, ' UTC$1'))
			}*/

			var d = b - c;
			var e = 1000,
				minute = e * 60,
				hour = minute * 60,
				day = hour * 24,
				week = day * 7;
			if (isNaN(d) || d < 0)
			{
				return "";
			}
			if (d < e * 7)
			{
				return "right now"
			}
			if (d < minute)
			{
				return Math.floor(d / e) + " seconds ago"
			}
			if (d < minute * 2)
			{
				return "about 1 minute ago"
			}
			if (d < hour)
			{
				return Math.floor(d / minute) + " minutes ago"
			}
			if (d < hour * 2)
			{
				return "about 1 hour ago"
			}
			if (d < day)
			{
				return Math.floor(d / hour) + " hours ago"
			}
			if (d > day && d < day * 2)
			{
				return "yesterday"
			}
			if (d < day * 365)
			{
				return Math.floor(d / day) + " days ago"
			}
			else
			{
				return "over a year ago"
			}
		};
		return H(a);
	}

	init();
};
