$(
    function() {

        /**
         * Modal form submit code
         */
        $(document)
            .on(
                'beforeSubmit', '#feedback-form', function(e) {
                    var f = this;
                    var form = $(this);
                    var formData = form.serialize();
                    $.ajax(
                        {
                            url: form.attr("action"),
                            type: form.attr("method"),
                            data: formData,
                            success: function(data) {
                                f.reset();
                                $('#feedback-modal')
                                    .modal('hide');
                                $('#success-modal')
                                    .modal('show');
                            },
                            error: function(XHRobj) {
                                var errorObj = XHRobj.responseJSON.error;

                                for(var key in errorObj){
                                    if(errorObj[key]){
                                        form.find('.field-feedback-'+key)
                                            .addClass('has-error')
                                            .children(".help-block-error")
                                            .text(errorObj[key][0]);
                                    }
                                }
                            }
                        }
                    );
                }
            )
            .on(
                'submit', '#feedback-form', function(e) {
                    e.preventDefault();
                }
            );

        /**
         * Contact form submitting
         */
        $(document)
            .on(
                'beforeSubmit', '#contact-form', function(e) {
                    var f = this;
                    var form = $(this);
                    var formData = form.serialize();
                    $.ajax(
                        {
                            url: form.attr("action"),
                            type: form.attr("method"),
                            data: formData,
                            success: function(data) {
                                f.reset();
                                form.replaceWith(data.alert)
                            },
                            error: function() {

                            }
                        }
                    );
                }
            )
            .on(
                'submit', '#contact-form', function(e) {
                    e.preventDefault();
                }
            );

        /**
         * Button UP code
         */
        if ($('#back-to-top').length) {
            var scrollTrigger = 100, // px
                backToTop = function() {
                    var scrollTop = $(window)
                        .scrollTop();
                    if (scrollTop > scrollTrigger) {
                        $('#back-to-top')
                            .addClass('show');
                    } else {
                        $('#back-to-top')
                            .removeClass('show');
                    }
                };
            backToTop();
            $(window)
                .on(
                    'scroll', function() {
                        backToTop();
                    }
                );
            $('#back-to-top')
                .on(
                    'click', function(e) {
                        e.preventDefault();
                        $('html,body')
                            .animate(
                                {
                                    scrollTop: 0
                                }, 700
                            );
                    }
                );
        }
        $('.do-order-but')
            .on(
                'click', function(e) {
                    e.preventDefault();
                    var text_title = $(this).data('modalname');
                    $(".modal-header .modal-title").html(text_title);
                    $(".t_subject").val(text_title);
                }
            );
        $('.but-first')
            .on(
                'click', function(e) {
                    e.preventDefault();
                    $(".btn-first-send").show();
                    $(".btn-second-send").hide();
                }
            );
        $('.but-writeus')
            .on(
                'click', function(e) {
                    e.preventDefault();
                    $(".btn-first-send").hide();
                    $(".btn-second-send").show();
                }
            );

        $(".modal-link")
            .on('click', function(){
                var modaltitle = $(this).data('modalname');
                $(".modal-title").html(modaltitle);
                $(".t_subject").val(modaltitle);
            });
    }
);