$(function() {
	$("#twitterGonow").show();
	
	$(".up").click(function() {
		$.getJSON(base+'/twitter/getNewest/ajax?currentId=' + $('#tweetId').val().replace(/[\.,]/g, ''),
			function(tweet) {
				if (tweet.id != null) {
					populate(tweet);
					$(".down").show();
				}
			}
		);	
	});
	
	$(".down").click(function() {
		$.getJSON(base+'/twitter/getOlder/ajax?currentId=' + $('#tweetId').val().replace(/[\.,]/g, ''),
			function(tweet) {
				if (tweet.id != null) {
					populate(tweet);
				} else {
					$(".down").hide();
				}
			}
		);	
	});
	
	function populate(tweet) {
		$("#twitterGonow").fadeOut("fast", function(){
			$("#tweetId").val(tweet.id);
			$("#twitterGonow img").attr("src", tweet.imageUrl);
			$("#twitterGonow .who").html(tweet.name);
			$("#twitterGonow .text").html(tweet.text);
			
			retweet(tweet.text);

			$("#twitterGonow").fadeIn("slow");
		});
	}
	
	function retweet(text) {
		var url = "http://twitter.com/home/?";
		var param = $.param({
			status: "RT " + text
		});
		
		$("#retweet").attr("href", url + param);
	}
	
	retweet($("#twitterGonow .text").html());
});
