var iFotoFons = 1;
function subMenuPrincipal(){
  
     $("#headMPrincipal li .tesub").hover(function() { //When trigger is clicked...  
 
         //Following events are applied to the subnav itself (moving subnav up and down)  
         $(this).parent().find(".subnav").slideDown('fast').show(); //Drop down the subnav on click  
   
         $(this).parent().hover(function() {  
         }, function(){  
             $(this).parent().find(".subnav").fadeOut(); //When the mouse hovers out of the subnav, move it back up  
         });  
   
         //Following events are applied to the trigger (Hover events for the trigger)  
         }).hover(function() {  
             $(this).addClass("MPsubhover"); //On hover over, add class "subhover"  
         }, function(){  //On Hover Out  
             $(this).removeClass("MPsubhover"); //On hover out, remove class "subhover"  
     });
}
function subMenuPrincipal2(){
  
     $(".tesub2").hover(function() { //When trigger is clicked...  
 
         //Following events are applied to the subnav itself (moving subnav up and down)  
         $(this).parent().find(".subnav2").show(); //Drop down the subnav on click  
   
         $(this).parent().hover(function() {  
         }, function(){  
             $(this).parent().find(".subnav2").fadeOut(); //When the mouse hovers out of the subnav, move it back up  
         });  
   
         //Following events are applied to the trigger (Hover events for the trigger)  
         }).hover(function() {  
             $(this).addClass("MPsubhover2"); //On hover over, add class "subhover"  
         }, function(){  //On Hover Out  
             $(this).removeClass("MPsubhover2"); //On hover out, remove class "subhover"  
     });
}
function slideFonsBody(){
	var times = 999;
	$('.bodyFons').show();
	$(document).everyTime(10000, function() {
	 	slideFonsBodyLoad();
	}, times);
}

function slideFonsBodyLoad(){
	
	if(iFotoFons==8){iFotoFons=0;}
	iFotoFons++;
	
	var fade = false;
	
	$(function () {
	  var img = new Image();
	  $('.bodyFons').fadeOut(1000, function(){
					 
		$(img).load(function () {
			
			$('.bodyFons').empty().append(this).fadeIn(700);
			
		}).attr('src', WROOT+'img/fondos/'+iFotoFons+".jpg").fadeIn(700);						
	});
	  // wrap our new image in jQuery, then:
	 
	});		
}

function liveFons(){
var $winwidth = $(window).width();
			
			if($winwidth > 1281){
				var widthFinal=1200;
				var lleft=((widthFinal/6)-150);
			}else{
				var widthFinal=$winwidth;
				var lleft="0";
			}
			
			$('.bodyFons img').css({
					'width': widthFinal,
					'margin': 'auto',
					'left': lleft
				});


	$(window).bind("resize", function(){
			var $winwidth = $(window).width();
			
			if($winwidth > 1281){
				var widthFinal=1200;
				var lleft=widthFinal/6;
			}else{
				var widthFinal=$winwidth;
				var lleft="0";
			}
			
			$('.bodyFons img').css({
					'width': widthFinal,
					'margin': 'auto',
					'left': lleft
				});
			
	});
}

function marcarMenuPrincipal(id){
	$("#hmp"+id).css("color","#ffde00");
}	
function mZona(capa,showLoad){
	if(showLoad){showLoading();}
	$(".zonaContent").hide();
	$('#'+capa).fadeIn('fast')
}
function showLoading(){
	$('#loadingAjax').show();
	$('#zonaContentDiv').hide();
}

function hideLoading(){
	$('#loadingAjax').hide();
	$("#zonaContentDiv").fadeIn(700);
}

function homePastillas(){

	$(".pastilla").hover(
		function () {//over
			$(this).stop();
			if($.browser.msie){$(this).animate({bottom: "-20px"},400);}
			else{$(this).animate({bottom: "20px"},400);}
		}, 
		function () {//out
			$(this).stop();
			if($.browser.msie){$(this).animate({bottom: "-100px"},300);}
			else{$(this).animate({bottom: "-45px"},300);}
		}
	);
}

function preinscribir(){
	
	var nombre = $("#preNombre").val();
	var apellidos = $("#preApellidos").val();
	var dni = $("#preDni").val();
	var categoria = $("#preCategoria").val();
	var mobil = $("#preMobil").val();
	var email = $("#preEmail").val();
	
	var valida = true;
	
	if(dni.length < 9 || dni =='12345678Z'){valida = false;alert("DNI incorrecto");}
	else if(nombre.length < 2 || apellidos.length < 2){valida = false;alert("Nombre o apellidos incorrectos");}
	else if(email.length < 6 || email.indexOf("@") == -1 || email.indexOf(".") == -1){valida = false;alert("Email incorrecto");}
	else if(mobil.length < 9 || mobil == '666111333'){valida = false;alert("Mobil incorrecto");}
	
	
	
	if(valida){
		if(confirm("Va a finalizar su preinscripci\u00f3n. Desea continuar?")){
		$("#paso1").hide();
		$("#paso2").show();
		$.ajax({
			type: "POST",
			url:"aj/AJ_preinscripciones.php",
			data:"nombre="+nombre+"&apellidos="+apellidos+"&dni="+dni+"&categoria="+categoria+"&mobil="+mobil+"&email="+email,
			success:function(response){   
				$("#paso2").hide();
				$("#paso3").show();
			}
		});
		
		}//confirm
	}//if valida
	
}

function desplegar(){
		 $(".desplegable").hover(
		 function () {
			$(this).css("background-position","bottom");
		  }, 
		  function () {
		   $(this).css("background-position","top");
		  }
	);
	 $(".desplegable").click(
		function(){
		if($("#desplegableDiv").height() < 675){
			if($.browser.msie){$("#desplegableDiv").height(700);}
			else{$("#desplegableDiv").height(709);}
		}
		if(!$(this).next().hasClass("active")){
			$(".active").slideUp('normal');
			$(".desplegableInfo.active").slideUp('normal');
			$(".active").removeClass("active");
			$(this).next().slideDown();
			$(this).next().addClass("active");
			$('html, body').animate({
					scrollTop: $(this).offset().top
				}, 400);	
		}
	});
}
function boxesValidar(){
		var frmOk;
		
		frmOk=false;
					if($("#nombre").val().length < 1){
						frmOk=false;
						alert('El nombre es obligatorio');
						$("#nombre").focus();
					}
					else if($("#apellidos").val().length < 1){
						frmOk=false;
						alert('El apellido es obligatorio');
						$("#apellidos").focus();
					}
					else if(validarDni($("#dni").val())==false){
						frmOk=false;
						alert('El dni es obligatorio');
						("#dni").focus();
					}
					else if($("#email").val().length < 6){
						frmOk=false;
						alert('El Email es obligatorio');
						$("#email").focus();
						
					}else if (compruebaMail($("#email").val())==false){
						frmOk=false;
						$("#email").focus();
					}else{frmOk = true;}
		
		if(frmOk){$("#boxesForm").submit();}
}
$.fn.extend({
	reset: function() {  
	return this.each(function() {
		$(this).is('form') && this.reset();  
	});  
    }  
});
function compruebaMail(emailStr,campo){
	
	var emailPat=/^(.+)@(.+)$/
	var specialChars="\\(\\)<>@,;:\\\\\\\"\\.\\[\\]"
	var validChars="\[^\\s" + specialChars + "\]"
	var quotedUser="(\"[^\"]*\")"
	var ipDomainPat=/^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/
	var atom=validChars + '+'
	var word="(" + atom + "|" + quotedUser + ")"
	var userPat=new RegExp("^" + word + "(\\." + word + ")*$")
	var domainPat=new RegExp("^" + atom + "(\\." + atom +")*$")
	var matchArray=emailStr.match(emailPat)
	

	if (matchArray==null) {
		if( campo=='')
		{
			alert('El email es incorrecto');
		}
		else
		{
			alert('El email es incorrecto');
		}
		return false
	}
	var user=matchArray[1]
	var domain=matchArray[2]
	if (user.match(userPat)==null) {
		alert('El email es incorrecto');
		return false
	}
	
	var IPArray=domain.match(ipDomainPat)
	if (IPArray!=null) {
		  for (var i=1;i<=4;i++) {
			if (IPArray[i]>255) {
			alert('El email es incorrecto');
			return false
			}
		}
		return true
	}
	
	var domainArray=domain.match(domainPat)
	if (domainArray==null) {
		
		
		if( campo=='')
		{
			alert('El email es incorrecto');
		}
		else
		{
			alert('El email es incorrecto');
		}
		return false
	}
	
	var atomPat=new RegExp(atom,"g")
	var domArr=domain.match(atomPat)
	var len=domArr.length
	if (domArr[domArr.length-1].length<2 || 
		domArr[domArr.length-1].length>3) {
		
			alert('El email es incorrecto');
			
		return false
	}
	
	if (len<2) {
	  
			alert('El email es incorrecto');
		
		return false
	}
	return true;
}

function letraDni(dni){
		cadena="TRWAGMYFPDXBNJZSQVHLCKET"
		posicion = (dni.substring(0,8) % 23);
		letra = cadena.substring(posicion,posicion+1)
		if(letra==dni.substring(8)){return true;}
		else{return false;}
	} 
function validarDni(dni){
		
		var valor = dni.toUpperCase();
		if(valor.length == 9){
			if(valor.substring(0,8) == parseInt(valor.substring(0,8))){
				if(letraDni(valor)){return true;}
				else{alert("La letra del DNI introducido no es correcta");return false;}
			}else{alert("El DNI introducido no es correcto");return false;}
		}else{
			return false;
		}
}

function homeVideoShow(video,panoramic){
	window.scrollTo(0,0);
	$("html, body").css("overflow","hidden");
	$("#homeBanner1Div").css("visibility","hidden");
	$("#videoHomeDiv").css("opacity","0");
	$("#videoHomeDiv").show();
	$("#videoHomeDivOverlay a").attr("href","/flv/"+video+".flv");
	if(!panoramic){$("#videoHomeDivOverlay a").css("width",parseInt(405*4/3)+"px");}
	$("#videoHomeDiv").fadeTo('normal',0.8,function(){
		$("#videoHomeDivOverlay").fadeIn('fast',function(){
				flowplayer("videoHome", "/flv/flowplayer-3.1.5.swf", { 
				clip:  { 
					autoPlay: true, 
					autoBuffering: true 
				} 
				}); 												 
		});				   
	});
}

function homeVideoShowHide(){
	$("#homeBanner1Div").css("visibility","visible");
	$("#videoHomeDivOverlay").hide();
	$("#videoHomeDiv").fadeOut('fast');
	$("html, body").css("overflow","auto");
	flowplayer("videoHome", "/flv/flowplayer-3.1.5.swf", { 
				clip:  { 
					autoPlay: false, 
					autoBuffering: true 
				} 
				}); 	
}