jQuery(document).ready(function () {

    jQuery('.ec-create-store-button').on('click', function () {

        if (ecwidParams.isWL) {
            location.href = ecwidParams.registerLink;
            return;
        }

        jQuery('.ec-create-store-button').addClass('btn--loading');
        jQuery('.ec-connect-store').addClass('disabled');

        var data = {
            action: 'ecwid_create_store',
            _ajax_nonce: ecwidParams._ajax_nonce
        };

        jQuery.ajax({
            'url': ajaxurl,
            'data': data,
            'success': function (result) {
                jQuery('.ec-create-store-note').hide();
                jQuery('.ec-create-store-success-note').show();

                setTimeout(function () {
                    location.href = "admin.php?page=ec-store&ec-store-page=complete-registration";
                }, 1000);
            },
            'error': function (error) {
                if (error.status == '409') {
                    location.href = 'admin-post.php?action=ec_connect';
                } else {
                    location.href = ecwidParams.registerLink;
                }
            }
        });
    });

});