// include Mayor news feed
$(document).ready(function() {
  $('#news-feed').each(function() {
    // Create and empty a unique container for feed.
    var $container = $(this);
    $container.empty();
    // Get feed xml and format title and link.
    $.get('/rss/mayor/mayor.xml', function(data) {
      $('rss item', data).each(function(i) {
        if(i<5) {
          //var $headText = ($('title', this).text());
          //var $headline = $('<h4></h4>').append($headText);
		  
		  if($('link', this).text()){
				  var $link = $('<a>').attr('href',$('link', this).text()).attr('style','display:inline;').append($('title',this).text());
				  
		          var $headline = $('<h4></h4>').append($link);
			  } else {
			  	var $headline = $('<h4></h4>').append($('title', this).text());
			  }
		  if ($('link', this).text().match(".pdf")) {
					var $pdftext = $('<span></span>').append(' (PDF)').attr('style','display:inline;');
                    var $headline = $headline.append($pdftext);
                }

          //var $headline = $headline.append('<br>');
          // Get pub date and format RFC 822 to mm/dd/yy.
          var pubDate = new Date(
          $('pubDate', this).text());
          var pubMonth = pubDate.getMonth() + 1;
          var pubDay = pubDate.getDate();
          var pubYear = pubDate.getFullYear();
          pubYear = pubYear.toString().slice(2);
          // Wrap pubDate in div with class tag.
          var $publication = $('<div></div>')
          .addClass('publication-date')
          .text(pubMonth + '/' + pubDay + '/' + pubYear);
          // Get description (summary info).
          var $summary = $('<div></div>')
          .addClass('summary')
          .html($('description', this).text());
          // Wrap complete item in div of class tag ("headline").
          $('<div></div>')
          .addClass('headline')
          .append($publication, $headline, $summary)
          .appendTo($container);
        }
      });
    });
  });
    
// Format newsfeed for Documents & Reports current year archive page.    
  $('#currentDocumentsReports').each(function() {
    // Create and empty a unique container for feed.
    var $container = $(this);
    $container.empty();
    // Create loading indicator.
    var $loadingIndicator = $('<div>Loading...</div>')
    .insertBefore($container);
    // Get feed xml and format title and link.
    $.get('/rss/mayor/mayor.xml', function(data) {
    	$loadingIndicator.remove();
      $('rss item', data).each(function(i) {
          // Get pub date and set condition:current year.      	
          var pubDate = new Date(
          $('pubDate', this).text());
          var pubYear = pubDate.getFullYear();
          var d = new Date(servertime);
          var curr_year = d.getFullYear();
          
          if(pubYear == curr_year) {
          // Format pubDate RFC 822 to mm/dd/yy.
          var pubMonth = pubDate.getMonth() + 1;
          var pubDay = pubDate.getDate();
          var pubYear = pubDate.getFullYear();
          pubYear = pubYear.toString().slice(2);
          // Wrap pubDate in table cell with class tag.
          var $publication = $('<td></td>')
          .addClass('publication-date')
          .text(pubMonth + '/' + pubDay + '/' + pubYear);
          // Get title and description.
          //var $headline = $('<td></td>').append($('title', this).text());
		  if($('link', this).text()){
				  var $link = $('<a>').attr('href',$('link', this).text()).append($('title',this).text());
		          var $headline = $('<td>').append($link);
				  if ($('link', this).text().match(".pdf")) {
                    $($headline).append(' (PDF)');
                }
			  } else {
			  	var $headline = $('<td>').append($('title', this).text());
			  }
          var $headline = $headline.append($('description', this).text());
          // Wrap complete newsItem in table row with class tag.
          $('<tr></tr>')
          .addClass('newsItem')
          .append($publication, $headline)
          .appendTo($container);
        }
      });
    });
  });
  
// Format newsfeed for Mayor's Home page "In the News section.    
  $('#homePageFeed').each(function() {
    // Create loading indicator.
    var $loadingIndicator = $('<div>')
    .html('Loading...')
    .insertBefore($(this));  	
    // Create and empty a unique container for feed.
    var $container = $(this);
    $container.empty();
    // Get feed xml and format title and link.
    $.get('/rss/mayor/mayor.xml', function(data) {
    	$loadingIndicator.remove();
    	var maxRecords = 5;
      $('rss item', data).each(function(i) {
        if($('category', this).text() != 'archiveonly'){
	        if(i<maxRecords) {
	          // Format pubDate RFC 822 to mm/dd/yy.
	          var pubDate = new Date(
	          $('pubDate', this).text());
	          var pubYear = pubDate.getFullYear();          
	          var pubMonth = pubDate.getMonth() + 1;
	          var pubDay = pubDate.getDate();
	          var pubYear = pubDate.getFullYear();
	          pubYear = pubYear.toString().slice(2);
	          // Wrap pubDate in cell with class tag.
	          var $publication = $('<td>')
	          .addClass('publication-date')
	          .text(pubMonth + '/' + pubDay + '/' + pubYear);
	          // Get headline
	          if($('link', this).text()){
				  var $link = $('<a>').attr('href',$('link', this).text()).append($('title',this).text());
		          var $headline = $('<td>').append($link);
				  if ($('link', this).text().match(".pdf")) {
                    $($headline).append(' (PDF)');
                }
			  } else {
			  	var $headline = $('<td>').append($('title', this).text());
			  }
	          var $headline = $headline.append($('description', this).text());
	          // Wrap complete item in table row with class tag.
	          $('<tr>')
	          .addClass('headline')
	          .append($publication, $headline)
	          .appendTo($container);
	        }
        } else {
        	maxRecords += 1;
        }
      });
    $('table#homePageFeed tr:nth-child(odd)').addClass('alt');      
    });

  });  
});
