﻿/* Author: Simon Gilhooly, Atom14 Ltd
* 
* This module reads from the Blogger API to present blog posts within a website
			<h2><a href="index.html">Match Report from Chelmsford</a></h2>
			
			<p class="post-info">Posted by <a href="index.html">Simon</a> | Filed under <a href="index.html">Match Reports</a></p> 
				
            <p>The Second XI enjoyed a successful visit to the Ford County Ground, Chelmsford, executing a convincing 7 wicket victory over Gidea Park &amp; Romford with Scottie Styris in the crowd.</p>
			
			<p class="postmeta">		
				<a href="index.html" class="readmore">Read more</a> |
				<a href="index.html" class="comments">Comments (7)</a> |				
				<span class="date">August 22, 2010</span>	
			</p>
*/

	function getMyBlogFeed(blogId){
		// Create the blogger service object
		var bloggerService = new google.gdata.blogger.BloggerService('GoogleInc-jsguide-1.0');
		
		// The feed URI used to retrieve blog
		var feedUri = 'http://www.blogger.com/feeds/' + blogId + '/posts/default';
		
		// A callback method invoked when getBlogPostFeed() returns data
		var handleBlogPostFeed = function(postsFeedRoot) {
			var nfCount = 0, mrCount = 0, tsCount = 0, post;
			var posts = postsFeedRoot.feed.getEntries();
		  		
			for (var i = 0; i <= posts.length && i <= 5; i++) {
			    post = posts[i];
			    // 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();
			    var commentCount = 1;
			    			
				var postCategories = post.category;
				var postTags = "";
				for (var j = 0, category; category = postCategories[j]; j++) {
					postTags = postTags + category.term + " ";
				}
			    
			    $('#newsfeed').append("<div class='blog-post'><h2>" + postTitle + "</h2><p class='post-info'>Posted by " + postAuthor + " on " + postDate + " | Filed under " + postTags + "</p><p>" + postContent + "</p><p><fb:like action='like' href='" + postURL + "' colorscheme='dark' layout='standard' show_faces='false' width='450'/></p></div>");
			}
		// inject the script to load Facebook JS library
/*			var e = document.createElement('script');
			e.src = document.location.protocol + "//connect.facebook.net/en_GB/all.js";
			e.async = true;
			document.getElementById('wrap').appendChild(e);
			nfCount ++;  */
		};
		
		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;
	}

