var SocialBox = Class.create({
	
	initialize: function(div, default_drawer){
		this.div = div;
		this.options = {};
		this.accordion = new Accordion(this.div.id);
		this.default_section = this.accordion.sections.detect((function(section){
			return section.elements.section.id == default_drawer;
		}).bind(this));
		this.accordion.showSection(this.default_section);
	},
	
	update_twitter: function(json){
		this.twitter_json = json;
		this.twitter_div = this.div.down('#social_twitter div.toggle');
		this.twitter_div.down('p').remove();
		this.twitter_json.each((function(el){
			this.format_tweet(el);
		}).bind(this));
   	},
	
	format_tweet: function(obj){
		this.twitter_div.insert('<div class="clearfix"><img src="' + obj.user.profile_image_url + '" class="twitter_avatar" width="40" height="40">' + this.format_tweet_text(obj.text) + '<small>' + this.time_ago_in_words_with_parsing(obj.created_at) + ' from ' + obj.source + '</small></div>');
	},
	
	format_tweet_text: function(text){
		return text;
	},
	
	time_ago_in_words_with_parsing: function(from) {
	    var date = new Date(Date.parse(from.replace('+0000', 'UTC+0000')));    
	    return this.time_ago_in_words(date);
	},

	time_ago_in_words: function(from){
		return this.distance_of_time_in_words(new Date, from);
	},

	distance_of_time_in_words: function(to, from){
		var distance_in_seconds = ((to - from) / 1000);
	  	var distance_in_minutes = (distance_in_seconds / 60).floor();
      	
	  	if (distance_in_minutes == 0) { return 'less than a minute ago'; }
	  	if (distance_in_minutes == 1) { return 'a minute ago'; }
	  	if (distance_in_minutes < 45) { return distance_in_minutes + ' minutes ago'; }
	  	if (distance_in_minutes < 90) { return 'about 1 hour ago'; }
	  	if (distance_in_minutes < 1440) { return 'about ' + (distance_in_minutes / 60).floor() + ' hours ago'; }
	  	if (distance_in_minutes < 2880) { return '1 day ago'; }
	  	if (distance_in_minutes < 43200) { return (distance_in_minutes / 1440).floor() + ' days ago'; }
	  	if (distance_in_minutes < 86400) { return 'about 1 month ago'; }
	  	if (distance_in_minutes < 525960) { return (distance_in_minutes / 43200).floor() + ' months ago'; }
	  	if (distance_in_minutes < 1051199) { return 'about 1 year ago'; }
      	
	  	return 'over ' + (distance_in_minutes / 525960).floor() + ' years ago';
	}
	
});

var DateHelper = {
  // Takes the format of "Jan 15, 2007 15:45:00 GMT" and converts it to a relative time
  // Ruby strftime: %b %d, %Y %H:%M:%S GMT
  
};