(function(){
/*
	[Xinkb Word] (C) 2007-2010 Xuezhou Inc.
	$Id: jquery.mask.js 2897 2009-10-17 09:49:46Z yongzh $
*/
jQuery.extend(jQuery.fn, {
    mask: function(msg, maskDivClass, _op){
        this.unmask('hide');
        // 参数
        var op = {
            opacity: 0.5,
            z: 10000,
            bgcolor: '#000'
        };
		if(typeof _op == 'object') {
			for(var _i in _op) {
				eval("op."+_i+ "=" + _op[_i]);
			}
		}
        var original = jQuery(document.body);
        var position = {
            top: 0,
            left: 0
        };
        if (this[0] && this[0] !== window.document) {
            original = this;
            position = original.position();
        }
        // 创建一个 Mask 层，追加到对象中
        var maskDiv = jQuery('<div class="maskdiv"> </div>');
        maskDiv.appendTo(original);
        var maskWidth = original.outerWidth();
        if (!maskWidth) {
            maskWidth = original.width();
        }
        var maskHeight = original.outerHeight();
        if (!maskHeight) {
            maskHeight = original.height();
        }
        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 = jQuery('<div style="position:absolute;border:#A05E00 1px solid; padding:2px;background:#ccca"><div style="line-height:32px;border:#BB7700 1px solid;background:white;padding:5px 10px 5px 10px">' + 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.fadeIn('fast', function(){
            // 淡入淡出效果
            jQuery(this).fadeTo('slow', op.opacity);
        })
        return maskDiv;
    },
    unmask: function(callback,time){
        var original = jQuery(document.body);
        if (this[0] && this[0] !== window.document) {
            original = jQuery(this[0]);
        }
		if(typeof callback == 'string' || typeof callback == "number"){
			time=callback;
		}
		if(time=='hide'){
            jQuery(".maskdiv","div").hide().remove();
		}else{
            original.find("> div.maskdiv").fadeOut(time || 'fast', function(){
                jQuery(this).remove();
                if (typeof callback == "function" || typeof callback == "object") {
                    callback();
                }
            });
		}
    }
});
})(jQuery);

