//Script to retrieve Google calendar events with a date query

google.load("gdata", "1");
// you don't need to call main anymore, it's all renamed to getFeedData
google.setOnLoadCallback(main);

function main() {
   if (document.getElementById('darling')) getFeedData('darling');
   if (document.getElementById('stamps')) getFeedData('stamps');
  if (document.getElementById('marshburn')) getFeedData('marshburn');
}

function getFeedData(libraryName) {

function PRINT(str) {
  document.write(str);
}

// Create the calendar service object
var calendarService = new google.gdata.calendar.CalendarService('APU Library Calendar');

// The default "private/full" feed is used to retrieve events from 
// the primary private calendar with full projection 
var feedUri;
if(libraryName == 'darling') {
  feedUri = 'http://www.google.com/calendar/feeds/kwilcox@apu.edu/public/full';
} else if(libraryName == 'stamps') {
  feedUri = 'http://www.google.com/calendar/feeds/dnsglt41lv5rr3g66kvecs8o94@group.calendar.google.com/public/full';
} else if(libraryName == 'marshburn') {
  feedUri = 'http://www.google.com/calendar/feeds/jdoeahb273ravt41intlm8lar4@group.calendar.google.com/public/full';
} else {
  feedUri = '';
}

//testing feedUri variable
//alert(feedUri);

// Create a CalendarEventQuery, and specify that this query is 
// applied toward the "private/full" feed
var query = new google.gdata.calendar.CalendarEventQuery(feedUri);

// Create and set the minimum and maximum start time for the date query
/* To pass a static date:
var startMin = google.gdata.DateTime.fromIso8601('2009-06-29T00:00:00.000-08:00');
var startMax = google.gdata.DateTime.fromIso8601('2009-06-30T00:00:00.000-08:00');
*/

// Get today's date and use it to pull today's events from the calendar
var today = new Date();
today.setHours(0);
var tomorrow = new Date();
tomorrow.setHours(12);
 
var startMin = new google.gdata.DateTime(today);
var startMax = new google.gdata.DateTime(tomorrow);

query.setMinimumStartTime(startMin);
query.setMaximumStartTime(startMax);
query.setSortOrder('ascending');
query.setOrderBy('starttime');

// The callback that will be called when getEventsFeed() returns feed data
var callback = function(root) {

  // Obtain the array of matched CalendarEventEntry
  var eventEntries = root.feed.getEntries();

  // If there is matches for the date query
  if (eventEntries.length > 0) {
    for (var i = 0; i < eventEntries.length; i++) {
      var event = eventEntries[i];
      // Print the event title of the matches
//      PRINT(event.getTitle().getText());
      document.getElementById(libraryName).innerHTML = event.getTitle().getText(); 
    }
  } else {
    // No match is found for the date query
//    PRINT('no information available');
      document.getElementById(libraryName).innerHTML = 'No information available'; 
  }
}

// Error handler to be invoked when getEventsFeed() produces an error
var handleError = function(error) {
//  PRINT(error);
}

// Submit the request using the calendar service object. Notice the CalendarEventQuery 
// object is passed in place of the feed URI
calendarService.getEventsFeed(query, callback, handleError);
}