var tweetUsers = ['littlebigonecom'];
var buildString = "";
var tweetLimit = 2;

$(document).ready(function(){

	$('#twitterTicker').slideDown('slow');

	try {
		$('.tweet .text').each(function(i) {
			var txt = this.innerHTML;
			this.innerHTML = formatTwitString(txt);
		});
	}catch (e) {}
	
	try {
		$('.tweet .time').each(function(i) {
			var time = this.textContent;
			mytest = time;
			this.textContent = relativeTime(time);
		});
	}catch (e) {}
	/*
	for(var i=0;i<tweetUsers.length;i++)
	{
		if(i!=0) buildString+='+OR+';
		buildString+='from:'+tweetUsers[i];
	}
	
	var fileref = document.createElement('script');
	
	fileref.setAttribute("type","text/javascript");
	fileref.setAttribute("src", "http://search.twitter.com/search.json?q="+buildString+"&callback=TweetTick&rpp="+tweetLimit);
	
	document.getElementsByTagName("head")[0].appendChild(fileref);
	*/
});
/*
function TweetTick(ob)
{
	var container = $('#tweets');
	container.html('');
	
	$(ob.results).each(function(el)
	{
		var str = '	<div class="tweet"><a class="avatar" href="http://twitter.com/'+this.from_user+'" target="_blank"><img src="'+this.profile_image_url+'" alt="'+this.from_user+'" /></a><br /><a class="user" href="http://twitter.com/'+this.from_user+'" target="_blank">'+this.from_user+'</a><br /><div class="time">'+relativeTime(this.created_at)+'</div><div class="txt">'+formatTwitString(this.text)+'</div></div>';
		
		container.append(str);
	});
	
	Cufon.replace('#twitterTicker .tweet', { fontFamily: 'CoconL' });
	//container.jScrollPane();
}
*/
function formatTwitString(str)
{
	str=' '+str;
	str = str.replace(/((ftp|https?):\/\/([-\w\.]+)+(:\d+)?(\/([\w/_\.]*(\?\S+)?)?)?)/gm,'<a href="$1" target="_blank">$1</a>');
	str = str.replace(/([^\w])\@([\w\-]+)/gm,'$1@<a href="http://twitter.com/$2" target="_blank">$2</a>');
	str = str.replace(/([^\w])\#([\w\-]+)/gm,'$1<a href="http://twitter.com/search?q=%23$2" target="_blank">#$2</a>');
	return str;
}

function relativeTime(pastTime)
{	
	// pastTime = "2011-11-07 10:35:21";
	var Y = pastTime.substring(0,4);
	var m = parseInt(pastTime.substring(5,7));
	var d = parseInt(pastTime.substring(8,10));
	var H = pastTime.substring(11,13);
	var i = pastTime.substring(14,16);
	var s = pastTime.substring(17,19);


	var origDate 	 = new Date(Y, m-1, d, H, i, s, 0);
	var origStamp 	 = origDate.getTime();
	var curDate 	 = new Date();
	var currentStamp = curDate.getTime();
	
	var difference = parseInt((currentStamp - origStamp)/1000);

	if(difference < 0) return false;

	if(difference <= 5)				return "Just now";
	if(difference <= 20)			return "Seconds ago";
	if(difference <= 60)			return "A minute ago";
	if(difference < 3600)			return parseInt(difference/60)+" minutes ago";
	if(difference <= 1.5*3600) 		return "One hour ago";
	if(difference < 23.5*3600)		return Math.round(difference/3600)+" hours ago";
	if(difference < 1.5*24*3600)	return "One day ago";
	
	
	var months = new Array(12);
	months[0] = "Jan";
	months[1] = "Feb";
	months[2] = "Mar";
	months[3] = "Apr";
	months[4] = "May";
	months[5] = "Jun";
	months[6] = "Jul";
	months[7] = "Aug";
	months[8] = "Sep";
	months[9] = "Oct";
	months[10] = "Nov";
	months[11] = "Dec";

	
	//var dateArr = pastTime.split(' ');
	return getGetOrdinal(d)+' '+months[m-1]+' '+Y;
}

function getGetOrdinal(n) {
	   var s=["th","st","nd","rd"],
	       v=n%100;
	   return n+(s[(v-20)%10]||s[v]||s[0]);
	}
