/* 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. */

 /* UPDATE: Now uses Google Feed functions to retrieve the feed from kayako (myShop support). */
	
	/*CONFIGURATION OPTIONS ***********************************************************************************************************/
				
	var xml_feed_file = 'http://support.mijnwinkel.nl/rss/index.php?/News/Feed';		/*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 display_news(){
		$.jGFeed(xml_feed_file,
		function(feeds){
		  // Check for errors
		  if(!feeds){
			// there was an error
			return false;
		  }
		  format_news(feeds);
		}, 10);
	}

	function format_news(x){
		document.getElementById('rss_link').href=x.link;
		x=x.entries;
		/*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].title);
			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].contentSnippet);
//			news_description_p.appendChild(news_description);
			news_description_p.innerHTML = x[i].contentSnippet;
			news_description_div.appendChild(news_description_p);
								
								
			/*ADDS THE READ MORE BUTTON*/
			if(x[i].link) {
				var enews_link = document.createElement('a');
				enews_link.setAttribute('href',x[i].link);
				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");
			});
	}

