/**
 *
 * 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    1.0
 * @link       http://www.michaelpshipley.com Michael Shipley
 */

/*
	Global variables
*/

// Set ajax update interval
var minutesBetweenUpdates = 120;

// Set feed url
var feedUrl = 'http://celebrity-worship.com/blog/feed/?nats=MTA1ODcuMi45LjkuMS42MDAwMDkzLjAuMC4w';

// Set url of PHP script that will fetch and return a feed or feeds using SimplePie
var scriptUrl = 'http://www.auroporn.com/blog/update_feed.php';

// Set SimplePie cache duration (seconds)
var cacheDuration = 3600;  // normally 3600

// Set div id where feed output will go
var divId = 'feed'; 

/*
	Initialize feed display
*/
updateFeed(feedUrl,scriptUrl,cacheDuration,divId); 

/*
	Start feed update timer
*/

// Calc milliseconds
var seconds = minutesBetweenUpdates * 60;
var milliseconds = seconds * 1000;

// Setup function that feed update timer will call
var command = 'updateFeed(feedUrl,scriptUrl,cacheDuration,divId)';

// Start feed update timer
var updateFeedTimer = setInterval(command,milliseconds);

/**
	Use ajax to call  SimplePie to get feed data
	@param: feedUrl  string  feed link
	@param: scriptUrl string php script url
	@param: cacheDuration integer SimplePie cache duration in seconds
	@param: divId string id of div to output html to
*/
function updateFeed(feedUrl,scriptUrl,cacheDuration,divId)
{
	// Display the "updating" message
	var div = document.getElementById(divId);
	div.innerHTML = div.innerHTML + '<p>Updating...</p>';

	// Get the http requester
	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;
	}

	// Process response
	http.onreadystatechange = function()
	{
		if(http.readyState == 4)
		{
			if(http.status != 200)
			{
				alert(http.responseText);
			}
			else
			{
				div.innerHTML = http.responseText;
			}
			return true;
		}
	}

	// Send http request via post
	params = 'url=' + feedUrl + '&cacheduration=' + cacheDuration;
	http.open("POST", scriptUrl, true);
	http.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	http.setRequestHeader("Content-length", params.length);
	http.send(params);
	return true;
}

/*
	Start countdown timer (optional)
*/
var countDownTimerDivId = 'countDownTimer';
var date = new Date();
var timeOut = date.getTime() + milliseconds;
var timerId = document.getElementById(countDownTimerDivId);
var timer = setInterval(countDownTimer,100);
function countDownTimer()
{
	var date = new Date();
	var timeLeft = timeOut - date.getTime();
	if(timeLeft <= 0)
	{
		timeLeft = 0;
		timeOut = date.getTime() + milliseconds;
	}
	timerId.innerHTML = 'Seconds to next update: ' + parseInt(timeLeft/1000);
}

