/**
 *
 * 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 = 1;

// Set feed url
var feedUrl = 'http://www.handelsblatt.com/rss/hb.xml';

// Set url of PHP script that will fetch and return a feed or feeds using SimplePie
var scriptUrl = 'http://www.bsg-services.de/wp-content/themes/bsg/update_feed.php';

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

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



/*
	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>... wird aktualisiert</p>';
	div.innerHTML = div.innerHTML + '<img class="loader" src="http://www.bsg-services.de/wp-content/themes/bsg/images/ajax-loader.gif" />';

	if (document.getElementById("intern_news")){
		posts = 2;
	
	}else{
		posts = 3;
	
	}
	
	// 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 + '&posts=' + posts;
	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 = 'Sekunden bis zum Update: ' + parseInt(timeLeft/1000);
}



