﻿/// <reference path="jquery-1.3.2.min-vsdoc2.js" />
/**
* Creates an unordered list of events in a human-readable form
*
* @param {json} root is the root JSON-formatted content from GData
* @param {string} divId is the div in which the events are added
*/ 
function listEvents(root, divId) {
  var feed = root.feed;
  var events = $(divId);

  events.empty();

  var news = "";
  var count = 0;
  $.each(feed.entry, function(i, cal) {
      if (count < 4) {
          var title = trim12(cal.title.$t);
          if (title != "Morning Service") {
              var description = cal.content.$t;
              var start = formatGCalDateTime(cal['gd$when'][0].startTime);
              var end = formatGCalTime(cal['gd$when'][0].endTime);
              description = description.replace(/\n/g, "<br />");
              news = news + '<div class="centerUnitBlock news"><h1>' + title + "</h1>" + (description.length == 0 ? "" : description + "<br/>") + (cal['gd$recurrence'] == null ? start + "-" + end : "") + "</div>";
              count++;
          }
      }
  });

  events.html(news);    
}

function trim12(str) {
    var str = str.replace(/^\s\s*/, ''),
		ws = /\s/,
		i = str.length;
    while (ws.test(str.charAt(--i)));
    return str.slice(0, i + 1);
}

/**
 * Callback function for the GData json-in-script call
 * Inserts the supplied list of events into a div of a pre-defined name
 * 
 * @param {json} root is the JSON-formatted content from GData
 */ 
function insertAgenda(root) {
  listEvents(root, '#agenda');
}
