// Four Javascript RSS feed functions
//
// setupRSSFeeds : download the RSS feeds for the news blocks. 
// setupRSSLatest: download the RSS feed for the latest news item.
//
// formatRSSfeed: format the RSS feed for the news blocks.
// formatRSSlatest: format the RSS feed for the "latest" news item.

// setupRSSFeeds: get RSS feeds from Wordpress
//
// Argument -
//   category: Wordpress category
//   divID : ID of the div ("<div id=foo>") where the content should be placed.
//   nitems: number of items to return. use "-1" to get all available.
//
// Notes :
//  - URL for a WP RSS feed is "/category/groups/<category-name>/feed"
//  - rssproxy.php must be present on the server to run properly
//
function setupRSSFeed(category,divID, nitems) {
  var rssfeedURL = "/globals/style-3.0/programming/php/rssproxy.php?url=http://itnews.web.tamhsc.edu/category/groups/"
    + category
    + "/feed";

//  alert('category:' + category + ', rssfeedURL:' + rssfeedURL);

  jQuery(function() {
  // Setup the documentReady function
  jQuery.getFeed({
    url: rssfeedURL,
    //url: 'rssproxy.php?url=http://itnews.web.tamhsc.edu/category/groups/supportgroup/feed',
    success: function(feed) {
        $(divID).append(formatRSSfeed(feed,nitems));
        }
    });
  });
}

// setupRSSLatest: get RSS feeds from Wordpress for latest news items
//
// Argument -
//   category: Wordpress category
//   divID : ID of the div ("<div id=foo>") where the content should be placed.
//   nitems: number of items to return. use "-1" to get all available.
//
// Notes :
//  - URL for a WP RSS feed is "/category/groups/<category-name>/feed"
//  - rssproxy.php must be present on the server to run properly
//
function setupRSSLatest(category,divID) {
  var rssfeedURL = "/globals/style-3.0/programming/php/rssproxy.php?url=http://oitnews.tamhsc.edu/category/"
    + category
    + "/feed/";

//  alert('category:' + category + ', rssfeedURL:' + rssfeedURL);

  jQuery(function() {
  // Setup the documentReady function
  jQuery.getFeed({
    url: rssfeedURL,
    //url: 'rssproxy.php?url=http://itnews.web.tamhsc.edu/category/groups/supportgroup/feed',
    success: function(feed) {

	   var item = feed.items[0];
	   $(divID + " #itemTitle" ).html("<a href='" + item.link + "'>" + item.title + "</a>");
           $(divID + " #itemSummary" ).load("/globals/style-3.0/programming/php/rssproxy.php?url=" + item.link + " section");
	   $(divID + " #itemDate" ).html(item.updated);
 	   $(divID + " #itemLink" ).html("<a href='" + item.link + "'>Read</a>");

        }
    });
  });
}





// formatRSSfeed: output the rssfeed with class-based divs
//
// Argument -
//   feed  : returned by the "getFeed" js function
//   nitems: number of items to format; -1 = all available
//
function formatRSSfeed(thefeed, nitems) {
  var html = '';

  // Define the RSS feed block
  html = "\n"
    + '<div class="rssFeedTitle">'
    + thefeed.title
    + '</div>';

  // And the block for all rssItems
  html += "\n" + "<!-- Start rssItems block -->";

  // Now enumerate the rss feed items
  if (thefeed.items.length == 0) {
      html += "\n"
          + '<div class="rssNoItems">'
          + "No news items found in category"
          + '</div>';
  } else {
  html += "\n" + '<ul class="rssItems"';
  if (nitems == -1) {
     itemCount = thefeed.items.length;
  } else {
     itemCount = nitems;
  }
  for(var i = 0; i < itemCount; i++) {

    var item = thefeed.items[i];

    html += "\n"
      + '<!-- Start rssItem div block -->'
      + "\n"
      + '<li class="rssItem">'
      + '<div class="itemTitle">'
      + '<a href="'
      + item.link
      + '">'
      + item.title
      + '</a>'
      + "\n</div>";;

    html += "\n" + '<div class="itemDate">'
      + item.updated
      + "\n</div>";

    html += "\n" + '<div class="itemContents">'
      + item.description
      + "\n</div>";

    html += "\n" + '<!-- Close rssItem  block -->';
    html += "\n</li>";
  }

  // Close the rssItems div block

  html += "\n</ul>"
    + '<!-- Close rssItems block -->';
  }

//*DEBUG
//  alert('html: ' + html);
  return html;
}



