$(document).ready(function(){

    $('#facebook-post').on('click', function(e){
        e.preventDefault();
        var $this = $(this),
            online_url = $this.data('url'),
            params = {
                online_url: online_url,
                catdom_id: window.global_domain_id
            };


        var facebook_dlg = BootstrapDialog.show({
            size: BootstrapDialog.SIZE_WIDE,
            closable: true,
            closeByBackdrop: false,

            title: 'Facebook',
            message: $('<div id="facebook"></div>').html('Loading ...')
        });

        $.ajax({
            url: '/amodules/social-media-actions/ajax/get_facebook_url_info.php',
            data: params,
            type: 'post',
            dataType: 'json',
            cache: false,
            success: function (r) {
                if(r.status === 1) {
                    var $facebook = facebook_dlg.getModalBody();

                    $facebook.empty().html(r.form);

                    $('#facebook-submit', $facebook).on('click', function(e){
                        e.preventDefault();

                        var $facebook_form = $('#facebook-form'),
                            form_params = $facebook_form.serialize(),
                            form_action = $facebook_form.attr('action'),
                            $facebook_result = $('#facebook-result', $facebook);

                        $.ajax({
                            url: form_action,
                            type: 'post',
                            data: form_params,
                            dataType: 'json',
                            cache: false,
                            success: function (r) {
                                if(r.status === 1) {
                                    //facebook_dlg.close();
                                    $facebook_result.removeClass('alert-danger').addClass('alert-success').html('<pre>' + r.post + '</pre>').removeClass('hidden');

                                }
                                else {
                                    $facebook_result.removeClass('alert-success').addClass('alert-danger').html('<span>' + r.error + '</span>').removeClass('hidden');
                                }

                            }
                        });
                    });
                }
                else {
                    console.log('error');
                }
            }
        });


    });

});