var Auth=Object.extend({constructor:function(A){window.auth=this;
this.statistics=new Statistics(this);
this.api=new API(this);
var B=this;
this.plate=document.create({name:"plate sign-out",append:document.getElementById("plate-container"),child:[{name:"arrow"},{name:"select"},{name:"border",child:[{name:"left"},{name:"center",child:{child:{text:"Authorization"}}},{name:"right"}]},{name:"icon"}]});
this.balloon=new Widget.BalloonAuth({application:this,width:240,arrow:"bottom-right",attach:function(C){B.plate.insertSiblingBefore(C)
}})
}});
Widget.BalloonAuth=Widget.Balloon.extend({constructor:function(D){this.$base();
this.api=D.application.api;
if(document.cookies.get("ticket")){this.app(true)
}Object.append(this.node,{signIn:{},signUp:{},passwordRecovery:{},passwordReset:{}});
document.create({name:"auth",append:this.node.content,reference:{main:this.node},on:{focus:{listener:this.focus.bind(this),capture:true}},child:[{name:"sign-in",tag:"form",attribute:{mode:"sign-in"},on:{submit:this.signIn.bind(this)},child:[{name:"caption",text:"Sign in"},{name:"field mail",tag:"label",child:[{text:"Email: "},new Widget.Input({hint:"Your email",field:"mail",input:{mail:this.node.signIn}}),{name:"error",text:"invalid email address"}]},{name:"field password",tag:"label",child:[{child:["Password (",{tag:"span",name:"link",text:"forgot?",on:{click:(function(){this.mode("password-recovery")
}).bind(this)}},"):"]},new Widget.Input({hint:"Your password",field:"password",type:"password",input:{password:this.node.signIn}}),{name:"error",text:"Are you sure? Once again please"}]},{name:"field remember",tag:"label",child:[new Widget.Toggle({checked:true,on:"remember me",off:"don't remember me",input:{remember:this.node.signIn}})]},{name:"field submit",child:[new Widget.Input({value:"Sign in",type:"submit"})]},{name:"switch",child:[{tag:"span",name:"link",text:"Sign up"},", if don’t have an account"],on:{click:(function(){this.mode("sign-up")
}).bind(this)}}]},{name:"sign-up",tag:"form",attribute:{mode:"sign-up"},on:{submit:this.signUp.bind(this)},child:[{name:"caption",text:"Sign up"},{name:"field mail",tag:"label",child:[{text:"Email: "},new Widget.Input({hint:"It will be your login",field:"mail",input:{mail:this.node.signUp}}),{name:"error",attribute:{error:"user-already-exists"},text:"user you specified already exists"},{name:"error",text:"Invalid email address"}]},{name:"field name",tag:"label",child:[{text:"Name: "},new Widget.Input({hint:"Your full name, please",field:"name",input:{name:this.node.signUp}}),{name:"error",text:"Please enter your full name"}]},{name:"field password",tag:"label",child:[{text:"Password: "},new Widget.Input({hint:"Six characters or more (be creative!)",field:"password",type:"password",input:{password:this.node.signUp}}),{name:"error",text:"We want more characters, at least six"}]},{name:"field remember",tag:"label",child:[new Widget.Toggle({checked:true,on:"remember me",off:"don't remember me",input:{remember:this.node.signUp}})]},{name:"field submit",child:[new Widget.Input({value:"Sign up",type:"submit"})]},{name:"switch",child:[{tag:"span",name:"link",text:"Sign in"},", if you already have an account"],on:{click:(function(){this.mode("sign-in")
}).bind(this)}}]},{name:"password-recovery",tag:"form",attribute:{mode:"password-recovery"},on:{submit:this.passwordRecovery.bind(this)},child:[{name:"caption",text:"Password recovery"},{name:"field mail",tag:"label",child:[{text:"Email: "},new Widget.Input({hint:"Enter email you used to register",field:"mail",input:{mail:this.node.passwordRecovery}}),{name:"error",text:"Invalid email address"},{name:"error",attribute:{error:"user-not-found"},text:"We dunno that email"}]},{name:"field submit",child:[new Widget.Input({value:"Reset my password",field:"password",type:"submit"})]},{name:"message",attribute:{message:"success"},child:[{text:"Instructions were sent by email, check it out."}]},{name:"switch",child:["You also may ",{tag:"span",name:"link",text:"sign in",on:{click:(function(){this.mode("sign-in")
}).bind(this)}}," or ",{tag:"span",name:"link",text:"sign up",on:{click:(function(){this.mode("sign-up")
}).bind(this)}},"."]}]},{name:"password-reset",tag:"form",attribute:{mode:"password-reset"},on:{submit:this.passwordReset.bind(this)},child:[{name:"caption",text:"Reset password"},{name:"field password",tag:"label",child:[{text:"Choose a new password: "},new Widget.Input({hint:"Six characters or more (be creative)",field:"password",type:"password",input:{password:this.node.passwordReset}}),{name:"error",text:"must have at least 6 characters"}]},{name:"field remember",tag:"label",child:[new Widget.Toggle({checked:true,on:"remember me",off:"don't remember me",input:{remember:this.node.passwordReset}})]},{name:"field submit",child:[new Widget.Input({value:"Change password and sign in",type:"submit"})]}]},{name:"throbber",attribute:{mode:"throbber"},text:"Loading..."}]});
this.modes={"sign-in":{title:"Sign In",focus:this.node.signIn.password,handler:function(){var E=this.mail();
if(E){this.node.signIn.mail.valueOf(E);
this.node.signIn.password.focus();
this.node.signIn.password.select()
}else{this.node.signIn.mail.focus();
this.node.signIn.mail.select();
this.node.signIn.password.valueOf("")
}}},"sign-up":{title:"Sign Up",focus:this.node.signUp.mail},"password-recovery":{title:"Password recovery",focus:this.node.passwordRecovery.mail,handler:function(){var E=this.mail();
if(E){this.node.passwordRecovery.mail.valueOf(E)
}}},"password-reset":{title:"Reset password",focus:this.node.passwordReset.password},throbber:{title:"Loading..."}};
var C;
var A=(location.search.match(/token=(\w+)/)||[])[1];
if(A){C="password-reset";
this.token=A
}var B=location.hash.replace("#","");
if(!C&&B in this.modes){C=B
}this.mode(C||"sign-in")
},mode:function(F,H){var C=this.node.main,B=C.firstChild,I=this.modes[F]||{},A;
C.setAttribute("mode",F);
while(B){if(B.hasAttribute("mode")){var G=B.getAttribute("mode")==F;
B.style.display=G?"block":"none";
if(G){A=B
}}B=B.nextSibling
}A.assignClass("show-message",H);
var D=A.getChildren("message");
for(var E=D.length;
E--;
){B=D[E];
B.style.display=(H==B.getAttribute("message"))?"block":"none"
}document.title="Smthngs → "+I.title||"...";
if(I.focus){I.focus.focus();
I.focus.select()
}if(I.handler){I.handler.call(this)
}if(F!="throbber"){location.hash="#"+F
}},app:function(B){var A=window.location,C=B?"/app":"/";
if(A.pathname==C){window.location.reload(true)
}else{A.pathname=C
}},signIn:function(E){E.preventDefault();
var D=this.node.signIn;
var C={mail:D.mail.value,password:D.password.value,remember:!!D.remember.checked};
var A=true;
if(!this.testMail(C.mail)){A=this.error(D.mail)
}if(C.password.length<6){A=this.error(D.password)
}if(!A){return 
}if(!C.mail||!C.password){return 
}this.mail(C.mail);
this.mode("throbber");
function B(F){if(F.status=="ok"){this.app()
}else{this.mode("sign-in");
this.error(D.password)
}}this.request({action:"sign-in",parameters:C,response:B})
},signUp:function(E){E.preventDefault();
var D=this.node.signUp;
var C={mail:D.mail.valueOf(),name:D.name.valueOf(),password:D.password.valueOf(),remember:!!D.remember.checked};
var A=true;
if(!this.testMail(C.mail)){A=this.error(D.mail)
}if(!C.name){A=this.error(D.name)
}if(C.password.length<6){A=this.error(D.password)
}if(!A){return 
}this.mail(C.mail);
this.mode("throbber");
function B(F){if(F.status=="ok"){this.app()
}else{this.mode("sign-up");
switch(F.code){case"user-already-exists":this.error(D.mail,"user-already-exists");
break;
default:this.error(D.mail)
}}}this.request({action:"sign-up",parameters:C,response:B})
},passwordRecovery:function(E){E.preventDefault();
var D=this.node.passwordRecovery;
var C={mail:D.mail.valueOf()};
var A=true;
if(!this.testMail(C.mail)){A=this.error(D.mail)
}if(!A){return 
}this.mail(C.mail);
this.mode("throbber");
function B(F){if(F.status=="ok"){this.mode("password-recovery","success")
}else{this.mode("password-recovery");
switch(F.code){case"user-not-found":this.error(D.mail,"user-not-found");
break;
default:this.error(D.mail)
}}}this.request({action:"password-recovery",parameters:C,response:B})
},passwordReset:function(E){E.preventDefault();
var D=this.node.passwordReset;
var C={password:D.password.valueOf(),remember:D.remember.checked,token:this.token};
var A=true;
if(!this.testPassword(C.password)){A=this.error(D.password)
}if(!A){return 
}this.mode("throbber");
function B(F){if(F.status=="ok"){this.app()
}else{this.mode("password-reset");
this.error(D.password)
}}this.request({action:"password-reset",parameters:C,response:B})
},focus:function(B){var A=B.target.getParent("field");
if(A){this.error(A,false)
}},error:function(C,A){if(A===undefined){A=true
}C=C.getParent("field");
var B=C.firstChild;
while(B){if(B.hasClass("error")){B.style.display=((A===true&&!B.hasAttribute("error"))||(A!==false&&B.getAttribute("error")==A))?"block":"none"
}B=B.nextSibling
}},testMail:function(A){return(/^([a-z0-9_\-]+\.)*[a-z0-9_\-]+@([a-z0-9][a-z0-9\-]*[a-z0-9]\.)+[a-z]{2,4}$/i).test(A)
},testPassword:function(A){return A.length>=6
},mail:function(A){if(typeof A=="string"){document.cookies.set("mail",A,{duration:365})
}else{return document.cookies.get("mail")||""
}},request:function(B){function A(){this.mode("error")
}B.context=this;
B.fail=A;
this.api.request(B)
}});
