Date.prototype.toRelativeTime = function(now_threshold) {
  var delta = new Date() - this;

  now_threshold = parseInt(now_threshold, 10);

  if (isNaN(now_threshold)) {
    now_threshold = 0;
  }

  if (delta <= now_threshold) {
    return 'Just now';
  }

  var units = null;
  var conversions = {
    millisecond: 1, // ms    -> ms
    second: 1000,   // ms    -> sec
    minute: 60,     // sec   -> min
    hour:   60,     // min   -> hour
    day:    24,     // hour  -> day
    month:  30,     // day   -> month (roughly)
    year:   12      // month -> year
  };

  for (var key in conversions) {
    if (delta < conversions[key]) {
      break;
    } else {
      units = key; // keeps track of the selected key over the iteration
      delta = delta / conversions[key];
    }
  }

  // pluralize a unit when the difference is greater than 1.
  delta = Math.floor(delta);
  if (delta !== 1) { units += "s"; }
  return [delta, units, "ago"].join(" ");
};

Date.fromString = function(str) {
  return new Date(Date.parse(str));
};

Array.prototype.remove = function(from, to) {
  var rest = this.slice((to || from) + 1 || this.length);
  this.length = from < 0 ? this.length + from : from;
  return this.push.apply(this, rest);
};

if(!Array.indexOf){
    Array.prototype.indexOf = function(obj){
        for(var i=0; i<this.length; i++){
            if(this[i]==obj){
                return i;
            }
        }
        return -1;
    }
}


var SocialStream = {
	json: null,
	current: 1,
	limit: 4,
	services: new Array('facebook', 'twitter', 'flickr'),
	
	init: function(selector, url) {
		var parent = jQuery(selector);
		jQuery.getJSON(url, function(json) {
			SocialStream.json   = json;
			SocialStream.parent = parent;
			SocialStream.parent.hide();
			
			SocialStream._draw();
			parent.css({ background: 'none' });
		});	
	},

	toggleService: function(service, button) {
		
		if(typeof SocialStream.parent == 'undefined') { return; }
		
		SocialStream.parent.fadeOut(200, function() {
			if(SocialStream.services.indexOf(service) != -1) {
				SocialStream.services = jQuery.grep(SocialStream.services, function(value) {
        			return value != service;
      			});	
      			
      			jQuery(button).css({ opacity: .3 });
			} else {
				SocialStream.services.push(service);
				
				jQuery(button).css({ opacity: 1 });	
			}
			
			SocialStream._draw();
		});
	},
	
	_draw: function() {
		SocialStream.parent.html("");
		
		SocialStream.current = 1;
		
		jQuery.each(SocialStream.json.entries, function(i, item) {
			if(item.via.name == '"professorgreen" via Marek in Google Reader') {
				item.via.name = 'Facebook';
			}
			
			if(SocialStream.services.indexOf(item.via.name.toLowerCase()) != -1 && SocialStream.current <= SocialStream.limit) {
				
				if('thumbnails' in item && item.via.name.toLowerCase() == 'flickr') {
					item.body = '';
						
					jQuery.each(item.thumbnails, function(i, thumb) {
						if(i < 3) { item.body += '<span><img src="' + thumb.url + '" alt="" /></span>';	}
					});
				}
					
				SocialStream.parent.append("<li><div class=\"body\">" + item.body + "<div> <em class=\"via\">" + new Date(item.date).toRelativeTime() + " via " + item.via.name + "</em> <span class=\"foot\"></span></li>");
					
				SocialStream.current++;
			}
			
			if(SocialStream.current > SocialStream.limit) {
				return false;
			}
			
		});
	
		if(typeof SocialStream.parent != 'undefined') {
			SocialStream.parent.fadeIn(200);
		}
	}
}