
jQuery.fn.jWindowOpen = function(s){
	defaults = {
		modal: false,
		center: false,
		drag: "",
		close: "",
		closeHoverClass: "",
		transfererFrom:"",
		transfererClass:""
	}

	$.extend(defaults,s);
	
	jWindow.init();
	
	//居中
	if(defaults.center){
		var frm = (this);
		jWindow.moveCenter(frm);
		$(document).scroll(function(){
			jWindow.moveCenter(frm);
		});
		$(document).resize(function(){
			alert("");
			jWindow.moveCenter(frm);
		});
	}
	
	if(defaults.modal){
		jWindow.showOverlayer();
	}
	
	if(defaults.drag){
		jWindow.setMoveable(this,defaults.drag);	
	}
	
	if(defaults.close != ""){
		
		var frm = this;
		$(defaults.close).bind("click",function(){			
			jWindow.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.jWindowClose = function(){
	jWindow.close(this);
}

var jWindow = {
	arrayPageSize:new Array(0,0,0,0),
	arrayPageScroll:new Array(0,0,0,0),
		
	init:function(){
		arrayPageScroll = this.getPageScroll();
		arrayPageSize = this.getPageSize();
	},
	
	showOverlayer:function(){
		this.removeOverlayer();
		var layer = "<div id=\"_______overlayer\" style=\"position:absolute; top:0px; left:0px;-moz-opacity:0.7; opacity:0.7;filter:alpha(opacity=30);background:#000;\"></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");
	},
	
	close:function(frm){
		frm.DraggableDestroy();
		this.removeOverlayer();	
		frm.remove();
	},
	
	removeOverlayer:function(){		
		var overlayer = $("#_______overlayer");
		if(overlayer != null)
		{
			overlayer.remove();
		}
	},

	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');
	},
	
	setMoveable:function(frm,title){
		frm.Draggable({
			zIndex: 	1000,
			handle:	title,
			opacity: 	0.7
		});
	},
	
	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: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;
	}
}	