(function($) {
	var ago = function (time){
		var date = new Date((time || "").replace(/-/g,"/").replace(/[TZ]/g," ")),
			diff = (((new Date()).getTime() - date.getTime()) / 1000),
			day_diff = Math.floor(diff / 86400);
				
		if ( isNaN(day_diff) || day_diff < 0 || day_diff >= 31 )
			return;
				
		return day_diff == 0 && (
				diff < 60 && "just now" ||
				diff < 120 && "1 minute ago" ||
				diff < 3600 && Math.floor( diff / 60 ) + " minutes ago" ||
				diff < 7200 && "1 hour ago" ||
				diff < 86400 && Math.floor( diff / 3600 ) + " hours ago") ||
			day_diff == 1 && "Yesterday" ||
			day_diff < 7 && day_diff + " days ago" ||
			day_diff < 31 && Math.ceil( day_diff / 7 ) + " weeks ago";
	}

	$(function() {
		var QS = (function () {
			var s = window.location.search.substring(1).split("&"),q={};
			for(var i=0; i<s.length; i++) { q[s[i].split('=')[0]] = s[i].split('=')[1]; }
			return q;
		})(),
		user = {};
		
		if(QS.feed == 'recent') {
			$("#container").html("<h2>Your Feed</h2><ul id='feed' class='step'>Loading from Facebook...</ul>").html();
		}
		
		$("#login_button").click(function() {
		  FB.login(function() { window.location.reload(); }, {'scope': 'offline_access,publish_stream,read_stream'});
		  return false;
		});
		
		$.get("/recent",function(res) {
			$("#recent").html(res);
		});
		
		window.fbAsyncInit = function() {
			FB.init({appId: '162799340480081', status: true, cookie: true, oauth: true});
			FB.getLoginStatus(function(res) {
			     if(res.session) {
			         $("#login_button").find("a").text("You are logged in");
			     }
				if(res.session && QS.feed == 'recent') {
					FB.api("me/home",function(res) {
					    var html = [];
					    for(var i=0;i<res.data.length;i++) {
					    	if(!res.data[i].message) {
					    		continue;
					    	}
					    	(function(id) {
					    		FB.api("/" + res.data[i].id + "/comments", function(res) {
					    			var count = 0;
					    			for(var i in res.data) {
					    				if(res.data[i].message == "***dislike***") {
					    					count++;
					    				}
					    			}
					    			$("#post_" + id).html("Dislike <span>(" + count + ")</span>");
					    		});
					    	})(res.data[i].from.id);
					    	html.push("<li>");
					    	html.push("<img class='pic' src='http://graph.facebook.com/" + res.data[i].from.id + "/picture' />");
					    	html.push("<div class='post_content'>");
					    	html.push("<a class='name' href='http://facebook.com/profile.php?id=" + res.data[i].from.id + "'>" + res.data[i].from.name + "</a>");
					    	if(res.data[i].to) {
					    	html.push(" &rarr; <a class='name' href='http://facebook.com/profile.php?id=" + res.data[i].to.data[0].id + "'>" + res.data[i].to.data[0].name + "</a>");
					    	}
					    	html.push(res.data[i].message);
					    	html.push("<p>");
					    	if(res.data[i].icon) { html.push("<img class='icon' src='" + res.data[i].icon + "' />");}
					    	res.data[i].attribution || (res.data[i].attribution = '');
					    	html.push(" posted " + ago(res.data[i].created_time) + " " + res.data[i].attribution + " &middot; ");
					    	html.push("<a href='#' class='like_link' title='" + res.data[i].id + "'>Like</a>  &middot; ");
					    	html.push("<a href='#' class='dislike_link' id='post_" + res.data[i].from.id + "' title='" + res.data[i].id + "'>Dislike</a>");
					    	html.push("</p>");
					    	html.push("</div>");
					    	html.push("</li>");
					    }
					    
					    $("#feed").html(html.join('')).find('.like_link').click(function() {
					    	var id = $(this).text('You Like This').attr('title');
					    	FB.api("/" + id + "/likes/", 'post');
					    	return false;
					    }).end().find('.dislike_link').click(function() {
					    	var id = $(this).text("Disliked").attr('title');
					    	FB.api("/" + id + "/comments/", 'post', {"message":"***dislike***"});
					    	return false;
					    }).end().hide().fadeIn();
					});
				}
			});
			
			
			
			
		};
	});
})(jQuery);
