﻿/**
* jQuery Easy Confirm Dialog plugin 1.1
*
* Copyright (c) 2010 Emil Janitzek (http://projectshadowlight.org)
* Based on Confirm 1.3 by Nadia Alramli (http://nadiana.com/)
*
* Samples and instructions at: 
* http://projectshadowlight.org/jquery-easy-confirm-dialog/
*
* This script is free software: you can redistribute it and/or modify it 
* under the terms of the GNU General Public License as published by the Free 
* Software Foundation, either version 3 of the License, or (at your option)
* any later version.
*/

(function ($) {
    $.confirmdialog = {};
    $.confirmdialog.showModal = function (callAfterConfirm, options) {
        var options = jQuery.extend({
            title: 'Save changes?',
            longText: 'Save these changes back to the live database?',
            closeText: 'Close'
        }, options);

        var dialog = $('<div class="dialog confirm">' + options.longText + '</div>');
        var buttons = {};
        buttons["Yes"] = function () {
            $(dialog).dialog("close");
            callAfterConfirm();
        };

        buttons["No"] = function () {
            $(dialog).dialog("close");
        };

        $(dialog).dialog({ autoOpen: false,
            resizable: false,
            draggable: true,
            closeOnEscape: true,
            width: 'auto',
            height: 120,
            minHeight: 120,
            maxHeight: 400,
            buttons: buttons,
            title: options.title,
            closeText: options.closeText,
            modal: true
        });

        $(dialog).dialog('open');
    }
})(jQuery);

(function ($) {
    $.confirmclick = {};
    $.confirmclick.locales = {};
    $.confirmclick.locales.enUS = {
        title: 'Are you sure?',
        text: 'Save these changes back to the live system now?',
        button: ['No', 'Yes'],
        closeText: 'close'
    };
    $.confirmclick.locales.svSE = {
        title: 'Är du säker?',
        text: 'Är du säker på att du vill genomföra denna åtgärden?',
        button: ['Avbryt', 'Bekräfta'],
        closeText: 'stäng'
    };

    $.fn.confirmclick = function (callAfterConfirm, options) {
        var options = jQuery.extend({
            eventType: 'click',
            icon: 'help'
        }, options);

        var locale = jQuery.extend({}, $.confirmclick.locales.enUS, options.locale);

        // Shortcut to eventType.
        var type = options.eventType;

        return this.each(function (method) {
            var target = this;
            var $target = jQuery(target);

            if ($target.attr('title').length > 0)
                locale.text = $target.attr('title');

            var dialog = (options.dialog == undefined || typeof (options.dialog) != 'object') ?
                     $('<div class="dialog confirm">' + locale.text + '</div>') :
                     options.dialog;

            var buttons = {};
            buttons[locale.button[1]] = function () {

                // Close dialog
                $(dialog).dialog("close");

                callAfterConfirm();
            };

            buttons[locale.button[0]] = function () {
                $(dialog).dialog("close");
            };

            $(dialog).dialog({ autoOpen: false,
                resizable: false,
                draggable: true,
                closeOnEscape: true,
                width: 'auto',
                height: 120,
                minHeight: 120,
                maxHeight: 200,
                buttons: buttons,
                title: locale.title,
                closeText: locale.closeText,
                modal: true
            });

            // Handler that will override all other actions
            var handler = function (event) {
                $(dialog).dialog('open');
                event.stopImmediatePropagation();
                event.preventDefault();
                return false;
            }

            var init = function () {
                $target.bind(type, handler);
            }

            init();

        });

    }
})(jQuery);
