
//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", "dc:creator",  "dc:publisher");
	var tmpElement = null;
	for (var i=0; i<properties.length; i++)
	{
		
		/*
		tmpElement = itemxml.getElementsByTagName(nm)[0];
		if (tmpElement != null)
		{
			this[nm]=tmpElement.childNodes[0].nodeValue;
		}
		*/
		
		var val = "";
		var nm = properties[i];
		tmpElement = itemxml.getElementsByTagName(nm)[0];
		if (tmpElement != null )
		{
			// handle case where empty.
			try{
				val = tmpElement.childNodes[0].nodeValue;
			}
			catch(e)
				{
					val = "";
				}
		//	alert("val=" + val);
			this[nm]=val;
		}
		
		
		
		
	}
	

	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++)
	{
		var nm = properties[i];
		
		var val = "";
		tmpElement = chanElement.getElementsByTagName(nm)[0];
		if (tmpElement != null )
		{
			// handle case where empty.
			try{
				val = tmpElement.childNodes[0].nodeValue;
			}
			catch(e)
				{
					val = "";
				}
		//	alert("val=" + val);
			this[nm]=val;
		}

	}

	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
var arrReq = new Array();
//var xmlReq = null;
function getRSS(url, templ, targ, guid)
{

	
var xmlReq = arrReq[targ];
// Create the XML request  
	if ( !xmlReq )
	{
		if(window.XMLHttpRequest)
			xmlReq = new XMLHttpRequest();
		else if(window.ActiveXObject)
			xmlReq = new ActiveXObject("Microsoft.XMLHTTP");
		if(xmlReq==null) return; // Failed to create the request
	}

	xmlReq.rssTempl = templ;
	xmlReq.rssTarget = targ;
	xmlReq.rssGuid = guid;

// Anonymous function to handle changed request states
	xmlReq.onreadystatechange = function()
	{
		switch(xmlReq.readyState)
		{
		case 0:	// Uninitialized
			break;
		case 1: // Loading
			break;
		case 2: // Loaded
			break;
		case 3: // Interactive
			break;
		case 4:	// Done!
	
		if (xmlReq.status == 200)
			{
				if (xmlReq.responseText != null)
				{
					//alert("xmlReq.responseText=" + xmlReq.responseText );
					//alert("2xmlReq.responseXML=" + xmlReq.responseXML );
					if ( xmlReq.rssGuid )
						processRSSStory(xmlReq.responseXML, xmlReq.rssTempl, xmlReq.rssTarget, xmlReq.rssGuid );
					else
						processRSS(xmlReq.responseXML, xmlReq.rssTempl, xmlReq.rssTarget);
				}
				else
				{
					alert("Failed to receive RSS file from the server - file not found.");
					return false;
				}
			}
			else
				alert("Error code " + xmlReq.status + " received: " + xmlReq.statusText);
		
			
			break;
		default:
			break;
		}
	}

// Make the request
	xmlReq.open ('GET', url, true);

	xmlReq.send (null);

arrReq[targ] = xmlReq;
	
}

//processes the received rss xml
function processRSS(rssxml, templ, targ)
{

	RSS = new RSS2Channel(rssxml);

	showRSS(RSS, templ, targ);
}

//processes the received rss xml
function processRSSStory(rssxml, templ, targ, guid)
{

	RSS = new RSS2Channel(rssxml);


	RSSItem = findItemByGuid(guid);
	showRSSItem(RSSItem, templ, targ);
}

//shows the RSS content in the browser
var SHORT_COUNT = 4;
function showRSS(RSS, templ, targ)
{

	var ts = $(templ).innerHTML;
	
	var arrItems = RSS.items;
	var ilen = arrItems.length;
	ilen = SHORT_COUNT < ilen ? SHORT_COUNT : ilen;

	var strHtml = "";

	var strTemp = "";
	
	for (var i = 0; i < ilen; i++)
	{
	
	
	
		var story = "";
		var dt = arrItems[i].pubDate ? new Date(arrItems[i].pubDate) : new Date();
		var strDate = "" + dt.format('mmmm dS, yyyy');
			var strC = arrItems[i]["author"];
		if ( !strC ) strC = arrItems[i]["dc:publisher"];
		if ( !strC ) strC = arrItems[i]["dc:creator"];
		if ( !strC ) strC = "";
		strTemp = replaceItemInTemplate(ts,arrItems[i].title, arrItems[i].link, strDate, strC, story)
	
	
		// accumulate fully formulated string
		strHtml += strTemp;
		
		
	}

	$(targ).innerHTML = strHtml;

	//we're done
	//document.getElementById("chan").style.visibility = "visible";
	return true;
}


function replaceItemInTemplate(ts, title, link, strDate, source, story)
{
	// title
		var strTemp = ts.replace(/rsstagTITLErsstag/gi, title );

		// pubdate
		
		strTemp = strTemp.replace(/rsstagPUBDATErsstag/gi, strDate );
			
		//link
		strTemp = strTemp.replace(/rsstagLINKrsstag/gi, link );
	
		// source
		strTemp = strTemp.replace(/rsstagSOURCErsstag/gi, source );
	
		
		return strTemp;
}


function showRSSItem(RSSItem, templ, targ)
{
	alert("sRRS");
}
