update_search = function (maxid, statistics) {
	$.getJSON('http://search.twitter.com/search.json?callback=?', {q: '#geekmeetsthlm', since_id: maxid, rpp: statistics ? 100 : 30}, function (data, textStatus) {
		var i = data.results.length - 1, box = $('#tweets'), tweet, postbody, result, url_to_a, user_to_a, hash_to_a, time, minutes;
		url_to_a = /\b(http:\/\/[a-z0-9-]+(?:\.[a-z0-9-]+)*\.[a-z]{2,4}(?:[-a-z0-9_:&?=+,.!\/*%\$]*)?)\b/gim;
		user_to_a = /@([a-z0-9-_]+)\b/gim;
		hash_to_a = /#([^"\s]+)\b/gim;

		for (; i >= 0; i--) {
			result = data.results[i];

			/*if (users[result.from_user] === undefined) {
				users[result.from_user] = {count: 1, image: result.profile_image_url};
			}
			else {
				users[result.from_user].count++;
			}*/

			if (i > 19 && statistics) {
				continue;
			}

			tweet = $('<section></section>').addClass('post');

			$('<a></a>').attr('href', 'http://twitter.com/' + result.from_user).append($('<img/>').addClass('avatar').attr({src: result.profile_image_url.replace('_normal.', '_bigger.'), width: 73, height: 73, alt: result.from_user})).appendTo(tweet);

			postbody = $('<div></div>').addClass('postbody');

			created = new Date(result.created_at);
			minutes = created.getMinutes();
			if (minutes < 10) {
				minutes = '0' + minutes;
			}

			$('<p></p>').addClass('postmeta').text(' skrev det här kl ' + created.getHours() + ':' + minutes).prepend($('<a></a>').text(result.from_user).attr('href', 'http://twitter.com/' + result.from_user)).appendTo(postbody);
			$('<p></p>').html(result.text.replace(url_to_a, '<a href="$1">$1</a>').replace(user_to_a, '<span class="at">@</span><a href="http://twitter.com/$1">$1</a>').replace(hash_to_a, '#<a href="http://search.twitter.com/search?q=%23$1">$1</a>')).appendTo(postbody);

			tweet.append(postbody).prependTo(box);
			if (!statistics) {
				tweet.hide().css('min-height', '0').slideDown(function () {
					$(this).css('min-height', '');
				});
			}
		}

		box.children(':gt(50)').remove();

		setTimeout(function() {
			update_search(data.max_id);
			}, 10000);
	});
}
