﻿// jQuery Alert Dialogs Plugin
//
// Version 1.1
//
// Cory S.N. LaViska
// A Beautiful Site (http://abeautifulsite.net/)
// 14 May 2009
//
// 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)
//
//		1.01 - Fixed bug where unbinding would destroy all resize events
//
// License:
// 
// This plugin is dual-licensed under the GNU General Public License and the MIT License and
// is copyright 2008 A Beautiful Site, LLC. 
//
(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: .55,                // transparency level of overlay
        overlayColor: '#000',               // base color of overlay
        draggable: true,                    // make the dialogs draggable (requires UI Draggables plugin)
        okButton: '&nbsp;确定&nbsp;',         // text for the OK button
        cancelButton: '&nbsp;取消&nbsp;', // text for the Cancel button
        dialogClass: null,                  // if specified, this class will be applied to all dialogs

        //Options

        _default: {
            message: 'Message',
            title: 'Title',
            value: 'Value',
            url: '',
            divID: '',
            width: 0,
            height: 0,
            callback: null,
            modelWindow: true,
            okButton: '',         // text for the OK button
            cancelButton: '', // text for the Cancel button
            closeDisplay: true
        },

        // Public methods

        alert: function(options) {
            var opts = $.alerts._getOptions(options);
            if (opts.title == null) opts.title = 'Alert';
            $.alerts._show(opts, 'alert', function(result) {
                if (opts.callback) opts.callback(result);
            });
        },

        confirm: function(options) {
            var opts = $.alerts._getOptions(options);
            if (opts.title == null) opts.title = 'Confirm';
            $.alerts._show(opts, 'confirm', function(result) {
                if (opts.callback) opts.callback(result);
            });
        },

        prompt: function(options) {
            var opts = $.alerts._getOptions(options);
            if (opts.title == null) opts.title = 'Prompt';

            $.alerts._show(opts, 'prompt', function(result) {
                if (opts.callback) opts.callback(result);
            });
        },
        //20090130
        showDialog: function(options) {
            var opts = $.alerts._getOptions(options);
            if (opts.title == null) opts.title = 'window';
            $.alerts._show(opts, 'openWin', function(result, data) {
                if (opts.callback) opts.callback(result, data);
            });
        },
        showDiv: function(options) {
            var opts = $.alerts._getOptions(options);
            if (opts.title == null) opts.title = 'window';
            $.alerts._show(opts, "openDiv", function(result, data) {
                if (opts.callback) opts.callback(result, data);
            });
        },

        // Private methods

        _getOptions: function(options) {
            return $.extend({}, $.alerts._default, options);
        },

        _show: function(options, type, callback) {

            $.alerts._hide();
            $.alerts._overlay('show');

            $("BODY").append(
			  '<div id="popup_container">' +
			    '<div id="popup_header">' +
			        '<div id="popup_title"></div>' +
			        '<div id="popup_close"></div>' +
			    '</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';

            var styles = {
                position: pos,
                zIndex: 99999,
                padding: 0,
                margin: 0
            };
            if (options.width)
                styles.width = options.width + "px";
            if (options.height)
                styles.height = options.height + "px";
            $("#popup_container").css(styles);

            $("#popup_close").click(function() {
                $.alerts._hide(options.divID);
            });
            if (options.closeDisplay == false)
                $("#popup_close").css({ display: "none" });
            $("#popup_title").text(options.title);
            $("#popup_content").addClass(type);
            if (type.indexOf('open') == -1) {
                $("#popup_content").html('<div id="popup_message"></div>');
                $("#popup_message").text(options.message);
                $("#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);

            var okText = (options.okButton) ? options.okButton : $.alerts.okButton;
            var cancelText = (options.cancelButton) ? options.cancelButton : $.alerts.cancelButton
            switch (type) {
                case 'alert':
                    $("#popup_message").after('<div id="popup_panel"><input type="button" value="' + okText + '" 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="' + okText + '" id="popup_ok" /> <input type="button" value="' + cancelText + '" 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 'prompt':
                    $("#popup_message").append('<br /><input type="text" size="30" id="popup_prompt" />').after('<div id="popup_panel"><input type="button" value="' + okText + '" id="popup_ok" /> <input type="button" value="' + cancelText + '" 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 (options.value) $("#popup_prompt").val(options.value);
                    $("#popup_prompt").focus().select();
                    break;
                case 'openWin': //20090130
                    $("#popup_content").append('<iframe id="popup_iframe" style="width:100%;height:100%;" frameborder="0"></iframe>');
                    $("BODY").append('<input type="button" style="width:0px;height:0px;display:none;" id="popup_hidden" value="0" /><input type="hidden" id="popup_value"/>');
                    $(document).keypress(function(e) {
                        if (e.keyCode == 27) {
                            $.alerts._hide();
                            if (callback) callback(false, null);
                        }
                    });
                    $("#popup_content").css({
                        padding: 0
                    });
                    $("#popup_iframe").css({
                        height: $("#popup_container").height() - $("#popup_header").height()
                    });
                    $("#popup_iframe").attr("src", options.url); //src="' + options.url + '"
                    $("#popup_hidden").click(function() {
                        $.alerts._hide();
                        if (callback) {
                            callback($(this).val() == "1", $("#popup_value").val());
                        }
                        $("#popup_hidden").remove();
                        $("#popup_value").remove();
                    });
                    break;
                case "openDiv":
                    var div = $("#" + options.divID);
                    if (div) {
                        //div = div.clone();
                        $("#popup_content").css({
                            padding: 0
                        });
                        div.css({
                            height: $("#popup_container").height() - $("#popup_header").height(),
                            overflow: "auto"
                        });
                        $("#popup_content").append(div);
                        div.show();
                    }
                    break;
            }

            // Make draggable
            if ($.alerts.draggable) {
                try {
                    $("#popup_container").draggable({ handle: $("#popup_header") });
                    $("#popup_header").css({ cursor: 'move' });
                } catch (e) { /* requires jQuery UI draggables */ }
            }
        },

        _hide: function(divID) {
            if (divID) {
                var div = $("#" + divID);
                if (div) {
                    div.hide();
                    $("BODY").append(div);
                } 
            }
            $("#popup_container").remove();
            $.alerts._overlay('hide');
            $.alerts._maintainPosition(false);
        },

        _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: '0px',
                        left: '0px',
                        width: '100%',
                        height: $(document).height(),
                        background: $.alerts.overlayColor,
                        opacity: $.alerts.overlayOpacity
                    });
                    break;
                case 'hide':
                    $("#popup_overlay").remove();
                    break;
            }
        },

        _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($(document).height());
        },

        _maintainPosition: function(status) {
            if ($.alerts.repositionOnResize) {
                switch (status) {
                    case true:
                        $(window).bind('resize', $.alerts._reposition);
                        break;
                    case false:
                        $(window).unbind('resize', $.alerts._reposition);
                        break;
                }
            }
        }
    }

    // Shortuct functions
    jAlert = function(options) {
        $.alerts.alert(options);
    }

    jConfirm = function(options) {
        $.alerts.confirm(options);
    };

    jPrompt = function(options) {
        $.alerts.prompt(options);
    };
    //20090130
    jShowDialog = function(options) {
        $.alerts.showDialog(options);
    };
    //20101209
    jShowDiv = function(options) {
        $.alerts.showDiv(options);
    }
    jCloseDialog = function(returnValue) {
        if (window.parent) {
            if (returnValue) {
                var hidInput = window.parent.document.getElementById("popup_value");
                if (hidInput) hidInput.value = returnValue;
            }
            var closeBtn = window.parent.document.getElementById("popup_hidden");
            if (closeBtn) {
                closeBtn.value = "1";
                closeBtn.click();
            }
        }
    };
    jCloseDiv = function(divID) {
        if (divID) {
            $.alerts._hide(divID);
        }
    }
})(jQuery);
