﻿(function () {
    jQuery.fn.dialogfeedback = function (msgtext, options) {
        var opts = jQuery.extend({
            type: "info",
            title: "Information",
            infoIcon: "ui-icon-info",
            infoClass: "ui-state-highlight ui-corner-all",
            errorIcon: "ui-icon-alert",
            errorClass: "ui-state-error ui-corner-all",
            duration: 10000,
            navigateUrl: "",
            navigateUrlName: ""
        }, options);

        var divclass = "";  // Class for container div - error or info . 
        var iconclass = "";  // Icon class- alert or info. 

        if (!msgtext) msgtext = "Feedback message";

        return this.each(function () {
            // handle to the element(s):  
            var me = $(this);
            if (opts.type == "error") {
                divclass = opts.errorClass;
                iconclass = opts.errorIcon;
            }
            else {
                divclass = opts.infoClass;
                iconclass = opts.infoIcon;
            }

            // if the icon class starts with "ui-" assume it's a valid Jquery UI class:  
            if (iconclass.substr(0, 3) == "ui-") iconclass = "ui-icon " + iconclass;

            if (opts.navigateUrlName != "") {
                msgtext += " <br /> Redirecting to " + opts.navigateUrlName;
            }
            // Create DOM elements of div, para (for text) and span (for image) and insert  after current DOM object: 
            var msg = $('<div></div>').css({ padding: "3px", width: "700px", height: "200px" }).addClass(divclass);
            msg.append('<p><span style="float:left;" class="' + iconclass + '"></span>' + msgtext + '</p>');

            // Insert after this DOM element: 
            me.after(msg);


            // create the modal dialog and close after 'duration' seconds and redirect if necessary 
            msg.dialog({
                dialogClass: divclass,
                title: opts.title,
                modal: true,
                open: function (event, ui) { setTimeout(function () { $(msg).dialog('close'); }, opts.duration); },
                close: function (event, ui) {
                 //   alert(opts.navigateUrl);
                    if (opts.navigateUrl != "") {
                        var url = opts.navigateUrl;
                        $(msg).remove();
                        window.location.replace(url);
                    }
                    else {
                        $(msg).remove();
                    }
                }

            });
        });
    };
})(jQuery); 

