$(document).ready(function(){
	// detectFlash( featureSWFO , $("#featureContainer") );

  var twitNum = 3;
	$.getJSON("http://twitter.com/statuses/user_timeline/uia2011_tokyo.json?callback=?&count=5", function(data){
		if(data[0]){
		  var htmlStr = '';
		  for (var i=0;i<data.length;i++) {
		    htmlStr += '<div class="body small12">' +
		        data[i]
		        .text
		        .replace(/http:\/\/(.+?)(\n|\r| |$)/g,'<br /><a href="http://$1">http://$1</a>')
		          +'<br /><span class="timeBefore">'
  		        +niceTime( dateFormat(data[i].created_at) )
  		        +'</span></div>';
		  }
		  $('#twitterArea div.tweets').html(htmlStr);
		}else{
			$('#twitterArea div.tweets').html('<div class="body small12">Failded to fetch twitter data.<br /><a href="http://twitter.com/uia2011_tokyo" target="_blank">http://twitter.com/uia2011_tokyo</a></div>');
		}
	});

});



var dateFormat=function(d){
	var str=""
	var l=d.split(" ");
	str=l[ 1 ]+" "+l[ 2 ]+", "+l[5]+" "+l[3];
	return str;
}



var niceTime = function() {
	var ints = {
	second: 1,
	minute: 60,
	hour: 3600,
	day: 86400,
	week: 604800,
	month: 2592000,
	year: 31536000
	};

	return function(time) {
		time = new Date(time);
		var gap = ((new Date()) - time) / 1000,
		amount, measure;

		for (var i in ints) {
			if (gap > ints[i]) { measure = i; }
		}
		amount = gap / ints[measure];
		amount = Math.round(amount);
		amount +=  ' ' + measure + (amount>1?'s':'') + ' before';
		return amount;
	};

}();
