﻿ (function($) {
    jQuery.fn.clubankModalWindow = function(s){
	    defaults = {
		    modal: true,
		    center: true,
		    transfererFrom:"",
		    close:"#clubankMessage_close",
		    closeHoverClass: "closeHover",
		    transfererClass:"transferer",
		    overlayCSS:  { backgroundColor: '#fff', opacity: '0.5' }
	    }

	    $.extend(defaults,s);
    	
	    modalWindow.init();
    	
	    if(defaults.center){
		    var frm = (this);
		    modalWindow.moveCenter(frm);
		    $(document).scroll(function(){
			    modalWindow.moveCenter(frm);
		    });
		    $(document).resize(function(){
			    alert("");
			    modalWindow.moveCenter(frm);
		    });
	    }
    	 $(this).show();	
	    //模式窗口
	    if(defaults.modal){
		    modalWindow.showOverlayer();
		    //setTimeout(this.focus, 20);
		    $(this).css("z-index",1003);
	    }
    	
	    //是否可移动
//	    if(defaults.drag){
//		    modalWindow.setMoveable(this,defaults.drag);	
//	    }
    	
	    //关闭按钮
	    if(defaults.close != ""){
		    var frm = this;
		    $(defaults.close).bind("click",function(){			
			    modalWindow.close(frm);
		    });
		    if(defaults.closeHoverClass != ""){
			    $(defaults.close).bind("mouseover",function(){			
				    $(this).addClass(defaults.closeHoverClass);
			    });
			    $(defaults.close).bind("mouseout",function(){			
				    $(this).removeClass(defaults.closeHoverClass);
			    });
		    }
	    }
    	
	    //显示窗口
	    //是否有状态
	    if(defaults.transfererFrom != "" && defaults.transfererClass != ""){
		    var frm = $(this);
		    $(defaults.transfererFrom).TransferTo({to:this,className:defaults.transfererClass, duration: 400,complete:function(){
				    frm.show();
			    }
		    });	
    		
	    }else{	
		    $(this).show();	
	    }	
    };

    jQuery.fn.modalWindowClose = function(hide){
        if (hide){
            modalWindow.hide(this);
	     }
	     else{
	       modalWindow.close(this);
	     }
	    
    }

    var modalWindow = {
	    arrayPageSize:new Array(0,0,0,0),
	    arrayPageScroll:new Array(0,0,0,0),
    		
	    //初始化，要使用必须先运行此方法
	    //init()
	    //Results none
	    //
	    init:function(){
		    arrayPageScroll = this.getPageScroll();
		    arrayPageSize = this.getPageSize();
	    },
    	
	    //显示modal层
	    //showOverlayer()
	    //Results none
	    //
	    showOverlayer:function(){
		    this.removeOverlayer();
		    var layer = "<div id=\"_______overlayer\" style=\"position:absolute; top:0px; left:0px;-moz-opacity:0.5; z-index:999;opacity:0.5;filter:alpha(opacity=30);background:#fff;\"></div>";
		    $("body").append(layer);
		    var overlayer = $("#_______overlayer");
		    overlayer.css("top","0px");
		    overlayer.css("left","0px");
		    overlayer.css("width",self.innerHeight ? "100%" :  arrayPageSize[0]+"px");
		    overlayer.css("height",(self.innerHeight ? arrayPageSize[1] : arrayPageSize[1] + 30)+ "px");
	    },
    	
    	hide:function(frm){
		    //frm.DraggableDestroy();
		    frm.hide();
		    this.removeOverlayer();	
	    },
	    //close(窗口的jquery Dom)
	    close:function(frm){
		    //frm.DraggableDestroy();
		    frm.remove();//hide();
		    this.removeOverlayer();	
	    },
    	
	    //removeOverlayer()
	    removeOverlayer:function(){		
		    var overlayer = $("#_______overlayer");
		    if(overlayer != null)
		    {
			    overlayer.remove();
		    }
	    },
    	
	    //setMoveable(窗口的jquery Dom,标题的对象名称)
	    setMoveable:function(frm,title){
		    frm.Draggable({
			    zIndex: 	1000,
			    handle:	title,
			    opacity: 	0.7
		    });
	    },
    	
	    //moveCenter(窗口的jquery Dom)
	    moveCenter:function(frm){
		    arrayPageScroll = this.getPageScroll();
		    arrayPageSize = this.getPageSize();
		    var frmLeft = (arrayPageSize[2] - frm.width()) / 2;
		    var frmTop = arrayPageScroll[1] + ((arrayPageSize[3]  - frm.height())/ 2);
	        frm.css("left",frmLeft + "px");
	        frm.css("top",frmTop + 'px');
	    },

	    // getPageScroll()
	    // Returns array with x,y page scroll values.
	    // Core code from - quirksmode.org
	    getPageScroll:function(){

		    var yScroll;

		    if (self.pageYOffset) {
			    yScroll = self.pageYOffset;
		    } else if (document.documentElement && document.documentElement.scrollTop){	 // Explorer 6 Strict
			    yScroll = document.documentElement.scrollTop;
		    } else if (document.body) {// all other Explorers
			    yScroll = document.body.scrollTop;
		    }

		    arrayPageScroll = new Array('',yScroll) 
		    return arrayPageScroll;
	    },



	    //
	    // getPageSize()
	    // Returns array with page width, height and window width, height
	    // Core code from - quirksmode.org
	    // Edit for Firefox by pHaez
	    //
	    getPageSize:function(){
    		
		    var xScroll, yScroll;
    		
		    if (window.innerHeight && window.scrollMaxY) {	
			    xScroll = document.body.scrollWidth;
			    yScroll = window.innerHeight + window.scrollMaxY;
		    } else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
			    xScroll = document.body.scrollWidth;
			    yScroll = document.body.scrollHeight;
		    } else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
			    xScroll = document.body.offsetWidth;
			    yScroll = document.body.offsetHeight;
		    }
    		
		    var windowWidth, windowHeight;
		    if (self.innerHeight) {	// all except Explorer
			    windowWidth = self.innerWidth;
			    windowHeight = self.innerHeight;
		    } else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
			    windowWidth = document.documentElement.clientWidth;
			    windowHeight = document.documentElement.clientHeight;
		    } else if (document.body) { // other Explorers
			    windowWidth = document.body.clientWidth;
			    windowHeight = document.body.clientHeight;
		    }	
    		
		    // for small pages with total height less then height of the viewport
		    if(yScroll < windowHeight){
			    pageHeight = windowHeight;
		    } else { 
			    pageHeight = yScroll;
		    }

		    // for small pages with total width less then width of the viewport
		    if(xScroll < windowWidth){	
			    pageWidth = windowWidth;
		    } else {
			    pageWidth = xScroll;
		    }


		    arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight) 
		    return arrayPageSize;
	    }
    };
})(jQuery);	