var ssoCls = {
	selfName: 'ssoCls',
	ssoURI: 'http://passport.5pk.com/sso.php',
	timestamp: null,
	msgElement: null,
	callFn: null,
	extendFn : null,
	data: null,
	query: {},

	/**
	 * 返回给定id的HTML元素对象
	 *
	 * @author qinchh at 2009-12-21
	 * @return void
	 */
	G: function(id){
		return $('#' + id);
	},
	/**
	 * 重定向URL
	 * @author qinchh at 2009-12-21
	 * @return void
	 */
	redirect: function(url, blank){
		blank ? window.open(url) : window.location = url;
	},
	/**
	 * 当前时间的秒
	 *
	 * @author qinchh at 2009-12-21
	 * @return void
	 */
	setTimestamp: function(){
		this.timestamp = parseInt((new Date()).getTime() / 1000);
	},
	setQuery: function(){
		var id = $.cookie('syo_ID');
		this.query = {
			id: ( id ? id : ''),
			timestamp: this.timestamp,
			refer: escape(window.location.href)
		};
	},
	/**
	 * 锁定提交按钮
	 *
	 * @author qinchh at 2009-12-23
	 * @return void
	 */
	lockSubmit: function(){
		this.G('loginSubmit').css('disabled', true);
	},
	/**
	 * 解锁提交按钮
	 *
	 * @author qinchh at 2009-12-23
	 * @return void
	 */
	unLockSubmit: function(){
		this.G('loginSubmit').removeAttr('disabled');
	},
	/**
	 * 信息提示
	 *
	 * @author qinchh at 2009-12-22
	 * @return void
	 */
	showMessage: function(msg, type){
		if( this.msgElement && this.G(this.msgElement) )
			this.G(this.msgElement).html(msg);
		else
			alert(msg);
	},
	/**
	 * 构造回调函数/函数组
	 *
	 * @author qinchh at 2010-07-13
	 * @return array
	 */
	makeCallFn : function(Fns, Fn){
		var temp;
		if( Fns ){
			switch( Fns.constructor ){
				case Function:
					temp = [];
					temp.push( Fns );
					temp.push( Fn );
					Fns = temp;
					break;
				case Array:
					Fns.push( Fn );
					break;
				default:
					temp = [];
					temp.push( Fn );
					Fns = temp;
					break;
			}
		}else{
			temp = [];
			temp.push( Fn );
			Fns = temp;
		}
		return Fns;
	},
	/**
	 * 执行回调函数/函数组
	 * 
	 * @author qinchh at 2010-07-13
	 * @return void
	 */
	exeCallFn : function( Fns ){
		if( Fns ){
			switch( Fns.constructor ){
				case Function:
					Fns();
					break;
				case Array:
					for( var i = 0; i < Fns.length; i++ ){
						Fns[i]();
					}
					break;
				default:
					break;
			}
		}
	},
	/**
	 * 判断是否处于有效登录状态
	 *
	 * @author qinchh at 2009-12-21
	 * @return void
	 */
	isLogin: function(){
		var self = this, callFn;
		//alert('id:' + id);
		self.setTimestamp();
		self.setQuery();
		callFn = function(response){
			//alert(response.msg);
			switch( response.status ){
				case 1:
					self.data = response.data;
					break;
				default:
					break;
			}
			if( self.callFn ){
				self.exeCallFn( self.callFn );		// fix by qinchh at 2010-07-13
				//self.callFn();
			}
		}
		if( Ajax ){
			Ajax.getJSON(self.ssoURI, $.extend(self.query, {action: 'checkLogin'}), callFn);
		}
		return false;
	},
	/**
	 * 用户登录
	 *
	 * @author qinchh at 2009-12-21
	 * @return void
	 */
	login: function(f){
		with(f){
			this.lockSubmit();
			if( this.query.timestamp ){
				if( username.value == ''){
					this.showMessage('请输入通行证');
					this.unLockSubmit();
					username.focus();
					return false;
				}
				if( password.value == ''){
					this.showMessage('请输入密码');
					this.unLockSubmit();
					password.focus();
					return false;
				}
				if( authen && authen.value == ''){
					this.showMessage('请输入验证码');
					this.unLockSubmit();
					authen.focus();
					return false;
				}

				var self = this, query, callFn;
				self.setQuery();
				query = $.extend(self.query, {
					action: 'login',
					username: escape(username.value),
					password: $.md5(password.value + self.timestamp),
					authen: escape(authen.value),
					remember: ( remember && remember.checked ) ? 1 : 0
				});

				callFn = function(response){
					//self.showMessage(response.msg);
					switch( response.status ){
						case 1:
							//self.showMessage('登录成功!');
							self.data = response.data;
							break;
						default:
							self.showMessage(response.msg)
							break;
					}
					if( self.callFn ){
						self.exeCallFn( self.callFn );		// fix by qinchh at 2010-07-13
						//self.callFn();
					}
					if( self.extendFn ){
						self.exeCallFn( self.extendFn );	// fix by qinchh at 2010-07-13
						//self.extendFn();
					}
				}
				if( Ajax ){
					Ajax.tip(self.G('submitInfo'), '正在登录中，请稍候 ...');
					Ajax.getJSON(this.ssoURI, query, callFn);
				}
			}else{
				alert('请等待页面加载完成后再登录!');
				this.unLockSubmit();
			}
			return false;
		}
		return false;
	},
	/**
	 * 根据返回数据重置页面用户信息
	 *
	 * @author qinchh at 2009-12-21
	 * @return void
	 */
	brushPage: function(){
		if( !this.G('unLogined') || !this.G('logined') )
			return false;

		if( this.data ){
			var self = this, data = self.data;
			//this.G('logoutLink').attr('href', "javascript:" + this.selfName + ".logout();");
			//this.G('logoutImg').bind('click', function(){self.logout();});
			this.G('usernameDisplay').html(data.NAME);
			this.G('usermoneyDisplay').html(data.MONEY);
			this.G('userscoreDisplay').html(data.SCORE);
			this.G('userlastloginDisplay').html(data.LAST_LOGIN_DATE);
			this.G('unLogined').css('display', 'none');
			this.G('logined').css('display', '');
		}else{
			if( document.getElementById('memberLogin') )
				document.getElementById('memberLogin').reset();
			this.G('unLogined').css('display', '');
			this.G('logined').css('display', 'none');
			this.G('loginsubmit').removeAttr('disabled');
			this.setImgCode();
		}
		return false;
	},
	/**
	 * 调用验证码
	 *
	 * @author qinchh at 2009-12-21
	 * @return void
	 */
	setImgCode: function(){
		var self = this;
		//var src = this.G('imgCode').attr('src');
		this.G('imgCode').attr('src', 'http://passport.5pk.com/imgcode.php?' + Math.random());
		this.G('imgCode').unbind('click');
		this.G('imgCode').bind('click', function(){self.setImgCode();});
		return false;
	},
	/**
	 * 退出登录
	 *
	 * @author dengxh at 2010-1-23
	 * @param mixed back 是否返回,false 不返回,true 返回,string 返回到指定地址
	 * @return void
	 */
	logout: function(back){
		var url = '';
		if(typeof back == 'string')
			url = back;
		else if(back == undefined || back === true)
			url = document.location.href;

		document.location.href = 'http://passport.5pk.com/login.php?action=loginOut&url=' + url;
	}
}