/* Ajax RSS Feeder 
   Author: Greg Long 
   Email: glong@ideaport.com
   Version: 2.0
   February 13, 2007
 */

initializeUserConstants();
initializeSystemConstants();

Event.observe(window, 'load', function() {
	// Create the container items to display feed data
	$(feedTitleContainer).innerHTML =startupContainerMessage;

	// Heading to show for this RSS Feed
//	$(feedHeadingContainer).innerHTML=feedHeading;
	
	// Set up the interval by which new feed data will be retrieved from source
	 
	setInterval(getRSSFeed, feedPullRate * 1000)

	// Set up the interval by which feed items will be changed
	 
	//setInterval (populateCurrentFeedItem, itemRefreshRate * 1000 );
	
	// Instead of waiting for the PeriodicalExecuter to fire...let's get the feed right away
	getRSSFeed();
});

function getRSSFeed() {
	// Make request to mod_proxy URL to get feed from "real" source
	WebKit.xhrRequest (feedURL, {method: 'get', onSuccess: processFeed, onFailure: noFeed});
	
}

function noFeed() {
	alert('The feed is currently unavailable');
}

function processFeed(resultObj) {
//  alert(resultObj.responseText);
	var rssFeed = resultObj.responseXML;
	var feedString = '';
	feedArray = [];
	
	// Just get the feed items ... ignore the rest
	var itemNodes = rssFeed.getElementsByTagName('item');
	
	// Set the number of feed items
	feedCount = itemNodes.length;

	// Loop through the feed items up to the maximum we specified 
	for (var x = 0; x < feedCount; x++) {
		// Get the feed item title
		var tString = itemNodes[x].getElementsByTagName('title')[0].firstChild.nodeValue
		tString = tString.replace(tagSearch, ''); // Strip our HTML if title
		
		// Get the feed time link
		var lString = itemNodes[x].getElementsByTagName('link')[0].firstChild.nodeValue
		lString = lString.replace(tagSearch, ''); // Strip our HTML if title
		
		//. Create a clickable link
		lString = '<a href="' + lString + '">' + tString + '</a>';
		
		// Get the feed item description
		var dString = itemNodes[x].getElementsByTagName('description')[0].firstChild.nodeValue
		
		// Strip out all the garbage in it
		dString = dString.replace(tagSearch, '');
		
		// Limit the size of it to the maximum length we set
		dString_before = dString.length;
		if (dString.length > maxDescriptionLength) {
			dString = dString.substring(0,maxDescriptionLength -3) + '...';
		}
		
		// push the feed items into the feed array
		feedArray.push({'title': lString, 'description': dString})

	}
//	alert(feedArray[0].description);

	// Display the current feed item in feed container
	populateCurrentFeedItem();
}

function populateCurrentFeedItem() {
//	alert(feedArray[0].description); 

	if (feedArray == null) return; // Not initialize yet, get out
	
	// Get the current feed data item and display it
	maxIndex = 4;
	if ( feedCount < maxIndex ) {
	  maxIndex = feedCount;
	}
	
	for ( index = 0; index < maxIndex; index++) {
	  $('FeedTitle' + index).innerHTML = feedArray[index].title;
	  $('FeedDescription' + index).innerHTML =  feedArray[index].description;
	}

}


/* User Definable Constants */
function initializeUserConstants() {
	// Maximum number of characters to store from description field of RSS field
	// Keep low to not overfill cookie allotement of 4K
	maxDescriptionLength = 75;

	// Number of seconds between feed item refreshes
	itemRefreshRate = 30;

	// Number of seconds between requests to feed source
	feedPullRate = 600;

	// Message to show in feed container until feed can be displayed
	startupContainerMessage = 'Initializing...';

	// ID of container to hold the feed
	feedContainer = 'FeedContainer';

	// ID of container to that holds feed heading
	feedHeadingContainer = 'FeedHeading';
	
	// ID of container to hold the feed title
	feedTitleContainer = 'FeedTitle0';

	// ID of container to hold the feed description
	feedDescriptionContainer = 'FeedDescription0';
	
	// Feed sources
	// These have to be setup through the Apache mod_proxy
	// Have to look like they are coming from the same domain
	//feedURL = "http://exercises.site/bbc_rss/rss.xml";
	feedURL = "http://www.buildyourwellness.com/blog/feed/";

	// Heading for feed
	feedHeading = "Recent Posts";

	// No Internet access?  Use this permanently cached feed
	//feedURL = "http://exercises.site/bbc_rss_no_internet/bbc_rss.xml";
}

/* System Definable Constants */
function initializeSystemConstants() {
	// Regular expression search to strip out HTML in RSS descriptions
	tagSearch = /<(.*)>.*<\/\1>/gi

	// Counter to keep track of which feed item is being displayed
	currentItemIndex = 0;
	
	// Array the will hold the current feed pull from the RSS source
	feedArray = [];
	
	// Set the feed items returned from Feed
	feedCount = 0;


}
