/* GET PRESS/NEWS/EVENTS FEED */
$(document).ready(function() {
		/* the maximum number of chars. The system search for the first space after this number and create a substring of that */
		var Limit = 65;
		/* max number of elements to be shown */
		var MaxShow = 3;
		var FeedArray = [ "press", "news", "events" ];

		jQuery.each(FeedArray, function() {
		/*
		old code. The filename (and position) is changed
		 
		var XmlFile = '/corporate/public/site_preferences/'+this+'.xml';

		*/
		
		/* get the first letter from the lilst of elements */
		var FeedId = this.substr(0,1);
		/* the XML to be read */
		var XmlFile = 'http://www.eurocontrol.int/xmlaggregator/fetchfeed-w-'+FeedId+'-output.xml';
		var FeedClass = this;
		var i=0;
		
		// alert (XmlFile);
		$.get(XmlFile, function(d){
			
			$(d).find('entry').each(function(){

				var $press = $(this);
				var title = $press.find('title').text();
				var date = $press.find('published').text();
				var link = $press.find('link').attr('href');
				var summary = TextReduction ($press.find('summary').text(),Limit);
				var html = '<li>';
				html += '<strong>' + date + '<br/></strong>' ;
				html += '<span><a href='+ link +'>' + title + '</a></span>';

				if (summary.length>0)
					html += '<br/>'+summary+'... <a href='+ link +' class="more">more</a>' ;
					html += '</li>';

				if (i<MaxShow)
					$("#"+FeedClass+"-feed").append($(html));
					i++;
				});
				
				$("#"+FeedClass+"-feed").wrapInner(document.createElement("ul"));
				
			});
		});
});

/* function to create a substring of a passed string. It search for the first space after the limit of char devined as passed variable */ 
function TextReduction (Text,Limit){
	/*  replaces escaped brackets with real ones  */
	Text = Text.replace(/&(lt|gt);/g, function (strMatch, p1){
 		 	return (p1 == "lt")? "<" : ">";
 		});
	/* eliminates all html tag */
	Text = Text.replace(/<\/?[^>]+(>|$)/g, "");
	/* reduce the text and return it */
	return Text.substr(0,Text.indexOf(' ',Limit)+1);  
}
