function requestGPlusActivities(appKey,usrId,maxResults){
	var url = "https://www.googleapis.com/plus/v1/people/" + usrId + "/activities/public?alt=json&pp=1&key=" + appKey;
	
	if(maxResults != 0)
		url += "&maxResults=" + maxResults
	
	$.ajax({
		type: 'GET',	
		url: url,
		dataType: "jsonp",
		success: function(json){
			
			if(json.error){
				// unsuccessful request. display error message
				$("#posts").html('<div class="errorMessage">An error occured!<br />The server returned the following information: ' + json.error.code + ' - ' + json.error.message + '</div>');	
			}else {
				// successful request. display activities
				$("#posts").html(getPosts(json));
			}
		},
		error:function (xhr, ajaxOptions, thrownError){
			$("#posts").html(xhr.status + ': ' + thrownError);
		}    
	});	
}
function getPosts(json){
	var output = '';
	$.each(json.items,function(i,data){
		var tag = getTag(data);
	
		// display post if tag exists
		if(tag){
			// get post and remove hash tag
			var postText = data.object.content.replace(tag,"");
			// set post's title
			var title = dateFormat(data.published, "dddd, mmmm dS, yyyy");
			// set post's css class (used as filter)
			var filterClass = tag.replace("#","");

			output += '<div class="post ' + filterClass + '">';
			output += '<div class="title">' + title + '</div>';
			output += '<div class="content">'
			
			// check if it's a shared post
			if(data.verb == 'share'){
				output += '<div class="sharedFrom">Shared <a href="' + data.object.url + '" target="_blank">post</a> ';
				output += 'from <a href="' + data.object.actor.url + '" target="_blank">' + data.object.actor.displayName + '</a></div>';
			}
			
			// check post attachments
			$.each(data.object.attachments,function(j,attachment){
				switch(attachment.objectType){
					case 'article':
						// display post type icon
						output += '<div class="type"><img src="images/postTypeArticle2.png" alt="Article" title="Article" /></div>';
			
						output += '<div class="typeArticle">';
						output += '<div class="text">' + postText + '</div>';
						output += '<div class="article">';
		
						// check if article has image
						if(data.object.attachments[1] && data.object.attachments[1].image.url != '')
							output += '<div class="image"><img src="' + data.object.attachments[1].image.url + '" /></div>';
		
						output += '<div class="link"><a href="' + attachment.url + '" target="_blank">';
						output += attachment.displayName;
						output += '</a></div>';
						
						if(attachment.content)
							output += '<div class="text">' + attachment.content + '</div>';
		
						output += '<br clear="left" /></div>'; // close postArticle
						output += '</div>'; // close typeArticle
						
						break;
					case 'video':
						// display post type icon
						output += '<div class="type"><img src="images/postTypeVideo.png" alt="Video" title="Video" /></div>';
						
						output += '<div class="typeVideo">';
						
						if(data.verb != 'share')
							output += '<div class="text">' + postText + '</div>';
						
						output += '<div class="article">';
						output += '<div class="video"><iframe width="250" src="' + attachment.url + 'autoplay=0" frameborder="0" allowfullscreen></iframe></div>';
						output += '<div class="text">';
						
						if(data.verb == 'share')
							output += postText + '<br /><br />';
						
						output += '<div class="link"><a href="' + attachment.url + '" target="_blank">' + attachment.displayName + '</a></div>';
						output += attachment.content;
						output += '</div>';
						output += '<br clear="left" /></div>'; // close postArticle
						output += '</div>'; // close typeVideo
						break;
					case 'photo-album':
						var imageUrl = data.object.attachments[1].url;
						var fullImageUrl = data.object.attachments[1].fullImage.url;
						var imageLegend = data.object.attachments[1].displayName;
						
						if(postText == ""){
							postText = data.object.attachments[1].displayName;
							imageLegend = "";
						}
						
						// display post type icon
						output += '<div class="type"><img src="images/postTypePhoto3.png" alt="Photo" title="Photo" /></div>';
						
						output += '<div class="typePhoto">';
						output += '<div class="article">';
						output += '<div class="photo">';
						output += '<a href="' + imageUrl + '" target="_blank">';
						output += '<img width="100%" src="' + fullImageUrl + '" title="' + imageLegend + '" alt="' + imageLegend + '" /></a>';
						
						if(imageLegend != "")
							output += '<div class="legend">' + imageLegend + '</div>';
						
						output += '</div>'; // close photo
						output += '<div class="text">' + postText + '</div>';
						output += '<br clear="left" /></div>'; // close postArticle	
						output += '</div>'; // close typePhoto
						break;
				}
			});
			
			output += '</div>'; // close postContent
			output += '</div>'; // close post
		}
	});	
	
	return output;
}

// function to retrieve the post's hash tag
function getTag(data){
	if(data.verb == 'share'){
		var content = data.annotation; 	
	}else{
		var content = data.object.content;
	}
	
	var lastIndex = content.lastIndexOf("#");
	if(lastIndex > -1 && content.charAt(lastIndex-1) != '&'){
		var tag = content.slice(lastIndex,lastIndex + (content.length - lastIndex));
		if(tag != "&" && tag != '')
			return tag;
	}
	
	return false;
}

