jQuery(document).ready(function($) {


	var rand_no = Math.random();
	rand_no = rand_no * 12;


 	//alert($('#bggallery').length);
	
	 $('#bggallery').galleria({
		autoplay: 15000,
		carousel: false,
		image_crop: true,
	    transition: 'fade',
		image_margin: 0,
		thumbnails: false,
		transition_speed: 1000,
		frame: false,
		show: (rand_no),
		preload: 1,
	});

	
	if($(".zoom").length >0 ){
		$(".zoom").click(function(e) {
			e.preventDefault();
				$("#modal").modal({
					overlayClose:true,
					minHeight:400,
					maxWidth: 700
				});
		});
	}
	

	$("#sendmail-form").submit(function(){

			$(".panelmail").html('');

			var emailReg = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/;
			var mailtoVal = $("#mailto").val();
			var nombreVal = $("#nombre").val();
			var nomentitatVal = $("#nomentitat").val();
			var condicionsVal = $("#acces").val();

	
			if($('input:checkbox:checked').length == 0){
				$(".panelmail").html('<p class="error">És necessari acceptar les condicions d&acute;ús per poder entrar a la xarxa.</p>');

				setTimeout(function(){$(".panelmail").html('')},3000);
			}else if(nombreVal == ''){
				$(".panelmail").html('<p class="error">El nom de la persona de contacte és obligatori.</p>');

				setTimeout(function(){$(".panelmail").html('')},3000);
			}else if(nomentitatVal == ''){
				$(".panelmail").html('<p class="error">El nom de l&acute;entitat és obligatori.</p>');

				setTimeout(function(){$(".panelmail").html('')},3000);

			}else if(mailtoVal == ''){
				$(".panelmail").html('<p class="error">El correu electrònic és obligatori.</p>');

				setTimeout(function(){$(".panelmail").html('')},3000);
			}else if(!emailReg.test(mailtoVal)) {
				$(".panelmail").html('<p class="error">L&acute;adreça de correu electrònic és incorrecta.</p>');

				setTimeout(function(){$(".panelmail").html('')},3000);
			}else{

				var subject = mailtoVal;

				$(".panelmail").html('<p class="notice">enviant...</p>');

				//procedemos al envio, 
				$.post("http://localhost:8888/arrels/sendmail.php",
					{ subject: subject, mailto:mailtoVal,nombre:nombreVal},
				   	function(data,textStatus){

						$(".panelmail").html('<p class="success">enviat amb éxit, gràcies</p>');

						setTimeout(function(){$(".panelmail").html('')},3000);

					}
				);

			}

			return false;
	});

	if( $("#jfrCalendar").length >0 ){

	

		loadCalendar( $('#jfrCalendar').attr("rel") );

	};
	
	
	function loadCalendar(url){
		
		$.ajaxSetup({cache:false});

		$("#jfrCalendar").animate({ 
			opacity: "0.5"
			}, 200 ,function(){
				
			$("#jfrCalendar").html("<span class='loaderajax'></span>");

				$('#jfrCalendar').load(url, function() {

					$("#jfrCalendar").animate({ 
						opacity: "1"
						}, 200);

				 	$(".calendar-nav a").click(function(e) {
						e.preventDefault();
						loadCalendar( $(this).attr("href") );
					});
				});

						
		});
		
		
		
		


	}
	
	




	

	

});
