$(document).ready(function() {
$('#tweet-from').coolinput({hint:'your name'});
	$('#tweet-message').coolinput({hint:'your message'});
	var current_tweet = $('.twtr-reference-tweet .twtr-tweet').length;
	var tweetId = 1;
	var tweets = '';

	var twtTicker = setInterval(function() {
		$('.twtr-reference-tweet .twtr-tweet:nth-child(' + current_tweet + ')').clone().prependTo('.twtr-tweets');
		$('.twtr-tweets .twtr-tweet:first-child').attr('id', 'tweet-id-' + tweetId).css({border: 'none'});
		if (tweetId == 1) {
			$('#tweet-id-1').children('hr').remove();
		}
		$('#tweet-id-' + tweetId).slideDown(1000, function() {
			current_tweet -= 1;
			tweetId += 1;
		});
		if (current_tweet < 0)	{
			current_tweet = $('.twtr-reference-tweet .twtr-tweet').length;
		}
		if (tweetId > 10) {
			$('#tweet-id-' + (tweetId - 10)).remove();
			$('#tweet-id-' + (tweetId - 9)).children('hr').remove();
		}
	}, 3000);
	var twtLoad = setInterval(function() {
		$('div[class^="twtr-spinner"]').removeClass('twtr-inactive').addClass('twtr-active');
		$.get('/twitter/list', function(data) {
			if (data.length > 10) {
				tweets = data;
				$('.twtr-reference-tweet').html(data);
			}
			$('div[class^="twtr-spinner"]').removeClass('twtr-active').addClass('twtr-inactive');
		});
	}, 50000);
	
	$('#tweet-my-web').submit(function(e) {
		e.preventDefault();

		if ($.trim($('#tweet-from').val()) == '') {
			alert('Please enter your name.');
			$('#tweet-from').val('').focus();
			return false;
		} else if ($.trim($('#tweet-message').val()) == '') {
			alert('Please enter your message.');
			$('#tweet-message').val('').focus();
			return false;
		}
		if ($.trim($('#tweet-from').val()).length > 0 && $.trim($('#tweet-message').val()).length > 0) {
			$.post('/twitter/tweet', $('#tweet-my-web').serialize(), function(data) {
				$('.twtr-reference-tweet').prepend(data);
				$('#tweet-message').val('').focus();
			});
		}

		return false;
	});
});