﻿       $.fn.extend({ 
          mask : function(msg, msgCls, maskCls, titleClass)
          {
             var masks;
	         if (!$(this).hasClass('masked')){
		        $(this).addClass('masked');			
	         }
		     if($(this).css('position' == 'static'))
		     {
		         $(this).css('position','relative');
		     } 		     
		     
		     masks = $('<div class="'+ maskCls +'"><iframe class="mask_iframe" /></div>');
		     if($.browser.msie && $(this).is('body')){
		        var h = document.body.clientHeight;
		        var w = document.body.clientWidth;	
		        var hs = document.body.scrollHeight;
		        var ws = document.body.scrollWidth; 
		        if (hs > h)
		        { h = hs;}
		        if (ws > w)
		        { w = ws;}		        
			    masks.width(w).height(h);
		     }
		     else{
		        var h = $("."+titleClass).height();
			    masks.width($(this).width()).height($(this).height());//.css('top',h);		         
		     }	
	         if(typeof msg == 'string'){
		        $('<div class="mask_msg">'+msg+'</div>')
		        .appendTo($(this)).center();
	         }	     
		     $(this).append(masks);
             return masks.show(); 
          },
          remask : function(maskCls,msgCls)
          {
             if ($(this).hasClass('masked')) {
                 $(this).children('div.'+ msgCls +':first').remove();
		         $(this).children('div.'+ maskCls +':first').remove();
		         return $(this).css('position','static');
	         }
	         else{
		         return $(this);
	         }
          },
          center : function()
          {
             var pWin = $(this).parent();
             if (pWin.is('body'))
             {
                 pWin = $(window);
             }
             return $(this).css({
                    position:'absolute',
                    top:(pWin.height()-$(this).height())/2.2,
                    left:(pWin.width()-$(this).width())/2
             });
          },
          position : function()
          {
             var o = $(this).offset();
             return {
  			    top:o.top,
			    left:o.left,
			    width:$(this).width(),
			    height:$(this).height()               
             }
          },
          mload : function(opt)
          {
		        var o=$(this);
		        var cfg = {
			        url:'',
			        beforeSend:function(){},
			        success:function(data){
				        o.children().not('.mask,.mask_msg').remove();
				        o.append('<div class="ajax_content">'+data+'</div>');
			        },
			        complete:function(){
				        o.children('.ajax_content').fadeIn("slow");
			        },
			        error:function(){},
			        msg:'Loading...',
			        data:'',
			        type:'GET',
			        msgCls:{},
			        maskCls:{}
		        };  
		        cfg=$.extend(cfg,opt);
		        if(cfg.url!='' && cfg.html == ''){
		            return $.ajax({
			            url:cfg.url,
			            type:cfg.type,
			            data:cfg.data,
			            beforeSend:function(){
			                o.mask(cfg.msg,cfg.msgCls,cfg.maskCls, cfg.titleCls);
				            cfg.beforeSend();
			            },
			            success:function(data){
				            cfg.success(data);
			            },
			            complete:function(){
				            o.remask(cfg.maskCls,cfg.msgCls);
				            cfg.complete();				
			            },
			            error:function(){
				            o.remask(cfg.maskCls,cfg.msgCls);
			            }
		            });			        
		        }
		        else if(cfg.html != '')
		        {
		           if (cfg.html != undefined)
		           {
		               var htmlStr = cfg.html;
		               if(htmlStr.substr(0,1) == "#")
		               {
		                   return $('.win_content').append($(htmlStr).html());
		               }
		               else{
		                   return $('.win_content').html(htmlStr);
		               } 
		           }
		        }
		        else
		        {
		          return o;
		        }
		                   
          },
          win : function(par)
          {
		    var cfg={
			    title:'数据录入',        //窗口标题
			    url:'',                  //Ajax提交地址 
			    html:'',                 //显示页面
			    button:['close','min','max'],  //窗口按钮   
			    content:'',                    //窗口内容  
			    maskCls:'mask',                //遮罩层样式
			    titleCls:'win_head',           //标题栏样式 
			    width:250,                     //窗口宽
			    height:150,                    //窗口高 
			    defaultWidth:260,              
			    defaultHeight:160,
			    borderWidth:5,                 //窗口边框宽 
			    msg:'Loading...',
			    msgCls:'mask_msg'		    
		    }
		    cfg=$.extend(cfg,par); 
		    var mask,win,main,border,head,close,title,content;
		    mask=$(document.body).mask(null, null, cfg.maskCls, null);
		    win = $('<div class="win"/>').css({width:cfg.width, height:cfg.height});		    
		    win.append(
	            border = $('<div class="win_border"/>').css({width:cfg.width, height:cfg.height})  	    
		    )
		    .append(
		        main = $('<div class="win_main"/>').css({width:(cfg.width - 2 * cfg.borderWidth), height:(cfg.height - 2 * cfg.borderWidth), top:cfg.borderWidth + 1, left:cfg.borderWidth + 1})
		               .append(head = $('<div class="'+ cfg.titleCls +'"/>')
		                    .append(title = $('<span class="win_head_title">'+ cfg.title +'</span>'))
		                    .append(close = $('<span class="win_head_btn"></span>'))
		               )
		    )
		    .appendTo(document.body).center();
		    $('.win_main:first').append(content = $('<div class="win_content"/>').css({height:(cfg.height - 4 - 2 * cfg.borderWidth - parseInt($('.'+cfg.titleCls).height()))}))
		    close.click(function(){
			    $.fn.rmwin();
		    });	
            content.mload(cfg);             
            $(window).resize(function(){
               $.fn.rescr(cfg.maskCls);   
            }); 
          },
          rmwin : function(win){
	        if (win == undefined) {
		        $(document.body).children('.win:last').remove();
		        $(document.body).children('.mask:last').remove();
	        }
	        else {
		        win.win.remove();
		        win.mask.remove();
	        }
         },
         rescr : function(maskCls){
		    var h = document.body.clientHeight;
		    var w = document.body.clientWidth;
	        var hs = document.body.scrollHeight;
	        var ws = document.body.scrollWidth; 
	        if (hs > h)
	        { h = hs;}
	        if (ws > w)
	        { w = ws;}			    		        
	       	if ($(document.body).hasClass('masked')){
		       $("."+maskCls).width(w).height(h);		
	        } 
         }          
       });
