
var DateHelper = {
	// Takes the format of "Jan 15, 2007 15:45:00 GMT" and converts it to a relative time
	// Ruby strftime: %b %d, %Y %H:%M:%S GMT
	time_ago_in_words_with_parsing: function(from) {
		var date = new Date; 
		date.setTime(Date.parse(from));
		return this.time_ago_in_words(date);
	},

	time_ago_in_words: function(from) {
		return this.distance_of_time_in_words(new Date, from);
	},

	distance_of_time_in_words: function(to, from) {
		var distance_in_seconds = ((to - from) / 1000);
		var distance_in_minutes = (distance_in_seconds / 60).floor();

		if (distance_in_minutes == 0) { return 'less than a minute ago'; }
		if (distance_in_minutes == 1) { return 'a minute ago'; }
		if (distance_in_minutes < 45) { return distance_in_minutes + ' minutes ago'; }
		if (distance_in_minutes < 90) { return 'about 1 hour ago'; }
		if (distance_in_minutes < 1440) { return 'about ' + (distance_in_minutes / 60).floor() + ' hours ago'; }
		if (distance_in_minutes < 2880) { return '1 day ago'; }
		if (distance_in_minutes < 43200) { return (distance_in_minutes / 1440).floor() + ' days ago'; }
		if (distance_in_minutes < 86400) { return 'about 1 month ago'; }
		if (distance_in_minutes < 525960) { return (distance_in_minutes / 43200).floor() + ' months ago'; }
		if (distance_in_minutes < 1051199) { return 'about 1 year ago'; }

		return 'over ' + (distance_in_minutes / 525960).floor() + ' years ago';
	}
};


String.prototype.linkify = function() {
	return this.replace(/[A-Za-z]+:\/\/[A-Za-z0-9-_]+\.[A-Za-z0-9-_:%&\?\/.=]+/g,
		function(m){
			return m.link(m);
		}
	);
};


String.prototype.linkuser = function() {
	return this.replace(/[@]+[A-Za-z0-9-_]+/g,
		function(u) {
			var username = u.replace("@", "");
			return u.link("http://twitter.com/" + username);
		}
	);
};

String.prototype.link_text = function() {
	return this.replace(/\b(((https*\:\/\/)|www\.).+?)(([!?,.\)]+)?(\s|$))/g,
		function(c){
			var e = c.startsWith("www") ? "http://" + c : c;
			return '<a target="_blank" href="' + e + '">' + e + "</a>"
		}
	);
};

(function(){
	var id = 0, head = $$('head')[0], global = this;
	global.getJSON = function(url, callback) {
		var script = document.createElement('script'), token = '__jsonp' + id;
		global[token] = callback;
		script.src = url.replace(/\?(&|$)/, '__jsonp' + id + '$1');
		script.onload = function() {
			script.remove();
			script = null;
			delete global[token];
		};
		head.appendChild(script);	
		id++;
	}
})();


var TwitterTicker = Class.create({

	initialize: function(){		
		this.data = {};
		this.container = $('TwitterTicker');
		this.auto = true;
		this.query = 'from:SimplyBowen';
		this.rate = 10000;

		if(this.auto)	
			this.revealPanel();
	},

	revealPanel: function() {
		new Effect.Morph($('TwitterTicker'), {
			style: {height : '40px'},
			afterFinish: function(){
				this.fetchTweetsFromTwitter();
			}.bind(this)
		});
	},

	fetchTweetsFromTwitter: function(){
		this.tweetNumber = 0;
		var url = 'http://twitter.com/statuses/user_timeline.json?screen_name=SimplyBowen&callback=?&count=30&refresh=true'
		getJSON(url, function(json){
			var data = json.reverse().map(
				function(tweet){
					return {
								tweet: tweet.text.link_text(),
								user: tweet.user.screen_name,
								time: tweet.created_at,
								id: tweet.id
							};
				}
			);
			
			if(data.size() == 0)
				this.showNotice();
			else
				this.displayTweet(data);

		}.bind(this));
	},

	displayTweet: function(tweets) {
		this.flashChirp(tweets[this.tweetNumber]);
		if(++this.tweetNumber < tweets.size())
			this.showTimer = setTimeout(this.displayTweet.bind(this,tweets), this.rate);
		else {
			this.fetchTweetsFromTwitter();
		}
	},

	flashChirp: function(chirp) {
		var tweet = new Element('dl');
		var description = chirp.tweet + "<br />";
		description += "<small><a href=\"http://twitter.com/" + chirp.user + "/status/" + chirp.id + "\">" + DateHelper.time_ago_in_words_with_parsing(chirp.time) + "</a></small>";
		tweet.appendChild(new Element('dd').update(description));
		this.tweet = tweet;
		this.container.update(tweet.hide());
		Effect.Appear(tweet);
		this.fadeTimer = setTimeout(this.fadeChirp.bind(this, tweet), this.rate - 1000);
	},

	showNotice: function() {
		var msg = new Element('h3').update('<strong>Whoops! No Tweets were found.</strong>');
		this.container.update(msg.hide());
		Effect.Appear(msg);
	},

	fadeChirp: function(tweet) {Effect.Fade(tweet);}
});

var tt = new TwitterTicker();

