// JavaScript Document
google.load("feeds", "1");
function initialize() {
  var feed = new google.feeds.Feed("http://www.google.com/calendar/feeds/6vqdopdv0nslhfvhdut00np6uo%40group.calendar.google.com/public/full?orderby=starttime&sortorder=ascending&futureevents=true&singleevents=true&max-results=4");
  feed.setResultFormat(google.feeds.Feed.XML_FORMAT);
  // Default value displays only four feed entries
  feed.setNumEntries(4);
  feed.load(function(result) {
  var container = document.getElementById("feed");
  if (!result.error) {
    var whenstring = '';
    var entries = google.feeds.getElementsByTagNameNS(result.xmlDocument, "http://www.w3.org/2005/Atom", "entry");
    var ul = document.createElement("ul");
    for (var i = 0; i < entries.length; i++) {
      var titleElement = google.feeds.getElementsByTagNameNS(entries[i], "http://www.w3.org/2005/Atom", "title")[0];
      var title = titleElement.firstChild.nodeValue;
      var mylinkElement = google.feeds.getElementsByTagNameNS(entries[i], "http://www.w3.org/2005/Atom", "link")[0];
      // var mylink = mylinkElement.getAttribute('href');
      var mylink = "http://www.alexandrabellerdances.org/calendar.php";
      // change the link for adding the calendar to make it so that it adds to the user's ASU domain calendar
      var whereElement = google.feeds.getElementsByTagNameNS(entries[i], "http://schemas.google.com/g/2005", "where")[0];
      var where = whereElement.getAttribute('valueString');
      var where2 = "";
      if (where.match(/[^,]+, .*/)) {
        var d = where.match(/([^,]+), (.*)/);
        where =  d[1];
        where2 = d[2];
      }

      var whenElement = google.feeds.getElementsByTagNameNS(entries[i], "http://schemas.google.com/g/2005", "when")[0];
      var starttime = whenElement.getAttribute('startTime');
      var showampm = 1;
      if (starttime.length>10) {
        var hour = parseInt(starttime.substring(11,13));
        var ampm = "am";
        if (hour>12) { 
          ampm = "pm";
          hour = hour - 12;
        } else if (hour == 12) {
          ampm = "pm";
        }
      } else {
        showampm = 0;
      }
      whenstring = starttime.substring(5,7) + '/' + starttime.substring(8,10) + '/' + starttime.substring(0,4);
      if (showampm==1) whenstring = whenstring + ' @ ' + hour + ':' + starttime.substring(14,16) + ampm;
      
      //embed the event location in a span element with class="where"
      var spanwhere = document.createElement("span");
      spanwhere.setAttribute("class", "where");
      spanwhere.appendChild(document.createTextNode(where));
      if (where2.length>0) {
        spanwhere.appendChild(document.createElement("br"));
        spanwhere.appendChild(document.createTextNode(where2));
      }
    
      //embed the event date in a span element with class="when"
      var spanwhen = document.createElement("span");
      spanwhen.setAttribute("class", "when");
      spanwhen.appendChild(document.createTextNode(whenstring));          

      //each feed entry is embedded in an HTML li element
      var li = document.createElement("li");
      var a = document.createElement("a");
      a.setAttribute("href", mylink);
      a.appendChild(document.createTextNode(title + ' '));
      li.appendChild(spanwhen);
      li.appendChild(document.createElement("br"));
      li.appendChild(a);
      li.appendChild(document.createElement("br"));
      li.appendChild(spanwhere);
      ul.appendChild(li);
      }
    container.appendChild(ul);
    }
  });
}
google.setOnLoadCallback(initialize);

