window.Parsley.setLocale(unicalendar.locale);

jQuery( document ).ready( function( $ ) {
    'use strict';

    $.datetimepicker.setLocale(unicalendar.locale);

    //
    $(".uni_calendar_min_max_time").datetimepicker({
        datepicker:false,
        lazyInit:true,
        dayOfWeekStart:1,
        format:'H:i',
        step: 15,
        closeOnDateSelect:true
    });

    //
    $(".uni_calendar_date_time").datetimepicker({
        datepicker:true,
        lazyInit:true,
        dayOfWeekStart:1,
        format:'Y-m-d H:i',
        step: 15,
        defaultTime:'08:00'
    });

    //
    $(".uni_calendar_date").datetimepicker({
        datepicker:true,
        lazyInit:true,
        format:'Y-m-d',
        closeOnDateSelect:true,
        timepicker:false
    });

    // Add color picker
    $('.uni-calendar-colour-field').wpColorPicker();

    //
    $(document).on("change", "#uni_input_calendar_view", function(){
        var this_value = $(this).val();
        if ( this_value == 'uniCustomView' ) {
            $(".uni_calendar_view_additional").show();
        } else {
            $(".uni_calendar_view_additional").hide();
        }
    });

    //
    $("body").on("click", ".uni_calendar_submit", function(e){

        var submit_button = $(this),
            this_form = submit_button.closest("form"),
            action = this_form.attr('action'),
            ajax_msg_container = $("#ajax-messages"),
            dataToSend = this_form.serialize();

        if ( action == 'uni_add_calendar' ) {
            var container = $("#uni-calendar-wrapper");
        } else {
            var container = this_form;
        }

        var form_valid = this_form.parsley({excluded: '[disabled]'}).validate();

            if ( form_valid ) {
                dataToSend = this_form.serialize();

			    $.ajax({
				    type: 'post',
	        	    url: action,
	        	    data: dataToSend + '&cheaters_always_disable_js=' + 'true_bro',
	        	    dataType: 'json',
	        	    beforeSend: function(){
	        	        ajax_msg_container.empty();
	        	        container.block({
	        	            message: null,
                            overlayCSS: { background: '#fff', opacity: 0.6 }
                        });
	        	    },
	        	    success: function(response) {
	        		    if ( response.status == "success" ) {

	        		        $(".ui-dialog-content").dialog("close");
                            container.unblock();
                            if ( response.message.length > 0 ) {
                                ajax_msg_container.html(response.message);
                            }

                            if ( response.redirect.length > 0 ) {
						        setTimeout(function() {
							        window.location.replace( response.redirect );
						        }, 500);
                            }

                            if ( response.refetch.length > 0 ) {
                                $("#uni-calendar").fullCalendar('refetchEvents');
                            }

	        		    } else if ( response.status == "error" ) {
                            container.unblock();
                            if ( $(".ui-dialog-content").length > 0 ) {
                                $(".ui-dialog-content").dialog("close");
                            }
                            if ( response.message.length > 0 ) {
                                ajax_msg_container.html(response.message);
                            }
	        		    }
	        	    },
	        	    error:function(response){
	        	        container.unblock();
                        if ( $(".ui-dialog-content").length > 0 ) {
                            $(".ui-dialog-content").dialog("close");
                        }
	        	        ajax_msg_container.html(response.message);
	        	    }
	            });
            }
            return false;
    });

    //
    $("body").on("click", ".uni_calendar_ajax_link", function(e){

	    e.preventDefault();
        var $this_link = $(this),
            container = $(this).parent(),
            ajax_msg_container = $("#ajax-messages");

                var dataToSend = $this_link.data();
                    dataToSend.cheaters_always_disable_js = 'true_bro';

			    $.ajax({
				    type: 'post',
	        	    url: ajaxurl,
	        	    data: dataToSend,
	        	    dataType: 'json',
	        	    beforeSend: function(){
	        	        ajax_msg_container.empty();
	        	        container.block({
	        	            message: null,
                            overlayCSS: { background: '#fff', opacity: 0.6 }
                        });
	        	    },
	        	    success: function(response) {
	        		    if ( response.status == "success" ) {

                            $(".ui-dialog-content").dialog("close");
                            container.unblock();
                            if ( response.message.length > 0 ) {
                                ajax_msg_container.html(response.message);
                            }

                            if ( response.redirect.length > 0 ) {
						        setTimeout(function() {
							        window.location.replace( response.redirect );
						        }, 500);
                            }

                            if ( response.refetch.length > 0 ) {
                                $("#uni-calendar").fullCalendar('refetchEvents');
                            }

	        		    } else if ( response.status == "error" ) {
                            container.unblock();
                            if ( $(".ui-dialog-content").length > 0 ) {
                                $(".ui-dialog-content").dialog("close");
                            }
                            if ( response.message.length > 0 ) {
                                ajax_msg_container.html(response.message);
                            }
	        		    }
	        	    },
	        	    error:function(response){
	        	        container.unblock();
                        if ( $(".ui-dialog-content").length > 0 ) {
                            $(".ui-dialog-content").dialog("close");
                        }
	        	        ajax_msg_container.html(response.message);
	        	    }
	            });
            return false;
    });

});