(function($){
	
	$.emptyFn  = function(){};

	$.loginDialog = {
		init:function(){
		if(!this.inited){
			if(this.containerId && typeof this.containerId == "string"){
				this.container = document.getElementById(this.containerId);
			}
			if(this.closeTriggerId && typeof this.closeTriggerId == "string"){
				this.closeTrigger = document.getElementById(this.closeTriggerId);
				$(this.closeTrigger).bind("click",function(){
					$.activeLoginDialog.close();
				});
			}
			$(document.body).append("<div id='dialogMask'></div>");
			this.$mask = $('#dialogMask');
			this.$mask.addClass('login_dialog_mask');
			this.$mask.css({height:$(document).height()+"px"});
			this.$mask.css({width:$(document).width()+"px"});
			this.$maskbg = $('<div></div>');
			this.$mask.append(this.$maskbg);
			this.$maskbg.addClass('login_dialog_mask_bg');
			 var version = navigator.appVersion.split("MSIE")
			 version = parseFloat(version[1]);
			 
			if(version<=6){
			   this.iframe = $('<iframe style="filter:alpha(opacity=0);display:none;top:0;left:0;position:absolute;z-index:0;cursor:not-allowed"></iframe>');
			   this.iframe.css({height:$(document).height()+"px"});
			   this.iframe.css({width:$(document).width()+"px"});
			   $(document.body).append(this.iframe);
			 
			}
			var dailog = this;
			$(window).bind("resize",function(){
				if($.activeLoginDialog){
				dailog.$mask.css({height:$(document).height()+"px"});
				dailog.$mask.css({width:$(document).width()+"px"});
				var y = document.documentElement.scrollTop+(document.documentElement.clientHeight - dailog.container.clientHeight)/3 ;
				var x = document.documentElement.scrollLeft+(document.documentElement.clientWidth - dailog.container.clientWidth)/2 ;
				dailog.setXY(dailog.container,x,y);
				}
			});
			
			document.onkeydown=function(event)
			 {
				var $dialog = $.activeLoginDialog;
			     var keycode;   
			     var event = window.event || event;
			     keycode=event.keyCode;   
			     if(keycode==13){
			         if($dialog&&!$dialog.ishidden){
			             $($dialog.loginEl).click();
			     　　　}
			 　   }
			 }
			this.loginEl = document.getElementById('affirm');
			this.alipayloginEl = document.getElementById('alipaylogin');
			this.nonmemberEl = document.getElementById('nonmember');
			this.registerEl = document.getElementById('freeregister');
			$(this.loginEl).bind("click",this.loginFn);
			if(this.alipayloginEl){
			$(this.alipayloginEl).bind("click",this.aliloginFn);
			}
			$(this.nonmemberEl).bind("click",this.nonmemberFn);
			$(this.registerEl).bind("click",this.registerFn);
			
			//如果ie6使用iframe遮盖 select 
			
	
			$(this.closeTrigger).css({cursor:'pointer'});
			
		
			this.inited = true;	
		}
	},
	setXY:function (dom, x,y) {
		dom.style.zIndex = 5000;
		if(dom.style.position!='absolute')
		dom.style.position = "absolute";
		if (x !== false) {
			dom.style.left = x + "px";
		}
		if (y !== false) {
			dom.style.top = y + "px";
		}
	},
	close:function(){
		this.hide();
	},
	showMask:function(){
		this.$mask.addClass('login_dialog_mask_show');	
		if(this.iframe){
			this.iframe.show();
		}
	},
	hideMask:function(){
		this.$mask.removeClass('login_dialog_mask_show');
		if(this.iframe){
			this.iframe.hide();
		}
	}
	,
	show:function(){
		this.container.style.display = "block";
		var y = document.documentElement.scrollTop+(document.documentElement.clientHeight - this.container.clientHeight)/3 ;
		var x = document.documentElement.scrollLeft+(document.documentElement.clientWidth - this.container.clientWidth)/2 ;
		this.setXY(this.container,x,y);
		if(this.automask){
			this.showMask();
		}
		this.ishidden = false;
	}
	,
	hide:function(){
			if(this.automask){
				this.hideMask();
			}
			
			this.container.style.display = "none";
			this.ishidden = true;
		}
	}
	$.loginDialog.defaults = {
			inited:false,
			autoshow:false,
			autohide:true,
			ishidden:true,
			automask:true,
			closeTriggerId:null,
			closeTrigger:null,
			container:null,
			containerId:null,
			fixX:0,
			fixY:0,
			beforeshow:$.emptyFn,
			aftershow:$.emptyFn,
			beforehide:$.emptyFn,
			afterhide:$.emptyFn,
			registerFn:$.emptyFn,
			nonmemberFn:$.emptyFn,
			aliloginFn:$.emptyFn,
			loginFn:$.emptyFn
		}
	
	
	var dialog = function(option){
		return $.extend({},$.loginDialog,$.loginDialog.defaults,option);
	}

	$.extend({
		showLoginDialog:function(options){
			if(!$.activeLoginDialog){
				$.activeLoginDialog = new dialog(options);
				$.activeLoginDialog.init();	
				
				if($.activeLoginDialog.autoshow){
					if($.activeLoginDialog.beforeshow()!==false){
					
					$.activeLoginDialog.show();
				
					}
					$.activeLoginDialog.aftershow();
				}
				if($.activeLoginDialog.autohide&&$.activeLoginDialog.autoshow!==true){
					
					if($.activeLoginDialog.beforehide()!==false){
						
						$.activeLoginDialog.hide();
						
					}
					$.activeLoginDialog.afterhide();
				}
			}	
	}
});
	
	
})(jQuery);
	


