function fbFeed(fbXMLFeedPath, renderIntoContainer, firstRenderCallback) {
	// initialize the visibility check
	this.trackingName = 'fb';
	this.firstRenderCallback = firstRenderCallback;
	this.renderIntoContainer = renderIntoContainer;
	this.fbXMLFeedPath = fbXMLFeedPath;
	this.fbPosts = "";
	
	this.config = {
		path: fbXMLFeedPath,
		limit: 20
	};
	
	this.data = [];
	this.current = 0;
	this.hasresponse = false;
	
	var thisObj = this;


	this.getFBPosts = function(callback){
		var thisObj = this;
		thisObj.hasresponse = false;
		thisObj.data = [];
		thisObj.current = 0;
		thisObj.postcallback = callback;
		$.getJSON(thisObj.config.path, thisObj._postsDataCallback);
	}
	


	this._postsDataCallback = function(json){
		thisObj.data = json.data;
		if(thisObj.data.length > thisObj.config.limit)
			thisObj.data.length = thisObj.config.limit;
		//auto render to element
		if(thisObj.postcallback){
			thisObj.postcallback(thisObj);
		}
		thisObj.hasresponse = true;
		sendLoadedTrackingEventIfLoaded(thisObj.trackingName);
	}
	
	this.next = function(){
		if(thisObj.data.length > thisObj.current - 1){
			thisObj.current++;
			return thisObj.data[thisObj.current];
		}
		return false;
	}
	
	this.rewind = function(){
		thisObj.current = 0;
	}
		
}

function renderFBPosts(fbobj) {
	var tobj = new fbPostTempl();
	var html = "";
	
	while (fb = fbobj.next()) {
		html += tobj.toHTML(fb);
	}
	$(fbobj.renderIntoContainer).html(html);

	fbobj.firstRenderCallback(fbobj.trackingName);
	fbobj.rewind();
}	


var fbPostTempl = function(){
	var self = this;
	var url = 'http://www.facebook.com/BlackBerry';
	this.toHTML = function(fb){
		//find the comment link
		if (fb.actions !== undefined) {
			for (var actionI = 0; actionI < fb.actions.length-1; actionI++) {
				if (fb.actions[actionI].name == "Comment") {
					var thisCommentURL = fb.actions[actionI].link;
				}
			}
			//if we find a "comment" link, send them there, otherwise just send them to the FB page
		} else {
			var thisCommentURL = url;
		}
			
		if (fb.message !== undefined) {
			//get id for auth_image
			var ids = fb.id.split('_');
			this.fburl = url + '/posts/' + ids[1];
		
			//Set the two dates
			var diff_message = postRelativeTimeString(fb.created_time * 1000);
		
			// Non-unique IDs guys?  Really?
			var content =	'<div id="community_item">\
								<div id="community_poster">\
									<div class="username">\
										<a class="trackable" objType="communityStreamClick" nstrack-asset="facebook_profile_photo" href="' + this.fburl + '" target="_blank">\
											<img class = "profile_photo" src="http://graph.facebook.com/' + fb.from.id + '/picture" />\
										</a>\
										<h2>\
											<a class="trackable" objType="communityStreamClick" nstrack-asset="facebook_user_name" href="' + this.fburl + '" target="_blank">' +
												fb.from.name + '\
											</a>\
										</h2>\
									</div>\
									<div class="comm_message_body">\
										<p>' + 
											parseURL(fb.message) + '\
										</p>\
									</div>\
									<div id="posted_on">\
										<div id="social_icon" class="facebook"></div>\
										Posted on Facebook ' + diff_message + '\
									</div>\
								</div>\
								<div class="clearer"></div>\
								<div class="clearer">&nbsp;</div>';

								if (fb.likes)
			content +=				'<div class="comm_facebook_likes">\
										<a class="trackable" objType="communityStreamClick" nstrack-asset="facebook_like_count" href="' + this.fburl + '" target="_blank">\
											<img src="' + environ['CDN_PATH'] + 'css/images/facebook_like.png" width="16" height="16" alt="Like This" border="0"/>' +
											(fb.likes.count == 1
												? '<span class="likers">' + fb.likes.count + ' person</span> likes this'
												: '<span class="likers">' + fb.likes.count + ' people</span> like this') + '\
										</a>\
									</div>';
								
								//check if the post has likes associated with it					
								if (fb.comments.count > 0) {
									if (fb.comments.data !== undefined) {
			content +=					'<div class="comm_facebook_comments">\
											<a class="trackable" objType="communityStreamClick" nstrack-asset="facebook_view_all_comments" href="' + this.fburl + '" target="_blank">\
												<img src="' + environ['CDN_PATH'] + 'css/images/comment.png" width="16" height="16" alt="Like This" border="0"/>' + 
												'View All '+ fb.comments.data.length + ' Comments\
											</a>\
										</div>\
										<div class="comm_facebook_comments comm_facebook_comments_unfolded">\
											<a class="trackable" objType="communityStreamClick" nstrack-asset="facebook_comment" href="' + this.fburl + '" target="_blank">';
												/*for (var commentNum = 0; commentNum < fb.comments.data.length; commentNum++) { */		var commentNum = fb.comments.data.length - 1;
													var thisComment = fb.comments.data[commentNum];
			content +=								'<div class="community_poster_unfolded">\
														<div id="community_avatar">\
															<img src="http://graph.facebook.com/' + thisComment.from.id + '/picture"  width = "40" height="40"/>\
														</div>\
														<div id="poster_name">\
															<h2>' + 
																fb.from.name + '\
															</h2>\
															<div class="comm_facebook_msg">' + 
																parseURL(thisComment.message) + '\
															</div>\
														</div>\
														<div id="posted_on">\
															<div id="social_icon" class="facebook"></div>\
															Posted on Facebook ' + diff_message + '\
														</div>\
														<div class="clearer"></div>';
												//}
			content +=						'</a>\
											<div class="clearer"></div>\
										</div>';
									} else {
										//no real comments came through, this is a POSTS feed so there will be only a number here.
			content += 					'<div class="comm_facebook_comments">\
											<a class="trackable" objType="communityStreamClick" nstrack-asset="facebook_view_all_comments" href="' + this.fburl + '" target="_blank">\
												<img src="' + environ['CDN_PATH'] + 'css/images/comment.png" width="16" height="16" alt="Like This" border="0"/>' + 
												(fb.comments.count == 1
													? 'View 1 Comment'
													: 'View All '+ fb.comments.count + ' Comments') + '\
											</a>\
										</div>';
									}
								}

			content += 		'</div>\
							<div class="clearer">&nbsp;</div>\
						</div>\
						<div class="msgSep">&nbsp;</div>';
			
			return content;
		}
	}
	
	function parseURL(value) {
		return value.replace(/[A-Za-z]+:\/\/[A-Za-z0-9-_]+\.[A-Za-z0-9-_:%&~\?\/.=]+/g, function(url) {
			var r = "<a objType='communityStreamClick' class='trackable' nstrack-asset='url."+url+"' href='"+url+"' target='_blank'>"+url+"</a>";
			return r;
		});
	}
}


