//requires jQuery 
if (typeof jQuery === 'undefined') { 
    throw new Error('this requires jQuery'); 
}

var cr = cr || {};

cr.mce = cr.mce || {}; 
 
if (cr.mce.data === undefined) {

    cr.mce.data = (function ($, cr) {
        'use strict';
        /*-----------------------------------------*/
        //  init 
        /*-----------------------------------------*/
        console.log("cr.mce.data: init");

        function noCacheParameter() {
            var d = new Date();
            return d.getTime();
        }



        //jqXHR objects returned by $.ajax() as of jQuery 1.5 implement the Promise interface (jqXHR.done, jqXHR.fail, jqXHR.always, jqXHR.then 
        function getMembersEventsRawHtmlForDay(eventDate, igd, rid) {
            var url = "/subscriber/events/html/edit/" + eventDate + "?" + "igd=" + igd + "&rid=" + rid + "&_ts=" + noCacheParameter();
            return $.ajax({
                type: "GET",
                url: url,
                dataType: "html" //text, xml, json, script, or html
            });
        }

        //jqXHR objects returned by $.ajax() as of jQuery 1.5 implement the Promise interface (jqXHR.done, jqXHR.fail, jqXHR.always, jqXHR.then 
        function getPublicEventsRawHtmlForDay(customerId, eventDate, igd, rid) {
            var url = "/calendar/events/html/" + customerId + "/" + eventDate + "?" + "igd=" + igd + "&rid=" + rid + "&_ts=" + noCacheParameter();
            return $.ajax({
                type: "GET",
                url: url,
                dataType: "html" //text, xml, json, script, or html
            });
        }

        //jqXHR objects returned by $.ajax() as of jQuery 1.5 implement the Promise interface (jqXHR.done, jqXHR.fail, jqXHR.always, jqXHR.then 
        function getPublicEventsSubscribeRawHtml(customerId, igd, rid) {
            var url = "/calendar/subscribe/html/" + customerId + "?" + "igd=" + igd + "&rid=" + rid + "&_ts=" + noCacheParameter();
            return $.ajax({
                type: "GET",
                url: url,
                dataType: "html" //text, xml, json, script, or html
            });
        }


        //jqXHR objects returned by $.ajax() as of jQuery 1.5 implement the Promise interface (jqXHR.done, jqXHR.fail, jqXHR.always, jqXHR.then 
        function insertQuickEvent(eventDate, eventName, startTime, endTime) {
            var url = "/api/subscriber/event/new/quickevent/?" + noCacheParameter();
            return $.ajax({
                type: "POST",
                url: url,
                dataType: "json", //text, xml, json, script, or html
                data: {
                    EventDate: eventDate,
                    EventName: eventName,
                    StartTime: startTime,
                    EndTime: endTime
                }
            });
        }

        //jqXHR objects returned by $.ajax() as of jQuery 1.5 implement the Promise interface (jqXHR.done, jqXHR.fail, jqXHR.always, jqXHR.then 
        function loginPrivateEvents(customerId, password) {
            var url = "/api/calendar/privateevents/login/" + customerId + "?" + noCacheParameter();
            return $.ajax({
                type: "POST",
                url: url,
                dataType: "json", //text, xml, json, script, or html
                data: {
                    CustomerId: customerId,
                    Password: password
                }
            });
        }

        //jqXHR objects returned by $.ajax() as of jQuery 1.5 implement the Promise interface (jqXHR.done, jqXHR.fail, jqXHR.always, jqXHR.then 
        function logoutPrivateEvents() {
            var url = "/api/calendar/privateevents/logout/" + "?" + noCacheParameter();
            return $.ajax({
                type: "GET",
                url: url,
                dataType: "json"
            });
        }

        //jqXHR objects returned by $.ajax() as of jQuery 1.5 implement the Promise interface (jqXHR.done, jqXHR.fail, jqXHR.always, jqXHR.then 
        function privateEventsSendPasswordEmail() {
            var url = "/api/subscriber/privateevents/sendpasswordemail/" + "?" + noCacheParameter();
            return $.ajax({
                type: "POST",
                url: url,
                dataType: "json" //text, xml, json, script, or html
            });
        }


        //jqXHR objects returned by $.ajax() as of jQuery 1.5 implement the Promise interface (jqXHR.done, jqXHR.fail, jqXHR.always, jqXHR.then 
        function uploadCustomImage(formData) {
            var url = "/api/customimage/upload/?" + noCacheParameter();
            return $.ajax({
                type: "POST",
                url: url,
                dataType: "json", //text, xml, json, script, or html
                data: formData,
                //Options to tell jQuery not to process data or worry about content-type.
                cache: false,
                processData: false,
                contentType: false
            });
        }

        //jqXHR objects returned by $.ajax() as of jQuery 1.5 implement the Promise interface (jqXHR.done, jqXHR.fail, jqXHR.always, jqXHR.then 
        function listCustomImages(customerId) {
            var url = "/api/customimage/list/" + customerId + "?" + noCacheParameter();
            return $.ajax({
                type: "GET",
                url: url,
                dataType: "json"
            });
        }

        //jqXHR objects returned by $.ajax() as of jQuery 1.5 implement the Promise interface (jqXHR.done, jqXHR.fail, jqXHR.always, jqXHR.then 
        function deleteCustomImage(customerId, imageId) {
            var url = "/api/customimage/delete/" + customerId + "?" + noCacheParameter();
            return $.ajax({
                type: "POST",
                url: url,
                dataType: "json", //text, xml, json, script, or html
                data: {
                    CustomerId: customerId,
                    ImageId: imageId
                }
            });
        }


        return {
            getMembersEventsRawHtmlForDay: getMembersEventsRawHtmlForDay,
            getPublicEventsRawHtmlForDay: getPublicEventsRawHtmlForDay,
            getPublicEventsSubscribeRawHtml: getPublicEventsSubscribeRawHtml,
            loginPrivateEvents: loginPrivateEvents,
            logoutPrivateEvents: logoutPrivateEvents,
            insertQuickEvent: insertQuickEvent,
            privateEventsSendPasswordEmail: privateEventsSendPasswordEmail,
            uploadCustomImage: uploadCustomImage,
            listCustomImages: listCustomImages,
            deleteCustomImage: deleteCustomImage
        };



    })(jQuery, cr);
}
 
 
//requires jQuery 
if (typeof jQuery === 'undefined') { 
    throw new Error('this requires jQuery'); 
}

var cr = cr || {}; 

cr.mce = cr.mce || {}; 

 
if (cr.mce.publicCalendar === undefined) {

    cr.mce.publicCalendar = (function ($, cr) {
        'use strict';
        /*-----------------------------------------*/
        //  init 
        /*-----------------------------------------*/
        console.log("cr.mce.publicCalendar: init");
        //scope variables 

        $(document).ready(function () {
            $(".js-day-show-events-click").click(function (e) {
                e.preventDefault();
                e.stopPropagation();
                var customerId = $(this).data("customerId");
                var eventDate = $(this).data("eventDate");
                var igd = $(this).data("igd");
                var rid = $(this).data("rid");
                console.log(eventDate);
                showEventsForDay(customerId, eventDate, igd, rid);
            });

            $(".js-calendar-month-select").change(function (e) {
                if ($(this).val() !== "") {
                    window.location.href = $(this).val();
                }
            });

            $(".js-private-events-login-button").click(function (e) {

                if (window.self !== window.top) {
                    // The page is in an iframe so show the messaging that they need to open the calendar in its own window to show private events.
                    $("#private-events-iframe-warning-modal").modal("show");

                    //window.top.location.href = window.location.href;
                    return;
                }


                var button = $(this);
                var customerId = button.data("customerId");
                console.log(customerId);

                var modal = $("#private-events-login-modal");

                //first hide any error messages and clear the form.
                modal.find("#private-events-password-error").hide();
                modal.find("#private-events-password").val("");
                modal.find(".alert").html("");
                modal.find(".alert").hide();

                modal.find("#private-events-password").off();
                modal.find("#private-events-password").keypress(function (e) {
                    if (e.which === 13)
                        modal.find(".btn-primary").click();
                });

                //primary button for unlocking private events with password
                modal.find(".btn-primary").off();
                modal.find(".btn-primary").click(function (e) {
                    showPrivateEvents(modal, customerId);
                });

                //set focus to password textbox
                modal.off("shown.bs.modal");
                modal.on("shown.bs.modal", function () {
                    modal.find("#private-events-password").focus();
                });

                modal.modal("show");
            });

            $(".js-private-events-logout-button").click(function (e) {
                e.preventDefault();
                e.stopPropagation();

                cr.mce.data.logoutPrivateEvents()
                    .done(function (data) {
                        window.location.reload(true);
                    })
                    .fail(function (jqXhr, textStatus, errorThrown) {
                        console.log(jqXhr);
                        alert(textStatus.toUpperCase() + ": " + jqXhr.statusText + " " + jqXhr.responseText);
                        window.location.reload(true);
                    });

            });


            $("#rss-feed-modal").on("show.bs.modal", function (event) {


                var button = $(event.relatedTarget);

                var customerId = button.data("customerId");
                var igd = button.data("igd");
                var rid = button.data("rid");
                console.log(customerId);
                var rssModal = $(this);

                //var rssModalInstance = new bootstrap.Modal(rssModal, {});

               //first hide any error messages and clear the form.
                rssModal.find("#number-of-days-error").hide();
                rssModal.find("#number-of-days").val("7");

                //primary button for unlocking private events with password
                rssModal.find(".btn-primary").off();
                rssModal.find(".btn-primary").click(function (e) {
                    //hide the error messages
                    rssModal.find("#number-of-days-error").hide();

                    var numberOfDays = rssModal.find("#number-of-days").val();

                    if (numberOfDays.length === 0 || isNaN(numberOfDays)) {
                        rssModal.find("#number-of-days-error").show();
                        return;
                    }

                    //redirect to the rss feed for this customer and number of days.
                    window.open("/calendar/rss.ashx?days=" + numberOfDays + "&ci=" + customerId + "&igd=" + igd + "&rid=" + rid, "rssWindow");
                    rssModal.modal("hide");
                });


                    rssModal.find("#number-of-days").focus();

            });

            $("#subscribe-modal").on("shown.bs.modal", function (event) {

                var button = $(event.relatedTarget);

                var customerId = button.data("customerId");
                var igd = button.data("igd");
                var rid = button.data("rid");
                console.log(customerId);

                var modal = $(this);

                //first hide any error messages.
                $(modal).find(".alert").hide();

                cr.mce.data.getPublicEventsSubscribeRawHtml(customerId, igd, rid)
                    .done(function (data) {
                        $(modal).find(".modal-body").html(data);
                    })
                    .fail(function (jqXhr, textStatus, errorThrown) {
                        console.log(jqXhr);

                        var alertDiv = $(modal).find(".alert");
                        alertDiv.html(textStatus.toUpperCase() + ": " + jqXhr.statusText + " <br/> " + jqXhr.responseText);
                        alertDiv.show();
                    });
            });



        });



        function showEventsForDay(customerId, eventDate, igd, rid) {
            //set the panel title and show the loading message
            $("#show-events-for-day-panel").find(".events-for-day-event-date").text(formatDate(new Date(eventDate)));
            $("#show-events-for-day-panel").find(".card-body").html($("<p />").html("Loading..."));
            cr.mce.data.getPublicEventsRawHtmlForDay(customerId, eventDate, igd, rid)
                .done(function (data) {
                    $("#show-events-for-day-panel").find(".card-body").html(data);
                })
                .fail(function (jqXhr, textStatus, errorThrown) {
                    console.log(textStatus.toUpperCase() + ": " + jqXhr.statusText + " <br/> " + jqXhr.responseText);
                    $("#show-events-for-day-panel").find(".card-body").html(textStatus.toUpperCase() + ": " + jqXhr.statusText);
                });
        }

        function formatDate(date) {
            var monthNames = [
              "January", "February", "March",
              "April", "May", "June", "July",
              "August", "September", "October",
              "November", "December"
            ];

            var day = date.getUTCDate();
            var monthIndex = date.getUTCMonth();
            var year = date.getUTCFullYear();

            return monthNames[monthIndex] + " " + day + ", " +  year;
        }

        function showPrivateEvents(modal, customerId) {
            //hide the error messages
            $(modal).find("#private-events-password-error").hide();
            $(modal).find(".alert").hide();

            var password = $(modal).find("#private-events-password").val();

            if (password.length === 0) {
                $(modal).find("#private-events-password-error").show();
                return;
            }

            cr.mce.data.loginPrivateEvents(customerId, password)
                .done(function (data) {
                    window.location.reload(true);
                })
                .fail(function (jqXhr, textStatus, errorThrown) {
                    console.log(jqXhr);
                    //console.log(textStatus.toUpperCase() + ": " + jqXhr.statusText + " <br/> " + jqXhr.responseText);
                    var alertDiv = $(modal).find(".alert");
                    if (jqXhr.status === 403) {
                        alertDiv.html(JSON.parse(jqXhr.responseText).message);
                    } else {
                        alertDiv.html(textStatus.toUpperCase() + ": " + jqXhr.statusText + " <br/> " + jqXhr.responseText);
                    }
                    alertDiv.show();
                });
        }


        return {
            formatDate: formatDate,
            showEventsForDay: showEventsForDay
        };



    })(jQuery, cr);
}
 
 
//requires jQuery 
if (typeof jQuery === 'undefined') {
    throw new Error('this requires jQuery');
}


var cr = cr || {};
cr.mce = cr.mce || {};

if (cr.mce.scrollingEvents === undefined) {

    cr.mce.scrollingEvents = (function ($, cr) {
        'use strict';
        /*-----------------------------------------*/
        //  init 
        /*-----------------------------------------*/
        console.log("cr.mce.scrollingEvents: init");
        //scope variables 


        //elements 
        var scrollingEventsList = $(".ct-mce-scrolling-events-list");
        var scrollingEventsListItem = $(".ct-mce-scrolling-events-list > ul li");

        //setup 

        var interval = 4200;
        var duration = 1200;

        var listItemHeight = scrollingEventsListItem.height();
        var scrollAmount = listItemHeight;        
        var totalScrollHeight = listItemHeight * scrollingEventsListItem.length;

        setScrollingSpeed();


        //events 

        $("#js-scrolling-events-speed").on("change", function() {

            $(".js-speed-change-spinner").show();
            setScrollingSpeed();

        });
        
        /*-----------------------------------------*/
        //  internal shared functions 
        /*-----------------------------------------*/


        function scrollNextEvent() {

            $(".js-speed-change-spinner").hide();

            if (scrollAmount === 0) {

                scrollingEventsList.scrollTop(0);
                scrollAmount = listItemHeight;
            }

            scrollingEventsList.animate({ scrollTop: scrollAmount }, duration, "swing", function () {

                scrollAmount += 100;

                if (scrollAmount === totalScrollHeight) {
                    scrollAmount = 0;
                }
            });

            setTimeout(scrollNextEvent, interval); //scroll next
        }
        setTimeout(scrollNextEvent, interval);  //scroll first

        function setScrollingSpeed() {

            //Speed
            // -2 = interval: 7000, duration: 2000
            // -1 = interval: 5600, duration: 1600
            //  0 = interval: 4200, duration: 1200 - current/default
            //  1 = interval: 2800, duration: 800
            //  2 = interval: 1400, duration: 400

            var scrollingEventsSpeed = parseInt($("#js-scrolling-events-speed").val());

            switch (scrollingEventsSpeed) {
                case -2:
                    interval = 7000;
                    duration = 2000;
                    break;
                case -1:
                    interval = 5600;
                    duration = 1600;
                    break;
                case 0:
                    interval = 4200;
                    duration = 1200;
                    break;
                case 1:
                    interval = 2800;
                    duration = 800;
                    break;
                case 2:
                    interval = 2200;
                    duration = 600;
                    break;
                default:
                    interval = 4200;
                    duration = 1200;
            }
        }

        /*-----------------------------------------*/
        //  private functions 
        /*-----------------------------------------*/




        /*-----------------------------------------*/
        //  public functions 
        /*-----------------------------------------*/
        return {

        };



    })(jQuery, cr);
}


















 //requires jQuery 
if (typeof jQuery === 'undefined') {
        throw new Error('this requires jQuery');
    }

    var cr = cr || {};

    cr.mce = cr.mce || {};

    if (cr.mce.publicSettings === undefined) {

        cr.mce.publicSettings = (function ($, cr) {
            'use strict';
            /*-----------------------------------------*/
            //  init 
            /*-----------------------------------------*/
            console.log("cr.mce.publicSettings: init");
            //scope variables 

            $(document).ready(function () {

                $(".js-mce-include-past-events").on("change", function () {
                    var include = $(this).prop("checked");
                    window.location = "/settings/setincludepastevent/" + include;
                });

                $(".js-mce-search-past-events").on("change", function () {
                    $(this)[0].form.submit();
                });

                if ($("#searchPastEvents").length) {
                    var searchPastEvents = $("#searchPastEvents").prop("checked");
                    if (searchPastEvents) {
                        $("#searchBackThrough").prop("disabled", false);
                    }
                    else {
                        $("#searchBackThrough").prop("disabled", true);
                    }
                }
            });


            return {
            };



        })(jQuery, cr);
    }


