Date.prototype.setISO8601 = function(dString) {
	var regexp = /(\d\d\d\d)(-)?(\d\d)(-)?(\d\d)(T)?(\d\d)(:)?(\d\d)(:)?(\d\d)(\.\d+)?(Z|([+-])(\d\d)(:)?(\d\d))/;  

	if (dString.toString().match(new RegExp(regexp))) {  
		var d = dString.match(new RegExp(regexp));  
		var offset = 0;

		this.setUTCDate(1);  
		this.setUTCFullYear(parseInt(d[1],10));  
		this.setUTCMonth(parseInt(d[3],10) - 1);  
		this.setUTCDate(parseInt(d[5],10));  
		this.setUTCHours(parseInt(d[7],10));  
		this.setUTCMinutes(parseInt(d[9],10));  
		this.setUTCSeconds(parseInt(d[11],10));  
		if (d[12])  
		this.setUTCMilliseconds(parseFloat(d[12]) * 1000);  
		else  
		this.setUTCMilliseconds(0);  
		if (d[13] != 'Z') {  
			offset = (d[15] * 60) + parseInt(d[17],10);  
			offset *= ((d[14] == '-') ? -1 : 1);  
			this.setTime(this.getTime() - offset * 60 * 1000);  
		}  
	}  
	else {  
		this.setTime(Date.parse(dString));  
	}
	
	return this;  
};

function timeAgo(timeValue) {
	var parsedDate = new Date().setISO8601(timeValue);
	var relativeTo = (arguments.length > 1) ? arguments[1] : new Date();
	var delta = parseInt((relativeTo.getTime() - parsedDate) / 1000);

	if(delta < 60) {
	    return 'less than a minute ago';
	} else if(delta < 120) {
	    return 'about a minute ago';
	} else if(delta < (45*60)) {
	    return (parseInt(delta / 60)).toString() + ' minutes ago';
	} else if(delta < (90*60)) {
	    return 'about an hour ago';
	} else if(delta < (24*60*60)) {
	    return 'about ' + (parseInt(delta / 3600)).toString() + ' hours ago';
	} else if(delta < (48*60*60)) {
	    return '1 day ago';
	} else {
	    return (parseInt(delta / 86400)).toString() + ' days ago';
	}
}

$(function() {
	$('.project_img').hover(
		function() { $('.project_rollover', this).fadeIn('fast'); },
		function() { $('.project_rollover', this).fadeOut('fast'); }
	);
	
	$('a[rel="email"]').click(function() {
		window.location.href = 'mailto:' + $(this).attr('href') + '@rbdistro.com';
		return false;
	});
	
	var feeds = [
		{ id: 'onbeats', friendfeed: 'onbeats' },
		{ id: 'autolect', friendfeed: 'autolect' },
		{ id: 'jihadalnafs', friendfeed: 'jihadalnafs' },
		{ id: 'audocs', friendfeed: 'audocs' },
		{ id: 'mikial', friendfeed: 'maturationmdavis' },
		{ id: 'newAndalus', friendfeed: 'newandalus' },
		{ id: 'veloxnur', friendfeed: 'veloxnur' }
	];
	
	$.each(feeds, function(a) {
	
		var feed = feeds[a];
		var list = $('#' + feed.id + ' .feed');
		
		$.getJSON('http://friendfeed.com/api/feed/user/' + feed.friendfeed + '?format=json&num=20&callback=?', function(response) {
			
			list.empty();

			$.each(response.entries, function(b) {
				
				var entry = response.entries[b];
				
				if(entry.title.match(/^@/)) { return; }
				
				var id = feed.id + '_feed_' + (b + 1);
								
				var item  = '<li id="' + id + '" class="' + entry.service.id + '">';
				try { item += '<img src="' + entry.media[1].thumbnails[1].url + '" />'; } catch(e) {}
					item += '<p class="content">' + entry.title + '</p>';
					item += '<p class="date">' + timeAgo(entry.published) + '</p>';
					item += '</li>';

				list.append(item);
				
				var $item = $('#' + id);
					$item.click(function() { window.open(entry.link); });
					$item.hover(
						function() { $item.addClass('hover'); },
						function() { $item.removeClass('hover'); }
					);
					
				return false;
			});
			
			if(list.html() == '') { list.html('<li>SCREAM LOUD</li>'); }
		});
	});
});
