(function(jQuery) {
        jQuery.popup = {
            ID: null,
            title: "",
            top: 0,
            left: 0,
            width: 0,
            height: 0,
            popType: "",
            repositionOnResize: false,          // 窗口调整大小后是否重新定位
            okButton: '确 定',                  // 确定按钮
            cancelButton: '取 消',              // 取消按钮
            isButtonRow: false,                  // 是否显示按钮
            isPopup: true,                      // 是否为popup窗口
            autoClose: 0,                       // 窗口自动关闭 (大于0时窗口自动关闭)

            // 公共方法
            tip: function(msg, top, left, autoClose) {
                this.ID = 'tip';
                this.popType = 'tip';
                this.title = '';
                this.isPopup = false;
                this.autoClose = autoClose || 0;
                this.width = 270;
                this.height = 30;
                this.top = top || (jQuery(document).height() - this.height) / 2;
                this.left = left || (jQuery(document).width() - this.width) / 2;
                jQuery.popup._show(null, msg, null);
            },

            help: function(elem, title, msg, height) {
                this.ID = 'help';
                this.title = title || this.title;
                this.width = 271;
                this.height = height || 40;
                var top = jQuery(elem).offset().top;
                if (top - 60 - this.height > 0) {
                    this.top = jQuery(elem).offset().top - 60 - this.height;
                    this.popType = 'help_up';
                }
                else {
                    this.top = top + 16;
                    this.popType = 'help_down';
                }
                this.left = jQuery(elem).offset().left - 30;
                jQuery.popup._show(elem, msg);
            },

            prompt: function(elem, title, msg, isButtonRow, isPopup, callback, top, left, width, height) {
                this.ID = 'prompt';
                this.title = title || this.title;
                this.popType = 'prompt';
                this.isButtonRow = isButtonRow || this.isButtonRow;
                this.isPopup = isPopup || this.isPopup;
                this.top = top || jQuery(elem).offset().top + 16;
                this.left = left || jQuery(elem).offset().left;
                this.width = width || 300;
                this.height = height || 120;
                jQuery.popup._show(elem, msg, function(result) {
                    if (!callback) callback(result);
                });
            },

            // 私有方法
            _show: function(elem, msg, callback) {
                if (jQuery("#_Popup_" + this.ID).length < 1) {
                    //jQuery.popup._hide();
                    var html =
              '<div class="popup_' + this.popType + '" id="_Popup_' + this.ID + '" style="width:' + this.width + 'px">\
                      <div class="popup_header" id="_Title_"><h1>' + this.title + '</h1><div class="h_r"></div></div>\
                      <div class="popup_content">\
                        <div id="_Container_' + this.ID + (this.height == 0 ? '">' : '" style="height:' + this.height + 'px">') + msg + '</div></div>' +
                        (this.isButtonRow ? '<div class="buttonRow" id="_ButtonRow_' + this.ID + '"></div>' : '') +
                      '<div class="popup_bottom"><div class="b_r"></div>\
                    </div>';

                    jQuery("BODY").append(html);

                    // IE6 Fix
                    //var pos = (jQuery.browser.msie && parseInt(jQuery.browser.version) <= 6) ? 'absolute' : 'fixed';

                    jQuery("#_Popup_" + this.ID).css({
                        position: 'absolute',
                        zIndex: 100,
                        padding: 0,
                        margin: 0
                    });

                    jQuery("#_Popup_" + this.ID).css({
                        minWidth: jQuery("#_Popup_" + this.ID).outerWidth(),
                        maxWidth: jQuery("#_Popup_" + this.ID).outerWidth()
                    });

                    jQuery.popup._reposition();
                    jQuery.popup._maintainPosition(true);

                    jQuery.popup._bindType();

                    // Popup 窗口方法待测试
                    if (this.isPopup) {
                        jQuery(elem).click(function(e) {
                            e ? e.stopPropagation() : event.cancelBubble = true;
                        });
                        jQuery("#_Popup_" + this.ID).click(function(e) {
                            e ? e.stopPropagation() : event.cancelBubble = true;
                        });
                        jQuery(document).click(function() {
                            jQuery.popup._hide();
                        });
                    }

                    if (this.autoClose > 0) {
                        jQuery.popup._autoClose();
                    }
                }
                else {
                    jQuery("#_Container_" + this.ID).html(msg);
                    jQuery.popup._bindType(callback);
                    jQuery.popup._reposition();
                    jQuery.popup._maintainPosition(true);
                    jQuery("#_Popup_" + this.ID).show();
                    if (this.autoClose > 0) {
                        jQuery.popup._autoClose();
                    }
                }
            },

            _bindType: function(callback) {
                switch (this.popType) {
                    case 'help':
                        if (this.isButtonRow) {
                            jQuery("#_ButtonRow_" + this.ID).after('<input type="button" value="' + jQuery.popup.okButton + '" id="_ButtonOK_' + this.ID + '" />');
                            jQuery("#_ButtonOK_" + this.ID).click(function() {
                                jQuery.popup._hide();
                                callback(true);
                            });
                            jQuery("#_ButtonOK_" + this.ID).keypress(function(e) {
                                if (e.keyCode == 13 || e.keyCode == 27) jQuery("#_ButtonOK_" + this.ID).trigger('click');
                            });
                        }
                        break;
                    case 'prompt':
                        if (this.isButtonRow) {
                            jQuery("#_ButtonRow_" + this.ID).html('<input type="hidden" id="hid_' + this.ID + '" />\
                            <input type="button" value="' + jQuery.popup.okButton + '" id="_ButtonOK_' + this.ID + '"/>\
                            <input type="button" value="' + jQuery.popup.cancelButton + '" id="_ButtonCancel_' + this.ID + '"/>');
                            jQuery("#_ButtonOK_" + this.ID).click(function() {
                                var val = jQuery("#hid_" + this.ID).val();
                                jQuery.popup._hide();
                                if (callback) callback(val);
                            });
                            jQuery("#_ButtonCancel_" + this.ID).click(function() {
                                jQuery.popup._hide();
                                if (callback) callback(null);
                            });
                            jQuery("#_ButtonOK_" + this.ID + ", #_ButtonCancel_" + this.ID).keypress(function(e) {
                                if (e.keyCode == 13) jQuery("#_ButtonOK_" + this.ID).trigger('click');
                                if (e.keyCode == 27) jQuery("#_ButtonCancel_" + this.ID).trigger('click');
                            });
                        }
                        break;
                    case 'tip':
                        break;
                    default:
                        break;
                }

            },

            _hide: function() {
                if (jQuery("#_Popup_" + this.ID).length > 0) {
                    if (this.popType == "tip") {
                        jQuery("#_Popup_" + this.ID).fadeOut(500);
                    }
                    else {
                        jQuery("#_Popup_" + this.ID).remove();
                    }
                    jQuery.popup._maintainPosition(false);
                }
            },

            _autoClose: function() {
                setTimeout("jQuery.popup._hide()", this.autoClose * 1000);
            },

            _reposition: function() {
                var top = this.top || ((jQuery(document).height() / 2) - (jQuery("#popup_container").outerHeight() / 2));
                var left = this.left || ((jQuery(document).width() / 2) - (jQuery("#popup_container").outerWidth() / 2));
                if (top < 0) top = 0;
                if (left < 0) left = 0;
                // IE6 fix
                //if (jQuery.browser.msie && parseInt(jQuery.browser.version) <= 6) top = top + jQuery(window).scrollTop();
                jQuery("#_Popup_" + this.ID).css({
                    top: top + 'px',
                    left: left + 'px'
                });
            },

            _maintainPosition: function(status) {
                if (jQuery.popup.repositionOnResize) {
                    switch (status) {
                        case true:
                            jQuery(window).bind('resize', jQuery.popup._reposition);
                            break;
                        case false:
                            jQuery(window).unbind('resize', jQuery.popup._reposition);
                            break;
                    }
                }
            }

        }

        // 显示Loading信息
        showLoading = function(msg, elem) {
            var loadingMsg = msg || '正在加载数据，请稍候...';
            if (elem == null) {
                jQuery.popup.tip('<table width="100%" height="100%" border="0" cellpadding="0" cellspacing="0"><tr>' +
                    '<td align="center"><img src="image/star_icon.gif" /> ' + loadingMsg + '</td></tr></table>', null, null, 0);
            } else {
                var middle = (jQuery(elem).height() - 30) / 2;
                var top = jQuery(elem).offset().top;
                jQuery.popup.tip('<table width="100%" height="100%" border="0" cellpadding="0" cellspacing="0"><tr>' +
                     '<td align="center"><img src="image/star_icon.gif" /> ' + loadingMsg + '</td></tr></table>', top, null, 0);
            }
        }
        hideTip = function() {
            jQuery("#_Popup_tip").fadeOut(500);
        }
        showTip = function(msg, elem, autoClose) {
            if (elem == null) {
                jQuery.popup.tip('<table width="100%" height="100%" border="0" cellpadding="0" cellspacing="0"><tr>' +
                    '<td align="center">' + msg + '</td></tr></table>', null, null, autoClose);
            } else {
                var middle = (jQuery("#" + elem).height() - 50) / 2;
                var top = jQuery("#" + elem).offset().top + (middle > 0 ? middle : 0);
                jQuery.popup.tip('<table width="100%" height="100%" border="0" cellpadding="0" cellspacing="0"><tr>' +
                    '<td align="center">' + msg + '</td></tr></table>', top, null, autoClose);
            }
        }
        showHelper = function(elem, title, msg, height) {
            jQuery.popup.help(elem, title, msg, height);
        }

        showPrompt = function(elem, title, msg, isButtonRow, isPopup, callback, top, left, width, height) {
            jQuery.popup.prompt(elem, title, msg, isButtonRow, isPopup, callback, top, left, width, height);
        }

    })(jQuery);
