﻿
$(document).ready(function() {
    /*
    * Retrieve events with a date query
    */

    var container = document.getElementById("listings");

    // Create the calendar service object
    var calendarService =
            new google.gdata.calendar.CalendarService('AndyWoodhull.com');

    // The "public/full" feed is used to retrieve events from the named public
    // calendar with full projection.
    var feedUri = 'http://www.google.com/calendar/feeds/u9nogvdfci7grbpqmro8guh514%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 now = new Date();
    var isoStart = now.getFullYear() + "-" + fixDatePart(now.getMonth() + 1) + "-" + fixDatePart(now.getDate()) + "T00:00:00.000-00:00";
    var isoEnd = now.getFullYear() + "-" + fixDatePart(now.getMonth() + 2) + "-" + fixDatePart(now.getDate()) + "T00:00:00.000-00:00";
    var startMin = google.gdata.DateTime.fromIso8601(isoStart);
    var startMax = google.gdata.DateTime.fromIso8601(isoEnd);
    query.setMinimumStartTime(startMin);
    query.setMaximumStartTime(startMax);
    query.setOrderBy("starttime");
    query.setSortOrder("ascending");


    // 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();

        var html = "";
        // If there is matches for the date query
        if (eventEntries.length > 0) {
            html += '<ul>';
            for (var i = 0; i < eventEntries.length; i++) {
                var event = eventEntries[i];
                var when = event.getTimes()[0];
                var startDate = when.getStartTime().getDate();
                var endDate = when.getEndTime().getDate();
                endDate.setDate(endDate.getDate() - 1);
                var sdFormated = (startDate.getMonth() + 1) + "." + startDate.getDate() + "." + startDate.getFullYear();
                var edFormated = (startDate.valueOf() == endDate.valueOf()) ? "" : " - " + (endDate.getMonth() + 1) + "." + endDate.getDate() + "." + endDate.getFullYear();

                // Print the event title of the matches
                html += '<li>' + event.getTitle().getText() + '<br />'
                     + sdFormated
                     + edFormated + '</li>';
            }
            html += '</ul>';
        } else {
            // No match is found for the date query
            html += '<p>No events are matched from the query!</p>';
        }

        // Output HTML and clear 'Loading...' text
        container.innerHTML = html;
    };

    // Error handler to be invoked when getEventsFeed() produces an error
    var handleError = function(error) {
        container.innerHTML = '<pre>' + error + '</pre>';
    };

    // 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);
});

function fixDatePart(part) {
    var p = part + ""; // cast to string
    if (p.length < 2) {
        p = "0" + p;
    }
    return p;
}
   
