//OBJECTS

var xhr = new Array();

//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 ( 'undefined' != typeof tmpElement && tmpElement != null && tmpElement.childNodes.length > 0 )
			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];
	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 i=0; i<properties.length; i++)
	{
		tmpElement = chanElement.getElementsByTagName(properties[i])[0];
		if ( typeof tmpElement != 'undefined' && tmpElement != null && tmpElement.childNodes.length > 0 )
			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

function getAllRSS() {
	var divs = document.getElementsByTagName('DIV');
	var channels = new Array;
	for( i=0; i<divs.length; i++ ) {
		if( elementHasClassName(divs[i],'rss') ) {
			channels[channels.length] = divs[i];
		}
	}
	for( i=0; i<channels.length; i++ ) {
		var feedURL =  channels[i].getElementsByTagName('A')[0].getAttribute('href');
		getRSS(feedURL,channels[i]);
//		break; // un-comment this to load the first (Instructify) feed!
	}
}

//uses xmlhttpreq to get the raw rss xml
function getRSS(feedURL,node)
{
	//call the right constructor for the browser being used
	if (window.ActiveXObject) {
		xhr[xhr.length] = new ActiveXObject("Microsoft.XMLHTTP");
		var k = xhr.length-1; // the current xhr
	}
	else if (window.XMLHttpRequest) {
		xhr[xhr.length] = new XMLHttpRequest();
		var k = xhr.length-1;
	}
	else
		alert("not supported");

	// Dummy variable append to URL to avoid caching
	var now = new Date();
	var timestamp = now.getTime();
	
	//prepare the xmlhttprequest object
	xhr[k].open("GET",'/feeds/proxy.php?feed=' + feedURL + '&time=' + timestamp,true);
	xhr[k].setRequestHeader("Cache-Control", "no-cache");
	xhr[k].setRequestHeader("Pragma", "no-cache");
	xhr[k].onreadystatechange = function() {
		if (xhr[k].readyState == 4)
		{
			if (xhr[k].status == 200)
			{
				if (xhr[k].responseText != null)
					processRSS(xhr[k].responseXML,node);
				else
				{
					alert("Failed to receive RSS file from the server - file not found.");
					return false;
				}
			}
			else
				alert("Error code " + xhr[k].status + " received: " + xhr[k].statusText);
		}
	}

	//send the request
	xhr[k].send(null);
}

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

//shows the RSS content in the browser
function showRSS(RSS,node)
{
	//default values for html tags used
	var imageTag = "<img id='chan_image'";

	// Identify the channel DIV and clean it out
	var channel = node;
	while( channel.childNodes.length > 0 ) {
		channel.removeChild(channel.firstChild);
	}
	
	// maximum number of items to display
	var maxItems = findNumericClassName(channel);
	if( maxItems === null ) maxItems = 100;

	// Get class name for channel node, to determine formatting
	if( elementHasClassName(channel,'full') ) {

		//populate channel data
		var chanTitle = document.createElement('H3');
		chanTitle.innerHTML = '<a href="' + RSS.link + '"><img class="icon" src="/lp/library/images/feed-icon-16x16.png" alt="RSS"></a> ';
		chanTitle.innerHTML += '<a href="' + RSS.link + '" title="read the blog">' + RSS.title + '</a>';
//		channel.appendChild(chanTitle);
		
		var chanDesc = document.createElement('P');
		chanDesc.innerHTML = RSS.description;
		channel.appendChild(chanDesc);
		
		//populate the items
		var chanItems = document.createElement('DL');
		
		// clear old items 
		while( chanItems.childNodes.length > 0 ) {
			chanItems.removeChild(chanItems.firstChild);
		}
		
		for (var i=0; i<RSS.items.length; i++)
		{
			var newTitle = document.createElement('DT');
			var newLink = document.createElement('A');
			newLink.setAttribute('href',RSS.items[i].link);
			newLink.innerHTML = RSS.items[i].title;
			newTitle.appendChild(newLink);
			var newDescription = document.createElement('DD');
			newDescription.innerHTML = RSS.items[i].description;
			
			chanItems.appendChild(newTitle);
			chanItems.appendChild(newDescription);
		
			if( i==maxItems-1 ) break;
		}
		
		channel.appendChild(chanItems);
		

	}
	else if( elementHasClassName(channel,'titles') ) {

		//populate channel data
		
		//populate the items
		var chanItems = document.createElement('UL');
		
		// clear old items 
		while( chanItems.childNodes.length > 0 ) {
			chanItems.removeChild(chanItems.firstChild);
		}
		
		for (var i=0; i<RSS.items.length; i++)
		{
			var newTitle = document.createElement('LI');
			var newLink = document.createElement('A');
			newLink.setAttribute('href',RSS.items[i].link);
			newLink.innerHTML = RSS.items[i].title;
			newTitle.appendChild(newLink);
			
			chanItems.appendChild(newTitle);
			
			if( i==maxItems-1 ) break;
		}
		
		channel.appendChild(chanItems);
	}

	//we're done
	return true;
}



