
//objects inside the fnRSSItem object
function fnRSSObject(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 fnRSSPermaLink(guidElement)
{
	if (guidElement == null)
	{
		this.isPermaLink = null;
		this.value = null;
	}
	else
	{
		this.isPermaLink = guidElement.getAttribute("isPermaLink");
		this.value = guidElement.childNodes[0].nodeValue;
	}
}

function fnRSSUrl(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 fnRSSItem(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;
	this.image;

	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 fnRSSDomain(itemxml.getElementsByTagName("category")[0]);
	this.image = new fnRSSImage(itemxml.getElementsByTagName("image")[0]);
	this.enclosure = new fnRSSObject(itemxml.getElementsByTagName("enclosure")[0]);
	this.guid = new fnRSSPermaLink(itemxml.getElementsByTagName("guid")[0]);
	this.source = new fnRSSUrl(itemxml.getElementsByTagName("source")[0]);
}

//objects inside the fnRSSChannel object
function fnRSSDomain(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 fnRSSImage(imgElement)
{
	if (imgElement == null)
	{
	this.url = null;
	this.title = 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++){
				
  		  tmpElement = imgElement.getElementsByTagName(imgAttribs[i])[0];
		  if (tmpElement!= null)
			eval("this."+imgAttribs[i]+"=tmpElement.childNodes[0].nodeValue");
				
		  }
	}
}

//object containing the parsed RSS 2.0 channel
function fnRSSChannel(rssxml)
{
	//required
	this.title;
	this.link;
	this.description;

	//array of fnRSSItem objects
	this.items = new Array();

	//vars
	this.language;
	this.copyright;
	this.managingEditor;
	this.webMaster;
	this.pubDate;
	this.lastBuildDate;
	this.generator;
	this.docs;
	this.ttl;
	this.rating;

	//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 fnRSSItem(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.image = new fnRSSImage(chanElement.getElementsByTagName("image")[0]);
	this.category = new fnRSSDomain(chanElement.getElementsByTagName("category")[0]);
}

//PROCESSES


//uses xmlhttpreq to get the raw rss xml
function fnPullRSS(val)
{
	
	//call optional arguments
	var count = (arguments[1]) ? arguments[1] : 100 ;
	var format = (arguments[2]) ? arguments[2] : item_template;
	var divid = (arguments[3]) ? arguments[3] : posting_collection;
	
	var obj;
	//call the right constructor for the browser being used
	if (window.ActiveXObject)
		obj = new ActiveXObject("Microsoft.XMLHTTP");
	else if (window.XMLHttpRequest)
		obj = new XMLHttpRequest();
	else
		alert("not supported");

	//prepare the xmlhttprequest object
	obj.open("GET",val,true);
	obj.setRequestHeader("Cache-Control", "no-cache");
	obj.setRequestHeader("Pragma", "no-cache");
	obj.onreadystatechange = function() {
		if (obj.readyState == 4)
		{
			if (obj.status == 200)
			{
				if (obj.responseText != null)
					fnInstantiateRSS(obj.responseXML,count,format, divid);
				else
				{
					alert("Failed to receive RSS file from the server - file not found.");
					return false;
				}
			}
			else
				alert("Error code " + obj.status + " received: " + obj.statusText);
		}
	}

	//send the request
	obj.send(null);
}

	
//processes the received rss xml
function fnInstantiateRSS(rssxml,count,format, divid)
{
	RSS = new fnRSSChannel(rssxml);
	fnDisplayRSS(RSS,count,format, divid);
}

//shows the RSS content in the browser
function fnDisplayRSS(RSS,count,itemFormat, divid)
{

	//populate channel data
	var properties = new Array("title","link","description","pubDate","copyright");
	for (var i=0; i<properties.length; i++)
	{
	   if (document.getElementById('channel_"+properties[i]+"') != null)
	     {
		eval("document.getElementById('channel_"+properties[i]+"').innerHTML = ''");
		curProp = eval("RSS."+properties[i]);
		if (curProp != null)
			eval("document.getElementById('channel_"+properties[i]+"').innerHTML = curProp");
	     }
	}

	//show the image
        if (document.getElementById("channel_image_link") != null)
	{
  	  document.getElementById("channel_image_link").innerHTML = "";
  	  if (RSS.image.src == null)
	    {
		document.getElementById("channel_image_link").href = RSS.image.link;
		document.getElementById("channel_image_link").innerHTML = "<img id='channel_image'"
			+" alt='"+RSS.image.description
			+"' border='0'"
			+"' src='"+RSS.image.url
			+"' "+"/>";
             }
	}

	eval("document.getElementById('"+divid+"').innerHTML = ''");
	

	// check count of items if xml has fewer items then requested number by user use smaller amount
	if (count > RSS.items.length) {
		count = RSS.items.length;
	}


// get the full string value

	for (var i=0; i<count; i++)
	{
	
		
		item_html = itemFormat
	if (RSS.items[i].image.url == null) {
		item_html = item_html.replace(/%p/g,"")
		//image link
		item_html = item_html.replace(/%i/g,"")
		//image title
		item_html = item_html.replace(/%z/g,"")
		
	} else {
		item_html = item_html.replace(/%p/g,"<img src='%i' alt='%z' style='padding:5px;' width='85px' align='left'>")
		//image link
		item_html = item_html.replace(/%i/g,RSS.items[i].image.url)
		//image title
		item_html = item_html.replace(/%z/g,RSS.items[i].image.title)
	}
		
			
		item_html = item_html.replace(/%t/g,RSS.items[i].title)
		item_html = item_html.replace(/%d/g,RSS.items[i].description)
		item_html = item_html.replace(/%l/g,RSS.items[i].link)
		item_html = item_html.replace(/%a/g,RSS.items[i].author)
		item_html = item_html.replace(/%c/g,RSS.items[i].comments)
		item_html = item_html.replace(/%p/g,RSS.items[i].pubDate)

		item_html = item_html.replace(/%m/g,RSS.items[i].category.value)
		
			
		item_html = item_html.replace(/%y/g,"")
		item_html = item_html.replace(/%e/g,RSS.items[i].enclosure.url)
		item_html = item_html.replace(/%g/g,RSS.items[i].guid)
		item_html = item_html.replace(/%s/g,RSS.items[i].source)

		

		
			eval("document.getElementById('"+divid+"').innerHTML += item_html");
		
	}


		
		
	return true;
}


//XMLHTTPOBJECT HANDLER



//DEFAULT Item Template - If nothing is provided by User
var item_template = "<div id='posting'>" +
			"<div id='posting_title'>%t</div>" +
		     	"<a id='posting_link' href='%l'>%l</a>" +
		     	"<div id='posting_description'>%d</div><br>" +
		    "</div>"

