// this function uses the fbresp array generated in the fbwallrelay.php to access live feed of wall posts from facebook
// when including the facebook.js in your head tag be sure to change the name of the name parameter to the applicable facebook site (ie. name=debeerlax)
// this feed data is assigned to divs on the include file (ie. sidebar.php) that have corresponding class names to build a
// dynamically generated feed of current wall post info from facebook
// this is the api doc for facebook posts - http://developers.facebook.com/docs/reference/api/post

$(document).ready(function() {
	var from_id = "";
	var post_link = ""
   	var idx = 0;
   	var date = 0;
   	var comment_cnt = 0;
   	var max_len = 120;                                              //change this value if you want to display longer post before See More link is activated
   	var l = max_len;
   	var str = "";
        var wall_link = "http://www.facebook.com/debeerlax" ;           //change the link to go to the applicable facebook site
   	var m = fbresp.data.length < 3 ? fbresp.data.length : 3;        //change the value here for # of entries to display

   	$('div#facebook_feed a.wall_link').attr('href', wall_link );

	for (var i = 0; i < m; i++) {
		if (! fbresp.data[i].message) {
			m++;
			continue;
		}
		from_id = fbresp.data[i].from.id;
		idx = fbresp.data[i].id.indexOf('_');
// date conversion using pretty.js
		date = prettyDate(fbresp.data[i].created_time.replace("+0000","Z"), true);
		if (fbresp.data[i].comments)
			comment_cnt = fbresp.data[i].comments.data.length;
		else
			comment_cnt = 0;
		post_link = fbresp.data[i].id.substring(idx+1);
		$('div#entry div.from').html(fbresp.data[i].from.name);
		$('div#entry div.date').html('Posted ' + date);
		str = fbresp.data[i].message;
		if (str.length > max_len) {
			var new_str = str.substring(0, l--);
			while (new_str.charAt(l) != " ")
				l--;
			new_str = str.substring(0, l) + '...<br /><a href=' + wall_link + ' target="_blank">See more</a>'
// uncomment line below & comment line above if you want the See more link to go to the individual post vs. the wall
//			new_str = str.substring(0, l) + '...<br /><a href=' + wall_link + '/posts/' + post_link + ' target="_blank">See more</a>'
		} else
			new_str = str;
		$('div#entry div.message').html(new_str);
		$('div#entry div.picture img').attr('src', 'http://graph.facebook.com/' + from_id + '/picture' );
// uncomment line below & change the Original Post link div class on your include (ie. sidebar.php) to class="post_link" to display link to individual post
//		$('div#entry div.link a.post_link').attr('href', wall_link + '/posts/' + post_link );
		$('div#entry div.link a.wall_link').attr('href', wall_link );
		$('div#entry span.comments').html(comment_cnt + '');
		$('div#entry').clone().attr('id', 'entry' + i).appendTo('#facebook_feed').show();
	}
});


