window.login = {


	login: function(user, opts) {
		jh.post('/api/auth/login', user, function(status, data) {
			if (status > 199 && status < 300) {
				var user = data;
				if (typeof opts.onSuccess == "function") {
					opts.onSuccess(user);
				}
			} else {
				if (typeof opts.onError == "function") {
					var err = data;
	
					if (err) {
						if (!(err instanceof Array)) {
							err = [err];
						}

						var errs = [];
						var i = null;
						for (i = 0; i < err.length; i++) {
							errs.push(err[i] || { code: 'UnexpectedError' });
						}

						opts.onError(errs);
					}
				}
			}
		});
	},

	
	form: function(opts) {
		if (!opts) {
			opts = {};
		}

		var 
			that = this,
			formId = opts.formId || "loginForm",
			usernameId = opts.usernameId || "loginUsername",
			passwordId = opts.passwordId || "loginPassword",
			loginErrorId = opts.loginErrorId || "loginErrorId",

			loginButtonId = opts.loginButtonId || "loginButton",
			activityIndicatorId = opts.activityIndicatorId || "authActivityIndicator",

			onSubmitHandler = typeof opts.onSubmitHandler == "function" ? opts.onSubmitHandler : function() {},
			onSuccessHandler = typeof opts.onSuccessHandler == "function" ? opts.onSuccessHandler : function() { 
				window.location= '/'; 
			},
			onErrorHandler = typeof opts.onErrorHandler == "function" ? opts.onErrorHandler : function(errs) {
				if (!errs || !errs.length) {
					return;
				}
	
				var 
					e = null, 
					i = null,
					el = null,
					field = null,
					errEl = null;
	
				for (i = 0; i < errs.length; i++) {
					e = errs[i];
					if (e.field) {
						if (e.field == "username") {
							field = usernameId;
						} else if (e.field == "password") {
							field = passwordId;
						}
					}
					if (field) {
						el = jh.dom(field);
						jh.addClass(el, "error");
						errEl = jh.dom(field + 'Error');
						errEl.innerHTML = e.label || e.error;
						jh.show(errEl);
						
					} else {
						errEl = jh.dom(signupErrorId);
						errEl.innerHTML = e.label || e.error;
						jh.show(errEl);
					}
				}
	
			},
				
			clearErrors = typeof opts.clearErrors == "function" ? opts.clearErrors : function() {
				clearError(usernameId + "Error");
				clearError(passwordId + "Error");
				clearError(loginErrorId);
			},
			clearError = typeof opts.clearError == "function" ? opts.clearError : function (id) {
				var el = jh.dom(id);
				el.innerHTML = "";
				jh.hide(el);
			},

			disable = function() {
				jh.disable(loginButtonId);
				jh.disable(usernameId);
				jh.disable(passwordId);
				jh.show(activityIndicatorId);
			},

			enable = function() {
				jh.enable(loginButtonId);
				jh.enable(usernameId);
				jh.enable(passwordId);
				jh.hide(activityIndicatorId);
			},

			form = jh.dom(formId),
			username = jh.dom(usernameId),
			password = jh.dom(passwordId),

			translate = typeof opts.translate === "function" ? opts.translate : function(key) {
				if (window.__labels) {
					return window.__labels[key] || key;
				}
				return key;
			};


		form.onsubmit = function() {
			var 
				user = {};

			user.username = username.value;
			user.password = password.value;

			disable();
			clearErrors();
			onSubmitHandler(user);


			var errors = [];
			if (!user.username) {
				errors.push({
					error: translate('UsernameRequired'),
					field: "username"
					});
			}
			if (!user.password) {
				errors.push({
					error: translate('PasswordRequired'),
					field: "password"
					});
			}
			if (errors.length) {
				onErrorHandler(errors);
				enable();
				return false;
			}


			//clear errors
			that.login(user, {
				onSuccess: function(user) {
					onSuccessHandler(user);
					enable();
				},

				onError: function(errors) {
					var 
						errs = [];

					for (var i = 0; i < errors.length; i++) {
						err = errors[i] || {};
						var label = translate(err.code || 'UnexpectedError');
						errs.push({
							field: err.field,
							label: label
						});
					}

					onErrorHandler(errs);
					enable();
				}
			});

			return false;
		};
	}



};

