// JavaScript Document
// http://www.quirksmode.org/dom/toc.html
// http://code.google.com/apis/ajaxfeeds/documentation/

google.load("feeds", "1");

var shortDayNames = new Array("Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat");
var shortMonthNames = new Array("Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec");

function getTrimmedDate(datestring)
{
	var d = new Date(datestring);
	// Tue, 19 Jun 2007
	return shortDayNames[d.getDay()] + ", " + d.getDate() + " " + shortMonthNames[d.getMonth()] + " " + d.getFullYear();
}

function initialize() {
  var feed = new google.feeds.Feed("http://blog.churchers.co.uk/feeds/posts/default/-/Articles");
  feed.setNumEntries(4);
  feed.load(function(result) {
	if (!result.error) {
	  var container = document.getElementById("latestarticlesfeed");
	   
	  // empty out all children
	  if ( container.hasChildNodes() )
	  {
		while ( container.childNodes.length >= 1 )
		{
			container.removeChild( container.firstChild );       
		} 
	  }	   
	  
	  /*
	  // building the following source code:
      <h3 class="newsHeadline"><a id="" href="">Headline Text</a></h3>
      <p>Content
	  <br /><a class="readMore" href="">Read More</a>
      </p>
	  */
	  
	  // header
	  var header = document.createElement('h2');
	  header.appendChild(document.createTextNode("Latest Articles"));
	  container.appendChild(header);
	  
	  // content
	  for (var i = 0; i < result.feed.entries.length; i++)
	  {
		var entry = result.feed.entries[i];
				
		// line one
		var headline_h3 = document.createElement('h3');
//		headline_h3.className += "newsHeadline";
		headline_h3.appendChild(document.createTextNode(entry.title));
		container.appendChild(headline_h3);
			
		
		// line two - as content
		var postitem_p = document.createElement('p');
		postitem_p.appendChild(document.createTextNode(entry.contentSnippet));

		// line two - as headline
//		var postitem_p = document.createElement('p');
//		postitem_p.appendChild(document.createTextNode(entry.title));
//		postitem_p.className += "latestnews_posttitle";
//		container.appendChild(postitem_p);

		// line three
		var postlink_br = document.createElement('br');
		postitem_p.appendChild(postlink_br);
		
		var postlink_a = document.createElement('a');
		postlink_a.innerHTML = "Read More";
		postlink_a.className += "readMore";
		postlink_a.href = entry.link;
		postitem_p.appendChild(postlink_a);

		container.appendChild(postitem_p);
	  }
	  
	// footer
	var footer_p = document.createElement('p');
	var footer_a = document.createElement('a');
	footer_a.innerHTML = "View All";
	footer_a.className += "viewAll";
	footer_a.href = "http://blog.churchers.co.uk/";
	footer_p.appendChild(footer_a);	
	container.appendChild(footer_p);
	}
  });
}

google.setOnLoadCallback(initialize);