
//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("url");
		this.length = encElement.getAttribute("length");
		this.type = encElement.getAttribute("type");
	}
}

function RSS2Guid(guidElement)
{
	if (guidElement == null)
	{
		this.isPermaLink = null;
		this.value = null;
	}
	else
	{
		this.isPermaLink = guidElement.getAttribute("isPermaLink");
		this.value = guidElement.childNodes[0].nodeValue;
	}
}

function RSS2Source(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
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(properties[i])[0];
		if (tmpElement != null)
			eval("this."+properties[i]+"=tmpElement.childNodes[0].nodeValue");
	}

	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 (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
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("+imgAttribs[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("channel")[0];
		

	if( chanElement == null ){ // Modified
		return;
	}
	var itemElements = rssxml.getElementsByTagName("item");

	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 j=0; j<properties.length; j++)
	{
			tmpElement = chanElement.getElementsByTagName(properties[j])[0];		
			if (tmpElement!= null && tmpElement.childNodes[0] != null)
			{
				eval("this."+properties[i]+"=tmpElement.childNodes[0].nodeValue");
			}
	}

	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, el, lim)
{
	var limit = 5;
	//call the right constructor for the browser being used
	if( lim != null ){
		limit = lim;
	}
	
	try{
		netscape.security.PrivilegeManager.enablePrivilege("UniversalBrowserRead");
	} catch (e) {
		// DO NOTHING
	}		
	
	var xhr = null;
	if (window.XMLHttpRequest) {
	  xhr = new XMLHttpRequest();
	  if ( typeof xhr.overrideMimeType != 'undefined') {
		xhr.overrideMimeType('text/xml');
	  }
	} else if (window.ActiveXObject) {
	  xhr = new ActiveXObject("Microsoft.XMLHTTP");
	} else {
	  alert('Perhaps your browser does not support xmlhttprequests?');
	}
	


	//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, el, limit);
				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, el, limit)
{
	RSS = new RSS2Channel(rssxml);
	showRSS(RSS, el, limit);
}

//shows the RSS content in the browser
function showRSS(RSS, el, limit)
{
	//default values for html tags used
	var imageTag = "<img id='chan_image'";
	var startItemTag = "<div id='item'>";
	var startTitle = "<span id='item_title'>";
	var endSpan = "</span>";
	var startLink = "<a href='";
	var endStartLink = "'>";
	var endLink = "</a>";
	var startDescription = "<div id='item_description'>";
	var endTag = "</div>";

	/*//populate channel data
	var properties = new Array("title","link");
	for (var i=0; i<properties.length; i++)
	{
		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
	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(el).innerHTML = "";
	for (var i=0; i< limit; i++)
	{
		if( RSS.items[i] === undefined ){
			break;
		}
		item_html = startItemTag;
		item_html += (RSS.items[i].link == null) ? "" : startLink + RSS.items[i].link + endStartLink;
		item_html += (RSS.items[i].title == null) ? "" : startTitle + "<b>" + RSS.items[i].title + "</b>" + endSpan;
		item_html += endLink;
		
		if(el.indexOf("calendar") != -1){
			var url = RSS.items[i].link;
			var index = ( url.search(/date=/) ) + 5;
			var dateString = url.substr(index, 8);
			
			var year = dateString.substr(0, 4); //parseInt(dateString.substr(0, 4));
			var month = dateString.substr(4, 2); //parseInt(dateString.substr(4, 2)) - 1; // subtract by one so that it works with the date object
			month -= 1;
			var day = dateString.substr(6, 2);//parseInt(dateString.substr(6, 2));
			
			var newDate = new Date();
			newDate.setUTCFullYear(year);
			newDate.setUTCMonth(month, day);
			
			date = newDate.toUTCString();
			date = date.substr(0, 16);
			
			item_html += startDescription + date + endTag;
			//item_html += dateString.substr(0, 4) + " " + dateString.substr(4, 2) + " " + dateString.substr(6, 2);
		}else{
			var date = RSS.items[i].pubDate.substring(0, 16);
			item_html += (RSS.items[i].pubDate == null) ? "" : startDescription + date + endTag;
		}
		
		item_html += endTag;
		document.getElementById(el).innerHTML += item_html;
	}
	
	//we're done
	//document.getElementById("chan").style.visibility = "visible";
	return true;
}

var xhr;


