/*+--------------------------------------+
* Name: login
* Function: Processing login, registration related operations
* Anwsin official:http://www.fbschool.com/tanyacms/
* developer:ms134n
* Email:ms134n@gmail.com 
* Copyright © 2012 - TanyaCMS community, All Rights Reserved 
* Date:2012-08-01
*+--------------------------------------+*/

//Log background carousel
var logPicture = [
		{url:GET_STATIC_URL + '/common/login_01.jpg',txt:'Welcome to TanyaCMS questions and answers community, do you want to know everything, and perhaps answers are all here.<br/><span class="tny-cl_05">1.0.0 version released!</span>'},
		{url:GET_STATIC_URL + '/common/login_02.jpg',txt:'Celebrate TanyaCMS 1.0.0 version!'},
		{url:GET_STATIC_URL + '/common/login_03.jpg',txt:'TanyaCMS is a new community web application, including questions and answers, wikis, and social networking features are rich and lightweight.<br/><span class="tny-cl_05">This will be your best choice!</span>'}
];


(function($){$.extend({login:{userName:function(options){var flgs=$.extend({fg:false,txt:"Email/Username",id:null},options||{});$(flgs.id).val()==""?$(flgs.id).val(flgs.txt):"";$(flgs.id).focus(function(){if($(this).val()==flgs.txt){$(this).val("")}flgs.fg?$(this).parent("li").addClass("cur"):$(this).addClass("tny-cur")}).blur(function(){flgs.fg?$(this).parents("li").removeClass("cur"):$(this).removeClass("tny-cur");flgs.val=$(this).val().replace(/^\s+|\s+/g,"");if(flgs.reg){flgs.val.length>16?$.register.reg.pwhandle("blur",$(this)):$.register.reg.pwhandle("right",$(this))}}).keydown($(flgs.id).parents("ul").attr("id")=="sn_oauth"?function(){$("#bingglobal_err").addClass("tny-hide")}:$.glo_Err);return this},password:function(options){var flgs=$.extend({txtval:"Password",txt:"#login_password_txt",pw:"#login_password",reg:false,fg:false,val:null},options||{});$(flgs.txt).focus(function(){$(this).addClass("tny-hide");$(flgs.pw).removeClass("tny-hide");$(flgs.pw).focus()});$(flgs.pw).focus(function(){flgs.fg?$(this).parents("li").addClass("cur"):$(this).addClass("tny-cur");if(flgs.reg){$.register.reg.pwhandle("focus",$(this))}}).blur(function(){if($(this).val()==""){$(this).addClass("tny-hide");$(flgs.txt).removeClass("tny-hide")}flgs.fg?$(this).parents("li").removeClass("cur"):$(this).removeClass("tny-cur");flgs.val=$(this).val().replace(/^\s+|\s+/g,"");if(flgs.reg){flgs.val.length>16?$.register.reg.pwhandle("blur",$(this)):$.register.reg.pwhandle("right",$(this))}}).keydown($(flgs.txt).parents("ul").attr("id")=="sn_oauth"?function(){$("#bingglobal_err").addClass("tny-hide")}:$.glo_Err);return this},keyEve:function(fn){$(document).keypress(function(e){var e=e?e:window.event;if((e.ctrlKey&&e.keyCode==13)||(e.ctrlKey&&e.keyCode==10)||e.keyCode==13){if(fn!=null){fn(e)}}})},loginSubmit:function(fn){if($.trim($("#login_userName").val())==""||$("#login_userName").val()=="Email/Username"){$("#login_userName").focus();if($("#login_userName").parents("ul").attr("id")=="sn_oauth"){$("#bingglobal_err").removeClass("tny-hide").find("span").html("Username cannot be empty")}else{$.EveErr("Username cannot be empty.")}return false}else{if(typeof(hex_md5)=="function"){$("#login_password").val(hex_md5($("#login_password").val()))}if(fn!=null&&fn.type=="keypress"){ajax_post($("#login_form"),_login_form_processer)}else{if(fn!=null){fn()}}return}},info:function(){var flgs=true;if($.isNull($("#login_userName"))&&$.isNull($("#login_password"))){$.isNull($("#sn_oauth"))?flgs=false:flgs;this.userName({fg:flgs,id:"#login_userName"}).password({fg:flgs})}if(flgs){!$.isNull($("#msn_login"))?$.scrollPic().scroll():"";this.keyEve(this.loginSubmit)}}},glo_Err:function(){if(!$("#global_err").hasClass("tny-hide")){$("#global_err").addClass("tny-hide")}},EveErr:function(html){$("#global_err").removeClass("tny-hide").find("span").html(html)},email_verify:function(){$.login.password();$.register.s.authCode()},pas_retrieve:function(){$.login.userName({id:"#login_userName"});$.register.s.authCode()},wb_Logins:function(flg){if($("#sn_oauth").css("display")=="none"){$("#sn_oauth").fadeIn("slow");$(flg).find("em:first-child").addClass("cur")}else{$("#sn_oauth").fadeOut("slow");$(flg).find("em:first-child").removeClass("cur")}},register:{s:{callFn:function(){$.login.userName({id:"#login_account",txt:"Account (email)"}).password({reg:false,txt:"#socialnet_password_txt",pw:"#socialnet_password"});return this},callback:function(fn){$.register.s.login(true,fn)},authCode:function(ev){var cd,val;cd=ev==null?false:ev;$("#authCode").focus(function(){if($(this).val()=="Verification code"){$(this).val("")}if(cd){$.register.reg.code("focus",$(this))}$(this).addClass("tny-cur")}).blur(function(){if($(this).val()==""){$(this).val("Verification code")}val=$(this).val().replace(/^\s+|\s+/g,"");if(val.length<4&&!cd){$.EveErr("Entry code error")}else{if(cd){if(val.length<4||val.length>4){$.register.reg.code("blur",$(this))}else{$.register.reg.code("right",$(this))}}}$(this).removeClass("tny-cur")}).keypress($.glo_Err);return this},usr_Treaty:function(){$("#user_agreement").click(function(){if($("#user_agreement").attr("checked")!="checked"){$.glo_Err()}})},EveClick:function(){$("#user_agre").css("display")=="none"?$("#user_agre").fadeIn("slow"):$("#user_agre").fadeOut("slow")},login:function(flgs,fn){if(!flgs){if($.trim($("#login_userName").val())==""||$.trim($("#login_userName").val())=="Username"){$("#login_userName").focus();$.EveErr("Username cannot be empty.");return false}}if($.trim($("#login_account").val())==""||$.trim($("#login_account").val())=="Account (email)"){$("#login_account").focus();$.EveErr("Account can not be empty.");return false}else{if($("#user_agreement").attr("checked")!="checked"){$.EveErr("Please check, I agreeUser agreement");return false}else{if(fn!=null){fn()}return false}}},info:function(){if($.isNull($("#login_account"))){this.callFn().authCode().usr_Treaty();var txt_pw,pw,val,txt_pw=$("#socialnet_password_txt");pw=$("#socialnet_password");$(txt_pw).focus(function(){$(this).addClass("tny-hide");$(pw).removeClass("tny-hide");$(pw).focus()});$(pw).focus(function(){$(this).addClass("tny-cur")}).blur(function(){if($(this).val()==""){$(this).addClass("tny-hide");$(txt_pw).removeClass("tny-hide")}$(this).removeClass("tny-cur");val=$(this).val().replace(/^\s+|\s+/g,"");if(val.length<6){$.EveErr("Password is incorrect, please enter the six-digit password is greater than.")}}).keydown($.glo_Err)}}},reg:{xExp:/^[A-Za-z0-9\u4e00-\u9fa5]{2,14}$/,email:/^[A-Za-z\d]+([-_\.\+]*[A-Za-z\d])*@([A-Za-z\d][A-Za-z\d-]{0,61}[A-Za-z\d]\.)+[A-Za-z\d]{2,6}$/,class_id:{blur:"reg_Err",focus:"reg_tips",right:"reg_right tny-small",load:"login_load"},User:{focus:"Enter 2-14 characters",blur:'<em class="regErr_ico tny-small"></em>Incorrect'},Acc:{focus:"Enter your Email",blur:'<em class="regErr_ico tny-small"></em>Entry email error'},Pw:{focus:"Enter 6-16 characters",blur:'<em class="regErr_ico tny-small"></em>Password Entry error'},Cd:{focus:"Click image to change",blur:'<em class="regErr_ico tny-small"></em>Entry code error'},n:null,register:function(){var ln=$("#login_userName"),la=$("#login_account"),_seif=this;return{username:function(){$(ln).val()==""?$(ln).val("Username"):"";$(ln).focus(function(){var s=$(this);if(s.val()=="Username"){s.val("")}_seif.tips($(this),_seif.class_id.focus,_seif.User.focus);if(s.val()){$.ajax({type:"GET",url:GET_BASE_URL+"/account/ajax/check_username/",beforeSend:function(){_seif.tips(s,_seif.class_id.load,"")},success:function(x){var result=new Function("return"+x)();if(result.errno==-1){_seif.tips(s,_seif.class_id.focus,_seif.n=result.err)}}})}s.addClass("tny-cur")}).blur(function(){var s=$(this);if(s.val()==""){s.val("Username");_seif.tips(s,"","")}else{$.ajax({type:"GET",url:GET_BASE_URL+"/account/ajax/check_username/username-"+encodeURIComponent(s.val()),beforeSend:function(){_seif.tips(s,_seif.class_id.load,"")},success:function(x){var result=new Function("return"+x)();if(result.errno==1){_seif.tips(s,_seif.class_id.right,"")}else{if(result.errno==-1){_seif.tips(s,_seif.class_id.blur,'<em class="regErr_ico tny-small"></em>'+result.err)}}}})}s.removeClass("tny-cur")}).keypress($.glo_Err);return this},accountnum:function(){$(la).val()==""?$(la).val("Account (email)"):"";$(la).focus(function(){if($(this).val()=="Account (email)"){$(this).val("")}_seif.tips($(this),_seif.class_id.focus,_seif.Acc.focus);$(this).addClass("tny-cur")}).blur(function(){if($(this).val()==""){$(this).val("Account (email)")}if(_seif.email.test($(this).val())){_seif.tips($(this),_seif.class_id.right,"")}else{_seif.tips($(this),_seif.class_id.blur,_seif.Acc.blur)}$(this).removeClass("tny-cur")}).keypress($.glo_Err);return this},password:function(){$.login.password({reg:true}).keyEve(this.callback);return this},authCode:function(){$.register.s.authCode(true);return this},callback:function(fn){$.register.s.login(false,fn)},info:function(){this.username().accountnum().password().authCode()}}},tips:function(obj,classname,html){$(obj).parent().find("span").eq(0).attr("class",classname).html(html)},pwhandle:function(ev,o){if(ev=="blur"){this.tips(o,this.class_id.blur,this.Pw.blur)}else{if(ev=="focus"){this.tips(o,this.class_id.focus,this.Pw.focus)}else{if(ev=="right"){this.tips(o,this.class_id.right,"")}}}},code:function(ev,o){if(ev=="blur"){this.tips(o,this.class_id.blur,this.Cd.blur)}else{if(ev=="focus"){this.tips(o,this.class_id.focus,this.Cd.focus)}else{if(ev=="right"){this.tips(o,this.class_id.right,"")}}}},info:function(){if($.isNull($("#login_userName"))){this.register().info()}}}},tanyacms:function(){if($.isNull($("#user_login"))||$.isNull($("#sn_oauth"))){$.login.info()}$.isNull($("#user_register"))?$.register.reg.info():"";$.isNull($("#login_socialnet"))?$.register.s.info():"";$.isNull($("#email_verify"))?$.email_verify():"";$.isNull($("#psw_retrieve"))?$.pas_retrieve():""},isNull:function(obj){if(obj==null){return}return typeof obj=="object"&&obj.length>0?true:false},scrollPic:function(){var flgArr=[],flg=false;$(logPicture).each(function(f){flgArr[f]=new Image();flgArr[f].src=logPicture[f].url});return this},scroll:function(){var ul=$("<ul/>");xx="",setTimes=null,flg=1,timer=6000,domWidth=$(document).innerWidth(),domHeight=$(document).innerHeight(),flgs=$("<div/>").attr("id","login_scroll").addClass("abs tny-hide").css({height:domHeight,overflow:"hidden"}).appendTo(document.body).fadeIn("slow");$(logPicture).each(function(x){$("<li/>").css({height:domHeight,backgroundImage:"url("+logPicture[x].url+")",opacity:x>0?0:1,zIndex:x>0?1:2}).appendTo(ul)});$(ul).attr("id","srcElem").addClass("l_xUler").appendTo(flgs).fadeIn("slow");setTimes=setInterval(function(){$("#login_explain").html($(logPicture)[flg].txt);$("#srcElem > li").eq(flg++).animate({opacity:1,zIndex:2},"slow").siblings().animate({opacity:0,zIndex:1},"fast");flg>=$(logPicture).length?flg=0:flg},timer)}})})(jQuery);function _login_form_processer(result){if(typeof(result.errno)=="undefined"){$.alert(result);$("#login_password").attr("value","")}else{if(result.errno!=1){$("#login_password").attr("value","");if(result.rsm){$.EveErr(result.err)}else{if(document.getElementById("tip_error_message")){$("#tip_error_message").html(result.err).show()}else{$.alert(result.err)}}}else{if(result.rsm&&result.rsm.url){window.location=decodeURIComponent(result.rsm.url)}else{window.location.reload()}}}};