
//OBJECTS

//objects inside the RSS2Item object
function RSS2Enclosure(encElement)
{
	if (encElement == null)
	{
		this.url = null;
		this.length = null;
		this.type = null;
	}
	else
	{
		this.url = encElement.getAttribute("feedurl");
		this.length = encElement.getAttribute("feedlength");
		this.type = encElement.getAttribute("feedtype");
	}
}

function RSS2Guid(guidElement)
{
	if (guidElement == null)
	{
		this.isPermaLink = null;
		this.value = null;
	}
	else
	{
		this.isPermaLink = guidElement.getAttribute("feedisPermaLink");
		this.value = guidElement.childNodes[0].nodeValue;
	}
}

function RSS2Source(souElement)
{
	if (souElement == null)
	{
		this.url = null;
		this.value = null;
	}
	else
	{
		this.url = souElement.getAttribute("feedurl");
		this.value = souElement.childNodes[0].nodeValue;
	}
}

function var_dump (variable) {
	var str = "";
	if (typeof (variable) == "array" || typeof(variable) == "object") {
		var first = true;
		for (i in variable) {
			if (!first) {
				str += ", ";
			} else {
				first = false;
			}
			str += var_dump (i) + ": " + var_dump (variable[i]);
		}
	} else if (typeof (variable) == "function") {
		str = typeof (variable) + ": ";
		str += var_dump (variable());
	} else {
		str = typeof(variable) + ": " + variable;
	}
	return str;
}

//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++)
	{
		tmpElement = itemxml.getElementsByTagName("feed" + properties[i])[0];
		if (tmpElement != null) {
			//eval("this."+properties[i]+"=tmpElement.childNodes[0].nodeValue");
			this[properties[i]]=tmpElement.childNodes[0].nodeValue;
		}
	}

	this.category = new RSS2Category(itemxml.getElementsByTagName("feedcategory")[0]);
	this.enclosure = new RSS2Enclosure(itemxml.getElementsByTagName("feedenclosure")[0]);
	this.guid = new RSS2Guid(itemxml.getElementsByTagName("feedguid")[0]);
	this.source = new RSS2Source(itemxml.getElementsByTagName("feedsource")[0]);
}

//objects inside the RSS2Channel object
function RSS2Category(catElement)
{
	if (catElement == null)
	{
		this.domain = null;
		this.value = null;
	}
	else
	{
		this.domain = catElement.getAttribute("feeddomain");
		this.value = catElement.childNodes[0].nodeValue;
	}
}

//object containing RSS image tag info
function RSS2Image(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(feed"+imgAttribs[i]+")");
				this[imgAttribs[i]] = imgElement.getAttributes(feed + imgAttributes[i]);
			}
	}
}

//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("feedchannel")[0];
	var itemElements = rssxml.getElementsByTagName("feeditem");

	for (var i=0; i<itemElements.length; i++)
	{
		Item = new 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("feed" + properties[i])[0];
		if (tmpElement!= null) {
			//eval("this."+properties[i]+"=tmpElement.childNodes[0].nodeValue");
			this[properties[i]] = tmpElement.childNodes[0].nodeValue;
		}
	}

	this.category = new RSS2Category(chanElement.getElementsByTagName("feedcategory")[0]);
	this.image = new RSS2Image(chanElement.getElementsByTagName("feedimage")[0]);
}

//PROCESSES

//uses xmlhttpreq to get the raw rss xml
function getRSS(feedUrl)
{
	//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;

	//prepare the xmlhttprequest object
	xhr.open("GET",feedUrl,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);
					return true;
				}
				else
				{
					alert("Failed to receive RSS file from the server - file not found.");
					return false;
				}
			}
			else
				alert("Error code " + xhr.status + " received: " + xhr.statusText);
		}
	}

	//send the request
	xhr.send(null);
}

//processes the received rss xml
function processRSS(rssxml)
{
	var RSS = new RSS2Channel(rssxml);
	showRSS(RSS);
}

//shows the RSS content in the browser
function showRSS(RSS)
{
	//default values for html tags used
	var startItemTag = "<span id='item'>";
	var startTitle = "<span id='item_title'>";
	var startLink = "<span id='item_link'>";
	var endTag = "</span>";

	//populate channel data
	var properties = new Array(/*"title","link", "description","pubDate","copyright"*/);
	for (var i=0; i<properties.length; i++)
	{
		document.getElementById("chan_" + properties[i]).innerHTML = "";
		//curProp = eval("RSS."+properties[i]);
		curProp = RSS[properties[i]];
		field = document.getElementById("chan_" + properties[i]);
		if (curProp != null) {
			//eval("document.getElementById('chan_"+properties[i]+"').innerHTML = curProp");
			if (properties[i] == "title") {
				link = RSS[properties[i+1]];
				if (link) {
					field.innerHTML = "<a href=\"" + link + "\">" + curProp + "</a>";
				} else {
					field.innerHTML = curProp;
				}
			} else if (properties[i] == "link") {
				/* no operation, see title special case */
			} else {
				field.innerHTML = curProp;
			}
		}
	}

	//show the image
	if (false) { 
document.getElementById("chan_image_link").innerHTML = "";
	if (RSS.image.src != null)
	{
		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 = "";
	var intUBound = RSS.items.length;
	for (var i=0; i< intUBound; i++)
	{
		var item_html = "<li>" + startItemTag;
		var has_title = RSS.items[i].title != null;
		var has_link = RSS.items[i].link != null;
		
		if (has_title) {
			item_html += startTitle;
			if (has_link) {
				strDate = RSS.items[i].pubDate;
				strDate = strDate.replace(",", "");
				strDate = strDate.replace("  ", " ");
				idate = strDate.split(" ");
				strDate = idate[2] + " " + idate[1] + ", " + idate[3];
				item_html += "<a href=\"" + RSS.items[i].link + "\">" + RSS.items[i].title + "</a> " + strDate;
			} else {
				item_html += RSS.items[i].title;
			}
			item_html += endTag;
		} else {
			if (has_link) {
				item_html + "<a href=\"" + RSS.items[i].link + "\">" + RSS.items[i].link + "</a>";
			}
		}
		//item_html += (RSS.items[i].description == null) ? "" : startDescription + RSS.items[i].description + endTag;
		item_html += endTag + "</li>";
		document.getElementById("chan_items").innerHTML += item_html;
	}

	//document.getElementById("chan_items").innerHTML = "<ul class=\"links\">" + document.getElementById("chan_items").innerHTML + "</ul>";
	//we're done
	document.getElementById("chan_items").style.visibility = "visible";
	return true;
}

var xhr;




