/*
 * Wordpress CalendarFeed data provider
 */

(function ($) {

    //BEGIN CATEGORY CLASS
    function CalendarCategory() {
        this.categoryFilter = "";
    };

    //function to update category filter (called from dropdown event handler)
    CalendarCategory.prototype.updateCategoryFilter = function (category) {
        if (category == "_all") {
            this.categoryFilter = "";
        }
        else {
            this.categoryFilter = category;
        }
    }

    window.calendarCategory = new CalendarCategory();
    //END CATEGORY CLASS

    $.fullCalendar.calendarData = function (feedUrl, options) {

        options = options || {};
        return function (start, end, callback) {
            var one_day = 1000 * 60 * 60 * 24;
            var days = Math.ceil((end.getTime() - start.getTime()) / (one_day));

            var params = {
                'startDate': $.fullCalendar.formatDate(start, 'u'),
                'eventDays': days,
                'endDate': $.fullCalendar.formatDate(end, 'u')
            };

            //check and see if a category was selected and if so add it to the parm list
            if (window.calendarCategory.categoryFilter != "") {
                params.eventCategory = window.calendarCategory.categoryFilter;
            }

            var ctz = options.currentTimezone;
            if (ctz) {
                params.ctz = ctz = ctz.replace(' ', '_');
            }

            var date = new Date();
            var d = date.getDate();
            var m = date.getMonth();
            var y = date.getFullYear();
            $.getJSON(feedUrl, params, function (data) {
                var events = [];
                var parseEntry = function (events, entry) {
                    var dStart = entry.DTSTART['#text'].replace(':00.0000000Z', 'Z').replace('-T', 'T');
                    var dEnd = entry.DTEND['#text'].replace(':00.0000000Z', 'Z').replace('-T', 'T');
                    var start = $.fullCalendar.parseISO8601(dStart, true);
                    var end = $.fullCalendar.parseISO8601(dEnd, true);
                    var url = entry.URL['#text'];
                    var allDay = entry.DTSTART['@ALLDAY'] == 'TRUE';
                    if (allDay) {
                        $.fullCalendar.addDays(end, -1); // make inclusive
                    }
                    var articleID = entry.UID.substring(0, entry.UID.indexOf('-'));
                    events.push({
                        title: entry.SUMMARY,
                        start: start,
                        end: end,
                        description: entry.DESCRIPTION['#text'],
                        url_alt: url, //url property triggers link follow - but we need the url to display in the lightwindow
                        allDay: allDay,
                        id: url,
                        articleID: entry.DESCRIPTION['@ARTICLEID']
                    });
                };
                if (data.VCALENDAR.VEVENT) {
                    if (data.VCALENDAR.VEVENT.length == undefined) {
                        //data is returned as single object if only one, array otherwise
                        parseEntry(events, data.VCALENDAR.VEVENT);
                    }
                    else {
                        $.each(data.VCALENDAR.VEVENT, function (i, entry) {
                            parseEntry(events, entry);
                        });
                    }
                }
                callback(events);
            });
        }

    }

})(jQuery);

