$(function() {

	var APP_ID = '169713603073663';
	var LIMIT = 4;
	var FAV_CNT = 0;
	var finish = false;

	FB.init({
		appId  : APP_ID,
		status : false, // check login status
		cookie : true, // enable cookies to allow the server to access the session
		xfbml  : true  // parse XFBML
	});
	
	if($('div#feed').length > 0) {
	
		$('div#feed em').css('display','block');
		FB.api('/'+APP_ID+'/feed', { limit: 100 }, function(re) {
			var cnt = 0;
			var load = 0;
			$('div#feed ul').hide().css('opacity',0);
			for(var i=0; i<re.data.length; i++) {
				var post = re.data[i];
				if(post.from.id == APP_ID) {
					var fav = 0;
					if(post.comments) {
						fav += post.comments.count;
					}
					if(post.likes) {
						fav += post.likes.count;
					}
					if(fav >= FAV_CNT) {
						load++;
						cnt++;
						$('div#feed ul').append('<li><a class="img" href="http://www.facebook.com/pages/Berlinki-Kreca/169713603073663?v=wall" onclick="window.open(this.href);return false;"><img src="http://graph.facebook.com/'+post.from.id+'/picture" alt="" /></a><p><a href="http://www.facebook.com/pages/Berlinki-Kreca/169713603073663?v=wall" onclick="window.open(this.href);return false;">'+post.message+'</a></p></li>');
						$('div#feed ul li:last img').load(function() {
							load--;
							if(load == 0 && (cnt == LIMIT || finish)) {
								$('div#feed em').animate({
									opacity: 0
								},250,function() {
									$('div#feed em').hide();
									$('div#feed ul').show().animate({
										opacity: 1
									},250);
								});
							}						
						});
						if(cnt == LIMIT) break;
					}
				}
			}
			var finish = true;
		});
		
	}
		
});

