/********************************************************************
 * RSS Viewer - ECMAScript Presentation Library                     *
 * © 2005 Justin Haygood <justin@xiondigital.net>                   *
 *                                                                  *
 * You may use this library in any way you see fit, and can         *
 * redistribute and reuse it an unlimited amount of times, but the  * 
 * above copyright notice must stay intact.                         *     
 *                                                                  *
 * Last Update: August 16, 2005                                     *
 ********************************************************************/

var request;
var bodydiv;
var loadingp;
var loadingpmade = false;

function InitRSS(){
	
	bodydiv = "newsbody";
	LoadXML("http://evolution-win32.sourceforge.net/common/rssget.php?filetype=xml");
	
}

function ProcessRSSFeed(){
	
	document.getElementById(bodydiv).removeChild(loadingp);
	
	var RSSFeed = request.responseXML;
	
	var RSSItems = RSSFeed.getElementsByTagName("item");
	
	// Loop through items
	
/*	for(var RSSItemNo in RSSItems){ Foreach in JavaScript, w00t */
	for(var RSSItemNo = 0; RSSItemNo < RSSItems.length; RSSItemNo++){
	
		var RSSItem = RSSItems[RSSItemNo];
		
		var title = document.createElement("h2");
		title.className = "news";
		var titletext = document.createTextNode(RSSItem.getElementsByTagName("title")[0].firstChild.data);
		title.appendChild(titletext)
		document.getElementById(bodydiv).appendChild(title);
		
		var ul = document.createElement("ul");
		var li = document.createElement("li");
		var p = document.createElement("p");
		p.innerHTML = RSSItem.getElementsByTagName("description")[0].firstChild.data; // innerHTML is supported widely enough to use it.
		li.appendChild(p);
		ul.appendChild(li);
		document.getElementById(bodydiv).appendChild(ul);
		
		
	}
}

function DisplayLoading(){
	
	if (loadingpmade == false){
	
	var loadingmessage = document.createTextNode("Loading...");
	loadingp = document.createElement("p");
	loadingp.appendChild(loadingmessage);
	document.getElementById(bodydiv).appendChild(loadingp);
	loadingpmade = true;
	}
	
}

function ProcessReqChange(){
	if (request.readyState == 1){ DisplayLoading(); }
	if (request.readyState == 4 && request.status == "200"){ ProcessRSSFeed(); }
}

function LoadXML(url){

	request = false;
	
	// Internet Explorer doesn't have native XMLHttpRequest support.
	
	if (!window.XMLHttpRequest && window.ActiveXObject){
		
		window.XMLHttpRequest = function(){ return new ActiveXObject("Msxml2.XMLHTTP"); }
		
	}
	
	if (window.XMLHttpRequest){
		try {
			request = new XMLHttpRequest();
		} catch(e){
			request = false;
		}
	}

	
	if (request){
		
		request.onreadystatechange = ProcessReqChange;
		request.open("GET", url, true);
		request.send(null);
		
	}
}

