﻿$(window).bind('load', function() {
	var preload = new Array();
	$(".hover").each( function() {
		s = $(this).attr("src").replace(/\.(.+)$/i, "_on.$1");
		preload.push(s)
	});
	var img = document.createElement('img');
	$(img).bind('load', function() {
		if (preload[0]) {
			this.src = preload.shift();
		}
	}).trigger('load');
});

$(document).ready( function() {

	/* general hovers */
	$(".hover").each( function() {
		if ($(this).attr("src").match(/_on\.(.+)$/i)) {
			$(this).removeClass("hover");
		}
	});
	$(".hover").hover( function() {
		s = $(this).attr("src").replace(/\.(.+)$/i, "_on.$1");
		$(this).attr("src", s);
	}, function() {
		s = $(this).attr("src").replace(/_on\.(.+)$/i, ".$1");
		$(this).attr("src", s);
	});

		// abre a caixa de modal de contacto
		$("#login-btn").click( function() {
			// reset ao formulário
			limpaFormLogin('total');
			$('#modal').popupBlock();
		});

		$(".close").click( function() {
			$('#modal').popupClose();
		});

		$("#do-login").click( function() {
			var valido = true;

			limpaFormLogin('avisos');

			$('.obg').each( function() {
				if ($(this).val().length == 0) {
					valido = false;
					$('#' + this.id + 'Obg').show();
				} else {
					$('#' + this.id + 'Obg').hide();
				}
			});

			if (valido) {
				$.ajax( {
					type :"post",
					url :"getLogin.php",
					data :$("#formLogin").serialize(),
					success : function(msg) {
						if (msg == 1) {
							$('#formLogin').submit();
						} else {
							$("#resultado-login").show();
						}
						$("#nomeUtilizador").html(msg);
					}
				});
			}
			return false;
		});
		
		$("#alterarPass").click(function(){	
			$("#result-ok").html('');
			$("#velhaPass").val('');
			$("#novaPass1").val('');
			$("#novaPass2").val('');
			$("#resultado-pass").html('&nbsp;');
			$("#password-control").show();
			$("#warning").show();
		});
		
		$("#fecharPass").click(function(){
			$("#warning").hide();
			$("#password-control").hide();
		});

		function dataHandler(data){			
			if (data.event == 1){
				$("#warning").hide();
				$("#password-control").hide();
				$("#result-ok").html(data.msg);
				setTimeout('$("#result-ok").html("");',5000);
			} else if (data.event == 0){
				$("#resultado-pass").html(data.msg);	
			}
		}
		
		$("#form-mudar-pass").submit( function() {
			var valido = true;

			$('.obg').each( function() {
				if ($(this).val().length == 0 || $(this).val().length < 6) {
					valido = false;					
				} 
			});
			
			
			if (!valido){
				$.ajax( {
					type :"post",
					url :"aviso.php",
					data :$("#form-mudar-pass").serialize(),
					success : function(msg) {
						$("#resultado-pass").html(msg);
					}
				});
			}else if (valido) {


				$.getJSON('mudarPass.php',$("#form-mudar-pass").serialize(),dataHandler);
/*
				$.ajax( {
					type :"post",
					url :"mudarPass.php",
					data :$("#form-mudar-pass").serialize(),
					success : function(msg) {												
						if (msg == 'Palavra-passe alterada' || msg == 'Password has been changed' || msg == 'Contraseña se ha cambiado'){

							$("#warning").hide();
							$("#password-control").hide();
							$("#result-ok").html(msg);
							setTimeout('$("#result-ok").html("");',5000);
						} else {
							$("#resultado-pass").html(msg);
						}
					}
				});*/
			}
			return false;
		});
	});

function limpaFormLogin(tipo) {

	if (tipo === 'total') {
		$('#username').val('');
		$('#password').val('');
	}

	$('#usernameObg').hide();
	$('#passwordObg').hide();
	$('#resultado-login').hide();
}