	
function rssFeedWidget(url, containerId, itemIndex, clickableTitles, clickableDescriptions, linkText, descriptionMaxLength) {

	var rssItems = null;
	var currentItem = null;
	var theWidgetContainerId = null;
	var totalItems = null;
	var titlesClickable = false;
	var descriptionsClickable = false;
	var linkTextString = null;
	var descMaxLength = 137;
	var containerName = null;
		
	var xml;

	url = encodeURIComponent(url);
	
	if ((typeof itemIndex == "undefined") || itemIndex == null) {
		
		itemIndex = 0;
		
	} // endif
	
	if ((typeof clickableTitles != "undefined") && clickableTitles) {
		
		titlesClickable = true;
		
	} // endif

	if ((typeof clickableDescriptions != "undefined") && clickableDescriptions) {
		
		descriptionsClickable = true;
		
	} // endif
		
	if (typeof linkText != "undefined") {
		
		linkTextString = linkText;
		
	} // endif
		
	if (typeof descriptionMaxLength != "undefined") {
		
		descMaxLength = descriptionMaxLength;
		
	} // endif
	
	currentItem = itemIndex;
	theWidgetContainerId = containerId;
	containerName = containerId;

	if (window.XMLHttpRequest) {
		
		// code for IE7+, Firefox, Chrome, Opera, Safari
		xml = new XMLHttpRequest();
	
	} else {
		
		// code for IE6, IE5
		xml = new ActiveXObject("Microsoft.XMLHTTP");
		
	} // endif
	
	
	xml.onreadystatechange=function() {
			
		if (xml.readyState == 4 && xml.status == 200) {
			
			// process the data
			rssItems = xml.responseXML;
			totalItems = rssItems.getElementsByTagName("item").length;

			displayItem();
			
		} // endif
		
	} // onreadystatechange()
	
	// local or remote URL? -- Do we need a proxy or not?
	var finalUrl;
			
	if (location.host.split(":")[0] == parseUri(url).host) {
		
		// resource is on the sane host as we are -- no proxy needed
		finalUrl = url;

	} else {

		// resource is remote -- use the proxy
		finalUrl = "/library/proxy.php?method=GET&url=" + url;
	
	} // endif
	
		
	xml.open("GET",encodeURI(finalUrl));
	xml.send();
	return this;

	
	function displayItem() {
				
		var itemNode =  rssItems.getElementsByTagName("item")[currentItem];
        var brokenIE = false;
		var html = null;
				
		theLink = itemNode.getElementsByTagName("link")[0].textContent;

        // check for broken IE DOM
        if (theLink == undefined) {
        
            brokenIE = true;
            theLink = itemNode.getElementsByTagName("link")[0].text;
            
        } // endif

		haveLink = (theLink.length > 0);
				
		html = "<div id='" + containerName + "_title'>";
		if (haveLink && titlesClickable) html += "<a href='" + theLink + "'>";
        if (brokenIE) {
            html += itemNode.getElementsByTagName("title")[0].text;
        } else {
            html += itemNode.getElementsByTagName("title")[0].textContent;
        } // endif
        
		if (haveLink && titlesClickable) html += "</a>";
		html += "</div>";
		
		html += "<div id='" + containerName + "_description'>";
		if (haveLink && descriptionsClickable) html += "<a href='" + theLink + "'>";
		if (descMaxLength > 0) {
            if (brokenIE) {
                html += itemNode.getElementsByTagName("description")[0].text.substring(0, descMaxLength);
                html += "...";
            } else {
                html += itemNode.getElementsByTagName("description")[0].textContent.substring(0, descMaxLength);
                html += "...";
            } // endif
		} else {
            if (brokenIE) {
                html += itemNode.getElementsByTagName("description")[0].text;
            } else {
                html += itemNode.getElementsByTagName("description")[0].textContent;
            } // endif
		} // endif
		if (haveLink && descriptionsClickable) html += "</a>";
		html += "</div>";
		
		html += "<div id='" + containerName + "_link'>";
		if (haveLink) {
			html += "<a href='" + theLink + "'>";
			if (linkTextString != null) {
				html += linkTextString;
			} else {	
				html += theLink;
			} // endif
			html += "</a>";
		} // endif
		html += "</div>";
		
		html += "<div id='" + containerName + "_buttons'>" +
				"<div id='" + containerName + "_prevButton'></div>" +
				"<div id='" + containerName + "_nextButton'></div>";
	
		document.getElementById(containerName).innerHTML = html;
		document.getElementById(containerName + '_nextButton').onclick = nextItem;
		document.getElementById(containerName + '_prevButton').onclick = previousItem;
		buttonEnableDisable();
					
	} // displayItem()
	

	function buttonEnableDisable() {
		
		if (currentItem == 0) {
			
			// disable previous button
			document.getElementById(containerName + "_prevButton").setAttribute("class", "disabled");
			
		} else {
			
			document.getElementById(containerName + "_prevButton").setAttribute("class", "");
			
		} // endif
		
		if (currentItem == (totalItems - 1)) {
			
			// disable next button
			document.getElementById(containerName + "_nextButton").setAttribute("class", "disabled");

		} else {
			
			document.getElementById(containerName + "_nextButton").setAttribute("class", "");
			
		} // endif
		
	} // buttonEnableDisable()
	
	
	function previousItem() {
	
		if (currentItem > 0) {
			
			--currentItem;
			displayItem();
			
		} // endif
		
	} // previousItem()
	
	
	function nextItem() {
					
		if (currentItem < (totalItems - 1)) {

			++currentItem;
			displayItem();
			
		} // endif
				
	} // nextItem()
	
	
	function parseUri (str) {
		
		parseUri.options = {
			strictMode: false,
			key: ["source","protocol","authority","userInfo","user","password","host","port","relative","path","directory","file","query","anchor"],
			q:   {
				name:   "queryKey",
				parser: /(?:^|&)([^&=]*)=?([^&]*)/g
			},
			parser: {
				strict: /^(?:([^:\/?#]+):)?(?:\/\/((?:(([^:@]*)(?::([^:@]*))?)?@)?([^:\/?#]*)(?::(\d*))?))?((((?:[^?#\/]*\/)*)([^?#]*))(?:\?([^#]*))?(?:#(.*))?)/,
				loose:  /^(?:(?![^:@]+:[^:@\/]*@)([^:\/?#.]+):)?(?:\/\/)?((?:(([^:@]*)(?::([^:@]*))?)?@)?([^:\/?#]*)(?::(\d*))?)(((\/(?:[^?#](?![^?#\/]*\.[^?#\/.]+(?:[?#]|$)))*\/?)?([^?#\/]*))(?:\?([^#]*))?(?:#(.*))?)/
			}
		};
		
		var	o   = parseUri.options,
			m   = o.parser[o.strictMode ? "strict" : "loose"].exec(str),
			uri = {},
			i   = 14;
	
		while (i--) {
			
			uri[o.key[i]] = m[i] || "";
			
		} // endwhile
	
		uri[o.q.name] = {};
		uri[o.key[12]].replace(o.q.parser, function ($0, $1, $2) {
			if ($1) uri[o.q.name][$1] = $2;
		});
	
		return uri;
		
	} // parseUri()
	
} // rssFeedWidget()

