/*
 * qeedoo-login          depend 'jquery'  'qeedoo.check'  'jquery.hitch' 'jquery.formToArray'
 * http://www.lezi.com/
 * Date: 2010-03-04
 */
var qeedooLogin = function(obj){
	this.obj = obj;
	this.init();
};
qeedooLogin.prototype = {
	init:function(){
		this.SetOptions(this.opt);
		this.getUrl();
		this.addCheck();
		if(this.opt.ucLogin){
			$(this.opt.loginOutBtn).hitch('click',this.loginOutUc,this);
		}else{
			$(this.opt.loginOutBtn).hitch('click',this.loginOut,this);
		}
	},
	SetOptions:function(opt){
		this.opt = {
			formItem:'#idDialLoginForm',//登录form
			loginInItem:'#idDialUserLogin',//登录前元素
			loginOutItem:'#idDialUserLogined',//登录后元素
			loginInUrl:'',//登录URL  如果没有指定  表示从form的action里读取
			loginOutUrl:'',//注销URL  如果没有指定  则通过登录URL计算得出
			loginUrlType:'',//登录的url类型   没有指定表示是直接把login换成loginout  指定为php表示把login.php 替换成loginout.php
			loginOutBtn:'#idUserLoginout',//注销按钮
			username:'#idDialUname',//用户昵称元素
			vipItem:'#idVipInfo',//vip元素
			safeItem:'#idSafeInfo',//账户安全元素
			integrityItem:'#idIntegrityInfo',//用户信息完成度元素
			ifLoginOutLink:false,//退出后是否跳转链接
			act_type:'',//登陆方式  serialize: 序列化返回状态及相关结果集 json: json_encode返回状态及相关结果集 redirect: 直接通过header方式跳转 bbs: 直接通过header方式跳转至BBS登陆：区服网站，这里需要写入BBS相关的cookie值
			ucLogin:false,//true调用UC同步代码
			ifAjax:false
		};
		jQuery.extend(this.opt,this.obj);
	},
	loginErrors:{
		'111':'通行证不能为空',
		'112':'通行证输入错误',
		'113':'通行证已被冻结',
		'114':'通行证不存在',
		'115':'通行证长度错误',
		'121':'通行证密码错误',
		'122':'通行证密码不能为空',
		'123':'通行证密码长度错误',
		'131':'验证码错误',
		'134':'您已多次输入错误密码，请用乐子网的安全登录',
		'141':'参数错误或输入不完整'
	},
	integMsg:{
		safeMsg:{
			'0':'危险',
			'1':'中等',
			'2':'安全'
		},
		strongMsg:function(n){
			return String(Number(n)*25)+'%';
		}
	},
	addCheck:function(){
		$(this.opt.formItem).check({
			userid:{
				name:"用户名",
				type:"text",
				min:4,
				max:20,
				empty:true
			},
			pwd:{
				name:"密码",
				type:"text",
				min:4,
				max:24,
				empty:true
			}
		},null,this.loginIn,this);
	},
	resetUserForm:function(){
		
		$(this.opt.formItem).find(":input").not(":hidden").val('');
	},
	setInfo:function(objItem,level,className){
		var obj = $(objItem);
		if (obj[0]) {	//等级信息
			var new_level = level ? className + level : className+'0';
			obj[0].className = '';
			obj.addClass(new_level);
		}
	},
	setUserInfo:function(userdata,uc_data){
		$(this.opt.loginInItem).toggle();
		$(this.opt.loginOutItem).toggle();
		$(this.opt.username).html(userdata ? userdata["uname"] : "");
		if(userdata){
			this.setInfo(this.opt.vipItem,userdata['level'],'vip');
			this.setInfo(this.opt.safeItem,userdata['security'],'safe');
			this.setInfo(this.opt.integrityItem,userdata['integrity'],'strong');
		}else{
			this.resetUserForm();
		}
	},
	getUrl:function(){
		this.opt.act_type = $(this.opt.formItem).find("[name='act_type']").val();
		this.opt.loginInUrl = this.opt.loginInUrl ? this.opt.loginInUrl : $(this.opt.formItem).attr("action");
		switch(this.opt.act_type) {
		  case 'serialize':  
			this.opt.ifAjax = this.opt.loginInUrl ? true : false;            
		    break; 
		  case 'redirect': 
		    break;               
		  case 'bbs':
		    break;
		  default: 
		    break;               
		};
		if(!this.opt.loginUrlType){
			this.opt.loginOutUrl = this.opt.loginOutUrl ? this.opt.loginOutUrl : this.opt.loginInUrl+'out';
		}else if(this.opt.loginUrlType = 'php'){
			this.opt.loginOutUrl = this.opt.loginOutUrl ? this.opt.loginOutUrl : this.opt.loginInUrl.slice(0,-4)+'out.php';
		}
		
	},
	loginIn:function(){
		if (this.opt.ifAjax) {
			$.ajax({
				type: "POST",
				dataType: "json",
				url: this.opt.loginInUrl,
				data: $(this.opt.formItem).formToArray(),
				getThis: this,
				success: function(msg){
					if (typeof(msg.type) != "undefined") {
						if (msg.type == 'loginok') {
							//alert('登录成功！');
							if (msg.datas) {
								this.getThis.setUserInfo(msg.datas,msg.uc_data);
							}
						}
						else {
							if (typeof(this.getThis.loginErrors[msg.code]) != "undefined") {
								alert(this.getThis.loginErrors[msg.code]);
								if(msg.code == 134){
									window.location = "http://www.lezi.com/index.php/login/index/?forward="+window.location.href+"&errno=134&userid="+$($(this.getThis.opt.formItem)[0].userid).val();
								}
							}
							else {
								alert('登陆失败!');
							}
							switch (msg.type) {
								case 'userid':
									$(this.getThis.opt.formItem)[0].userid.focus();
									break;
								case 'pwd':
									$(this.getThis.opt.formItem)[0].pwd.focus();
									break;
								case 'error':
									break;
								default:
									break;
							}
						}
					}
				},
				error: function(){
					alert('连接服务器错误，登录失败！');
				}
			});
			return false;
		}else{
			return true;
		}
	},
	loginOut:function(){
		$.ajax({
			type: "POST",
			url:this.opt.loginOutUrl,
			getThis:this,
			success: function(msg){
    			if (msg == 0){
					this.getThis.setUserInfo("");
					if(!this.getThis.opt.ifAjax && this.getThis.opt.ifLoginOutLink){
						window.location = $(this.getThis.opt.formItem).find("[name='forward']").val();
					}
				}else{
					alert("退出失败！");
				}  
  			},
			error:function(){
				alert('连接服务器错误，退出失败！');
			}
		})
		return false;
	},
	loginOutUc:function(){
		$.ajax({
			type: "POST",
			url:this.opt.loginOutUrl,
			getThis:this,
			dataType: "json",
			success: function(msg){
    			if (msg.error == 0){
					$("body").append(msg.uc_data);
					this.getThis.setUserInfo("");
					if(!this.getThis.opt.ifAjax && this.getThis.opt.ifLoginOutLink){
						window.location = $(this.getThis.opt.formItem).find("[name='forward']").val();
					}
				}else{
					alert("退出失败！");
				}  
  			},
			error:function(){
				alert('连接服务器错误，退出失败！');
			}
		})
		return false;
	}
} 
