function date(ts)
{
    /* do anything with the date you want, the date is formatted in a timestamp. */
    var month = ts.slice(5,7);
    var day = ts.slice(8,10);
    return day + "/" + month;
}

function isUrl(s) {
    var regexp = /(ftp|http|https):\/\/(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?/
    return regexp.test(s);
}

function syncGoogleCalendar(target) {
    
    /*
    * Retrieve events with a date query
    */

    // Create the calendar service object
    //var calendarService = new google.gdata.calendar.CalendarService('GoogleInc-jsguide-1.0');
    var calendarService = new google.gdata.calendar.CalendarService('bsm-gigs');

    // The default "private/full" feed is used to retrieve events from
    // the primary private calendar with full projection
    var feedUri = 'https://www.google.com/calendar/feeds/bsm.news@gmail.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 startMin = new google.gdata.DateTime(new Date(), true);
    query.setMinimumStartTime(startMin);
    query.setMaximumStartTime(null);
    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();

        $(target).children().remove();
        // If there is matches for the date query
        if (eventEntries.length > 0) {
            $.each(eventEntries, function(i, event){
                var times = event.getTimes();
                var eventDateTime = times[0].startTime;
                var itemMarkup = "<li><strong> " + date(eventDateTime) + "</strong> ";
                var eventLinkHref = null;
                if (event.getHtmlLink() != null) {
                  eventLinkHref = event.getHtmlLink().getHref();
                  itemMarkup += "<a href='" + eventLinkHref + "' target='_blank'>" + event.getTitle().getText() + "</a>";
                } else {
                  itemMarkup += event.getTitle().getText();
                }
                var eventLocations = event.getLocations();
                if (eventLocations.length > 0)
                    itemMarkup += " - <small>" + eventLocations[0].valueString + "</small>";
                var eventContent = event.getContent();
                if (eventContent)
                {
                    var contentText = eventContent.getText();
                    if ((contentText.length > 0) && (isUrl(contentText)))
                        itemMarkup += " <small><a href=\"" + contentText + "\" target=\"_blank\" >" + "<img src=\"images/link.png\" />" + "</a></small>";
                 }
                itemMarkup += "</li>";
                $(target).append(itemMarkup);
            });
        } else {
            // No match is found for the date query
            $(target).append("<li>no events are matched from the query</li>");
        }
    }

    // Error handler to be invoked when getEventsFeed() produces an error
    var handleError = function(error) {
        $(target).append("<li><small><strong>" + error + "</strong></small></li>");
    }

    // 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);
}


