$(document).ready(function(){
	
	$.getJSON('/frontend/fb.php', function(data) {
		if(data.data){
			var num_items = data.data.length;
			var used = 0;
			for(var i = 0; i < num_items && used < max_fb_items ; ++i){
				var item = data.data[i];
				var text = item.message;
				var created = item.created_time.replace("+0000", "Z");
				var pretty_created = prettyDate(created);
				if(!text){
					text = "";
				}
				if(item.link && item.name){
					text += ' ' + item.name;
				}
				if(use_short_fb){
					text = shorten_text(text); //shorten to 100 char
				}
				var post_id = item.id;
				post_id = post_id.substr(post_id.indexOf('_')+1);		//get post id relative to LinasMatkasse page
				$('<div class="fbnewsitem" id="post_'+ post_id +'">'+
						'<img src="http://graph.facebook.com/LinasMatkasse/picture" alt="facebook_avatar" width="30" height="30" />'+
						'<h3>'+text+' <span>' + pretty_created + '</span></h3>'+
					'</div>').appendTo("#fbnewsfeed");
				++used;
			}
			$('.fbnewsitem').click(function(){
				var post_id = $(this).attr('id');
				post_id = post_id.substr(5);
				window.open('http://www.facebook.com/LinasMatkasse/posts/' + post_id, '_blank');
			});
		}
	});
});

/**
 * Shorten the text to a maximum of 100 chars
 * 
 * @param text
 */
function shorten_text(text){
	var length = text.length;
	var end = ""
	if(length > 100){
		length = 100;
		end = "...";
	}
	return text.substr(0, length) + end;
}
