

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 marcarMenuPrincipal(id){
	$("#hmp"+id).css("color","#FFFFFF");
}	
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 selTots(zona){
	$("#"+zona+" input[type='checkbox']").attr("checked",true);
}
function dSelTots(zona){
	$("#"+zona+" input[type='checkbox']").attr("checked",false);
}
function homeTabs(zona,zona2){
	$("#"+zona+" .homeNttab").click(function () {
		if($("#"+zona+" .tabActivo:first").attr('title') != $(this).attr('title')){
			$("#"+zona+" .tabActivo").removeClass("tabActivo");
			$(this).addClass("tabActivo");
			$("#"+zona2+" div").hide();
			$("#"+$(this).attr("title")).fadeIn(400);
		}
	});	
}
function homeTabsNoticies(){
	$("#homeNoticiesDiv .homeNttab").click(function () {
		if($("#homeNoticiesDiv .tabActivo:first").attr('title') != $(this).attr('title')){											 
			$("#homeNoticiesDiv .tabActivo").removeClass("tabActivo");
			$(this).addClass("tabActivo");
			$("#homeNtTabUltima").hide();
			$("#homeNtTabAltres").hide();
			$("#"+$(this).attr("title")).fadeIn(400);
		}
	});	
}
function homeCalendariHover(){
	 $(".homeCalendariCircuit").hover(
		 function () {
			$(this).css("background-position","bottom");
		  }, 
		  function () {
		   $(this).css("background-position","top");
		  }
	);
	 $(".homeCalendariCircuit").click(
		function(){
			window.location = WROOT+"circuits/campionat.php?id="+$(this).attr("title");
	});
}
function homeNoticies(){
	$.ajax({
			type: "POST",
			url:"home/AJ_homeNoticies.php",			
			success:function(response){
				var llista = response.firstChild;
				
					$('.homeNttabUltimaTitol').html($(llista).find('ultima').find('titol').text());
					$('.homeNttabUltimaData').html($(llista).find('ultima').find('data').text());
					$('.homeNttabUltimaNoticia').html($(llista).find('ultima').find('noticia').text());
			
					if($(llista).find('ultima').find('adjunt').text().length < 1){$('.homeNttabUltimaAdjunt').css("display","none");}
					else{
						$('#homeNttabUltimaAdjuntLink a').attr("href","adjunts/"+$(llista).find('ultima').find('adjunt').text());
					}
					if($(llista).find('ultima').find('imatge').text() == "no"){$('#homeNtTabUltima .imatge').css("visibility","hidden");}
					else{
						$('#homeNtTabUltima .imatge').css("visibility","visible");
						$('#homeNtTabUltima img').attr("src","webconf/getImage.php?t=noticies&c=imatge&id="+$(llista).find('ultima').find('id').text()+"&w=100");
						$('#homeNtTabUltima a[rel="lightbox"]').attr("href","webconf/getImage.php?t=noticies&c=imatge&id="+$(llista).find('ultima').find('id').text());
					 $('a[rel=lightbox]').lightBox();
					}													   
				
				
				$(llista).find('dades').children().each(function(){
					$('#homeNtTabAltres').append('<div>'+$(this).find('data').text()+' <a href="adjunts/'+$(this).find('adjunt').text()+'">'+$(this).find('titol').text()+'</a></div>');
																  
				});
			}
	});
}
function loadClassificacio(uurl){
	$.ajax({
			type: "POST",
			url:uurl+"home/AJ_homeClassif.php",	
			data:"max=8",
			success:function(response){
				var llista = response.firstChild;
				
				$('#homeCl50').append("<table></table>");
				$('#homeCl70').append("<table></table>");
				var fondo = true;
				var classFondo = "classifLlistafondo2";
				var classs;
				var txtLang;
				
				/*if(LANG == 'CA'){txtLang = "VEURE CLASSIFICACI&Oacute; COMPLETA DE 2009";}
				else if(LANG == 'ES'){txtLang = "VER CLASIFICACI&Oacute;N COMPLETA DEL 2009";}
				else if(LANG == 'FR'){txtLang = "CLASSMENT COMPL&Egrave;TE 2009";}
				
				$('#homeCl50 table').append('<tr class="'+classs+'" style="padding-bottom:5px;font-weight:bold;text-align:right"><td colspan="2"><a href="'+WROOT+'classif/TrofeuRACCPromovelocitat2009.pdf">'+txtLang+'</a></td></tr>');	
				$('#homeCl70 table').append('<tr class="'+classs+'" style="padding-bottom:5px;font-weight:bold;text-align:right"><td colspan="2"><a href="'+WROOT+'classif/TrofeuRACCPromovelocitat2009.pdf">'+txtLang+'</a></td></tr>');	*/
					
					
				$(llista).find('categoria1').children().each(function(){//50cc
					if(fondo){
						classs = classFondo;
						fondo = false;
					}else{
						classs="";
						fondo = true;
					}			
					
					if($(this).find('id').text() == "0"){var linkk = 'http://www.promovelocitat.cat/classif/classif.php?trofeuRacc';}
					else{var linkk = WROOT+'pilots/pilots.php?idpilot='+$(this).find('id').text();}
					
					
						$('#homeCl50 table').append('<tr class="'+classs+'"><td><a href="'+linkk+'">'+$(this).find('nom').text()+'</a></td><td style="text-align:right;width:50px">'+$(this).find('puntuacio').text()+'</td></tr>');																  
				});
				fondo = true;
				$(llista).find('categoria2').children().each(function(){//70cc
					if(fondo){
						classs = classFondo;
						fondo = false;
					}else{
						classs="";
						fondo = true;
					}				
					
					if($(this).find('id').text() == "0"){var linkk = 'javascript:void(0)';}
					else{var linkk = WROOT+'pilots/pilots.php?idpilot='+$(this).find('id').text();}
						
						$('#homeCl70 table').append('<tr class="'+classs+'"><td><a href="'+linkk+'">'+$(this).find('nom').text()+'</a></td><td style="text-align:right;width:50px">'+$(this).find('puntuacio').text()+'</td></tr>');																  
				});
			}
	});
}
function loadClassificacioGran(becats){
	var classFondo = "llistaPilotsFondo2";
	
	$('#classificacioGeneral table tr:gt(0)').remove();
	$('#classificacioGeneral table').append('<tr class="'+classFondo+'"><td colspan="5" style="padding-left:30px">carregant dades...</td></tr>');
	$.ajax({
			type: "POST",
			url:"AJ_classif.php",
			data: "categoria="+$("#classificacioCategoria").val()+"&becats="+becats,
			success:function(response){
				$('#classificacioGeneral table tr:gt(0)').remove();
				var llista = response.firstChild;
				
				var fondo = true;				
				var classs;
				
				var posicion = 1;
				
				$(llista).children().each(function(){
					if(fondo){
						classs = classFondo;
						fondo = false;
					}else{
						classs="";
						fondo = true;
					}	
					if($(this).find('id').text() == "0"){var linkk = 'javascript:void(0)';}
					else{var linkk = WROOT+'pilots/pilots.php?idpilot='+$(this).find('id').text();}
					
						$('#classificacioGeneral table').append('<tr class="'+classs+'"><td style="text-align:center">'+posicion+'</td><td style="padding-left:30px">'+$(this).find('dorsal').text()+'</td><td><a href="'+linkk+'">'+$(this).find('nom').text()+'</a></td><td class="moto">'+$(this).find('marca').text()+'</td><td style="text-align:right;width:50px">'+$(this).find('puntuacio').text()+'</td></tr>');	
						
						posicion++;
				});
			}
	});
}
function circuitsCalendariHover(num){
	 $(".calendariCircuit").hover(
		 function () {
			$(this).css("background-position","bottom");
		  }, 
		  function () {
		   $(this).css("background-position","top");
		  }
	);
	 $(".calendariCircuit").click(function(){desplegarCircuit(this)});
		
	 
	 if(num > 0){
		 desplegarCircuit($("#ccc"+num).next());		
	 }
}

function desplegarCircuit(x){
		if($("#llistaCircuits").height() < 899){
			if($.browser.msie){$("#llistaCircuits").height(1155);}
			else{$("#llistaCircuits").height(1174);}
		}
			$(".objectFlash").css("visibility","hidden");
			if(!$(x).next().hasClass("active")){$(".active").slideUp('normal');}
			$(".active").removeClass("active");
			$(x).next().slideDown('normal',function(){
				$(".objectFlash").css("visibility","visible");
				$('html, body').animate({
					scrollTop: $(x).offset().top
				}, 400);								
			});
			$(x).next().addClass("active");
}

function motosAcordio(){
	 $(".calendariCircuit").hover(
		 function () {
			$(this).css("background-position","bottom");
		  }, 
		  function () {
		   $(this).css("background-position","top");
		  }
	);
	 $(".calendariCircuit").click(
		function(){
		if($("#llistaCircuits").height() < 675){
			if($.browser.msie){$("#llistaCircuits").height(700);}
			else{$("#llistaCircuits").height(709);}
		}
		if(!$(this).next().hasClass("active")){
			$(".active").slideUp('normal');
			$(".infoCircuit.active").slideUp('normal');
			$(".active").removeClass("active");
			$(this).next().slideDown();
			$(this).next().addClass("active");
			$('html, body').animate({
					scrollTop: $(this).offset().top
				}, 400);	
		}
	});
}
function loadLlistaPilots(){
	$("#detallPilotDiv").hide();
	$("#llistaPilots").show();
	
	$("#pilotsCategoria").val();
	
	$.ajax({
			type: "POST",
			data:"AJaccio=llista&categoria="+$("#pilotsCategoria").val(),
			url:"AJ_pilots.php",			
			success:function(response){
				var llista = response.firstChild;
				
				$('#llistaPilots table tr:gt(0)').remove();
				var fondo = true;
				var classFondo = "llistaPilotsFondo2";
				var classs;
				$(llista).children().each(function(){//50cc
					if(fondo){
						classs = classFondo;
						fondo = false;
					}else{
						classs="";
						fondo = true;
					}				
						$('#llistaPilots table').append('<tr class="dorsal '+classs+' pointer" onclick="loadPilotDades('+$(this).find('id').text()+')"><td>'+$(this).find('dorsal').text()+'</td><td class="pilot">'+$(this).find('cognoms').text()+', '+$(this).find('nom').text()+'</td></tr>');																  
				});
			}
	});
}

function veureLlistaPilots(){
	$("#detallPilotDiv").hide();
	$("#llistaPilots").show();
	$("#back").hide();
}

function loadPilotDades(idPilot){
	$("#llistaPilots").hide();
	$("#back").show();
	$("#dPFotoCarnet").css("background-image","url(../pilots/loadingImage.png)");
	$("#dPFotoMoto").css("background-image","url(../pilots/loadingImage2.png)");
	$("#dPFotoCarrera").css("background-image","url(../pilots/loadingImage2.png)");
		$.ajax({
			type: "POST",
			data:"AJaccio=veure&idp="+idPilot,
			url:"AJ_pilots.php",			
			success:function(response){
				
				$('#dPFotoCarnet').empty();
				$('#dPFotoMoto').empty();
				$('#dPFotoCarrera').empty();
				$("#detallPilotDiv").show();
				var llista = response.firstChild;
				
				$("#dPPilotDadesNom").html($(llista).find("nom").text()+" "+$(llista).find("cognoms").text());
				$("#dPPilotDadesDatan").html($(llista).find("datan").text());
				$("#dPPilotDadesResidencia").html($(llista).find("ciutat").text()+"("+$(llista).find("pais").text()+")");
				$("#dPPilotDadesCategoria").html($(llista).find("categoria").text());
				$("#dPPilotDadesEquip").html($(llista).find("equip").text());
				$("#dPPilotDadesMoto").html($(llista).find("moto").text());
				$("#dPPilotDadesContacte").html($(llista).find("email").text());
				$("#dPPilotDadesWeb").html($(llista).find("web").text());
				$("#dPPilotDadesCurriculum").html($(llista).find("curriculum").text());
				
				loadPilotImage($(llista).find("id").text(),"Carnet");
				loadPilotImage($(llista).find("id").text(),"Moto");
				loadPilotImage($(llista).find("id").text(),"Carrera");
			}
		});	
}

function loadPilotImage(id,foto){

	  var img = new Image();
	  
	  // wrap our new image in jQuery, then:
	  $(img)
		// once the image has loaded, execute this code
		.load(function () {
		  // set the image hidden by default    
		  $(this).hide();
		
		  // with the holding div #loader, apply:
		  $('#dPFoto'+foto)
			// then insert our image
			.append(this);
		
		  // fade our image in to create a nice effect
		  $(this).fadeIn(function(){$("#dPFoto"+foto).css("background","none")});
		})
		// *finally*, set the src attribute of the new image to our image
		
		.attr('src', WROOT+'webconf/getImage.php?t=pilots_fotos&c=foto&idpilot='+id+'&foto='+foto);
		setTimeout(function(){$("#dPFoto"+foto).css("background","none")},3000);
}

function carregaPoblacions(zona){
	
	var prov = $("#"+zona+"Provincia").val();
	$("#"+zona+"Poblacio").empty();
	$.ajax({
			type: "POST",
			data:"provincia="+prov,
			url:WROOT+"webconf/AJ_getPoblacions.php",			
			success:function(response){
				var llista = response.firstChild;
		
				$(llista).children().each(function(){
					$("#"+zona+"Poblacio").append('<option value="'+$(this).find("id").text()+'">'+$(this).find("poblacio").text()+'</option>');
				});
			}
	});
}

function enviarFormulariBeques(){
	
	var inputs = $("#formbeques input");
	var enviar = true;
	
	/*$(inputs).each(function(){
		if($(this).val().length < 1){$(this).val("0");}	
	});*/
	
	var tn = $('#becaNomTutor').val();
	var td = $('#becaDniTutor').val();
	var nom = $('#becaNomPilot').val();
	var dni = $('#becaDniPilot').val();
	var llicencia = $('#becaLlicencia').val();
	var equip = $('#becaEquip').val();
	var ts = $('#becaTargetaSalut').val();
	var datan = $('#becaDatan').val();
	var telefon = parseInt($('#becaTelefon').val());
	var mobil = parseInt($('#becaMobil').val());
	var adresa = $('#becaAdresa').val();
	var poblacio = $('#becaPoblacio').val();
	var cp = parseInt($('#becaCp').val());
	var email = $('#becaEmail').val();
	var g1 = $('#becaGermans').val();
	var g2 = $('#becaGermans2').val();
	var ge1 = $('#becaGermanes').val();
	var ge2 = $('#becaGermanes2').val();
	var profmare = $('#becaProfmare').val();
	var profpare = $('#becaProfpare').val();
	var comentaris = $('#becaComentaris').val();
	
	if(tn.length < 1){enviar = false;}
	else if(td.length < 1){enviar = false;}
	else if(nom.length < 1){enviar = false;}
	else if(mobil.length < 9){enviar = false;}
	else if(email.length < 6){enviar = false;}
	else if(poblacio.length < 1){enviar = false;}
	else if(g1.length < 1){enviar = false;}
	else if(g2.length < 1){enviar = false;}
	else if(ge1.length < 1){enviar = false;}
	else if(ge2.length < 1){enviar = false;}
	else if(profmare.length < 1){enviar = false;}
	else if(profpare.length < 1){enviar = false;}

	
	if(enviar){
		$("#formBequesDiv").hide();
		$("#formEnviant").show();
		var variables = "becaNomTutor="+tn+"&becaDniTutor="+td+"&becaNomPilot="+nom+"&becaDniPilot="+dni+"&becaLlicencia="+llicencia+"&becaEquip="+equip+"&becaTargetaSalut="+ts+"&becaDatan="+datan+"&becaTelefon="+telefon+"&becaMobil="+mobil+"&becaAdresa="+adresa+"&becaPoblacio="+poblacio+"&becaComentaris="+comentaris+"&becaEmail="+email+"&becaG1="+g1+"&becaG2="+g2+"&becaGe1="+ge1+"&becaGe2="+ge2+"&becaProfmare="+profmare+"&becaProfpare="+profpare;
	alert(variables);
	$.ajax({
			type: "POST",
			data: variables,
			url:"AJ_beques.php",			
			success:function(response){
				$("#formbeques").reset();
				$("#formEnviant").hide();
				$("#formRebut").show();
			}
	});	
	}else{
		alert("S'han d'omplir totes les dades per poder enviar el formulari");	
	}
}

function enviarFormulariInfo(){
	$("#formDiv").hide();
	$("#formEnviant").show();
	var inputs = $("#inscriures input");
	
	$(inputs).each(function(){
		if($(this).val().length < 1){$(this).val("0");}	
	});
	
	var nom = $('#infoNom').val();
	var edat = parseInt($('#infoEdat').val());
	var telefon = parseInt($('#infoTelefon').val());
	var email = $('#infoEmail').val();
	var comentaris = $('#infoComentaris').val();
	
	var variables = "infoNom="+nom+"&infoEdat="+edat+"&infoTelefon="+telefon+"&infoEmail="+email+"&infoComentaris="+comentaris;
	
	$.ajax({
			type: "POST",
			data: variables,
			url:"AJ_info.php",			
			success:function(response){
				$("#inscriures").reset();
				$("#formEnviant").hide();
				$("#formRebut").show();
			}
	});	
}

function scrollToPos(id){
	$('html, body').animate({
			scrollTop: $("#"+id).offset().top
	}, 400);	
}

$.fn.extend({
	reset: function() {  
	return this.each(function() {
		$(this).is('form') && this.reset();  
	});  
    }  
});	
function homeVideoShow(){
	window.scrollTo(0,0);
	$("html, body").css("overflow","hidden");
	$("#homeBanner1Div").css("visibility","hidden");
	$("#videoHomeDiv").css("opacity","0");
	$("#videoHomeDiv").show();
	
	$("#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 
				} 
				}); 	
}