/*
Ajax-RSS feed for NEAT

Based on xx code.

Author: JDS
Date:   1.1.2007 
*/

//Test null value
function isNotNull(value){
  return (value != null);
}

//objects inside the RSS2Item object
function RSS2Enclosure(encElement){
	if (isNotNull(encElement)){
		this.url = encElement.getAttribute("url");
		this.length = encElement.getAttribute("length");
		this.type = encElement.getAttribute("type");
	}
	else{
		this.url = null;
		this.length = null;
		this.type = null;
	}
}

function RSS2Guid(guidElement){
	if (isNotNull(guidElement)){
		this.isPermaLink = guidElement.getAttribute("isPermaLink");
		this.value = guidElement.childNodes[0].nodeValue;
	}
	else{
		this.isPermaLink = null;
		this.value = null;
	}
}

function RSS2Source(souElement){
	if (isNotNull(souElement)){
		this.url = souElement.getAttribute("url");
		this.value = souElement.childNodes[0].nodeValue;
	}
	else{
		this.url = null;
		this.value = null;
	}
}

//object containing the RSS 2.0 item
function RSS2Item(itemxml)
{
	//required
	this.title;
	this.link;
	this.description;

	//optional vars
	this.author;
	this.comments;
	this.pubDate;

	//optional objects
	this.category;
	this.enclosure;
	this.guid;
	this.source;

	var properties = new Array("title", "link", "description", "author", "comments", "pubDate");
	var tmpElement = null;
	for (var i=0; i<properties.length; i++){
    try{
      tmpElement = itemxml.getElementsByTagName(properties[i])[0];
	    if (isNotNull(tmpElement))
        eval("this."+properties[i]+"=tmpElement.childNodes[0].nodeValue");
    }
    catch(error){
      //property staies empty
    }
	}

	this.category = new RSS2Category(itemxml.getElementsByTagName("category")[0]);
	this.enclosure = new RSS2Enclosure(itemxml.getElementsByTagName("enclosure")[0]);
	this.guid = new RSS2Guid(itemxml.getElementsByTagName("guid")[0]);
	this.source = new RSS2Source(itemxml.getElementsByTagName("source")[0]);
}

//objects inside the RSS2Channel object
function RSS2Category(catElement){
	if (isNotNull(catElement)){
		this.domain = catElement.getAttribute("domain");
		this.value = catElement.childNodes[0].nodeValue;
	}
	else{
		this.domain = null;
		this.value = null;
	}
}

//object containing RSS image tag info
function RSS2Image(imgElement)
{
	if (isNotNull(imgElement)){
		imgAttribs = new Array("url","title","link","width","height","description");
		for (var i=0; i<imgAttribs.length; i++)
			if (isNotNull(imgElement.getAttribute(imgAttribs[i])))
				eval("this."+imgAttribs[i]+"=imgElement.getAttribute("+imgAttribs[i]+")");
	}
	else{
  	this.url = null;
  	this.link = null;
  	this.width = null;
  	this.height = null;
  	this.description = null;
	}
}

//object containing the parsed RSS 2.0 channel
function RSS2Channel(rssxml)
{
	//required
	this.title;
	this.link;
	this.description;

	//array of RSS2Item objects
	this.items = new Array();

	//optional vars
	this.language;
	this.copyright;
	this.managingEditor;
	this.webMaster;
	this.pubDate;
	this.lastBuildDate;
	this.generator;
	this.docs;
	this.ttl;
	this.rating;

	//optional objects
	this.category;
	this.image;

	var chanElement = rssxml.getElementsByTagName("channel")[0];
	var itemElements = rssxml.getElementsByTagName("item");
	
	for (var i=0; i<itemElements.length; i++){
		Item = new RSS2Item(itemElements[i]);
		this.items.push(Item);
	}

	var properties = new Array("title", "link", "description", "pubDate");
	var tmpElement = null;
	for (var i=0; i<properties.length; i++){
		try{
      tmpElement = chanElement.getElementsByTagName(properties[i])[0];
		  if (isNotNull(tmpElement))
			  eval("this."+properties[i]+"=tmpElement.childNodes[0].nodeValue");
		}
		catch(error){
      //property remains empty
    }
	}

//	this.category = new RSS2Category(chanElement.getElementsByTagName("category")[0]);
//	this.image = new RSS2Image(chanElement.getElementsByTagName("image")[0]);
}

//PROCESSES

//uses xmlhttpreq to get the raw rss xml
function getRSS(url, target_div, with_date){
  var xhr;
	//call the right constructor for the browser being used
	if (window.ActiveXObject)
		xhr = new ActiveXObject("Microsoft.XMLHTTP");
	else if (window.XMLHttpRequest)
		xhr = new XMLHttpRequest();
	else
		return "Ajax n'est pas supporté par votre navigateur. Pour  voir les actualités, veuillez mettre à jour celui-ci.";

	//prepare the xmlhttprequest object
	xhr.open("GET",url,true);
	xhr.setRequestHeader("Cache-Control", "no-cache");
	xhr.setRequestHeader("Pragma", "no-cache");
	xhr.onreadystatechange = function() {
		if (xhr.readyState == 4){
			if (xhr.status == 200){
				if (xhr.responseText != null){
					processRSS(xhr.responseXML, target_div, with_date);
				}
				else
					return "Les actualités ne sont pas disponibles en ce moment. Veillez nous en excuser.";
			}
			else
				return "Les actualités ne sont pas disponibles en ce moment. Veillez nous en excuser.<br /> Error code " + xhr.status + " received: " + xhr.statusText;
		}
	}

	//send the request
	xhr.send(null);
}

//processes the received rss xml
function processRSS(rssxml, target_div, with_date){
	RSS = new RSS2Channel(rssxml);
	renderRSS(RSS,target_div, with_date);
}

//shows the RSS content in the browser
function renderRSS(RSS, target_div, with_date){
	//default values for html tags used
	var startItemTag = "<div class='rss' id='rss_item'>";
	var startDate = "<div class='rss' id='rss_date'>";
	var startTitle = "<div class='rss' id='rss_title'>";
	var startLinkTag = "<a class='rss' id='rss_link' href='";
	var endStartLinkTag = "'>";
	var endLinkTag = "</a>";
	var endTag = "</div>";
	//populate the items
	document.getElementById(target_div).innerHTML = "";
	for (var i=0; i<RSS.items.length; i++){
    var pubDate;
    (isNotNull(RSS.items[i].pubDate)) ? pubDate = new Date(RSS.items[i].pubDate) : pubDate=null;
    
		item_html = startItemTag;
		if (with_date){
      item_html += startDate + ((isNotNull(pubDate)) ? ((pubDate.getDate()<10?"0":"")+pubDate.getDate()+"."+(pubDate.getMonth()<9?"0":"")+(pubDate.getMonth()+1)+"."+((pubDate.getYear()>1900?pubDate.getYear():pubDate.getYear()+1900))) : "") + endTag;
    }
		item_html += startTitle;
    item_html += ((isNotNull(RSS.items[i].link)) ? startLinkTag + RSS.items[i].link + endStartLinkTag : "");
    item_html += ((isNotNull(RSS.items[i].title)) ? RSS.items[i].title : "");
    item_html += ((isNotNull(RSS.items[i].link)) ? endLinkTag : "") + endTag;
		item_html += endTag;
		document.getElementById(target_div).innerHTML += item_html;
	}

	return true;
}
