$(document).ready(function() {


    function aviseme(){

      $('.ftr-main').on('click', '.js-btn-aviseme', function(){
            $('#aviseme').remove();

            var
                data = {},
                $this = $(this);

            data['product_id']   = $this.data('product-id');
            data['product_name'] = $this.data('product-name');

            $.ajax({
                url: 'index.php?route=product/avise_me&display=true',
                dataType: 'html',
                type: 'get',
                data: data,
                beforeSend: function() {
                    $('.ftr-loading-bar').removeClass('ftr-none');
                },
                complete: function() {
                    $('.ftr-loading-bar').addClass('ftr-none');
                },
                success: function(html) {
                    $('.js-content-aviseme').append(html);
                }
            });
        });

        $('.js-content-aviseme').on('click','.js-btn-aviseme-send', function(){
            $('.ftr-field__error, .ftr-js-error-recaptcha').remove();
            $('.ftr-field').removeClass('error');

            var data = {};
            data['product_id'] = $('.js-btn-aviseme-send').data('product-id');
            data['name']       = $("input[type=text][name=name]").val();
            data['email']      = $("input[type=text][name=email]").val();
            data['captcha']    = (typeof $("input[type=text][name=captcha]").val() == "undefined") ? false : $("input[type=text][name=captcha]").val();
            data['g-recaptcha-response'] = ($('.g-recaptcha').length > 0) ? grecaptcha.getResponse() : false ;

            $.ajax({
                url: 'index.php?route=product/avise_me/send',
                dataType: 'json',
                type: 'post',
                data: data,
                beforeSend: function() {
                    $('.js-btn-aviseme-send').prop('disabled', true);
                },
                complete: function() {
                    $('.js-btn-aviseme-send').prop('disabled', false);
                },
                success: function(json) {
                    if(json['error']){
                        $.each(json['error'], function(index, value){
                            $("input[name="+index+"]").closest('.ftr-field').addClass('error').append('<span class="ftr-field__error">' + value + '</span>');
                        });
                        if(json['error']['google_recaptcha']){
                            $('#g-recaptcha').after('<span class="ftr-text--s ftr-js-error-recaptcha" style="color: rgb(213,0,0);">' + json['error']['google_recaptcha'] + '</span>');
                        }
                    }
                    if(json['success']){
                        notificationMsg(json['success'], 'success');
                        $('#aviseme').removeAttr('style');
                        $('.ftr-js-swipe-content-overlay').fadeOut(300);
                    }
                }
            });
        });

    }


    window.initAviseme = function(){
        aviseme();
    }

    initAviseme();

});
