/*	Simple Javascript RSS Reader Version 1.0
	Copyright (c) 2006 CS Truter
	Written by Christoff Truter
	email: Christoff@cstruter.com - (Please let me know if you intend to use the script) */

/* Replace all occurances of a string
  (Parameters) totalValue:'complete string'
		oldValue:'value to be replaced' newValue:'value used for replace' */

/* AutoDB: Fixed ReadRSS so it works in Chrome, uses XMLHttpRequest instead of
   document.implementation */

function Replace(totalValue,oldValue,newValue)
{
	while(totalValue.indexOf(oldValue) > -1) totalValue=totalValue.replace(oldValue,newValue);
	return totalValue;
}

/* Get XML Node
   (Parameters) TagName:'XML Element' node:'Element row number' */

function getNode(TagName, node) {
	var currentNode = (node == null) ? xmlDoc.getElementsByTagName(TagName) : items[node].getElementsByTagName(TagName);
	if(currentNode.length > 0)
	{
		if( currentNode[0].firstChild )
		{
			return currentNode[0].firstChild.nodeValue;
		}
		else
		{
			return('');
		}
	}
}

function getNodeAttr(TagName, node, attrname) {
  var currentNode = (node == null) ? xmlDoc.getElementsByTagName(TagName) : items[node].getElementsByTagName(TagName);

	if(currentNode.length > 0)
	{
		return currentNode[0].getAttribute(attrname);
	}
}


/* Load XML Object
   (Parameters) rssFeed:'RSS File' Body:'Layer for RSS Body' Title:'Layer for RSS Title' */

function ReadRSS(rssFeed, Body, Title,callback)
{
	rssTitle = document.getElementById(Title);
	rssBody = document.getElementById(Body);

	try
	{
		var xmlDoc;

		if(window.XMLHttpRequest)
		{
			xmlDoc = new window.XMLHttpRequest();

			xmlDoc.open("GET", rssFeed, false);
			xmlDoc.send("");
			xmlDoc = xmlDoc.responseXML;
		}
		else if(window.ActiveXObject)
		{
			xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
			xmlDoc.async = false;
			xmlDoc.load(rssFeed);
		}
		else
		{
			throw Error("XML loading not supported.");
		}

		items=xmlDoc.getElementsByTagName('item');
		SetRSSTemplates();
	}
	catch(e)
	{
		rssBody.innerHTML = "";
		//console.log(e);
	}

  callback = callback || function() {}

  callback();
}

/* Set HTML Template
	Did it this way to make the look and feel of the feed easy customizable, dont like mixing
	layout with code. */

function SetRSSTemplates() {
	if (rssBody) {
		var buffer = "";
    var maxitems = items.length;
    if (maxitems > 6) {
      maxitems = 6;
    }
		for(var i=0; i< maxitems; i++) {
			var output = (document.all) ? Replace(rssBody.innerHTML,"(::Link::)", getNode('link',i)) : Replace(rssBody.innerHTML,"%28::Link::%29", getNode('link',i));
			output = Replace(output,"(::Title::)", getNode('title',i));
			output = Replace(output,"(::Pubdate::)", getNode('pubDate',i));
			output = Replace(output,"(::Description::)", getNode('description',i));
      output = Replace(output,"(::Image::)", getNodeAttr('enclosure',i, "url"));
      output = Replace(output,"%28::Image::%29", getNodeAttr('enclosure',i, "url"));
			buffer+=output;
		}
		rssBody.innerHTML = buffer;
	}

	if (rssTitle) {
		var output = Replace(rssTitle.innerHTML,"(::Title::)", getNode('title'));
		output = (document.all) ? Replace(output,"(::Link::)", getNode('link')) : Replace(output,"%28::Link::%29", getNode('link'));
		output = Replace(output,"(::Description::)", getNode('description'));
		rssTitle.innerHTML = output;
	}
}

