// JavaScript Document

// JavaScript Document
// http://www.quirksmode.org/dom/toc.html
// http://code.google.com/apis/ajaxfeeds/documentation/

google.load("feeds", "1");

function initialize() {
  var feed = new google.feeds.Feed("http://blog.plasterboardrecycling.co.uk/feeds/posts/default");
  feed.setNumEntries(5);
  feed.load(function(result) {
	if (!result.error) {
	  var container = document.getElementById("latest_news");
	  
	  // empty out all children
	  if ( container.hasChildNodes() )
	  {
		while ( container.childNodes.length >= 1 )
		{
			container.removeChild( container.firstChild );       
		} 
	  }	   
	  
	  // abort if no headlines to display
	  if (result.feed.entries.length == 0)
	  	return;
	  	  	  
	  // build html and insert headlines into the DOM
	  var header = document.createElement('h2');
	  header.className += "news_header";
	  header.appendChild(document.createTextNode("News"));
	  container.appendChild(header);

/*
  <div class="news_item">
	<p> <span class="news_title">20/02/2008</span><br />
	  Headline </p>
  </div>
*/

/*	  var postlist = document.createElement('ul');
  	  postlist.className += "fp_list";	  
	  container.appendChild(postlist);*/
	  
	  for (var i = 0; i < result.feed.entries.length; i++)
	  {
		var entry = result.feed.entries[i];

		var postlistitem = document.createElement('div');
		postlistitem.className += "news_item";
		
		var dateofpost = new Date(entry.publishedDate);
		
		var postwrap = document.createElement('p');
        var postspan = document.createElement('span');
		postspan.className += "news_title";
		postspan.appendChild(document.createTextNode(dateofpost.format("dd/mmm/yy")));
		postwrap.appendChild(postspan);
		
		var postbr = document.createElement('br');
		postwrap.appendChild(postbr);
		
		//postwrap.appendChild(document.createTextNode(entry.title));
		
		var postlink = document.createElement('a');
		postlink.appendChild(document.createTextNode(entry.title));

		postwrap.appendChild(postlink);
		postlink.href = entry.link;
		
		postlistitem.appendChild(postwrap);
		container.appendChild(postlistitem);
	  }
	}
  });
}

google.setOnLoadCallback(initialize);