/*
* CRS RoomRate calendar data function
*/

(function ($) {

    $.fullCalendar.calendarData = function (feedUrl, options) {
        //alert(options);
        options = options || {};
        return function (start, end, callback) {
            var params = {},
                ctz = options.currentTimezone,
                today = new Date(),
                one_day = 86400000;

            if (ctz) {
                params.ctz = ctz = ctz.replace(' ', '_');
            }

            //feedUrl = "http://localhost/svdevelopment/sunvalley/site.x/trip-planner/lodging2/search/2010/6/3/3/2/0/LM";
            //var today = new Date(); //today
            if (start < today) {
                start = today;
            }
            //set departure to 1 day later - calendar will do 42 day range which will return incorrect ava
            end = new Date(start); //needs to be passed arrDate in constructor or dateAdd at month boundary will not work
            end.setDate(start.getDate() + 1);
            feedUrl = crs.getUrlDateRange(start, end);
            if (feedUrl.indexOf('?') > -1) {
                feedUrl = feedUrl.replace('?', '/' + options + '?');
            }
            else {
                feedUrl = feedUrl + '/' + options;
            }

            //var one_day = 86400000;

            //$.fancybox.showActivity();
            $.getJSON(feedUrl, params, function (data) {
                //$.fancybox.hideActivity();
                var calRates = [];
                if (data.RoomStays) {
                    $.each(data.RoomStays, function (i, roomStay) {
                        if (roomStay.RoomRates) {
                            $.each(roomStay.RoomRates, function (j, roomRate) {
                                if (roomRate.Rates) {
                                    $.each(roomRate.Rates, function (k, rate) {
                                        var rateStart = $.fullCalendar.parseISO8601(rate.EffectiveDate, true),
                                            rateEnd = $.fullCalendar.parseISO8601(rate.ExpireDate, true);
                                        rateEnd = new Date(rateEnd.getTime() - one_day);
                                        //alert(rateStart);
                                        calRates.push({
                                            title: '$' + rate.DailyRates[0].Amount.toFixed(2),
                                            start: rateStart,
                                            end: rateEnd,
                                            description: roomRate.RoomType.Description,
                                            allDay: true
                                        });
                                    });
                                }
                            });
                        }
                    });
                }
                //add event for prospective stay
                calRates.push({
                    title: 'Your Stay',
                    start: crs.searchParameters.Arrival,
                    //end: new Date(crs.searchParameters.Departure.getTime() - one_day),
                    end: crs.searchParameters.Departure,
                    allDay: true,
                    id: 'crs.ArrivalDeparture',
                    className: 'crs_ArrivalDeparture',
                    editable: true
                });
                callback(calRates);
            });
        };

    };

})(jQuery);

