// Generated by CoffeeScript 1.10.0
(function () {
    jQuery(document).ready(function ($) {
        var activated, container, currentData, div, embeddedData, embeddedMap, i, len, main_form, oldData, pdfaid, security, setShortcode, span, span2, span3, span4, span5;
        $('#wpfx_form, #wpfx_form2').on('change upddisabled', function () {
            var allowed, el;
            if (!$('#frm-bg').data('limitdropdowns')) {
                return true;
            }
            allowed = $(this).find(':selected').data('allowedlayouts');
            allowed || (allowed = []);
            allowed.push(-6);
            el = $('#wpfx_layout');
            if ($(this).attr('id') === 'wpfx_form2') {
                el = $('#wpfx_layout2');
            }
            var active_layout = false;
            el.find('option').each(function () {
                var a, b, i, id, len, opt;
                opt = $(this);
                for (i = 0, len = allowed.length; i < len; i++) {
                    id = allowed[i];
                    a = parseInt(id) + 9;
                    b = parseInt(opt.attr('value'));
                    if (a === b) {
                        opt.prop('disabled', false);
                        active_layout = b;
                        return true;
                    }
                }
                opt.prop('disabled', true);
                return true;
            });
            if (el.find(':selected').prop('disabled') && active_layout) {
                el.val(active_layout).trigger('change');
            } else if (el.find(':selected').val() == 3) {
                el.val('3').trigger('change');
            }
            return true;
        });
        $('#wpfx_form, #wpfx_form2').trigger('upddisabled');
        $('.embedded_options_field_template').each(function () {
            $(this).find('.button_del').click(function () {
                if (confirm('Do you really what to delete this embedded form field map?')) {
                    $(this).parent().remove();
                }
                return false;
            });
            $(this).find('select:first').change(function () {
                var currentFormId, field, fields, i, len, opt, results, sel2;
                sel2 = $(this).parent().find('select:last');
                currentFormId = $(this).val();
                sel2.find('option').not(sel2.find('option:first')).remove();
                fields = window.fpropdfEmbeddedFieldData.fields[currentFormId + ''];
                if (fields) {
                    results = [];
                    for (i = 0, len = fields.length; i < len; i++) {
                        field = fields[i];
                        opt = $('<option />');
                        opt.attr('value', field.id);
                        opt.html(field.title);
                        results.push(sel2.append(opt));
                    }
                    return results;
                }
            });
            return true;
        });
        embeddedMap = function (embeddedData) {
            var currentFieldId, currentFormId, tpl;
            tpl = $('.embedded_options_field_template').clone(true);
            tpl.appendTo('.embedded_options_field_container');
            tpl.find('[data-name]').each(function () {
                var name;
                name = $(this).attr('data-name');
                name = name.replace('NUMBER', tpl.index() - 1);
                return $(this).attr('name', name);
            });
            tpl.removeClass('embedded_options_field_template');
            if (embeddedData) {
                currentFormId = embeddedData.form;
                if (currentFormId) {
                    tpl.find('select:first option').removeAttr('selected');
                    tpl.find('select:first option[value="' + currentFormId + '"]').attr('selected', 'selected');
                    tpl.find('select:first').trigger('change');
                }
                currentFieldId = embeddedData.field;
                if (currentFormId) {
                    tpl.find('select:last option').removeAttr('selected');
                    tpl.find('select:last option[value="' + currentFieldId + '"]').attr('selected', 'selected');
                    return tpl.find('select:last').trigger('change');
                }
            }
        };
        $('#fpropdf_add_embedded_form').click(function () {
            embeddedMap();
            return false;
        });
        if (window.fpropdfEmbeddedFieldData) {
            currentData = window.fpropdfEmbeddedFieldData.currentData;
            if (currentData) {
                if (currentData.length) {
                    for (i = 0, len = currentData.length; i < len; i++) {
                        embeddedData = currentData[i];
                        if (embeddedData.field && embeddedData.form) {
                            if (embeddedData.field.length && embeddedData.form.length) {
                                embeddedMap(embeddedData);
                            }
                        }
                    }
                }
            }
        }
        $('#fpropdf_add_embedded_form').click();
        $('#use-second-layout').change(function () {
            $('.hidden-use-second').toggle();
            return true;
        });
        container = $('#wpbody-content .parent ._first');
        div = $('<div />');
        div.addClass('formidable-shortcode-container').html('Shortcode for Download Link or Button&nbsp;').insertAfter(container).hide();
        span = $('<input />');
        span.appendTo(div);
        activated = parseInt($('#frm-bg').data('activated'));
        security = parseInt($('#frm-bg').data('security'));
        pdfaid = parseInt($('#frm-bg').data('pdfaid'));
        if (activated) {
            div.append('Shortcode for Automatic Download');
        }
        span2 = $('<input />');
        if (activated) {
            span2.appendTo(div);
        }
        if (activated && pdfaid) {
            div.append('Shortcode for Downloading DOCX');
        }
        span4 = $('<input />');
        if (activated && pdfaid) {
            span4.appendTo(div);
        }
        span3 = $('<span />');
        if (activated) {
            span3.appendTo(div);
        }
        span5 = $('<span />');
        if (activated && pdfaid) {
            span5.appendTo(div);
        }
        div.find('input').focus(function () {
            this.select();
            return true;
        });
        main_form = container.find("form");
        oldData = false;
        setInterval(function () {
            var data;
            data = main_form.serialize();
            if (oldData !== data) {
                setShortcode();
            }
            return oldData = data;
        }, 10);
        setShortcode = function () {
            var dataset, dataset2, datasetOrig, datasetOrig2, form, form2, layout, layout2, second, secondUrl, securityPart;
            form = $('#wpfx_form').val();
            form2 = $('#wpfx_form2').val();
            dataset = $('#wpfx_dataset').val();
            dataset2 = $('#wpfx_dataset2').val();
            layout = $('#wpfx_layout').val();
            layout2 = $('#wpfx_layout2').val();
            dataset || (dataset = '-3');
            dataset2 || (dataset2 = '-3');
            if (dataset === "Loading datasets...") {
                dataset = '-3';
            }
            if (dataset2 === "Loading datasets...") {
                dataset2 = '-3';
            }
            datasetOrig = "";
            datasetOrig2 = "";
            if (layout !== '3' && form) {
                div.slideDown();
                $('#tr-export').slideDown();
            } else {
                div.slideUp();
                $('#tr-export').slideUp();
            }
            if (dataset !== '-3') {
                datasetOrig = dataset;
                dataset = " dataset=\"" + dataset + "\"";
            } else {
                dataset = "";
            }
            if (dataset2 !== '-3') {
                datasetOrig2 = dataset2;
                dataset2 = " dataset2=\"" + dataset2 + "\"";
            } else {
                dataset2 = "";
            }
            second = "";
            secondUrl = "";
            if ($('#use-second-layout').is(':checked')) {
                second = " form2=\"" + form2 + "\"" + dataset2 + " layout2=\"" + layout2 + "\"";
                secondUrl = "&form2=" + form2 + "&dataset2=" + datasetOrig2 + "&layout2=" + layout2;
            }
            span.val("[formidable-download form=\"" + form + "\"" + dataset + " layout=\"" + layout + "\"" + second + "]");
            span4.val("[formidable-download form=\"" + form + "\"" + dataset + " layout=\"" + layout + "\"" + second + " format=\"docx\"]");
            span2.val("[formidable-download form=\"" + form + "\"" + dataset + " layout=\"" + layout + "\"" + second + " download=\"auto\"]");
            span3.html('');
            span5.html('');
            securityPart = '';
            if (security) {
                securityPart = '&redirect_to_secure=1';
            }
            if (dataset.length) {
                span3.html("<a href='" + window.ajaxurl + "?action=wpfx_generate&form=" + form + "&layout=" + layout + "&dataset=" + datasetOrig + secondUrl + "&format=pdf&inline=1" + securityPart + "' target='_blank' class='button'>Preview (PDF)</a>");
                if (pdfaid) {
                    span5.html("&nbsp; <a href='" + window.ajaxurl + "?action=wpfx_generate&form=" + form + "&layout=" + layout + "&dataset=" + datasetOrig + secondUrl + "&format=docx&inline=1" + securityPart + "' target='_blank' class='button'>Preview (DOCX)</a>");
                }
            }
            $('#main-export-btn').attr('href', window.ajaxurl + "?action=wpfx_generate&form=" + form + "&layout=" + layout + "&dataset=" + datasetOrig + secondUrl + "&format=pdf&inline=1" + securityPart);
            return $('#main-export-btn-docx').attr('href', window.ajaxurl + "?action=wpfx_generate&form=" + form + "&layout=" + layout + "&dataset=" + datasetOrig + secondUrl + "&format=docx&inline=1" + securityPart);
        };
        container.find('select, input[type="checkbox"]').change(setShortcode);
        if (false) {
            setInterval(function () {
                var bottom;
                bottom = $('.layout_builder');
                if (bottom.is(':visible')) {
                    return div.show();
                }
            }, 10);
        }
        $(document).on('change', '#wpfx_layout_form table table select.with-preview', function () {
            var base, img, select, src;
            select = $(this);
            select.parent().find('.custom-preview-img').remove();
            img = $('<img />');
            img.addClass('custom-preview-img');
            img.insertAfter(select);
            base = select.data("base");
            src = select.find('option:selected').data("src");
            src = "admin-ajax.php?page=fpdf&action=wpfx_preview_pdf&_wpnonce=" + window.fpropdf_ajax_preview_nonce + "&file=" + encodeURIComponent($('#wpfx_clfile').val()) + "&field=" + encodeURIComponent(select.val()) + "&form=" + encodeURIComponent($('#wpfx_clform').val()) + "&ext=.png";
            img.wrap("<a href='" + src + "&TB_iframe=true' target='_blank' class='thickbox' />");
            img.data('src', src);
            img.load(function () {
                var a, href;
                $(this).css('max-width', 'initial');
                a = $(this).parent();
                href = a.attr('href');
                href += "&width=" + ($(this).width() - (626 - 597)) + "&height=" + ($(this).height() - (200 - 188));
                a.attr('href', href);
                $(this).css('max-width', '');
                $(this).removeClass('custom-preview-img-loading').addClass('custom-preview-img-loaded');
                fpro2pdf_load_previews();
            });
            img.error(function () {
                $(this).removeClass('custom-preview-img-loading').addClass('custom-preview-img-loaded');
                fpro2pdf_load_previews();
            });
            fpro2pdf_load_previews();
        });

        function fpro2pdf_load_previews() {
            if ($('#wpfx_layout_form table table .custom-preview-img-loading').length === 0) {
                var img = $('#wpfx_layout_form table table .custom-preview-img').not(".custom-preview-img-loaded").first();
                img.addClass('custom-preview-img-loading');
                img.attr('src', img.data("src"));
            }
        }

        $(document).on('click', '.upl-new-pdf', function () {
            var form, overlay;
            overlay = $('<div />');
            overlay.addClass('fpropdf-overlay').appendTo('body');
            form = $('<form method="post" enctype="multipart/form-data" />');
            form.append('Select a file: <input type="hidden" name="action" value="upload-pdf-file" /> <input accept="application/pdf" required="required" type="file" name="upload-pdf" />').append('<input type="hidden" name="_wpnonce" value="' + window.fpropdf_upload_pdf_nonce + '" />').append('<input type="submit" class="button-primary" value="Upload" /> &nbsp; <a href="#" class="button cancel">Cancel</a>').appendTo(overlay).submit(function () {
                var btn;
                btn = $(this).find('.button-primary');
                setTimeout(function () {
                    return btn.html('Please wait...').val('Please wait...').prop('disabled', true);
                }, 50);
                return true;
            }).wrapInner('<div />');
            form.find('.cancel').click(function () {
                overlay.remove();
                return false;
            });
            return false;
        });
        $(document).on('click', 'input.remove-pdf', function () {
            var btn, fname, fnameEncoded, html, option;
            option = $('#wpfx_clfile option:selected');
            btn = $(this);
            fnameEncoded = option.attr('value');
            html = btn.val();
            fname = option.text();
            if (!confirm("Are you sure you want to delete " + fname + " ?")) {
                return false;
            }
            if (!confirm("This action cannot be undone. The file " + fname + " will be lost. Do you really want to delete it?")) {
                return false;
            }
            btn.prop('disabled', true).html('Please wait...');
            $.ajax({
                url: window.ajaxurl,
                data: {
                    file: fnameEncoded,
                    action: 'fpropdf_remove_pdf',
                    '_wpnonce': window.fpropdf_wpfx_remove_pdf_nonce
                },
                method: 'POST',
                success: function () {
                    option.remove();
                    alert(fname + " has been removed.");
                    return btn.prop('disabled', false).val(html);
                }
            });
            return false;
        });
        return $(document).on('click', '.fpropdf-activate', function () {
            var form, overlay;
            overlay = $('<div />');
            overlay.addClass('fpropdf-overlay').appendTo('body');
            form = $('<form method="post" enctype="multipart/form-data" />');
            form.append('Your activation code: <br /><br /> <input type="hidden" name="action" value="activate-fpropdf" /> <input type="text" name="activation-code" value="" required="required" placeholder="Paste your activation code here..." style="width: 300px; text-align: center;" /><br /><br />').append('<input type="submit" class="button-primary" value="Activate" /> &nbsp; <a href="#" class="button cancel">Cancel</a>').append('<br /><br /><a href="http://formidablepro2pdf.com/" target="_blank">How can I get an activation code?</a>').appendTo(overlay).submit(function () {
                var btn;
                btn = $(this).find('.button-primary');
                setTimeout(function () {
                    return btn.html('Please wait...').val('Please wait...').prop('disabled', true);
                }, 50);
                return true;
            }).wrapInner('<div />');
            form.find('.cancel').click(function () {
                overlay.remove();
                return false;
            });
            form.find('input[type="text"]').focus();
            return false;
        });
    });

}).call(this);
