(function () {
    var calendar = window.calendar = {};
    var clientExportManager;
    var contactList;
    var scheduler;
    var categoryNames = [];
    var calendar1 = null;
    var SchedulerNavigationCompleteAlreadyOccurred = false;

    calendar.initialize = function () {
        clientExportManager = $find("clientExportManager");
        scheduler = $find($('div.RadScheduler').attr('id'));
        contactList = $(".scroller");
        calendar1 = $telerik.findControl(document.getElementById('sidebar'), "RadCalendar1");
        var $newEventTextArea = $(".rsTextarea textarea");

        if (document.getElementById("nav").value === "mobile") {
            // hide calendar for mobile devices
            calendar1.set_visible(false);
        } else {
            // hide "Add New Folder" for desktop
            $(".btn-new-node").hide();
        }
        
        worker.addFolderClicked();
    };

    calendar.ExportToPdf = function () {
        var pdfSettings = {
            fileName: "calendar.pdf",
            proxyURL: "contacts.aspx",
            margin: { top: 25, left: 15, bottom: 10, right: 5 },
            title: "PDF",
            author: "Telerik",
            subject: "Contacts",
            keywords: "a keyword",
            creator: "Telerik"
        };

        clientExportManager.set_pdfSettings(pdfSettings);
        clientExportManager.exportPDF(contactList);
    };

    calendar.ExportToImage = function () {
        var imageSettings = {
            fileName: "calendar.png",
            proxyURL: "contacts.aspx"
        };
        clientExportManager.set_imageSettings(imageSettings);
        clientExportManager.exportImage(contactList);
    };

    calendar.clientNodeClicked = function (sender, args) {
        var command = args.get_node().get_text();

        switch (command) {
            case "New":
                var firstSlot = $('.rsContentTable td').first();
                var activeModel = scheduler.get_activeModel();
                var slot = activeModel.getTimeSlotFromDomElement(firstSlot[0]);
                scheduler.showInsertFormAt(slot);
                break;
            case "Export to PDF":
                calendar.ExportToPdf();
                break;
            case "Export to Image":
                calendar.ExportToImage();
                break;
            case "Import":
                $(".ruFileInput").click();
                break;
            default:
        }
    };

    calendar.rebindScheduler = function () {
        scheduler.rebind();
    };

    // Dictionary containing the advanced template client object
    // for a given RadScheduler instance (the control ID is used as key).
    var schedulerTemplates = {};

    calendar.schedulerFormCreated = function (scheduler, eventArgs) {

        // Create a client-side object only for the advanced templates
        var mode = eventArgs.get_mode();
        if (mode === Telerik.Web.UI.SchedulerFormMode.AdvancedInsert || mode === Telerik.Web.UI.SchedulerFormMode.AdvancedEdit) {
            // Initialize the client-side object for the advanced form
            var formElement = eventArgs.get_formElement();
            var templateKey = scheduler.get_id() + "_" + mode;
            var advancedTemplate = schedulerTemplates[templateKey];
            if (!advancedTemplate) {
                // Initialize the template for this RadScheduler instance
                // and cache it in the schedulerTemplates dictionary
                var schedulerElement = scheduler.get_element();
                var isModal = scheduler.get_advancedFormSettings().modal;
                advancedTemplate = new window.SchedulerAdvancedTemplate(schedulerElement, formElement, isModal);
                advancedTemplate.initialize();

                schedulerTemplates[templateKey] = advancedTemplate;

                // Remove the template object from the dictionary on dispose.
                scheduler.add_disposing(function () {
                    schedulerTemplates[templateKey] = null;
                });
            }

            // Are we using Web Service data binding?
            if (!scheduler.get_webServiceSettings().get_isEmpty()) {
                // Populate the form with the appointment data
                var apt = eventArgs.get_appointment();
                var isInsert = mode === Telerik.Web.UI.SchedulerFormMode.AdvancedInsert;
                advancedTemplate.populate(apt, isInsert);
            }
        } else {
            // Set default text to pass subject required filed valdiation
            $(".rsTextarea textarea").val("New event");
        }
    };

    calendar.onClientNavigationComplete = function (sender, args) {
        SchedulerNavigationCompleteAlreadyOccurred = true;
        var selectedDate = sender.get_selectedDate();
        calendar1.navigateToDate([selectedDate.format("yyyy"), selectedDate.format("MM"), selectedDate.format("dd")]);
    };

    calendar.onCalendarDateSelected = function (sender, args) {
        var selectedDateTriplet = sender.get_selectedDates()[0];
        if (selectedDateTriplet) {

            var selectedDate = new Date(selectedDateTriplet[0], selectedDateTriplet[1] - 1, selectedDateTriplet[2]);
            scheduler.set_selectedDate(selectedDate);
        }
    };

    calendar.onCalendarViewChanged = function (sender, eventArgs) {
    };

    calendar.onClientValidationFailed = function (sender, args) {
    };

    calendar.filterGrid = function (value) {
    }

    calendar.fileUploaded = function (sender, args) {
        var info = args.get_fileInfo();

        if (info.SuccessfullyUploaded) {
            calendar.rebindScheduler();
        } else {
            alert('error');
        }
    };
}());