﻿function loadLatestN(root, targetNode) {
 targetNode.innerHTML = "";
 var tempLink; var i;
 var tempNode = document.createElement("h4"); tempNode.appendChild(document.createTextNode("Latest News")); targetNode.appendChild(tempNode);
 var tempArray = root.feed.entry || [];
 for (i = 0; i < tempArray.length; ++i) {
  var published = tempArray[i].published.$t.substr(0,10);
  var title = tempArray[i].title.$t;
  var content = tempArray[i].content.$t;
  tempLink = null;
  if (content.length > 375) {
   tempLink = document.createElement("a"); tempLink.setAttribute("class", "news"); tempLink.setAttribute("href", "news.html"); tempLink.appendChild(document.createTextNode("read more"));
   content = content.substr(0,200);
  }
  tempNode = document.createElement("h5"); tempNode.setAttribute("class", "news"); tempNode.appendChild(document.createTextNode(title)); targetNode.appendChild(tempNode);
  tempNode = document.createElement("p"); tempNode.setAttribute("class", "news-date"); tempNode.appendChild(document.createTextNode(published)); targetNode.appendChild(tempNode);
  tempNode = document.createElement("p"); tempNode.setAttribute("class", "news"); tempNode.innerHTML = content;
  if (tempLink != null) { tempNode.appendChild(document.createTextNode(" ... [")); tempNode.appendChild(tempLink); tempNode.appendChild(document.createTextNode("]")); }
  targetNode.appendChild(tempNode);
 }
 tempArray = root.feed.link || [];
 for (i = 0; i < tempArray.length; ++i) if (tempArray[i].rel == "alternate") {
  tempLink = document.createElement("a");
  tempLink.setAttribute("class", "sidebar action");
  tempLink.setAttribute("href", tempArray[i].href);
  tempLink.setAttribute("target", "_top");
  tempLink.setAttribute("title", "View Stepper Point News archive");
  tempLink.appendChild(document.createTextNode("News archive"));
  targetNode.appendChild(tempLink);
 }
 for (i = 0; i < tempArray.length; ++i) if (tempArray[i].rel == "http://schemas.google.com/g/2005#feed") {
  tempLink = document.createElement("a");
  tempLink.setAttribute("class", "sidebar subscribe");
  tempLink.setAttribute("href", tempArray[i].href);
  tempLink.setAttribute("target", "_top");
  tempLink.setAttribute("title", "Subscribe to Stepper Point News feed");
  tempLink.appendChild(document.createTextNode("Subscribe"));
  targetNode.appendChild(tempLink);
 }
}

function loadNews(root, targetNode) {
 var entries = root.feed.entry || [];
 var html = [];
 for (var i = 0; i < entries.length; ++i) {
  var entry = entries[i];
  var published = entry.published.$t.substr(0,10);
  var title = entry.title.$t;
  var content = entry.content.$t;
  var links = entry.link || [];
  for (var j = 0; j < links.length; ++j) if (links[j].rel == "alternate") var href = links[j].href;
  html.push('<div class="newsItem">');
  html.push('<h5><a href="', href, '" target="_top">', title, '</a></h5>');
  html.push('<p class="pubDate">', published, '</p>');
  html.push('<p>', content, '</p>');
  html.push('</div>');
 }
 targetNode.innerHTML += html.join("");
}

function loadRecentNews(root) {
 var targetNode = document.getElementById("recentNews");
 loadNews(root, targetNode);
}

function loadLatestNews(root) {
 var targetNode = document.getElementById("latestNews");
 loadLatestN(root, targetNode);
}

