$(document).ready(function(){FM_login=new FMLogin();});function FMLogin(){this.is_login_shown=false;this.hasCookie=function(name){var arg=name+"=";var alen=arg.length;var clen=document.cookie.length;var i=0;while(i<clen){var j=i+alen;if(document.cookie.substring(i,j)==arg){return true;}
i=document.cookie.indexOf(" ",i)+1;if(i==0)break;}
return false;}
$('#loginFormSubmit').click(this.doLogin.bind(this));$('#logout').click(this.doLogOut.bind(this));if(!this.hasCookie("USERAUTH")){$('#loginForm').show('fast');this.is_login_shown=true;}else{this.verifyCookie();}}
FMLogin.prototype.doLogin=function(){var params=$('#loginForm').formSerialize()+"&form=true";$.ajax({type:'POST',url:'/map_edit/login.php',data:params,success:this.loginReply.bind(this),error:this.loginError.bind(this)});$('#loginForm').fadeOut('slow');return false;}
FMLogin.prototype.doLogOut=function(){$.ajax({type:'POST',url:'/map_edit/login.php',data:'logout=true',success:this.loginReply.bind(this),error:this.loginError.bind(this)});$('#loggedIn').hide('slow');$('#collectionList').hide('slow');return false;}
FMLogin.prototype.verifyCookie=function(){$.ajax({type:'POST',url:'/map_edit/login.php',data:'',success:this.loginReply.bind(this),error:this.loginError.bind(this)});}
FMLogin.prototype.showLoginBox=function(){if(!this.is_login_shown){$('#loggedIn').hide();$('#collectionList').hide('slow');$('#loginForm').fadeIn('slow');this.is_login_shown=true;}}
FMLogin.prototype.loginReply=function(responseText){var jsonReply=eval("("+responseText+")");if(jsonReply.reply.login){$('#userID')[0].innerHTML=jsonReply.reply.message;$('#loggedIn').fadeIn('slow');$('#collectionList').fadeIn('slow');this.is_login_shown=false;$('#password').val("");}else{$('#loginForm').fadeIn(500);this.is_login_shown=true;if(jsonReply.reply.message&&jsonReply.reply.message.legth){$('#userID').val(jsonReply.reply.message);$('#userID').fadeIn('slow');}}}
FMLogin.prototype.loginError=function(t){alert('Error '+t.status+' -- '+t.statusText);}