// JavaScript Document
// Modified from O'Reilly's exemple code
// HTML full options :
// <div id="chan_title"></div>
// <div id="chan_link"></div>
// <div id="chan_description"></div>
// <a id="chan_image_link" href=""></a>
// <div id="chan_items"></div>
// <div id="chan_pubDate"></div>
// <div id="chan_copyright"></div>

var rssajax = {
	init:function(url){
		rssajax.showchannel = false;
		rssajax.showimage = false;
		rssajax.duration = 4000;
		rssajax.current = 0;
		rssajax.scrollit = new Fx.Scroll('chan_items', {
			wait: false,
			duration: 1000,
			offset: {'x': 0, 'y': 0}
		});
		rssajax.getRSS(url);
		rssajax.timer=setTimeout('rssajax.latesnext();',rssajax.duration);
		
	},
	latesnext:function(){	
		if((rssajax.current+1)>=rssajax.nbrnws){
			rssajax.current=0;
		} else {
			rssajax.current=(rssajax.current+1);
		}
		rssajax.scrollit.toElement('item'+rssajax.current);
		rssajax.timer=setTimeout('rssajax.latesnext();',rssajax.duration);
	},
	RSS2Enclosure: function(encElement){
		if (encElement == null){
			this.url = null;
			this.length = null;
			this.type = null;
		} else {
			this.url = encElement.getAttribute("url");
			this.length = encElement.getAttribute("length");
			this.type = encElement.getAttribute("type");
		}
	},
	RSS2Guid: function(guidElement){
		if (guidElement == null){
			this.isPermaLink = null;
			this.value = null;
		} else {
			this.isPermaLink = guidElement.getAttribute("isPermaLink");
			this.value = guidElement.childNodes[0].nodeValue;
		}
	},
	RSS2Source: function(souElement){
		if (souElement == null){
			this.url = null;
			this.value = null;
		} else {
			this.url = souElement.getAttribute("url");
			this.value = souElement.childNodes[0].nodeValue;
		}
	},

	//object containing the RSS 2.0 item
	RSS2Item: function(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++){
			tmpElement = itemxml.getElementsByTagName(properties[i])[0];
			if (tmpElement != null)
				eval("this."+properties[i]+"=tmpElement.childNodes[0].nodeValue");
		}

		this.category = new rssajax.RSS2Category(itemxml.getElementsByTagName("category")[0]);
		this.enclosure = new rssajax.RSS2Enclosure(itemxml.getElementsByTagName("enclosure")[0]);
		this.guid = new rssajax.RSS2Guid(itemxml.getElementsByTagName("guid")[0]);
		this.source = new rssajax.RSS2Source(itemxml.getElementsByTagName("source")[0]);
	},

	//objects inside the RSS2Channel object
	RSS2Category: function(catElement){
		if (catElement == null){
			this.domain = null;
			this.value = null;
		} else {
			this.domain = catElement.getAttribute("domain");
			this.value = catElement.childNodes[0].nodeValue;
		}
	},

	//object containing RSS image tag info
	RSS2Image: function(imgElement){
		if (imgElement == null){
			this.url = null;
			this.link = null;
			this.width = null;
			this.height = null;
			this.description = null;
		} else {
			imgAttribs = new Array("url","title","link","width","height","description");
			for (var i=0; i<imgAttribs.length; i++)
				if (imgElement.getAttribute(imgAttribs[i]) != null)
					eval("this."+imgAttribs[i]+"=imgElement.getAttribute("+imgAttribs[i]+")");
		}
	},

	//object containing the parsed RSS 2.0 channel
	RSS2Channel: function(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 rssajax.RSS2Item(itemElements[i]);
			this.items.push(Item);
			//chanElement.removeChild(itemElements[i]);
		}

		var properties = new Array("title", "link", "description", "language", "copyright", "managingEditor", "webMaster", "pubDate", "lastBuildDate", "generator", "docs", "ttl", "rating");
		var tmpElement = null;
		for (var i=0; i<properties.length; i++){
			tmpElement = chanElement.getElementsByTagName(properties[i])[0];
			if (tmpElement!= null)
				eval("this."+properties[i]+"=tmpElement.childNodes[0].nodeValue");
		}

		this.category = new rssajax.RSS2Category(chanElement.getElementsByTagName("category")[0]);
		this.image = new rssajax.RSS2Image(chanElement.getElementsByTagName("image")[0]);
	},

	//PROCESSES

	//uses xmlhttpreq to get the raw rss xml
	getRSS: function(url){
		//call the right constructor for the browser being used
		if (window.ActiveXObject)
			 rssajax.xhr = new ActiveXObject("Microsoft.XMLHTTP");
		else if (window.XMLHttpRequest)
			rssajax.xhr = new XMLHttpRequest();
		else
			alert("not supported");

		//prepare the xmlhttprequest object
		rssajax.xhr.open("GET",url,true);
		rssajax.xhr.setRequestHeader("Cache-Control", "no-cache");
		rssajax.xhr.setRequestHeader("Pragma", "no-cache");
		rssajax.xhr.onreadystatechange = function() {
			if (rssajax.xhr.readyState == 4){
				if (rssajax.xhr.status == 200){
					if (rssajax.xhr.responseText != null)
						rssajax.processRSS(rssajax.xhr.responseXML);
					else {
						alert("Failed to receive RSS file from the server - file not found.");
						return false;
					}
				} else
					alert("Error code " + rssajax.xhr.status + " received: " + rssajax.xhr.statusText);
			}
		}

		//send the request
		rssajax.xhr.send(null);
	},

	//processes the received rss xml
	processRSS: function(rssxml){
		RSS = new rssajax.RSS2Channel(rssxml);
		rssajax.showRSS(RSS);
	},

	//shows the RSS content in the browser
	showRSS: function(RSS){
		//default values for html tags used
		var imageTag = "<img id='chan_image'";
		var startTitle = "<div id='item_title'>";
		var startDescription = "<div id='item_description'>";
		var endTag = "</div>";

		//populate channel data
		var properties = new Array("title","link","description","pubDate","copyright");
		for (var i=0; i<properties.length; i++){
			if(rssajax.showchannel == true){
				eval("document.getElementById('chan_"+properties[i]+"').innerHTML = ''");
				curProp = eval("RSS."+properties[i]);
				if (curProp != null)
					eval("document.getElementById('chan_"+properties[i]+"').innerHTML = curProp");
			}
		}

		//show the image
		if (RSS.image.src != null && rssajax.showimage == true){
			document.getElementById("chan_image_link").innerHTML = "";
			document.getElementById("chan_image_link").href = RSS.image.link;
			document.getElementById("chan_image_link").innerHTML = imageTag
				+" alt='"+RSS.image.description
				+"' width='"+RSS.image.width
				+"' height='"+RSS.image.height
				+"' src='"+RSS.image.url
				+"' "+"/>";
		}

		//populate the items
		document.getElementById("chan_items").innerHTML = "";
		rssajax.nbrnws=0;
		for (var i=0; i<RSS.items.length; i++){
			item_html = "<div id='item"+i+"' class='item'>";
			item_html += startTitle;
			item_html += (RSS.items[i].link == null) ? "" : "<a href='" + RSS.items[i].link + "'>";
			item_html += RSS.items[i].title;
			item_html += (RSS.items[i].link == null) ? "" : "</a>";
			item_html += endTag;
			item_html += (RSS.items[i].description == null) ? "" : startDescription + RSS.items[i].description + endTag;
			item_html += endTag;
			document.getElementById("chan_items").innerHTML += item_html;
			rssajax.nbrnws+=1;
		}

		//we're done
		//document.getElementById("chan").style.visibility = "visible";
		return true;
	}
}