// pretty dates
function human_date(date_str) {
	var time_formats = [
		[60, 'Enkele ogenblikken'],
		[90, '1 minuut'], // 60*1.5
		[3600, 'minuten', 60], // 60*60, 60
		[5400, '1 uur'], // 60*60*1.5
		[86400, 'uren', 3600], // 60*60*24, 60*60
		[129600, '1 dag'], // 60*60*24*1.5
		[604800, 'dagen', 86400], // 60*60*24*7, 60*60*24
		[907200, '1 week'], // 60*60*24*7*1.5
		[2628000, 'weken', 604800], // 60*60*24*(365/12), 60*60*24*7
		[3942000, '1 maand'], // 60*60*24*(365/12)*1.5
		[31536000, 'maanden', 2628000], // 60*60*24*365, 60*60*24*(365/12)
		[47304000, '1 jaar'], // 60*60*24*365*1.5
		[3153600000, 'jaren', 31536000], // 60*60*24*365*100, 60*60*24*365
		[4730400000, '1 eeuw'], // 60*60*24*365*100*1.5
	];

	var dt = new Date,
		// seconds = ((dt - new Date(date_str) + (dt.getTimezoneOffset() * 60000)) / 1000),
		seconds = Math.ceil((dt - new Date(date_str)) / 1000),
		token = ' geleden',
		i = 0,
		format;

	if (seconds < 0) {
		seconds = Math.abs(seconds);
		token = '';
	}
	
	while (format = time_formats[i++]) {
		if (seconds < format[0]) {
			if (format.length == 2) {
				return format[1] + (i > 1 ? token : ''); // Conditional so we don't return Just Now Ago
			} else {
				return Math.round(seconds / format[2]) + ' ' + format[1] + (i > 1 ? token : '');
			}
		}
	}

	// overflow for centuries
	if(seconds > 4730400000)
		return Math.round(seconds / 4730400000) + ' Centuries' + token;

	return date_str;
};

// linkify twitter status
function twitter_linkify(text) {
	return text.replace(/((https?|s?ftp|ssh)\:\/\/[^"\s\<\>]*[^.,;'">\:\s\<\>\)\]\!])/g, function(url) {
		return '<a href="'+url+'">'+url+'</a>';
	}).replace(/\B@([_a-z0-9]+)/ig, function(reply) {
		return reply.charAt(0)+'<a href="http://twitter.com/'+reply.substring(1)+'">'+reply.substring(1)+'</a>';
	}).replace(/\B#([_a-z0-9]+)/ig, function(hashtag) {
		return '<a href="http://twitter.com/search?q=%23'+hashtag.substring(1)+'">'+hashtag+'</a>';
	});
}

jQuery(function() {
	
	// twitter
	$.jTwitter('bramds', 1, function(data) {
		$('#posts').empty();
		$.each(data, function(i, post){
			$('#posts').append(
				'<div class="post">'
				+' <p class="txt">'
				+    twitter_linkify(post.text)
				+' </p><p class="date">'
				+    human_date(post.created_at)
	            +'</p></div>'
	        );
		});
	});
    
});


