/*!
 * jquery.confirm
 *
 * @version 2.3.1
 *
 * @modifiedBy Matthew Muro
 * @author My C-Labs
 * @author Matthieu Napoli <matthieu@mnapoli.fr>
 * @author Russel Vela
 * @author Marcus Schwarz <msspamfang@gmx.de>
 *
 * @license MIT
 * @url http://myclabs.github.io/jquery.confirm/
 */
(function ($) {

    /**
     * Confirm a link or a button
     * @param [options] {{title, text, confirm, cancel, confirmButton, cancelButton, post, confirmButtonClass}}
     */
    $.fn.confirm = function (options) {
        if (typeof options === 'undefined') {
            options = {};
        }

        this.click(function (e) {
            e.preventDefault();

            var newOptions = $.extend({
                button: $(this)
            }, options);

            $.confirm(newOptions, e);
        });

        return this;
    };

    /**
     * Show a confirmation dialog
     * @param [options] {{title, text, confirm, cancel, confirmButton, cancelButton, post, confirmButtonClass}}
     * @param [e] {Event}
     */
    $.confirm = function (options, e) {
        // Do nothing when active confirm modal.
        if ($('.confirmation-modal').length > 0) {
            return;
        }

        // Parse options defined with "data-" attributes
        var dataOptions = {};
        if (options.button) {
            var dataOptionsMapping = {
                'title': 'title',
                'text': 'text',
                'confirm-button': 'confirmButton',
                'cancel-button': 'cancelButton',
            };
            $.each(dataOptionsMapping, function(attributeName, optionName) {
                var value = options.button.data(attributeName);
                if (value) {
                    dataOptions[optionName] = value;
                }
            });
        }

        // Default options
        var settings = $.extend({}, $.confirm.options, {
            confirm: function () {
                var url = e && (('string' === typeof e && e) || (e.currentTarget && e.currentTarget.attributes.href.value));
                if (url) {
                    if (options.post) {
                        var form = $('<form method="post" class="hide" action="' + url + '"></form>');
                        $("body").append(form);
                        form.submit();
                    } else {
                        window.location = url;
                    }
                }
            },
            cancel: function () {
            },
            button: null
        }, dataOptions, options);

        // Modal
        var modalHeader = '';
        if (settings.title !== '') {
            modalHeader =
                '<h1>' + settings.title + '</h4>';
        }

        var modalHTML =
                '<div id="vfb-confirm-overlay">' +
                    '<div id="vfb-confirm-box">' +
                        modalHeader +
                        '<p>' + settings.text + '</p>' +
                        '<div id="vfb-confirm-buttons">' +
                            '<button class="vfb-btn-confirm" type="button">' +
                                settings.confirmButton +
                            '</button>' +
                            '<button class="vfb-btn-confirm-cancel" type="button">' +
                                settings.cancelButton +
                            '</button>' +
                        '</div>' +
                    '</div>' +
                '</div>';

        var modal = $(modalHTML);

        modal.find( '.vfb-btn-confirm' ).click(function () {
            settings.confirm(settings.button);
            modal.remove();
        });
        modal.find( '.vfb-btn-confirm-cancel' ).click(function () {
            settings.cancel(settings.button);
            modal.remove();
        });

        // Show the modal
        $( 'body' ).append( modal );
    };

    /**
     * Globally definable rules
     */
    $.confirm.options = {
        text: 'Are you sure?',
        title: '',
        confirmButton: 'Yes',
        cancelButton: 'Cancel',
        post: false
    };
})(jQuery);
