
var TwitterFeed = new Class({

	initialize: function(feed, div, count, maxLength) {
		this.feed = feed;
		this.div = div;
		this.count = count ? count : 3;
		this.maxLength = maxLength ? maxLength : 100;
	},

	loadJS: function(src) {
		var el = document.createElement('script');
		el.setAttribute("type","text/javascript");
		el.setAttribute("src", src);
		if (el) {
			document.getElementsByTagName("head")[0].appendChild(el);
		}
	},

	createTweet: function(text, date, id) {
		var container = new Element('div', {
			'class': 'tweet'
		}).inject(this.div);
		new Element('a', {
			html: text,
			href: 'http://twitter.com/' + this.feed + '/status/' + id,
			target: '_new',
			'class': 'tweetText'
		}).inject(container);
		new Element('br').inject(container);
		new Element('a', {
			html: date.format("%B %e, %Y"),
			href: 'http://twitter.com/' + this.feed + '/status/' + id,
			target: '_new',	
			'class': 'tweetDate'
		}).inject(container);
	},

	twitterCallback: function(tweets) {
		var count = 0;
		for (var i = 0; i < tweets.length; i++) {
			var tweet = tweets[i];
			var textTrunk = tweet.text.substr(0, this.maxLength);
			if (tweet.text.length > this.maxLength)
				textTrunk += '...';
			if (tweet.in_reply_to_screen_name)
				continue;
			this.createTweet(textTrunk, Date.parse(tweet.created_at), tweet.id_str);
			count++;
			if (count >= this.count)
				break;
		}
		this.div.style.display = 'block';
	},
	
	load: function() {
		window._twitterCallback = this.twitterCallback.bind(this);

		this.loadJS('https://twitter.com/statuses/user_timeline/' + this.feed
				+ '.json?callback=_twitterCallback&count=10');
	}
});

