jQuery(function ($) {

    var config = window.$customizer, dirty = false;
    var button = $(`<a class="tm-button js-customizer-open" href="#">Page Builder</a>
        <style>

            .tm-button {
                display: inline-block;
                box-sizing: border-box;
                width: 250px;
                max-width: 100%;
                margin-bottom: 20px;
                padding: 20px 30px;
                border-radius: 2px;
                background: linear-gradient(140deg, #FE67D4, #4956E3);
                box-shadow: inset 0 0 1px 0 rgba(0,0,0,0.5);
                line-height: 10px;
                vertical-align: middle;
                color: #fff !important;
                font-size: 11px;
                font-weight: bold;
                font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
                text-align: center;
                text-decoration: none !important;
                text-transform: uppercase;
                letter-spacing: 2px;
            }

        </style>

        `);

    if (config.context == 'com_content.article') {

        var editor = $('.editor');

        if (!editor.length) {
            editor = $('#editor-xtd-buttons').prev();
        }

        editor.before(button);
    }

    $('.js-customizer-open')
        .attr('href', config.url + '&return=' + encodeURIComponent(window.location.href))
        .on('click', function (e) {
            if (dirty && !window.confirm('You\'ve made changes. Do you really want to leave?')) {
                e.preventDefault();
            }
        });

    $('[name="adminForm"]').on('change', function () {
        dirty = true;
    });

});
