var TwitterTicker = new Class({

	Implements: [Options],

	options: {
		delay: 5000
	},

	item: -1,
	date: null,

	initialize: function(term, container) {
		this.date = new Date();
		this.element = $(container);
		this.element.empty();
		this.twitter = new RequestTwitter({
			parameters: {
				screen_name: 'FiredroidGames',
				count: 3,
				include_rts: 1
			}
		}).addEvents({
			success: this.show.bind(this)
		});
		this.twitter.send();
	},

	show: function(data) {
		//console.log(data);
		this.tweets = data;
		// Build tweets
		for(var i=0; i < Object.getLength(this.tweets); i++) {
			var datePart = ' <span class="date">' + Date.parse(this.tweets[i].created_at).timeDiffInWords(Date.now()) + '</span>';
			this.tweets[i].html = '';
			if (this.tweets[i].retweeted_status != null) {
				this.tweets[i].html = '<strong>' + this.tweets[i].retweeted_status.user.name + '</strong><br />' + this.twitter.linkify(this.tweets[i].retweeted_status.text) . datePart;
			} else {
				this.tweets[i].html = this.tweets[i].text + datePart;
			}
			el = new Element('li', {html: this.tweets[i].html});
			el.inject(this.element, 'bottom');
		}
		this.gallery = new slideGallery($('twitter-gallery'), {
			mode: "circle",
			autoplay: false,
			duration: 4000,
			speed: 800,
			direction: "vertical"
		});
	},

	rotate: function() {
		this.item += 1;
		if (this.item > Object.getLength(this.tweets) - 1) this.item = 0;
		var tweetHtml = '';

		if (this.tweets[this.item].retweeted_status != null) {
			tweetHtml = '<img src="' + this.tweets[this.item].retweeted_status.user.profile_image_url.replace("\\",'') + '" align="left" alt="' + this.tweets[this.item].retweeted_status.user.name + '" /> <strong>' + this.tweets[this.item].retweeted_status.user.name + '</strong><br />' + this.twitter.linkify(this.tweets[this.item].retweeted_status.text);
		} else {
			tweetHtml = '<img src="' + this.tweets[this.item].user.profile_image_url.replace("\\",'') + '" align="left" alt="' + this.tweets[this.item].user.name + '" /> <strong>' + this.tweets[this.item].user.name + '</strong><br />' + this.tweets[this.item].text
		}
		var tweet = new Element('span', {

			html: tweetHtml
			}).fade('hide');
		$(this.tweet).fade('out').get('tween').chain(function() {
			this.tweet.dispose();
			this.tweet = $(tweet);
			this.element.adopt(this.tweet);
			this.tweet.fade('in');
		}.bind(this));
	}

});
