var twitter = {
	time: 10,
	running: false,
	date: false,
	ids: '',

	init: function() {
		//$('#tweets-list li:last-child').addClass('last');
		this.date = wemedia.load_time;
		this.fetch();
		if (this.time != undefined)
			this.timer();
	},

	timer: function() {
		//this.running = true;
		var tempo = this.time * 1000;
		setTimeout("twitter.update()", tempo);
	},

	update: function() {
		if (twitter.running)
			return;

		twitter.running = true;
		this.fetch();
		if (this.time != undefined)
			this.timer();
	},

	fetch: function() {
		var args = {
			nonce: wemedia.nonce,
			load_time: this.date,
			action: 'get_tweets',
			ids: this.ids
		}
		$.ajax({
			url: wemedia.url,
			data: args,
			type: 'POST',
			dataType: 'json',
			timeout: 60*60*60,
			error: this.error,
			success: function(data) {
				twitter.running = false;
				this.date = data.load_time;
				if (data.founds > 0) {
					$('#tweets .loading').hide('fast');
					$('#tweets-list').show();
					$.each(data.tweets, function(i, item) {
						//alert(item.author);
						twitter.ids += item.id+',';
						$('<li class="tweet-item"></li>')
							.html(item.avatar+item.author+item.content+item.permalink)
							.attr('id', 'tweet-'+item.id)
							.prependTo('#tweets-list');
						$('tweet-'+item.id).hide();
						$('tweet-'+item.id).show('slow');

						if ($('#tweets-list li').length > 1) {
							$('#tweets-list li:last-child').hide('slow').remove();
						}
						//alert(twitter.running);
					});
					$('#tweets-list li').removeClass('last');
					$('#tweets-list li:last-child').addClass('last');
				}
			}
		});
	},

	error: function(reques, status, thrown) {
		twitter.running = false;
	}
}

$(document).ready(function(){
	twitter.init();
	//Carousel	
	$('#carousel').jcarousel({ scroll: 3, animation: 1500 });
	
});