function GetTweets(div, search, count, progressbar) {
	var dataString = "http://search.twitter.com/search.json?q=" + encodeURIComponent(search) + "&callback=?&lang=en&rpp=" + count;
	$(progressbar).show();
	$.getJSON(dataString,
	function(data){
		$(div).empty();
		var curDate = new Date();
		$.each(data.results, function(i, item){
			var postedBy = item.from_user;
			var text = item.text;
			var postDate = new Date(item.created_at);
			var id = item.id;

			var text = FormatTweetText(text);

			var html = '';
			html += '<li>';
			html += '	<a href="http://twitter.com/' + postedBy + '" target="_blank"><b>' + postedBy + '</b></a>: <span id="msgtxt' + id + '">' + text + '</span>';
			html += '	&middot; <a style="font-size: 85%;" href="http://twitter.com/' + postedBy + '/statuses/' + id + '" target="_blank">' + GetTimeDiff(curDate, postDate) + '</a>';
			html += '</li>';
			$(div).append(html);
		});
	});
	$(progressbar).hide();
	return false;
}

function GetTweetsEx(div, search, count, progressbar) {
	var dataString = "http://search.twitter.com/search.json?q=" + encodeURI(search) + "&callback=?&lang=en&rpp=" + count;
	$(progressbar).show();
	$.getJSON(dataString,
	function(data){
		$(div).empty();
		var curDate = new Date();

		$.each(data.results, function(i, item){
			var postedBy = item.from_user;
			var originalText = item.text;
			var postDate = new Date(item.created_at);
			var id = item.id;

			var text = FormatTweetText(originalText);

			var html = '';
			html = '<li>';
			html += '	<div class="avatar">';
			html += '		<a href="http://twitter.com/' + postedBy + '" target="_blank"><img alt="' + postedBy + '" src="' + item.profile_image_url + '" /></a>';
			html += '	</div>';
			html += '	<div class="msg">';
			html += '		<a href="http://twitter.com/' + postedBy + '" target="_blank">' + postedBy + '</a>: <span id="msgtxt' + id + '" class="msgtxt en">' + text + '</span>';
			html += '		<div class="info" style="text-align:right;">';
			html += GetTimeDiff(curDate, postDate);
			html += ' &middot; ';
			html += '			<a href="http://twitter.com/home?status=@' + postedBy + '%20&in_reply_to_status_id=' + id + '&in_reply_to=' + postedBy + '" class="lit" target="_blank">Reply</a>';
			html += ' &middot; ';
			html += '			<a href="http://twitter.com/home?status=' + encodeURI('RT: @' + postedBy + ' ' + originalText) + '" class="lit" target="_blank">Retweet</a>';
			html += ' &middot; ';
			html += '			<a href="http://twitter.com/' + postedBy + '/statuses/' + id + '" class="lit" target="_blank">View Tweet</a>';
			html += '		</div>';
			html += '		<p class="clearleft"></p>';
			html += '		<div id="thr' + id + '" class="thread" style="display:none;"></div>';
			html += '	</div>';
			html += '</li>';
			$(div).append(html);
		});
	});
	$(progressbar).hide();
	return false;
}

function GetTimeDiff(date1, date2) {
	var ONE_DAY = 1000 * 60 * 60 * 24;
	var ONE_HOUR = 1000 * 60 * 60;
	var ONE_MIN = 1000 * 60;
	var ONE_SEC = 1000;

	var date1_ms = date1.getTime();
	var date2_ms = date2.getTime();

	var diff_ms = Math.abs(date1_ms - date2_ms);

	var diff_days = Math.round(diff_ms/ONE_DAY);

	if (diff_days > 0) {
		return 'about ' + diff_days + ' day' + GetPluralSuffix(diff_days) + ' ago';
	}

	var diff_hours = Math.round(diff_ms/ONE_HOUR);

	if (diff_hours > 0) {
		return 'about ' + diff_hours + ' hour' + GetPluralSuffix(diff_hours) + ' ago';
	}

	var diff_mins = Math.round(diff_ms/ONE_MIN);

	if (diff_mins > 0) {
		return '' + diff_mins + ' minute' + GetPluralSuffix(diff_mins) + ' ago';
	}

	var diff_secs = Math.round(diff_ms/ONE_SEC);

	return '' + diff_secs + ' second' + GetPluralSuffix(diff_secs) + ' ago';
}

function GetPluralSuffix(number) {
	if (number == 1) {
		return '';
	} else {
		return 's';
	}
}

function FormatTweetText(tweetText)
{
	var formattedText = tweetText;

	formattedText = FormatTweetURL(formattedText);
	formattedText = FormatTweetUser(formattedText);
	formattedText = FormatTweetHash(formattedText);

	return formattedText;
}

function FormatTweetURL(tweetText)
{
  var regexp = /((ftp|http|https):\/\/(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?)/gi;
  return tweetText.replace(regexp, "<a href=\"$1\">$1</a>");
}

function FormatTweetUser(tweetText)
{
  var regexp = /[\@]+([A-Za-z0-9-_]+)/gi;
  return tweetText.replace(regexp, "<a href=\"http://twitter.com/$1\">@$1</a>");
}

function FormatTweetHash(tweetText)
{
  var regexp = / [\#]+([A-Za-z0-9-_]+)/gi;
  return tweetText.replace(regexp, ' <a href="http://search.twitter.com/search?q=$1">#$1</a>');
}

