var headerBannerNum = -1;
var headerBannerTotal = 0;
$(function() {
	/* FORM ACCESO */
	$("#acceso").bind('submit', function(e) { 
		e.preventDefault();
		var error = false;
		var mensaje = '';
		var valorUsuario = $("#acceso label.input input#user").val().split(' ').join('');
		var valorPass = $("#acceso label.input input#pass").val().split(' ').join('');
		if (valorUsuario == '' || valorUsuario == 'usuario') { 
			error = true; 
			if (mensaje.length > 0) mensaje += "\n"; 
			mensaje += "No has introducido tu identificador de usuario"; 
		}
		if (valorPass == '' || valorPass == 'contraseña') { 
			error = true; 
			if (mensaje.length > 0) mensaje += "\n"; 
			mensaje += "No has introducido tu contraseña"; 
		}
		if (error) { alert(mensaje); return false; }
		this.submit();
		return false;
	});
	$("#acceso label.input input#user")
		.bind('focus', function() { 
			if ($(this).val() == 'usuario') 
				$(this).val('').css('color', '#000'); 
		}).bind('blur', function() { 
			if ($(this).val().split(' ').join('') == '' || $(this).val().split(' ').join('') == 'usuario') 
				$(this).val('usuario').css('color', '#3BC0DD'); 
		}).trigger('blur');
	$("#acceso label.input input#pass")
		.replaceWith('<input type="text" name="pass" id="pass" value="contraseña" rel="contraseña" style="color:#3BC0DD" onfocus="campoPass()"/>');
	
	/* FORM OBRAS */
	$("#obras").bind('submit', function(e) { 
		e.preventDefault();
		var error = false;
		var mensaje = '';
		var valorTelefono = $("input#telefono").val().split(' ').join('');
		var valorEmail = $("input#email").val().split(' ').join('');
		if (valorTelefono == '' || valorTelefono == 'telefono' || valorEmail == '' || valorEmail == 'email') { 
			error = true; 
			if (valorEmail == 'email') valorEmail = ''; 
			if (mensaje.length > 0) mensaje += "\n"; 
			mensaje += "Al menos un email o un telefono es necesario!!"; 
		}
		var filtroEmail = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
		if (valorEmail.length > 0 && !filtroEmail.test(valorEmail)) { 
			error = true; 
			if (mensaje.length > 0) mensaje += "\n"; 
			mensaje += "Si pones un email, mira que sea correcto!"; 
		}
		if (error) { alert(mensaje); return false; }
		this.submit();
		return false;
	});
	$("#obras label.input input#telefono")
		.bind('focus', function() { 
			if ($(this).val() == 'teléfono') 
				$(this).val('').css('color', '#000'); 
		}).bind('blur', function() { 
			if ($(this).val().split(' ').join('') == '' || $(this).val().split(' ').join('') == 'teléfono') 
				$(this).val('teléfono').css('color', '#3BC0DD'); 
		}).trigger('blur');
	$("#obras label.input input#email")
		.bind('focus', function() { 
			if ($(this).val() == 'email') 
				$(this).val('').css('color', '#000');
		}).bind('blur', function() { 
			if ($(this).val().split(' ').join('') == '' || $(this).val().split(' ').join('') == 'email')
			 	$(this).val('email').css('color', '#3BC0DD'); 
		}).trigger('blur');
	
	/* BANNER HEADER */
	$("#header .banner ul li").hide();
	headerBannerTotal = $('#header .banner ul li').length - 1;
	headerBanner();
});
function headerBanner() {
	if (headerBannerNum < headerBannerTotal) headerBannerNum++; else headerBannerNum = 0;
	$('#header .banner ul li').fadeOut('slow');
	$($('#header .banner ul li')[headerBannerNum]).stop().fadeIn('slow');
	setTimeout(headerBanner, 4000);
}
function campoPass(modo) {
	var campo = $("#acceso label.input input[rel='contraseña']");
	if (modo) {
		if (campo.val().split(' ').join('') == '' || campo.val().split(' ').join('') == 'contraseña')
			campo.replaceWith('<input type="text" name="pass" id="pass" value="contraseña" rel="contraseña" style="color:#3BC0DD" onfocus="campoPass()"/>');
	} else {
		campo.replaceWith('<input type="password" name="pass" id="pass" value="" rel="contraseña" style="color:#000" onblur="campoPass(true)"/>');
		$("#acceso label.input input[rel='contraseña']").focus();
	}
}