/*
	Login modal used by the AJAX Login available at every page
*/

Login = Class.create()
Login.prototype = {
	
	initialize: function() {
		this.Root = globalRoot;

		this.onLogin;
		this.onLogout;

		Event.observe(window, 'load', this.Setup.bind(this));
	},

	Setup: function() {
		Event.observe('txtLoginEmail', 'keyup', this.KeyUp.bindAsEventListener(this));
		Event.observe('txtLoginPassword', 'keyup', this.KeyUp.bindAsEventListener(this));
	},

	KeyUp: function(event) {
		if (event.keyCode == 13) {
			this.Login();
		}
	},
	
	PopUp: function() {
		Effect.toggle('login','blind');
		setTimeout(function() { $('txtLoginEmail').focus(); }, 500);
	},

	/*Logout: function() {
		new Ajax.Request(globalRoot + '/users/logout', { onSuccess: function(transport) {
				res = transport.responseText.evalJSON();
				new Effect.BlindUp('topLogout', { duration: 0.5, scaleX: true, scaleY: false });
				$('loginMessage').hide();

				utils.SetCookie('vknowrememberemail', '', 60);
				utils.SetCookie('vknowrememberpass', '', 60);

				setTimeout(function() {
					if(typeof this.onLogout == "function")
						this.onLogout();

					new Effect.BlindDown('login', { duration: 0.5 });

					if(res.Reload)
						setTimeout('window.location.reload();', 510);
				}, 510);
			}.bind(this)
		});
	},
	*/

	Login: function() {
		//var ref = (arguments.length == 1) ? arguments[0] : '';
			
		var email = $F('txtLoginEmail');
		var password = $F('txtLoginPassword');
		
		if (email.length == 0) {
			$('txtLoginEmail').activate();
			return;
		}

		if (password.length == 0) {
			$('txtLoginPassword').activate();
			return;
		}
		
		$('loginError').innerHTML = 'Logging in...';
		new Effect.BlindDown('loginError', { duration: 0.2 });
		
		setTimeout(function() {
			var createCookie = $('cbLoginRememberMe').checked;
			var params = 'data[User][email]=' + email + '&data[User][password]=' + password;
			
			new Ajax.Request(globalRoot + '/users/login', { parameters: params, onSuccess: function(transport) {
					
					new Effect.BlindUp('loginError', { duration: 0.3 });
					
					if (typeof this.onLogin == "function") {
						onlogin = this.onLogin;
					}

					var res = transport.responseText.evalJSON();

					if (res.Success) {
						this.UserId = res.UserId;

						if (createCookie) {
							utils.SetCookie('vknowrememberemail', email, 60);
							utils.SetCookie('vknowrememberpass', password, 60);
						}
					}

					setTimeout(function() {
						if (res.Success) {

							new Effect.BlindUp('login', { duration: 0.5 });
							setTimeout(function() {					
								if (ref) {
									location.href = globalRoot + ref;
								} else {
									if (typeof onlogin == "function") {
										onlogin(res.UserId, res.Username, res.Role);
									}
									if (res.Reload) {
										window.location.reload();
									}
								}
							}, 510);
						
						} else {

							$('loginError').innerHTML = res.Message;
							new Effect.BlindDown('loginError', { duration: 0.3 });
							$('txtLoginEmail').activate();

							if (res.RedirectUrl && res.RedirectUrl.length > 0) {
								location.href = globalRoot + res.RedirectUrl;
							}
						}
					}, 310);
				}.bind(this)
			});
		}.bind(this), 210);
	},

	MyPage: function() {
		location.href = globalRoot + '/users/view/' + this.UserId;
	}
}

var login = new Login();