var rssBox = '#rss-box';
var rssWait = 5000;
var rssSpeed = 'slow';
jQuery(document).ready(function() {

    // Retrieve elements from the News Feed
    jQuery(rssBox).each(function() {
        jQuery(this).empty();
        jQuery.get(rssFeed, function(data) {
            jQuery('item', data).each(function() {
                var title = jQuery('title', this).text();
                var link = jQuery('<a></a>').attr('target', '_blank').attr('href', jQuery('link', this).text()).text(title);
                var linkHtml = jQuery('<h3></h3>').html(link);

                var pubDate = new Date(jQuery('pubDate', this).text());
                var pubMonth = pubDate.getMonth() + 1;
                var pubDay = pubDate.getDate();
                var pubYear = pubDate.getFullYear();
                var pubHtml = jQuery('<div></div>').addClass('publication-date').text(pubMonth + '/' + pubDay + '/' + pubYear);

                var summaryText = jQuery('description', this).text();
                var summaryHtml = jQuery('<div></div>').addClass('summary').html(summaryText);

                // Append
                jQuery('<div></div>').addClass('headline').append(linkHtml).append(pubHtml).append(summaryHtml).appendTo(rssBox);
            });

            // Set up the Rotator
            var curHeadline = 0;
            var oldHeadline = 0;
            var hiddenPosition = (jQuery(rssBox).height() + 10);
            var headlineCount = jQuery('div.headline').length;
            var headlineTimer = false;

            // Position the first Headline
            jQuery('div.headline:eq(' + curHeadline + ')').css('top', 0);

            // Set up the Rotator Function
            var headlineRotate = function() {
                curHeadline = (oldHeadline + 1) % headlineCount;
                jQuery('div.headline:eq(' + oldHeadline + ')').animate({top: -hiddenPosition}, rssSpeed, function() {
                    jQuery(this).css('top', hiddenPosition);
                });
                jQuery('div.headline:eq(' + curHeadline + ')').animate({top: 0}, rssSpeed, function() {
                    headlineTimer = setTimeout(headlineRotate, rssWait);
                });
                oldHeadline = curHeadline;
            };
            
            // Remove all embedded images
//            jQuery(rssBox + ' img').remove();

            // Call initial function
            headlineTimer = setTimeout(headlineRotate, rssWait);
        }); // End $.get
    }); // End $.each
}); // End $.ready
