(function(){
        $.extend($.fn,{
        	getMaxZindex: function(){
        		var maxZ = Math.max.apply(null, $.map($('body > *'), function (e, n) {
                    if ($(e).css('position') == 'absolute' && $(e).css('display') != 'none')
                        return parseInt($(e).css('z-index')) || 1;
                    })
                );
        		return maxZ;
        	},
            mask: function(msg,maskDivClass){
                //this.unmask();
                // 参数
            	
                var op = {
                    opacity: 0.5,
                    z: 1000,
                    bgcolor: '#ccc'
                };
                var original=$(document.body);
                var position={top:0,left:0};
                var isBody = true;
                if(this[0] && this[0]!==window.document){
                	isBody = false;
                	original=this;
                	position=original.position();
                }
                // 创建一个 Mask 层，追加到对象中
                var maskDiv=$('<div class="maskdivgen">&nbsp;</div>');
                maskDiv.appendTo(original);
                maskDiv.attr("overflowX",original.css("overflowX"));
                maskDiv.attr("overflowY",original.css("overflowY"));
                original.css("overflowX","hidden");
                original.css("overflowY","hidden");
                var maskWidth = isBody?$(window).width():original.outerWidth();
                if(!maskWidth){
                    maskWidth=original.width();
                }
                var maskHeight = isBody?$(window).height():original.outerHeight();
                if(!maskHeight){
                    maskHeight=original.height();
                }
                if(isBody){
                	maskDiv.css({
                        position: 'fixed',
                        top: 0,
                        right:0,
                        bottom:0,
                        left: 0,
                        'background-color':op.bgcolor,
                        'z-index': op.z,
                        width: maskWidth,
                        height:maskHeight,
                        opacity: 0
                    });
                }else{
	                maskDiv.css({
	                    position: 'absolute',
	                    top: position.top,
	                    left: position.left,
	                    'z-index': op.z,
	                    width: maskWidth,
	                    height:maskHeight,
	                    'background-color': op.bgcolor,
	                    opacity: 0
	                });
                }
                if(maskDivClass){
                    maskDiv.addClass(maskDivClass);
                }
                if(msg){
                    var msgDiv=$('<div qmthDivType="msgDiv" style="position:absolute;border:#6593cf 1px solid; padding:2px;background:#ccca;"><div style="line-height:24px;border:#a3bad9 1px solid;background:white;padding:2px 10px 2px 10px;color:#000000">'+msg+'</div></div>');
                    msgDiv.appendTo(maskDiv);
                    var widthspace=(maskDiv.width()-msgDiv.width());
                    var heightspace=(maskDiv.height()-msgDiv.height());
                    msgDiv.css({
                                cursor:'wait',
                                top:(heightspace/2-2),
                                left:(widthspace/2-2)
                    });
                    maskDiv.css('z-index',this.getMaxZindex() + 1);
                    //msgDiv.css('z-index',op.z + 1);
                    //msgDiv.css('opacity',1);
                    
                }
                maskDiv.css('opacity',op.opacity);
                //maskDiv.fadeIn('fast', function(){
                //    // 淡入淡出效果
                //    $(this).fadeTo('fast', op.opacity);
                //});
                return maskDiv;
            },
         unmask: function(){
             var original=$(document.body);
             if(this[0] && this[0]!==window.document){
                 original=$(this[0]);
              }
             var maskdivgen = original.find("> div.maskdivgen");
             if(maskdivgen.length == 1){
       	  	 	original.css("overflowX",maskdivgen.attr("overflowX"));
       	  	 	original.css("overflowY",maskdivgen.attr("overflowY"));
             }
          	 maskdivgen.last().remove();
          	// maskdivgen.last().fadeOut('fast',0,function(){
            //      $(this).remove();
            //  });
            }
        });
    })();
/**
 * 测试例子：
<div id="test" style="width:200px;height:100px; border:black 1px solid;">
</div>
<a href="#" onclick="$('#test').mask('DIV层遮罩')">div遮罩</a>
<a href="#" onclick="$('#test').unmask()">关闭div遮罩</a>
<a href="#" onclick="$(document).mask('全屏遮罩').click(function(){$(document).unmask()})">全部遮罩</a>
**/
