/////////////////////////////////////////////////////////////////
// To the next SLAIS webmonkey, 
//
// This is a variation on the code found here:
// http://willworkforart.net/tutorials/google-feed-api-tutorial
// For more information on this code in general, see
// http://code.google.com/apis/ajaxfeeds/documentation/#Examples
/////////////////////////////////////////////////////////////////

// Initializes Google API
google.load("feeds", "1");

function initialize() {
	// Main feed instance
	var feed = new google.feeds.Feed("http://blogs.ubc.ca/lassa/feed");
	
	// Set how many posts appear here
	feed.setNumEntries(5);
	
	feed.load(function(result) {
		if (!result.error) {
			// Grabs the lassaFeed div to work on
			var container = document.getElementById("lassaFeed");
			container.innerHTML = '';
			
			// These lines generate the title and associated link
			var heading = document.createElement("h2");
			var blogLink = document.createElement("a");
			var blogTitle = document.createTextNode("The LASSA Blog");
			heading.appendChild(blogLink);
			blogLink.appendChild(blogTitle);
			blogLink.href = 'http://blogs.ubc.ca/lassa/';
			container.appendChild(heading);
			
			//This code loops to generate each post's title and content
			for (var i = 0; i < result.feed.entries.length; i++) {
				var entry = result.feed.entries[i];
				
				if (i == 0) {
					// These variables become the post title link
					var para = document.createElement("p");
					var link = document.createElement("a");
					var title = document.createTextNode(entry.title);
					
					// This variable becomes the post content
					var content = document.createElement("div");
					content.className = "postText";
					
					// This code will truncate entry.content
					// see http://www.barelyfitz.com/projects/truncate/
					// Set the length (in chars) here:
					var len = 600;				
					var trunc = entry.content;
					
					// If the post is longer than our limit, truncate it
					if (trunc.length > len) {
						trunc = trunc.substring(0, len);
						trunc = trunc.replace(/\w+$/, '');					
						trunc += '<a href="' + entry.link + '">' + '[More...]</a>';
						content.innerHTML = trunc;
					} else {
						content.innerHTML = entry.content;
					}
					
					// Drop everything into our container div and repeat (if necessary)
					container.appendChild(para);
					container.appendChild(content);
					para.appendChild(link);
					link.appendChild(title);
					link.href = entry.link;
					link.className = "mainPost";
					
					// Add the "Previous Entries" text
					var prev = document.createElement("h2");
					prev.className = "prev";
					var prevText = document.createTextNode("Previous Entries");
					prev.appendChild(prevText);
					container.appendChild(prev);
					
				} else {
					var para = document.createElement("p");
					var link = document.createElement("a");
					var title = document.createTextNode(entry.title);
					
					container.appendChild(para);
					para.appendChild(link);
					link.appendChild(title);
					link.href = entry.link;
					link.className = "subPost";
				}
			}
		}
	});
}
// Loads the feed content after the rest of the page has loaded
google.setOnLoadCallback(initialize);
