﻿// ****************
// INSYS CMS Confirm plugin
// ****************


jQuery.fn.confirm = function(options) {
    return this.each(function() {

        var defaults = {
            text: "Are you sure?",
            yes: "Yes",
            no: "No"
        }

        var config = $.extend(defaults, options || {})

        var question = document.createElement("div");
        $(question).css("display", "none");
        $(question).css("cursor", "default");
        $(question).attr("class", "confirmPopup");
        $(question).append("<h3>" + config.text + "</h3><input type='button' class='btnConfirmYes' value='" + config.yes + "' /><input type='button' class='btnConfirmNo' value='" + config.no + "' />");
        $("body").append(question);

        var triggeringControl = $(this);
        var href = $(this).attr("href");

        $(this).click(function(e) {
            e.preventDefault();
            $.blockUI({ message: $(question), css: { width: '275px'} });
            $(question).find("input").unbind();
            $(question).find(".btnConfirmYes").click(function() {
                $.unblockUI();
                if (href != null) {
                    document.location = href;
                } else {
                    triggeringControl.unbind("click");
                    triggeringControl.click();
                    triggeringControl.parents("form").submit();
                }
            });
            $(question).find(".btnConfirmNo").click(function() {
                $.unblockUI();
            });
        });
    });
}



