var Auth = new Class({
	initialize: function(active_area) {
		
		this.link_signup = $('lf-signup-link');
		this.link_le_signup = $('tb-le-signup');
		this.link_reg = $('tb-reg-link');
		this.link_signin = $('tb-le-signin');
		this.link_login = $('tb-login');
		this.link_passwdrec = $('tb-le-passwdrec');
		this.link_pr_send = $('tb-pr-link');
		this.link_pr_new_pwd = $('tb-chp-link');

		//Registration
		this.input_email = $('reg-email');
		this.input_password = $('reg-password');
		this.input_conf_password = $('reg-conf-password');
		this.input_nickname = $('reg-nickname');
		this.input_captcha = $('reg-captcha');
		this.input_agree = $('reg-agree');
		
		//Password recovery
		this.input_pr_email = $('pr-email');
		this.input_pr_captcha = $('pr-captcha');
		this.input_chp_email = $('pr-h-email');
		this.input_chp_code = $('pr-h-code');
		this.input_chp_pwd = $('pr-password');
		this.input_chp_pwd_conf = $('pr-conf-password');
		
		//Signin
		this.input_si_login = $('login');
		this.input_si_password = $('password');
		this.input_si_remember = $('remember_me');
		this.input_si_captcha = $('login-captcha');
		
		this.area_signin = $('tb-signin');
		this.area_reg = $('tb-registration');
		this.area_msg = $('tb-msg');
		this.area_si_error = $('tb-signin-error');
		this.area_pr = $('tb-password-recovery');
		this.area_chp = $('tb-change-password');
		
		this.bar_height = 30;
		this.show_error_timeout = 2500;
		
		if(active_area) {
			this.area_current = $(active_area);
		}
		else {
			this.area_current = this.area_signin;
		}
		
		this.setActionHandlers();
	}
});

Auth.implement({
	
	setActionHandlers: function() {
		
		var obj = this;
		
		this.link_signup.addEvent('click', function(e){
			
			new Event(e).stop();
			obj.showArea(obj.area_reg);
		});
		
		this.link_le_signup.addEvent('click', function(e){
			
			new Event(e).stop();
			obj.showArea(obj.area_reg);
		});
		
		this.link_signin.addEvent('click', function(e){
			
			new Event(e).stop();
			obj.showArea(obj.area_signin);
		});
		
		this.link_reg.addEvent('click', function(e){
			
			new Event(e).stop();
			obj.register();
		});
		
		this.link_login.addEvent('click', function(e){
			
			new Event(e).stop();
			obj.login();
		});
		
		this.link_passwdrec.addEvent('click', function(e){
			
			new Event(e).stop();
			obj.showArea(obj.area_pr);
		});
		
		this.link_pr_send.addEvent('click', function(e){
			
			new Event(e).stop();
			obj.passwordRecovery();
		});
		
		this.link_pr_new_pwd.addEvent('click', function(e){
			
			new Event(e).stop();
			obj.changePassword();
		});
	},
	
	changePassword: function() {
		
		var email = this.input_chp_email.value;
		var code = this.input_chp_code.value;
		var password = this.input_chp_pwd.value;
		var conf_password = this.input_chp_pwd_conf.value;
		var obj = this;
		
		new Request.JSON({
			url: window.location.toString(),
			onSuccess: function(resp) {
				
				if(resp.status == 'error') {
					obj.showMessage(resp.msg, obj.area_chp);
					if(resp.clear_password) {
						obj.input_chp_pwd.value = '';
						obj.input_chp_pwd_conf.value = '';
						
						obj.input_chp_pwd.addClass('hidden');
						$('pr-password-txt').removeClass('hidden');
						obj.input_chp_pwd_conf.addClass('hidden');
						$('pr-conf-password-txt').removeClass('hidden');
					}
				}
				else {
					window.location = '/';
				}
			}
		}).get({
			Event: 'SetPassword', 
			email: email, 
			code: code, 
			password: password, 
			conf_password: conf_password
		});
	},
	
	passwordRecovery: function() {
		
		var email = this.input_si_login.value;
		var captcha = this.input_pr_captcha.value;
		var obj = this;
		
		new Request.JSON({
			url: window.location.toString(),
			onSuccess: function(resp) {
				
				if(resp.status == 'error') {
					obj.showMessage(resp.msg);
				}
				else {
					obj.showMessage(resp.msg, obj.area_signin, true);
				}
			}
		}).get({
			Event: 'PasswordRecovery', 
			email: email, 
			captcha: captcha
		});
	},
	
	login: function(){
		
		var email = this.input_si_login.value;
		var password = this.input_si_password.value;
		var captcha = this.input_si_captcha.value;
		var obj = this;
		
		new Request.JSON({
			url: window.location.toString(), 
			onSuccess: function(resp){
				
				if(resp.status == 'error') {
					
					if(resp.msg) {
						obj.showMessage(resp.msg, null, false);
					}
					else {
						obj.showArea(obj.area_si_error, function(){
							
							if(resp.show_captcha) {
								$('login-captcha-img').removeClass('hidden');
								$('login-captcha').removeClass('hidden');
								$('al-search-form').addClass('hidden');
							}
						});
					}
				}
				else {
					if(resp.setcookie) {
						Cookie.write(resp.cookie.hash.name, resp.cookie.hash.value, {duration: resp.cookie.duration, path: resp.cookie.path});
						Cookie.write(resp.cookie.email.name, resp.cookie.email.value, {duration: resp.cookie.duration, path: resp.cookie.path});
					}
					window.location = window.location;
				}
			}
		}).get({
			Event: 'Login', 
			email: email, 
			password: password, 
			remember: obj.input_si_remember.checked ? 1 : 0,
			captcha: captcha
		});
	},
	
	showMessage: function(txt, area_return, is_success) {
		
		if(!area_return) {
			area_return = this.area_current;
		}
		
		var container = this.area_msg.getFirst('p');
		container.innerHTML = txt;
		
		if(is_success) {
			container.addClass('msg-success');
		}
		else {
			container.removeClass('msg-success');
		}
		
		this.showArea(this.area_msg);
		var obj = this;
		setTimeout(function(){
			obj.showArea(area_return);
		}, this.show_error_timeout);
	},
	
	register: function() {
		
		var email = this.input_email.value != this.input_email.title ? this.input_email.value : '';
		var password = this.input_password.value != this.input_password.title ? this.input_password.value : '';
		var conf_password = this.input_conf_password.value != this.input_conf_password.title ? this.input_conf_password.value : '';
		var nickname = this.input_nickname.value != this.input_nickname.title ? this.input_nickname.value : '';
		var captcha = this.input_captcha.value != this.input_captcha.title ? this.input_captcha.value : '';
		var agree = this.input_agree.checked ? 1 : 0;
		
		var obj = this;
		
		new Request.JSON({
			url: window.location.toString(), 
			onSuccess: function(resp){
				
				if(resp.status == 'error') {
					obj.showMessage(resp.msg);
					if(resp.clear_password) {
						obj.input_password.value = '';
						obj.input_conf_password.value = '';
						
						obj.input_password.addClass('hidden');
						$('reg-password-txt').removeClass('hidden');
						obj.input_conf_password.addClass('hidden');
						$('reg-conf-password-txt').removeClass('hidden');
					}
				}
				else {
					
					var container = obj.area_msg.getFirst('p');
					
					container.innerHTML = resp.msg;
					container.addClass('msg-success');
					obj.showArea(obj.area_msg);
					
					obj.input_email.value = '';
					obj.input_password.value = '';
					obj.input_conf_password.value = '';
					obj.input_nickname.value = '';
					obj.input_captcha.value = '';
					obj.input_agree.checked = false;
					
					setTimeout(function(){
						window.location = window.location.toString();
					}, obj.show_error_timeout);
				}
			}
		}).get({
			Event: 'Register', 
			email: email, 
			password: password, 
			conf_password: conf_password, 
			nickname: nickname, 
			captcha: captcha, 
			agree: agree
		});
	},
	
	showArea: function(area, complete_func) {
		
		complete_func = complete_func || function(){};
		
		var obj = this;
		
		area.setStyle('top', -obj.bar_height);
		area.removeClass('hidden');
		
		new Fx.Morph(obj.area_current, {
			duration: 250,
			onComplete: function() {
				obj.area_current.addClass('hidden');
				new Fx.Morph(area, {
					duration: 250,
					onComplete: function(){
						obj.area_current = area;
						complete_func();
					}
				}).start({
					top: [-obj.bar_height, 0]
				});
			}
		}).start({
			top: [0, obj.bar_height]
		});
	}
});
