﻿/* Author: Simon Gilhooly, Atom14 Ltd
* 
* This module reads from the Blogger API to present blog posts within a website
*/

	function getMyBlogFeed(){
		// Create the blogger service object
		var bloggerService = new google.gdata.blogger.BloggerService('GoogleInc-jsguide-1.0');
		
		// The feed URI used to retrieve Cavaliers CC blog
		var feedUri = 'http://www.blogger.com/feeds/4094584845935258771/posts/default';
		
		// A callback method invoked when getBlogPostFeed() returns data
		var handleBlogPostFeed = function(postsFeedRoot) {
			var mrCount = 0, nfCount = 0, tnCount = 0;
			var posts = postsFeedRoot.feed.getEntries();

			for (var i = 0, post; post = posts[i]; i++) {
				var docFrag = document.createDocumentFragment();

			    // read all the parts of this post into variables
			    var postTitle = post.getTitle().getText();
			    var postURL = post.getHtmlLink().getHref();
			    var postContent = post.content.$t;
			    var postAuthor = post.author[0].name.$t;
			    var postDate = formatGTime(post.published.$t).toDateString();
			    			
				for (var j = 0, category; category = post.category[j]; j++) {
					// add this post to the appropriate section of the document based on the category(s) given
				    if ((category.term == "News") && (nfCount < 8)) {
						var frag = "<div class='newsitem'><h4>" + postTitle + "</h4><p class='newstext'>";
						frag += postContent + "</p><p class='author'>Added by ";
						frag += postAuthor + " on " + postDate + "</p></div>";
						
						$('#newsfeed').append(frag);
						nfCount += 1;
					} else {
						if ((category.term == "Match Report") && (mrCount < 8)) {
//							document.getElementById("matchreports").appendChild(docFrag);
							$('#matchreports').append("<div class='newsitem'><h4>" + postTitle + "</h4><p class='newstext'>" + postContent + "</p><p class='author'>Added by " + postAuthor + " on " + postDate + "</p></div>");
							mrCount += 1;
						} else {
							if ((category.term == "Team") && (tnCount < 1)) {
//								document.getElementById("teamnews").appendChild(docFrag);
								$('#teamnews').append("<div class='newsitem'><h4>" + postTitle + "</h4><p class='newstext'>" + postContent + "</p><p class='author'>Added by " + postAuthor + " on " + postDate + "</p></div>");

								tnCount += 1;
							}
						}
					}
				}
			}
		};
		
		var handleError = function(error) {
		  alert(error);
		};
		
		bloggerService.getBlogPostFeed(feedUri, handleBlogPostFeed, handleError);

	}

	function formatGTime(gCalTime) { 
		//this function converts the string representation of a date used by Google into a Javascript date object
		var remtxt = gCalTime;
	
		function consume(retxt) {
			var match = remtxt.match(new RegExp('^' + retxt));
			if (match) {
				remtxt = remtxt.substring(match[0].length);
				return match[0];
			}
			return '';
		}
	
		// minutes of correction between gCalTime (2009-09-21T21:00:00.000Z) and GMT
		var totalCorrMins = 0;
	
		var year = consume('\\d{4}');		//match first four numeric characters
		consume('-?');
		var month = consume('\\d{2}');
		consume('-?');
		var dateMonth = consume('\\d{2}');
		var timeOrNot = consume('T');
	
		// if a DATE-TIME was matched in the regex 
		if (timeOrNot == 'T') {
			var hours = consume('\\d{2}');
			consume(':?');
			var mins = consume('\\d{2}');
			consume('(:\\d{2})?(\\.\\d{3})?');
			var zuluOrNot = consume('Z');
	
			// if time from server is not already in GMT, calculate offset
			if (zuluOrNot != 'Z') {
				var corrPlusMinus = consume('[\\+\\-]');
				if (corrPlusMinus != '') {
					var corrHours = consume('\\d{2}');
					consume(':?');
					var corrMins = consume('\\d{2}');
					totalCorrMins = (corrPlusMinus=='-' ? 1 : -1) * 
					(Number(corrHours) * 60 + 
					(corrMins=='' ? 0 : Number(corrMins)));
				}
			} 
		} else {
			// if only a DATE was matched
			var hours = 0;
			var mins = 0;
		}
	
		// get time since epoch and apply correction, if necessary
		// relies upon Date object to convert the GMT time to the local
		// timezone
		var originalDateEpoch = Date.UTC(year, month - 1, dateMonth, hours, mins);
		var gmtDateEpoch = originalDateEpoch + totalCorrMins * 1000 * 60;
		var ld = new Date(gmtDateEpoch);
	
		return ld;
	}

