// AT NAMESPACE
// used for all JavaScript functions on the website -- mainly the twitter feed :)
var at = {
	copyright: "All content Copyright(C) Arthur Thornton 2009. All rights reserved.",
	continueUpdating: false,
	getTwitter: function() {
		this.timeLeft = 0;
		this.continueUpdating = false;
		$("#twitter").html("<div class=\"tweetHdr\"><div class=\"hdrLeft\">Recent Tweets</div><div class=\"hdrRight\"><a href=\"javascript:void(0);\" onclick=\"at.resetTweets(5);\" style=\"font-size: 10px;\">5</a>&nbsp;<a href=\"javascript:void(0);\" onclick=\"at.resetTweets(10);\" style=\"font-size: 12px;\">10</a>&nbsp;<a href=\"javascript:void(0);\" onclick=\"at.resetTweets(15);\" style=\"font-size: 14px;\">15</a><span id=\"tweetTimer\"></span></div></div><div class=\"tweetBfr\"></div>");
		$.getJSON("http://twitter.com/statuses/user_timeline/atsoftware.json?count=" + this.count + "&callback=?", null, 
			function (data) {
				var tweet = '';
				this.continueUpdating = true;
				for (var i=0; i<data.length; i++) {
					if(this.continueUpdating) {
						tweet = at.cleanTweet(data[i]);
						setTimeout('$(".tweetBfr").before("<div id=\'tweet'+i+'\'>'+(i+1)+". "+tweet+'</div>");', (i*750));
					}
				}
			}
		);
		this.continueUpdating = false;
		this.timeLeft = 59;
		this.resetTimer();
	},
	timeLeft: 59,
	resetTimer: function() {
		if (this.timeLeft>0) {
			this.timeLeft--;
			$("#tweetTimer").text(this.timeLeft);
			setTimeout('at.resetTimer()', 1000);
		}
	},
	count: 5,
	cleanTweet: function(data) {
		var tweet = data.text;
		var id = data.id;
		var time = data.created_at;
		tweet = tweet.replace("\n", "<br />");
		tweet = (tweet+" ").replace(/((https?|s?ftp|ssh)\:\/\/[^"\s\<\>]*[^.,;'">\:\s\<\>\)\]\!])/gi, "<a href='$1' target='_blank'>$1</a>");
		tweet = tweet.replace(/#(\w+)/gi, "<a href='http://www.twitter.com/search?q=#$1'>#$1</a>");
		tweet = tweet.replace(/@(\w+)/gi, "@<a href='http://www.twitter.com/$1'>$1</a>");
		tweet = tweet + '<a href=\'http://www.twitter.com/atsoftware/statuses/'+id+'\'>'+this.relativeTime(time)+'</a>';
		tweet = tweet.replace('"', '\\"');
		tweet = tweet.replace("'", "\\'");
		return tweet;
	},
	relativeTime: function(timeVal) {
		var vals = timeVal.split(" ");
		timeVal = vals[1] + " " + vals[2] + ", " + vals[5] + " " + vals[3];
		var parsedDate = Date.parse(timeVal);
		var relativeTo = new Date();
		var seconds = parseInt((relativeTo.getTime() - parsedDate) / 1000);
		seconds = seconds + (relativeTo.getTimezoneOffset() * 60);
	      
		if (seconds < 20) {
			return 'less than 20 seconds ago';
		} else if (seconds < 30) {
			return 'less than 30 seconds ago';
		} else if (seconds < 45) {
			return 'less than 45 seconds ago';
		} else if (seconds < 60) {
			return 'less than a minute ago';
		} else if(seconds < 120) {
			return 'about a minute ago';
		} else if(seconds < 3600) {
			return (parseInt(seconds / 60)).toString() + ' minutes ago';
		} else if(seconds < 7200) {
			return 'about an hour ago';
		} else if(seconds < 86400) {
			return 'about ' + (parseInt(seconds / 3600)).toString() + ' hours ago';
		} else if(seconds < 172800) {
			return '1 day ago';
		} else {
			return (parseInt(seconds / 86400)).toString() + ' days ago';
		}
	},
	resetTweets: function(howMany) {
		this.continueUpdating = false;
		this.count = howMany;
	},
	init: function() {
		this.getTwitter();
		setInterval("at.getTwitter();", 60500);
	}
}
window.onload = at.init();