(function(){
	var username = null;
	var password = null;
	
	var check = function(){
		if(username == ''){
			showMessage('Tens que escrever o teu nome de utilizador!');
			return false;
		}
		if(password == ''){
			showMessage('Tens que escrever a tua palavra chave!');
			return false;
		}
		
		return true;
	};
	
	var A = {
		authenticate : function(username, password, stored, remember, handler){
			WindowShade.load();
			
			this.username = username;
			this.password = password;
			
			if(!check) return;
		
			if(!stored) password = hex_md5(password);
			
			$.ajax({
				url: '/Comunidade/html/SessionRequests.jsp',
				type: 'GET',
				dataType: 'json',
				data: 'action=authenticate&username=' + username + '&password=' + password,
  				error: function(fault){ showMessage('Ocorreu um erro! Tenta novamente.'); WindowShade.unload();},
				success: function(result){
					if(result.status == 1){
						if(remember){
							Cookies.set(Cookies.TAG_USER, username, 720);
							Cookies.set(Cookies.TAG_HASH, password, 720);
						} else {
							Cookies.set(Cookies.TAG_USER, username, 1);
						}
						
						Cookies.set(Cookies.TAG_SESSION, result.session, 1);
						
						handler(true);
					} else {
						showMessage(result.message);
						handler(false);
					}
					
					WindowShade.unload();
				}
			});
		},
		logout : function(){
			WindowShade.load();
			$.ajax({
				url: '/Comunidade/html/SessionRequests.jsp',
				type: 'GET',
				dataType: 'json',
				data: 'action=logout',
  				error: function(fault){ showMessage('Ocorreu um erro! Tenta novamente.'); WindowShade.unload();},
				success: function(result){
					if(result.status == 1){
						Cookies.reset(Cookies.TAG_USER);
						Cookies.reset(Cookies.TAG_HASH);
						Cookies.reset(Cookies.TAG_SESSION);
						window.location = '/Comunidade/';
					} else showMessage(result.message);
					
					WindowShade.unload();
				}
			});
		}
	};

	window.AuthenticationApplication = A;
})();

(function(window){
	var template = '\
		<div id="noContent" class="noContent"> \
		<span class="noContentTitle">{{title}}</span> \
		<span class="noContentMessage">{{message}}</span> \
	</div> \
	';

	var D = {};
	
	D.load = function(titleMsg, textMsg){
		var view = { title : titleMsg, message : textMsg };
		
		$('#content').css('height', null);
		
		Mustache.to_html(template, view, {}, function(val){
			$('#content').html(val);
		});
	};
	
	window.NoContent = D;
})(window);

(function(){
	var template = '\
		<div id="authentication_panel_container"> \
			<div id="authentication_panel_box"> \
				<div id="authentication_panel_message"></div> \
				<form id="authentication_panel_form" action="javascript:AuthenticationPanel.authenticate();"> \
					<input id="authentication_panel_user" type="text" \
								onkeydown="javascript:AuthenticationPanel.keyEvent(event.keyCode);" \
								class="authentication_panel_user"/> \
					<input id="authentication_panel_pass" type="password" \
								onkeydown="javascript:AuthenticationPanel.keyEvent(event.keyCode);" \
								class="authentication_panel_password"/> \
					<input id="authentication_panel_remember" type="checkbox" \
								class="authentication_panel_remember"/> \
					<div class="authentication_panel_button" \
							id="authentication_panel_submit" \
							onclick="javascript:AuthenticationPanel.authenticate();">ENTRAR</div> \
					<div class="authentication_panel_button" \
							id="authentication_panel_cancel" \
							onclick="javascript:AuthenticationPanel.close();">CANCELAR</div> \
				</form> \
				<a href="javascript:Registration.recover();" class="authentication_panel_recover"></a> \
				<div class="authentication_panel_button" id="authentication_panel_register" \
						onclick="javascript:window.open(\'/Comunidade/registar.jsp\', \'_self\');" \>REGISTAR</div> \
			</div> \
		</div> \
		';

	var link;
	var message;
	var defaultMsg = 'A tua sess\u00E3o expirou! Autentica-te novamente.';
	var storedHash = false;
	var username;
	
	var A = {
		load : function(link, msg){
			if($('#application_panel_container').html() != null) return;
			
			TimelineApplication.stopUpdates();
			Shoutbox.stopUpdates();
			Notifications.stopUpdates();
			
			WindowShade.load();
		
			this.link = link;
			this.message = (msg) ? msg : defaultMsg;
			this.storedHash = false;
			
			Mustache.to_html(template, {}, {}, function(val){
				$('body').append(val);
			});
			
			if(this.message != null){
				$('#authentication_panel_message').html(this.message); 
				$('#authentication_panel_message').show();
			} else $('#authentication_panel_message').hide(); 
			
			var userCookie = getStoredCookie('TAG-User');
			var hashCookie = getStoredCookie('TAG-Hash');
			
			if(userCookie != null && userCookie != '' &&
				hashCookie != null && hashCookie != ''){
				$('#authentication_panel_user').val(userCookie)
				$('#authentication_panel_pass').val(hashCookie);
				
				storedHash = true;
			}
		},
		authenticate : function(){
			WindowShade.load();
			var user = $('#authentication_panel_user').val();
			var pass = $('#authentication_panel_pass').val();
			var remember = ($('#authentication_panel_remember:checked') != null);
			
			this.username = user;

			AuthenticationApplication.authenticate(user, pass, storedHash, remember, AuthenticationPanel.authenticate_result);
		},
		authenticate_result : function(ok){
			AuthenticationPanel.authenticated(ok);
		},
		authenticated: function(ok){
			if(!ok) return;

			/*if(this.link == null)
				window.location = '/Comunidade/' + this.username;	
			else if(this.link == '')*/
				window.location.reload(true);
			/*else window.location = '/Comunidade' + link;*/
			
			this.link = null;
			this.message = '';
			
			AuthenticationPanel.close();
		},
		keyEvent : function(code){
			if(code == 13) AuthenticationPanel.authenticate();
		},
		close : function(){
			$('#authentication_panel_container').remove();
			WindowShade.unload();
		}
	};
	
	window.AuthenticationPanel = A;
})();

(function(){
	var username;

	var C = {
		authenticate : function(){
			var user = $('#authentication_top_user').val();
			var pass = $('#authentication_top_pass').val();
			var remember = $('#authentication_top_remember').val();
			
			this.username = user;
			
			AuthenticationApplication.authenticate(user, pass, false, remember, AuthenticationTopPanel.authenticate_result);
		},
		authenticate_result : function(ok){
			AuthenticationTopPanel.authenticated(ok);
		},
		authenticated : function(ok){
			if(!ok) return;
			
			window.location = '/Comunidade/' + this.username;
		}
	};
	
	window.AuthenticationTopPanel = C;
})();

