google.load("feeds", "1");

// Initialise the feeds
function initializeFeeds()
	{
	// Create and load the feed
	var feed = new google.feeds.Feed("http://suffolkcyclist.co.uk/feed/");
	feed.includeHistoricalEntries();
	feed.setNumEntries(1);
	feed.load(feedLoaded);
	}

// Feed has loaded
function feedLoaded(result)
	{
	if (!result.error)
		{
		// Find the template feed entry
		var entry = document.getElementById("feed");
		if (entry)
			{
			// Create a copy and add it to the end
			var obj = entry.cloneNode(true);
			entry.parentNode.appendChild(obj);
			
			// Set the fields of the entry
			setClassVal(obj, 'feed_title', result.feed.title);
			var feed_list = getClass(obj, 'feed_list');
			var feed_entry = getClass(feed_list, 'feed_entry');

			// Iterate through the entries in the feed
			for (var i = 0; i < result.feed.entries.length; i++)
				{
				// Find the list entry template
				// Create a copy and add it to the end
				var entry = result.feed.entries[i];
				var li = feed_entry.cloneNode(true);
				feed_entry.parentNode.appendChild(li);
				
				// Set the fields in the list
				setClassVal(li, 'feed_entry_title', entry.title);
				setClassVal(li, 'feed_entry_summary', entry.contentSnippet);
				var link = getClass(li, 'feed_entry_link');
				link.href = entry.link;

				// Make the list item visible
				li.style.display = 'block';
				}
			
			// Make this feed visible
			obj.style.display = 'block';
			}
		}
	}
function setClassVal(element, name, value)
	{
	var child = getClass(element, name);
	if (child)
		{
		child.innerHTML = value;
		return child;
		}
	}
function getClass(element, name)
	{
	var children = element.childNodes;
	for(var i = 0; i < children.length; i++)
		{
		if(children.item(i).className == name)
			return children.item(i);
		var sub = getClass(children.item(i), name);
		if (sub)
			return sub;
		}
	}
google.setOnLoadCallback(initializeFeeds);

