// JavaScript Document
// this script will only allow one iTunes feed per page. 
// send itunes feed and url root as variable in hidden input. 
// get the feed url by right clicking on the subscribe button in the iTunes course or "album"
// get the root url by changing the feed url: change  /Feed/ to /Browse/
google.load("feeds", "1");
  function initialize() {
var root = document.getElementById("iTunesRoot").getAttribute("value");
var iTunesWants = document.getElementById("iTunesUrl").getAttribute("value");

var feed = new google.feeds.Feed(iTunesWants);
feed.setResultFormat(google.feeds.Feed.XML_FORMAT);
feed.load(function(result) {
  var container = document.getElementById("feed");
  if (!result.error) {
		  var items = result.xmlDocument.getElementsByTagName("item");
		  for (var i = 0; i < items.length; i++) {
		  var titleElement = items[i].getElementsByTagName("title")[0];
		  var title = titleElement.firstChild.nodeValue;
		  var guidElement = items[i].getElementsByTagName("guid")[0];
		  var guid = guidElement.firstChild.nodeValue;
		  var authorElement = google.feeds.getElementsByTagNameNS(items[i], "http://www.itunes.com/dtds/podcast-1.0.dtd","author")[0];
		  var author = authorElement.firstChild.nodeValue;
		  var durationElement = google.feeds.getElementsByTagNameNS(items[i],"http://www.itunes.com/dtds/podcast-1.0.dtd","duration")[0];
		  var duration = durationElement.firstChild.nodeValue;
		  var li = document.createElement("li");
		  var a = document.createElement("a");
		  var p = document.createElement("p");
		  var em = document.createElement("em");
		  p.appendChild(document.createTextNode(author));	
		  p.appendChild(document.createElement("br"));
		  em.appendChild(document.createTextNode("(" + duration + " min.)" ));
		  p.appendChild(em);
		  a.setAttribute("href",root + guid);
		  a.appendChild(document.createTextNode(title));
		  li.appendChild(a);
		  li.appendChild(p);
		  container.appendChild(li);
    }
  }
});
}
google.setOnLoadCallback(initialize);