/*
* Retrieve events with a date query
*/ 

// Load Libraries
google.load("gdata", "1");
//google.load("jquery", "1.4.2");

function OnLoad() {

// Create the calendar service object
var calendarService = new google.gdata.calendar.CalendarService('heubisch_eu');

// The default "private/full" feed is used to retrieve events from 
// the primary private calendar with full projection 
var feedUri = 'http://www.google.com/calendar/feeds/vh88b55vj16qmlielcpej2lkm8%40group.calendar.google.com/public/full';

// 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
var today = new Date();

var startMin = new google.gdata.DateTime(today);
var startMax = google.gdata.DateTime.fromIso8601('2013-01-01T00:00:00.000-08:00');
query.setMinimumStartTime(startMin);
//query.setMaximumStartTime(startMax);
query.setMaxResults(5);
query.setOrderBy('starttime');
query.setSortOrder('ascending');

// 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);

}

google.setOnLoadCallback(OnLoad);

// 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];
	  
	  var startdate = event.getTimes()[0].getStartTime().getDate();
	  var strdate = pad(startdate.getDate(),2) + '.' + pad((startdate.getMonth()+1),2) + "." + startdate.getFullYear();
      // Print the event title of the matches
      PRINT(strdate + "<br/>" + event.getTitle().getText());
    }
  } else {
    // No match is found for the date query
    PRINT('no events are matched from the query!');
  }
}

// Error handler to be invoked when getEventsFeed() produces an error
var handleError = function(error) {
  PRINT(error);
}

function pad(number, length) {
   
    var str = '' + number;
    while (str.length < length) {
        str = '0' + str;
    }
   
    return str;

}

function PRINT(text) {

	$("#termine").append('<p>'+text+'</p>');
}



