/*------------------------------------------------------------------------------------------------

 *Filename:				google_feeds_api.js
 *Description:			RSS feed aggrigator/page-block for HVHS minisite
 *Version:				1.0.0 (07-20-2009) MM-DD-YYYY
 *Website:				http://www.louisianahighschools.org
 *Author:				Christopher Barnette

== STRUCTURE: ====================================================================================

 *Explaination:			This script is called from the [body.onload] event of every page where 
 						the HVHS Blog RSS feed needs to be embedded. In its original design, the 
						script has three possible formats in which the RSS feed entries can be 
						displayed. The format required changes on each of the three page types; 
						home (case 1), blog (case 2), and interior (case 3). To assist with date 
						formatting issues, two new methods () have been added to the default 
						JS Date() object through the prototype() object. RSS Entries coming from
						the Google Feeds API are in [JSON] format.
 *Custom Functions:		initialize_feed_embed()
 *Custom Properties:	Date.toDDMMYYYYString, Date.toDDDDMMMMDYYYYString
 *Custom Arrays:		month_names[], weekday_names[]
 *RSS Entry Props:		title, link, content, contentSnippet, publishedDate, categories[]
 *Incl. Frameworks:		Google Feeds API (AJAX) -
 						http://code.google.com/apis/ajaxfeeds/documentation/reference.html

== NOTES: ========================================================================================

 *Any other notes or commentary goes here.

------------------------------------------------------------------------------------------------*/

google.load("feeds", "1");

function initialize_feed_embed(display_type) {
	var feed = new google.feeds.Feed("http://highvaluehighschools.blogspot.com/feeds/posts/default");
	feed.load(function(result) {
		if (!result.error) {
			var container;
			var max_items = 0;
			
			switch (display_type) {
				case 1: //Home page, 2 entries, Date[span]/Title[h3]/Snippet[p]/Link[a]
				
					//## Output Example ##
					//<span class="blog_date">Monday, July 17, 2009</span>
                    //<h3>Phasellus Vitae Dolor Nulla Risus Augue</h3>
                    //<p>Maecenas elit. Donec sit amet augue. Quisque ut augue sit amet purus cursus fermentum ... <a class="read_more_link" href="#">Read More &raquo;</a></p>
				
					max_items = 2; // or "result.feed.entries.length;" for all items in feed
					container = document.getElementById("blog_recent");
					for (var i = 0; i < max_items; i++) {
						var entry = result.feed.entries[i];
						
						var date_span = document.createElement("span");
						var date_formatted = new Date(entry.publishedDate);
						date_formatted = date_formatted.toDDDDMMMMDYYYYString(); //See prototype function below
						date_span.appendChild(document.createTextNode(date_formatted));
						date_span.setAttribute("class", "blog_date");
						date_span.setAttribute("className", "blog_date");
						container.appendChild(date_span);
						
						var title_h3 = document.createElement("h3");
						title_h3.appendChild(document.createTextNode(entry.title));
						container.appendChild(title_h3);
						
						var snippet_p = document.createElement("p");
						snippet_p.appendChild(document.createTextNode(entry.contentSnippet + " "));
						snippet_p.innerHTML = entry.contentSnippet + " " + "<a class=\"read_more_link\" href=\"" + entry.link + "\">Read More &raquo;</a>";
						container.appendChild(snippet_p);
					}
				break;
				case 2: //Blog side bar, 3 entries, Date[span]/Snippet[span]/Link[span+a]
				
					//## Output Example ##
					//<div class="recent_post" classname="recent_post">
					//<span class="post_date" classname="post_date">07/17/2009</span>
					// - 
					//<span class="post_snippet" classname="post_snippet">Nullam sollicitudin euismod libero sit amet feugiat. Sed et libero nisi. Aenean tincidunt tempor mattis. In hac habitasse ... </span>
					//<span class="post_more_link" classname="post_more_link">
					//<a href="http://highvaluehighschools.blogspot.com/2009/07/mauris-aliquam-aliquam-mollis.html">Read More &raquo;</a>
					//</span>
					//</div>
				
					max_items = 2;
					container = document.getElementById("blog_recent");
					for (var i = 0; i < max_items; i++) {
						var entry = result.feed.entries[i];
						
						var entry_container = document.createElement("div");
						entry_container.setAttribute("class", "recent_post");
						entry_container.setAttribute("className", "recent_post");
						container.appendChild(entry_container);
						
						var date_span = document.createElement("span");
						var date_formatted = new Date(entry.publishedDate);
						date_formatted = date_formatted.toDDMMYYYYString();
						date_span.appendChild(document.createTextNode(date_formatted));
						date_span.setAttribute("class", "post_date");
						date_span.setAttribute("className", "post_date");
						entry_container.appendChild(date_span);
						
						entry_container.appendChild(document.createTextNode(" - "));
						
						var snippet_span = document.createElement("span");
						snippet_span.appendChild(document.createTextNode(entry.contentSnippet + " "));
						snippet_span.setAttribute("class", "post_snippet");
						snippet_span.setAttribute("className", "post_snippet");
						entry_container.appendChild(snippet_span);
						
						var more_span = document.createElement("span");
						more_span.setAttribute("class", "post_more_link");
						more_span.setAttribute("className", "post_more_link");
						var more_link = document.createElement("a");
						more_link.setAttribute('href', entry.link);
						more_link.innerHTML = "Read More &raquo;";
						more_span.appendChild(more_link);
						entry_container.appendChild(more_span);
						
					}
				break;
				case 3: //Interior page, 1 entry, Date[p]/Title[p]/Snippet[p]/Link[p]
				
					//## Output Example ##
					//<p class="date">Monday, July 17, 2009</p>
                    //<p class="title">Phasellus Vitae Dolor Nulla Risus Augue</p>
                    //<p class="body">Maecenas elit. Donec sit amet augue. Quisque ut augue sit amet purus cursus fermentum ...</p>
                	//<p class="more"><a href="#">Read More &raquo;</a></p>
					
					max_items = 1;
					container = document.getElementById("blog_recent");
					for (var i = 0; i < max_items; i++) {
						var entry = result.feed.entries[i];
						
						var date_p = document.createElement("p");
						var date_formatted = new Date(entry.publishedDate);
						date_formatted = date_formatted.toDDDDMMMMDYYYYString();
						date_p.appendChild(document.createTextNode(date_formatted));
						date_p.setAttribute("class", "date");
						date_p.setAttribute("className", "date");
						container.appendChild(date_p);
						
						var title_p = document.createElement("p");
						title_p.appendChild(document.createTextNode(entry.title));
						title_p.setAttribute("class", "title");
						title_p.setAttribute("className", "title");
						container.appendChild(title_p);
						
						var snippet_p = document.createElement("p");
						snippet_p.setAttribute("class", "body");
						snippet_p.setAttribute("className", "body");
						snippet_p.appendChild(document.createTextNode(entry.contentSnippet));
						container.appendChild(snippet_p);
						
						var more_p = document.createElement("p");
						more_p.setAttribute("class", "more");
						more_p.setAttribute("className", "more");
						var more_link = document.createElement("a");
						more_link.setAttribute('href', entry.link);
						more_link.innerHTML = "Read More &raquo;";
						more_p.appendChild(more_link);
						container.appendChild(more_p);
					}
				break;
			}
		}
	});    
}

var month_names=new Array(12);
month_names[0]="January";
month_names[1]="February";
month_names[2]="March";
month_names[3]="April";
month_names[4]="May";
month_names[5]="June";
month_names[6]="July";
month_names[7]="August";
month_names[8]="September";
month_names[9]="October";
month_names[10]="November";
month_names[11]="December";

var weekday_names=new Array(7);
weekday_names[0]="Sunday";
weekday_names[1]="Monday";
weekday_names[2]="Tuesday";
weekday_names[3]="Wednesday";
weekday_names[4]="Thursday";
weekday_names[5]="Friday";
weekday_names[6]="Saturday";

Date.prototype.toDDMMYYYYString = function () { // ex. 05/29/1981
	return isNaN (this) ? 'NaN' : [this.getMonth() > 8 ? this.getMonth() + 1 : '0' + (this.getMonth() + 1), this.getDate() > 9 ? this.getDate() : '0' + this.getDate(), this.getFullYear()].join('/');
}

Date.prototype.toDDDDMMMMDYYYYString = function () { // ex. Friday, May 29, 1981
	return isNaN (this) ? 'NaN' : weekday_names[this.getDay()] + ', ' + month_names[this.getMonth()] + ' ' + this.getDate() + ', ' + this.getFullYear();
}
