/**
 * Update RSS/ATOM feed and display it using SimplePie and Ajax
 *
 * LICENSE: This source file is subject to the BSD license
 * that is available through the world-wide-web at the following URI:
 * http://www.opensource.org/licenses/bsd-license.php.
 *
 * @author     Michael P. Shipley <michael@michaelpshipley.com>
 * @copyright  2008 Michael P. Shipley
 * @license    http://www.opensource.org/licenses/bsd-license.php BSD
 * @version    2.0  added ability to handle multiple instances
 * @link       http://www.michaelpshipley.com Michael Shipley
 */

 
function updateFeed(minutesBetweenUpdates,feedUrl,scriptUrl,cacheDuration,divId)
{
	this.minutesBetweenUpdates = minutesBetweenUpdates;
	this.feedUrl = feedUrl;
	this.scriptUrl = scriptUrl;
	this.cacheDuration = cacheDuration;
	this.divId = divId;
	this.savedHtml = '';

	seconds = this.minutesBetweenUpdates * 60;
	milliseconds = seconds * 1000;

	var o = this;
	var f = function() {runUpdateFeed(o)}

	f();
	
	setInterval(f,milliseconds);	
}
function runUpdateFeed(o)
{
	
	var div = document.getElementById(o.divId);
	div.innerHTML = o.savedHtml + '<div class="loading"><img src="img/loading.gif"></div>';


	if (window.XMLHttpRequest)
	{
		var http = new XMLHttpRequest();
	}
	else if (window.ActiveXObject)
	{
		var http = new ActiveXObject('Microsoft.XMLHTTP')
	}
	else
	{
		alert('browser doesn\'t support javascript http connections');	
		return true;
	}
	
	http.onreadystatechange = function()
	{
		if(http.readyState == 4)
		{
			if(http.status != 200)
			{
				alert(http.responseText);
			}
			else
			{
				o.savedHtml = http.responseText;			
				div.innerHTML = http.responseText;
			}
			return true;				
		}
	}
	
	params = 'url=' + encodeURIComponent(o.feedUrl) + '&cacheduration=' + o.cacheDuration;
	http.open("POST", o.scriptUrl, true);
	http.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	http.setRequestHeader("Content-length", params.length);	
	http.send(params);
	return true;
}

