﻿// jQuery Alert Dialogs Plugin
//
// Version 1.0
//
// Cory S.N. LaViska
// A Beautiful Site (http://abeautifulsite.net/)
// 29 December 2008
//
// Visit http://abeautifulsite.net/notebook/87 for more information
//
// Usage:
//		jAlert( message, [title, callback] )
//		jConfirm( message, [title, callback] )
//		jPrompt( message, [value, title, callback] )
// 
// History:
//
//		1.00 - Released (29 December 2008)
//
// License:
// 
//		This plugin is licensed under the GNU General Public License: http://www.gnu.org/licenses/gpl.html
//
(function ($) {

    $.alerts = {

        // These properties can be read/written by accessing $.alerts.propertyName from your scripts at any time

        verticalOffset: -75,                // vertical offset of the dialog from center screen, in pixels
        horizontalOffset: 0,                // horizontal offset of the dialog from center screen, in pixels/
        repositionOnResize: true,           // re-centers the dialog on window resize
        overlayOpacity: .1,                // transparency level of overlay
        overlayColor: '#000',               // base color of overlay
        draggable: true,                    // make the dialogs draggable (requires UI Draggables plugin)
        okButton: '&nbsp;Ok&nbsp;',         // text for the OK button
        cancelButton: '&nbsp;Cancel&nbsp;', // text for the Cancel button
        dialogClass: null,                  // if specified, this class will be applied to all dialogs

        // Public methods

        alert: function (message, title, callback) {
            if (title == null) title = 'Alert';
            $.alerts._show(title, message, null, 'alert', function (result) {
                if (callback) callback(result);
            });
        },

        ajaxconfirm: function (message, title, url, configParamater, callback) {
            if (title == null) title = 'Confirm';
            $.alerts._show(title, message, null, 'ajaxconfirm', callback, url, configParamater);
        },

        ajaxstatistics: function (message, title, url, configParamater, callback) {
            if (title == null) title = 'Statistics';
            $.alerts._show(title, message, null, 'ajaxstatistics', callback, url, configParamater);
        },

        confirm: function (message, title, callback) {
            if (title == null) title = 'Confirm';
            $.alerts._show(title, message, null, 'confirm', function (result) {
                if (callback) callback(result);
            });
        },

        prompt: function (message, value, title, callback) {
            if (title == null) title = 'Prompt';
            $.alerts._show(title, message, value, 'prompt', function (result) {
                if (callback) callback(result);
            });
        },

        showloading: function (IsRemoveBG, MessageTemp) {
            if (IsRemoveBG) {
                $.alerts.hideloading();
                $.alerts._overlay('show');
            } else {
                $("#popup_loadingcontainer").remove();
            }
            $("BODY").append(
			  '<div id="popup_loadingcontainer">' +
			    '<div id="popup_content">' +
			      '<div id="popup_loadingmessage"></div>' +
				'</div>' +
			  '</div>');

            if ($.alerts.dialogClass) $("#popup_loadingcontainer").addClass($.alerts.dialogClass);
            // IE6 Fix
            var pos = ($.browser.msie && parseInt($.browser.version) <= 6) ? 'absolute' : 'fixed';

            $("#popup_loadingcontainer").css({
                position: pos,
                zIndex: 99999,
                padding: 0,
                margin: 0
            });
            $("#popup_content").addClass("alert");
            if (MessageTemp) {
                $("#popup_loadingmessage").html("<table><tr><td><img src=\"/Images/Image24.gif\" alt=\"\" /></td><td>" + MessageTemp + "</td></tr></table>");
            } else {
                $("#popup_loadingmessage").html("<table><tr><td><img src=\"/Images/Image24.gif\" alt=\"\" /></td><td>Please wait...</td></tr></table>");
            }
            
            $("#popup_loadingcontainer").css({
                minWidth: $("#popup_loadingcontainer").outerWidth(),
                maxWidth: $("#popup_loadingcontainer").outerWidth()
            });
            $.alerts._loadingreposition();
            $.alerts._maintainPosition(true);
            // Make draggable
            if ($.alerts.draggable) {
                try {
                    $("#popup_loadingcontainer").draggable({ handle: $("#popup_content") });
                    $("#popup_content").css({ cursor: 'move' });
                } catch (e) { /* requires jQuery UI draggables */ }
            }
        },

        hideloading: function (IsRemoveBG) {
            $("#popup_loadingcontainer").remove();
            if (IsRemoveBG) {
                $.alerts._overlay('hide');
                $.alerts._maintainPosition(false);
            }
        },
        // Private methods

        _show: function (title, msg, value, type, callback, url, configParamater) {
            $.alerts._hide();
            $.alerts._overlay('show');
            $("BODY").append(
			  '<div id="popup_container">' +
			    '<div id="popup_title"></div>' +
			    '<div id="popup_content">' +
			      '<div id="popup_message"></div>' +
				'</div>' +
			  '</div>');

            if ($.alerts.dialogClass) $("#popup_container").addClass($.alerts.dialogClass);

            // IE6 Fix
            var pos = ($.browser.msie && parseInt($.browser.version) <= 6) ? 'absolute' : 'fixed';

            $("#popup_container").css({
                position: pos,
                zIndex: 99999,
                padding: 0,
                margin: 0
            });

            $("#popup_title").html("<img src=\"/Images/Image30.gif\" alt=\"\" />" + title);
            $("#popup_content").addClass(type);
            $("#popup_message").text(msg);
            $("#popup_message").html($("#popup_message").text().replace(/\n/g, '<br />'));

            $("#popup_container").css({
                minWidth: $("#popup_container").outerWidth(),
                maxWidth: $("#popup_container").outerWidth()
            });

            $.alerts._reposition();
            $.alerts._maintainPosition(true);

            switch (type) {
                case 'alert':
                    $("#popup_message").after('<div id="popup_panel"><input type="button" value="' + $.alerts.okButton + '" id="popup_ok" /></div>');
                    $("#popup_ok").click(function () {
                        $.alerts._hide();
                        callback(true);
                    });
                    $("#popup_ok").focus().keypress(function (e) {
                        if (e.keyCode == 13 || e.keyCode == 27) $("#popup_ok").trigger('click');
                    });
                    break;
                case 'confirm':

                    $("#popup_message").after('<div id="popup_panel"><input type="button" value="' + $.alerts.okButton + '" id="popup_ok" /> <input type="button" value="' + $.alerts.cancelButton + '" id="popup_cancel" /></div>');
                    $("#popup_ok").click(function () {
                        $.alerts._hide();
                        if (callback) callback(true);
                    });
                    $("#popup_cancel").click(function () {
                        $.alerts._hide();
                        if (callback) callback(false);
                    });
                    $("#popup_ok").focus();
                    $("#popup_ok, #popup_cancel").keypress(function (e) {
                        if (e.keyCode == 13) $("#popup_ok").trigger('click');
                        if (e.keyCode == 27) $("#popup_cancel").trigger('click');
                    });
                    break;
                case 'ajaxconfirm':
                    $("#popup_message").after('<div id="popup_panel"><input type="button" value="' + $.alerts.okButton + '" id="popup_ok" /> <input type="button" value="' + $.alerts.cancelButton + '" id="popup_cancel" /></div>');
                    $("#popup_ok").click(function () {
                        $("#popup_container").remove();
                        $.alerts.showloading(false);
                        $.ajax({
                            type: "POST",
                            url: url,
                            data: configParamater,
                            success: function (data) {
                                if (data == "error") {
                                    alert('Sorry.We have encountered a problem while processing the page you requested.');
                                } else if (data == "login") {
                                    ReLoading();
                                } else if (data == "ok") {
                                    if (callback) callback();
                                } else {
                                    alert(data);
                                }
                                $.alerts._hide();
                            }
                        });
                    });
                    $("#popup_cancel").click(function () {
                        $.alerts._hide();
                    });
                    $("#popup_ok").focus();
                    $("#popup_ok, #popup_cancel").keypress(function (e) {
                        if (e.keyCode == 13) $("#popup_ok").trigger('click');
                        if (e.keyCode == 27) $("#popup_cancel").trigger('click');
                    });
                    break;
                case 'ajaxstatistics':
                    $("#popup_message").after('<div id="popup_panel"><input type="button" value="' + $.alerts.okButton + '" id="popup_ok" /> <input type="button" value="' + $.alerts.cancelButton + '" id="popup_cancel" /></div>');
                    $("#popup_message").after('<table><tr><td><input type="text" id="txtStatisticsBeginTime" style="width:100px;" onkeypress="javascript:FilterUserInputDateTime(\'popup_ok\');" class="FormBase" /></td><td><img src="/Images/Image28.gif" alt="打开日历控件" style="cursor:pointer;" onclick="javascript:calendar.setHook(document.getElementById(\'txtStatisticsBeginTime\'))" /></td></tr><tr><td><input type="text" id="txtStatisticsEndTime" style="width:100px;" onkeypress="javascript:FilterUserInputDateTime(\'popup_ok\');" class="FormBase" /></td><td><img src="/Images/Image28.gif" alt="打开日历控件" style="cursor:pointer;" onclick="javascript:calendar.setHook(document.getElementById(\'txtStatisticsEndTime\'))" /></td></tr></table>');
                    //                    $.alerts._overlay('hide');
                    $("#popup_ok").click(function () {
                        if ($("#txtStatisticsBeginTime").val() != "" && $("#txtStatisticsEndTime").val() != "") {
                            if (!CheckDate($("#txtStatisticsBeginTime").val(), $("#txtStatisticsEndTime").val())) {
                                return;
                            } else {
                                var BeginTime = $("#txtStatisticsBeginTime").val();
                                var EndTime = $("#txtStatisticsEndTime").val();
                                $("#popup_container").remove();
                                $.alerts.showloading(false);
                                $.getJSON(url, { TableName: configParamater, BeginTime: BeginTime, EndTime: EndTime, JqueryTime: (new Date()).getTime() }, function (json) {
                                    if (json) {
                                        if (json.Message) {
                                            alert(json.Message);
                                        } else {
                                            if (callback) callback(json, BeginTime, EndTime);

                                        }
                                    }
                                    $.alerts._hide();
                                });
                            }
                        } else {
                            alert("没有输入日期范围!请重新输入！");
                        }

                    });
                    $("#popup_cancel").click(function () {
                        $.alerts._hide();
                    });
                    $("#popup_ok").focus();
                    $("#popup_ok, #popup_cancel").keypress(function (e) {
                        if (e.keyCode == 13) $("#popup_ok").trigger('click');
                        if (e.keyCode == 27) $("#popup_cancel").trigger('click');
                    });
                    break;
                case 'prompt':
                    $("#popup_message").append('<br /><input type="text" size="30" id="popup_prompt" />').after('<div id="popup_panel"><input type="button" value="' + $.alerts.okButton + '" id="popup_ok" /> <input type="button" value="' + $.alerts.cancelButton + '" id="popup_cancel" /></div>');
                    $("#popup_prompt").width($("#popup_message").width());
                    $("#popup_ok").click(function () {
                        var val = $("#popup_prompt").val();
                        $.alerts._hide();
                        if (callback) callback(val);
                    });
                    $("#popup_cancel").click(function () {
                        $.alerts._hide();
                        if (callback) callback(null);
                    });
                    $("#popup_prompt, #popup_ok, #popup_cancel").keypress(function (e) {
                        if (e.keyCode == 13) $("#popup_ok").trigger('click');
                        if (e.keyCode == 27) $("#popup_cancel").trigger('click');
                    });
                    if (value) $("#popup_prompt").val(value);
                    $("#popup_prompt").focus().select();
                    break;
            }

            // Make draggable
            if ($.alerts.draggable) {
                try {
                    $("#popup_container").draggable({ handle: $("#popup_title") });
                    $("#popup_title").css({ cursor: 'move' });
                } catch (e) { /* requires jQuery UI draggables */ }
            }
        },

        _hide: function () {
            //            $.alerts._hideScroll();
            $("#popup_container").remove();
            $.alerts._overlay('hide');
            $.alerts.hideloading(true);
        },
        _hideScroll: function () {
            var a = document.body.clientHeight;
            var b = document.documentElement.clientHeight;
            var c = (b <= a && b != 0) ? true : false;
            var d = c ? document.documentElement : document.body;
            if (d.style.overflow == "hidden") {
                d.style.overflow = "auto";
            } else {
                d.style.overflow = "hidden";
            }
        },
        _overlay: function (status) {
            switch (status) {
                case 'show':
                    $.alerts._overlay('hide');
                    $("BODY").append('<div id="popup_overlay"></div>');
                    $("#popup_overlay").css({
                        position: 'absolute',
                        zIndex: 99998,
                        top: document.documentElement.scrollTop + 'px',
                        left: '0px',
                        width: '100%',
                        height: window.screen.height, //$(document).height(),
                        background: $.alerts.overlayColor,
                        opacity: $.alerts.overlayOpacity
                    });
                    break;
                case 'hide':
                    $("#popup_overlay").remove();
                    break;
            }
        },

        _loadingreposition: function () {
            var top = (($(window).height() / 2) - ($("#popup_loadingcontainer").outerHeight() / 2)) + $.alerts.verticalOffset;
            var left = (($(window).width() / 2) - ($("#popup_loadingcontainer").outerWidth() / 2)) + $.alerts.horizontalOffset;
            if (top < 0) top = 0;
            if (left < 0) left = 0;

            // IE6 fix
            if ($.browser.msie && parseInt($.browser.version) <= 6) top = top + $(window).scrollTop();

            $("#popup_loadingcontainer").css({
                top: top + 'px',
                left: left + 'px'
            });
            $("#popup_overlay").height($(document).height());
        },

        _reposition: function () {
            var top = (($(window).height() / 2) - ($("#popup_container").outerHeight() / 2)) + $.alerts.verticalOffset;
            var left = (($(window).width() / 2) - ($("#popup_container").outerWidth() / 2)) + $.alerts.horizontalOffset;
            if (top < 0) top = 0;
            if (left < 0) left = 0;

            // IE6 fix
            if ($.browser.msie && parseInt($.browser.version) <= 6) top = top + $(window).scrollTop();

            $("#popup_container").css({
                top: top + 'px',
                left: left + 'px'
            });
            $("#popup_overlay").height(window.screen.height);
        },

        _maintainPosition: function (status) {
            if ($.alerts.repositionOnResize) {
                switch (status) {
                    case true:
                        $(window).bind('resize', function () {
                            $.alerts._reposition();
                        });
                        break;
                    case false:
                        $(window).unbind('resize');
                        break;
                    default:
                        $(window).bind('resize', function () {
                            $.alerts._reposition();
                        });
                        break;
                }
            }
        }
    }

    // Shortuct functions
    jAlert = function (message, title, callback) {
        if (title) {
            $.alerts.alert(message, title, callback);
        } else {
            $.alerts.alert(message, "Alert", function () { });
        }

    }

    jConfirm = function (message, title, callback) {
        $.alerts.confirm(message, title, callback);
    };

    jAjaxConfirm = function (message, title, url, configParamater, callback) {
        $.alerts.ajaxconfirm(message, title, url, configParamater, callback);
    };

    jAjaxStatistics = function (message, title, url, configParamater, callback) {
        //        alert('dd');
        $.alerts.ajaxstatistics(message, title, url, configParamater, callback);
    };

    jPrompt = function (message, value, title, callback) {
        $.alerts.prompt(message, value, title, callback);
    };

    jShowLoading = function (MessageTemp) {
        $.alerts.showloading(true, MessageTemp);
    };

    jHideLoading = function () {
        $.alerts.hideloading(true);
    }
})(jQuery);
