// RSS feed reader
// Jacob Tews (jt) <jacob [at] webteks.com> 2007-01-25
// Pulls XML from provided URL and displays items in a list form with links to
// either a display of the full content in the layout or a link to another page

var localRSS = "rss.php"; // local RSS proxy (needed for remote feeds)
var feedURL = "http://www.inman.com/rss/customer/mattmorneault/rss.aspx"; // URL of feed
var getvars = "cust=2493"; // things that go after the '?'
var quickFeedContainer = "quick-feed"; // ID of quick feed container element
var fullFeedContainer = "content-area"; // ID of full feed container element (where the full story will display)
var fullFeedTitle = "page-heading"; // ID of full content area's title container
var detailPage = "story_detail.htm"; // filename of story detail page (don't need full path)

Event.observe(window, "load", loadQuickFeed);

////////////////////////////////////////////////////////////////////////////////
// Please don't mess with anything below this line

var xml; // responseXML object from the AJAX
// Creates the Ajax Request to retreive XML
function loadQuickFeed () {
	if ($(quickFeedContainer)) {
		new Ajax.Request(
			localRSS,
			{
				method:"post",
				parameters:"url=" + feedURL + "&" + getvars,
				onCreate:quickFeedCreate,
				onComplete:quickFeedComplete
			}
		);
		// called explicitly because sometimes onCreate doesn't get called properly
		quickFeedCreate();
	}
}

// Initializes stuff like the loading graphic
function quickFeedCreate () {
	removeInnerNodes(quickFeedContainer);
	var styles = {
		height:"32px",
		background:"url('images/loader.gif') no-repeat center"
	};
	Element.setStyle(quickFeedContainer, $H(styles));
}

// Cleans up what quickFeedCreate did and adds in the news article listing
function quickFeedComplete (responseXML) {
	// globalize:
	xml = responseXML.responseXML;
	
	var styles = {
		height:"",
		background:""
	};
	Element.setStyle(quickFeedContainer, $H(styles));
	removeInnerNodes(quickFeedContainer);
	
	var ul = document.createElement("ul");
	$(quickFeedContainer).appendChild(ul);
	
	var items = xml.getElementsByTagName("item");
	for (var i = 0; i < items.length; i++) {
		var li = document.createElement("li");
		var a = document.createElement("a");
		var div = document.createElement("div");
		var title = items[i].getElementsByTagName("title")[0].firstChild.nodeValue;
		var pubDate = items[i].getElementsByTagName("pubDate")[0].firstChild.nodeValue;
		var guid = items[i].getElementsByTagName("guid")[0].firstChild.nodeValue;
		
		// Newsitem link
		if (document.location.pathname.indexOf(detailPage) > -1) {
			a.setAttribute("href", "#" + guid);
			Event.observe(a, "click", handleFeedLink);
		} else {
			a.setAttribute("href", detailPage + "#" + guid);
		}
		a.innerHTML = title;
		
		// Date string
		var d = new Date();
		d.setTime(Date.parse(pubDate));
		div.className = "date";
		//div.innerHTML = (d.getMonth() + 1) + '/' + d.getDate() + '/' + d.getFullYear();
		div.innerHTML = pubDate.substring(0,14);

		// DOM
		li.appendChild(a);
		li.appendChild(div);
		ul.appendChild(li);
	}
	handleFeedLink(null);
}

// Handles the "click" of the feed links and loads the content (<description>) 
// into the content area
function handleFeedLink (event) {
	var hash = document.location.hash;
	if (event != null) {
		var a = Event.element(event);
		hash = a.hash;
	}
	hash = hash.substring(1);
	if (xml == null) {
		alert("XML Feed did not make it. Please contact the web development company that created this site.");
	}
	var items = xml.getElementsByTagName("item");
	var i;
	// increment up to the proper item and use that index after the loop
	for (i = 0; i < items.length; i++) {
		var guid = items[i].getElementsByTagName("guid")[0].firstChild.nodeValue;
		if (guid == hash) {
			break;
		}
	}
	// hack check - we didn't find the guid
	if (i == items.length) {
		return;
	}
	var title = items[i].getElementsByTagName("title")[0].firstChild.nodeValue;
	var pubDate = items[i].getElementsByTagName("pubDate")[0].firstChild.nodeValue;
	var description = "";
	for (var d = 0; d < items[i].getElementsByTagName("description")[0].childNodes.length; d++) {
		description += items[i].getElementsByTagName("description")[0].childNodes[d].nodeValue;
	}
	// Date string
	var d = new Date();
	var weekday = new Array(7);
	weekday[0] = "Sun";
	weekday[1] = "Mon";
	weekday[2] = "Tue";
	weekday[3] = "Wed";
	weekday[4] = "Thu";
	weekday[5] = "Fri";
	weekday[6] = "Sat";
	var month = new Array(12);
	month[0] = "Jan";
	month[1] = "Feb";
	month[2] = "Mar";
	month[3] = "Apr";
	month[4] = "May";
	month[5] = "Jun";
	month[6] = "Jul";
	month[7] = "Aug";
	month[8] = "Sep";
	month[9] = "Oct";
	month[10] = "Nov";
	month[11] = "Dec";
	d.setTime(Date.parse(pubDate));
	var strDate = weekday[d.getDay()] + ", " + d.getDate() + " " + month[d.getMonth()] + " " + d.getFullYear();
	strDate = pubDate.substring(0,14);
	$(fullFeedTitle).innerHTML = title;
	$(fullFeedContainer).innerHTML = "<div style='text-align:right'>" 
		+ strDate 
		+ "</div><br><br>" 
		+ description;
}

// Utility function to clear out all elements from any element
function removeInnerNodes (element) {
	var rootNode = $(element);
	while (rootNode.childNodes.length > 0) {
		Element.remove(rootNode.childNodes[0]);
	}
}
