/* ENEWS READER: Reads the content of an XML file and creates a list, with the XML data.
 * We use it to create a newsletter links box. */
	
	/*CONFIGURATION OPTIONS ***********************************************************************************************************/
				
	var xml_feed_file = '/site/e-news-reader/feed/feed.xml';		/*The URL or path of the XML to show*/
	var v_news_box_id = 'enews';								/*ID of the box where the news will appear*/
	var v_news_list_class = 'rss-list';							/*Class name of the enew's list */
	var v_news_item_class = 'rss-item';							/*Class name of the enew's item list*/
	var v_news_title_class = 'rss-title';						/*Class name of the title of the enew's item*/
	var v_news_description_box_class = 'rss-description';		/*Class name of the enew's description box*/
	var v_read_more_btn = 'Lees meer';  						/*The read more button text*/
	var v_read_more_target = '_blank';  						/*For open the complete news in a new window (_blank) or in the same one (_self)*/
	var v_read_more_btn_class = 'read-more';  					/*The read more button class name*/
				
	/**********************************************************************************************************************************/

	
	window.onload = display_news;

		
	function readRSS(){

			var xmlDoc=null;
			var browser = navigator.userAgent;
			
			if (window.ActiveXObject)
			{// code for IE
				xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
				xmlDoc.async = false;
				xmlDoc.load(xml_feed_file);
			}
			else if (document.implementation.createDocument)
			{
				if(browser.toLowerCase().indexOf('safari') > -1 || browser.toLowerCase().indexOf('chrome') > -1){
					//code for Safari or Chrome
					var xmlhttp = new window.XMLHttpRequest();
					xmlhttp.open("GET",xml_feed_file,false);
					xmlhttp.send(null);
					xmlDoc = xmlhttp.responseXML.documentElement;
				}else{
					// code for Mozilla, Firefox, Opera, etc.
					xmlDoc=document.implementation.createDocument("","",null);
					xmlDoc.async = false;
					xmlDoc.load(xml_feed_file);
				}
			}
			else
			{
				alert('Your browser cannot handle this script');
			}
			
			if (xmlDoc != null) {					
				var items = xmlDoc.getElementsByTagName("item");
				return items;
			}
	}


	function display_news(){
			
			var x = readRSS();
			
				/*Creates an UL element for the enews list and add it to the DOM*/
				var container = document.getElementById(v_news_box_id);
				var enews_list = document.createElement('ul');
				enews_list.setAttribute('class', v_news_list_class);
				container.innerHTML='';
				container.appendChild(enews_list);	
				
				
				/*Read the XML*/
				for (i=0; i< x.length ; i++)
				{ 
					var enews_item = document.createElement('li');
					enews_item.setAttribute('class',v_news_item_class);
					
					/*ADDS THE TITLE OF THE NEWS*/
					var news_title_elem = document.createElement('a');
					news_title_elem.setAttribute('href','javascript:void(0)');
					news_title_elem.setAttribute('class',v_news_title_class);
					
					var news_title = document.createTextNode(x[i].getElementsByTagName("title")[0].childNodes[0].nodeValue);
					news_title_elem.appendChild(news_title);
					enews_item.appendChild(news_title_elem);
					
					
					/*ADDS THE DESCRIPTION OF THE NEWS*/
					var news_description_div = document.createElement('div');
					news_description_div.setAttribute('class',v_news_description_box_class);
					
					var news_description_p = document.createElement('p');
					var news_description = document.createTextNode(x[i].getElementsByTagName("description")[0].childNodes[0].nodeValue);
					news_description_p.appendChild(news_description);
					news_description_div.appendChild(news_description_p);
										
										
					/*ADDS THE READ MORE BUTTON*/
					if(x[i].getElementsByTagName("link")[0].childNodes[0]) {
						var enews_link = document.createElement('a');
						enews_link.setAttribute('href',x[i].getElementsByTagName("link")[0].childNodes[0].nodeValue);
						enews_link.setAttribute('target',v_read_more_target);
						enews_link.setAttribute('class',v_read_more_btn_class);
						var read_more_btn = document.createTextNode(v_read_more_btn);
						enews_link.appendChild(read_more_btn);
						news_description_div.appendChild(enews_link);
					}
					
					
					enews_item.appendChild(news_description_div);
										
					enews_list.appendChild(enews_item);	
				
				}
					
					/*Show the enews*/
					//alert(container.innerHTML);
					document.getElementById(v_news_box_id).innerHTML = container.innerHTML;

					
					
					//hide the div that appears after the .rss-title elements
					$("."+v_news_title_class).next("div").hide();
					
					//toggle the components with class rss-title
					$("."+v_news_title_class).click(function(){
						$(this).next("div").slideToggle(300);
						$(this).toggleClass(v_news_title_class+"-active");
					});
			
		}

