(function() {
    // set up request for AJAX calls
    var ajaxRequestFactory = null;
    
    var getAjaxRequest = function() {
	if (ajaxRequestFactory) {
	    return ajaxRequestFactory();
	}
	
	// Each one will be tried in turn to see which the user's browser supports
	var factories = [function () { return new XMLHttpRequest(); },
			 function () { return new ActiveXObject("Msxml2.XMLHTTP"); },
			 function () { return new ActiveXObject("Microsoft.XMLHTTP"); }];
	
	var request;
	for (var i = 0; i < factories.length; i++) {
	    try {
		request = (factories[i])();
		if (request) {
		    ajaxRequestFactory = factories[i];
		    return ajaxRequestFactory();
		}
	    } catch (e) {
		// this factory failed, move on
	    }
	}
    };
    
    // Pull of top items from news feed
    var getTopNews = function (targetElement) {
	var theRequest = getAjaxRequest();
	var theFeed;
	
	if (theRequest) {
	    // listen for the issued request
	    theRequest.onreadystatechange = function () {
		if (4 == theRequest.readyState) {
		    document.body.style.cursor = "default";
		    if (theRequest.status == 200) {
			theFeed = theRequest.responseXML;
			targetElement.innerHTML = formatNewsFeed(theFeed);		    
		    } else {
			// failure
		    }
		}
	    }
//	    theRequest.open("GET","http://www.toyonassociates.com/news/category/featured/feed");
	    theRequest.open("GET","/news/category/featured/feed");
	    theRequest.send(null);
	}
    };
    
    // Reformat the XML of the feed into something appropriate for the front page
    var formatNewsFeed = function (theFeed) {
	
	var theItems = theFeed.getElementsByTagName("item");
	var itemMaximum = 3; // how many to show on the main page

	var thisItem;
	var theTitle, theLink, theSummary, theDate, niceDate;
	var formattedResult = "";
	
	for (var i = 0; (i < itemMaximum && i < theItems.length) ; i++) {
	    thisItem = theItems[i];
	    theTitle = ""; theLink = ""; theStory = "";
	    
	    formattedResult += "<div id =\"news"+(i+1)+"\">";
	    
	    for (var j = 0; j < thisItem.childNodes.length ; j++) {
		if ("title" == thisItem.childNodes[j].nodeName) {
		    theTitle = thisItem.childNodes[j].childNodes[0].nodeValue;
		} else if ("link" == thisItem.childNodes[j].nodeName) {
		    theLink = thisItem.childNodes[j].childNodes[0].nodeValue;
		} else if ("description" == thisItem.childNodes[j].nodeName) {
		    theSummary = thisItem.childNodes[j].childNodes[0].nodeValue;
		} else if ("pubDate" == thisItem.childNodes[j].nodeName) {
		    theDate = thisItem.childNodes[j].childNodes[0].nodeValue;
		    niceDate = new Date(theDate);
		    if (niceDate) { theDate = niceDate.toLocaleDateString(); };
		}
	    }
	    formattedResult += "<p class=\"newstitle\"><a class=\"news_title\" href=\""+theLink+"\">"+theTitle+"</a><br />"+theDate+"</p>";
	    formattedResult += "<p class=\"newsstory\">"+theSummary+"</p>";
	    formattedResult += "</div>";
	}
	formattedResult += "<br class=\"clearfloat\" />";
	return(formattedResult);
    };

    // Put the news feed into the named element
    getTopNews(document.getElementById("newssection"));
})();

