function getCalendarDate(now) {
   var months = new Array(13);
   months[0]  = "Jan";
   months[1]  = "Feb";
   months[2]  = "Mar";
   months[3]  = "Apr";
   months[4]  = "May";
   months[5]  = "Jun";
   months[6]  = "Jul";
   months[7]  = "Aug";
   months[8]  = "Sep";
   months[9]  = "Oct";
   months[10] = "Nov";
   months[11] = "Dec";
   var monthnumber = now.getMonth();
   var monthname   = months[monthnumber];
   var monthday    = now.getDate();
   var year        = now.getYear();
   if(year < 2000) { year = year + 1900; }
//   var dateString = monthname + ' ' + monthday + ', ' + year;
   var dateString = monthname + ' ' + monthday;
   return dateString;
} // function getCalendarDate()

function formatClockTime(now) {
   var hour   = now.getHours();
   var minute = now.getMinutes();
   var ap = "AM";
   if (hour   > 11) { ap = "PM";             }
   if (hour   > 12) { hour = hour - 12;      }
   if (hour   == 0) { hour = 12;             }
   if (hour   < 10) { hour   = hour;   }
   if (minute < 10) { minute = "0" + minute; }
   var timeString = hour + ':' + minute + " " + ap;
   return timeString;
} // function getClockTime()

function getElementsByTagNameWithNamespace(tagName, ns, prefix, scope){
	var elementListForReturn = scope.getElementsByTagName(prefix+':'+tagName);
	if(elementListForReturn.length == 0) {
		elementListForReturn = scope.getElementsByTagName('ns:'+tagName);
		if(elementListForReturn.length == 0 && document.getElementsByTagNameNS) {
			elementListForReturn = scope.getElementsByTagNameNS(ns, tagName);
		} // if
	} // if
	return elementListForReturn;
} // function..getElementsByTagNameWithNamespace

var insertTopRssArticle_defaults = {
	isLoadingHTML: '<img src="/sites/all/themes/sram_2011/_images/general-skin/icon-library/ajax-loader.gif" height="16" width="16" alt="Loading..." />&nbsp;Loading...',
	dateLinePrefix: 'Posted:',
	errorCopy: "There was difficulty in retrieving the RSS data.",
	locale: 'en',
	showAuthor: true,
	showTitle: true,
	showCommentCount: true,
	showDescription: true,
	descriptionFilterCallback: null,
	articleLimit: 1
};

function insertTopRssArticle(rssContainer, rssURL, options) {
	var insertTopRssArticle_options = $.extend(insertTopRssArticle_defaults, options);
	if(insertTopRssArticle_options.isLoadingHTML) rssContainer.html(insertTopRssArticle_options.isLoadingHTML);

	var currentXhr = $.ajax({
		url : rssURL,
		dataType : "text",
		processData: false,
		insertTopRssArticle_options: jQuery.extend({}, insertTopRssArticle_options), // hack!!!
		error : function(xhr) {
			rssContainer.empty();
			rssContainer.append('<p class="postDate">'+options.errorCopy+'</p>');
			rssContainer.append('<span style="display: none">transport.readyState = '+xhr.readyState+' / transport.status '+xhr.status+'</span>');
		},
		success : function(responseXmlString) {
			if($.browser.msie) {
				//-- MSIE has a problem if MIME type is anything but "text/xml", so we have to do this by hand!!!
				responseXML = new ActiveXObject("Microsoft.XMLDOM");
				responseXML.async = false;
				responseXML.loadXML(responseXmlString);
			} else {
				var parser = new DOMParser();
				responseXML = parser.parseFromString(responseXmlString, "application/xml");
			} // if

			var opts = this.insertTopRssArticle_options;
			var items = responseXML.documentElement.getElementsByTagName('item');
			rssItems = new Array();
			if(items.length!=0) {
				for(var i = 0; (i < items.length) && (i < opts.articleLimit); i++) {
					//-- get title
					var articleTitle = "";
					if(items[i].getElementsByTagName("title").length) {
						if(items[i].getElementsByTagName("title")[0].firstChild) {
							articleTitle = items[i].getElementsByTagName("title")[0].firstChild.nodeValue;
						} // if
					} // if..else

					//-- get pubDate
					var dInt = Date.parse(items[i].getElementsByTagName("pubDate")[0].firstChild.nodeValue)
					dInt = (!isNaN(parseInt(dInt))) ? dInt : 0; //-- graceful failure on a bad parse
					var d = new Date();
					d.setTime(dInt);

					//-- get author
					var articleAuthor = "";
					if(items[i].getElementsByTagName("author").length) {
						articleAuthor = items[i].getElementsByTagName("author")[0].firstChild.nodeValue;
					} else {
						var articleAuthorItems = getElementsByTagNameWithNamespace("creator","http://purl.org/dc/elements/1.1/","dc",items[i]);
						if(articleAuthorItems.length) {
							articleAuthor = articleAuthorItems[0].firstChild.nodeValue;
						} // if
					} // if..else

					//-- get description
					var articleDescription = "";
					if(items[i].getElementsByTagName("description").length) {
						if(items[i].getElementsByTagName("description")[0].firstChild) {
							if(opts.descriptionFilterCallback) {
								articleDescription = opts.descriptionFilterCallback(items[i].getElementsByTagName("description")[0].firstChild.nodeValue);
							} else {
								articleDescription = items[i].getElementsByTagName("description")[0].firstChild.nodeValue;
							} // if..else
						} // if
					} // if..else

					//-- get comment count, if exists
					var commentCount = 0;
					var commentItems = getElementsByTagNameWithNamespace("comments","http://purl.org/rss/1.0/modules/slash/","slash",items[i]);
					if(commentItems.length) {
						commentCount = commentItems[0].firstChild.nodeValue;
					} // if

					rssItems[i] = {
						"title"				: articleTitle,
						"author"			: articleAuthor,
						"link"				: items[i].getElementsByTagName("link")[0].firstChild.nodeValue,
						"description"		: articleDescription,
						"pubDateFormatted"	: getCalendarDate(d) + " " + formatClockTime(d),
						"commentCount"		: commentCount
					} // rssItem

					rssContainer.empty();
					jQuery(rssItems).each( function() {
						if(opts.showCommentCount) {
							rssContainer.append('<div class="bubble">'+(this.commentCount)+'</div>');
						} // if
						if(opts.showTitle) {
							rssContainer.append('<a href="'+(this.link)+'" class="title bvLinkTargetBlank">'+(this.title)+'</a>');
						} // if
						if(opts.showAuthor) {
							rssContainer.append('<p class="postDate">'+opts.dateLinePrefix+' '+(this.author)+', '+(this.pubDateFormatted)+'</p>');
						} else {
							rssContainer.append('<p class="postDate">'+opts.dateLinePrefix+' '+(this.pubDateFormatted)+'</p>');
						} // if
						if(opts.showDescription) {
							rssContainer.append('<p class="pLarge">'+(this.description)+'</p>');
						} // if
					}); // each
				} // for

			} else {
				jQuery(rssContainer).css('display','none');
			} // if..else
		}
	}); // ajax
};  // insertTopRssArticle

