// JavaScript Document
Cufon.replace('#menu li a', {hover: true});
Cufon.replace('h1', {hover: true});
Cufon.replace('h2:not(.nocufon)', {hover: true});

$(function(){
	$("input#sHuman").hide();
	$("input#human").hide();
	$(".external").attr("target","_blank");

	var h = window.location.host.toLowerCase();
	$("a[href^='http']:not([href^='http://" + h + "']):not([href^='http://www." + h + "']), a[href$='.pdf']").attr("target", "_blank");
	
	$('img.bgmaximage').maxImage({
		isBackground: true,
		overflow: 'auto'
 	});

	$('#content').fadeIn(400);

	$('#btnContactar').click(function() {
		$('#contact').addClass('active');
	});

	/*main menu*/
	var apartat = $('body').attr('id');
	$('#menu li a.'+ apartat).addClass('current');
	
	var seccio = $('body').attr('class');
	$('#menu li a.'+ seccio).addClass('current');
	
	/*right*/
	var ID = $('#content #entry').attr('class');
	$('#right a.'+ ID).addClass('current');
		
	//ajustar alçada del menu
	menuResize();
	$(window).resize(function() {
		menuResize();
	});
	
	//profesionals bg image
	$('body.equip #entry img').hide();
	$bgEquip = $("body.equip #entry img").attr("src");
	$('#content.equip').css('background-image', 'url('+$bgEquip+')');
	
	$('body.equipo #entry img').hide();
	$bgEquipo = $("body.equipo #entry img").attr("src");
	$('#content.equipo').css('background-image', 'url('+$bgEquipo+')');

	//footer logos
	theRotator();

	$('#slider').nivoSlider({
		effect:'fold', //Specify sets like: sliceDown,sliceDownLeft,sliceUp,sliceUpLeft,sliceUpDown,sliceUpDownLeft,fold,fade,random
		slices:15,
		animSpeed:500,
		pauseTime:5750,
		startSlide:0, //Set starting Slide (0 index)
		directionNav:true, //Next & Prev
		directionNavHide:true, //Only show on hover
		controlNav:true, //1,2,3...
		controlNavThumbs:false, //Use thumbnails for Control Nav
		controlNavThumbsFromRel:false, //Use image rel for thumbs
		controlNavThumbsSearch: '.jpg', //Replace this with...
		controlNavThumbsReplace: '_thumb.jpg', //...this in thumb Image src
		keyboardNav:true, //Use left & right arrows
		pauseOnHover:false, //Stop animation while hovering
		manualAdvance:false, //Force manual transitions
		captionOpacity:0.65, //Universal caption opacity
		beforeChange: function(){},
		afterChange: function(){},
		slideshowEnd: function(){} //Triggers after all slides have been shown
	});

	$('input,textarea').each(function() {
		var default_value = this.value;
		$(this).focus(function() {
			if(this.value == default_value) {
				this.value = '';
				$(this).addClass('active');
			}
		});
		$(this).blur(function() {
			if(this.value == '') {
				this.value = default_value;
				$(this).removeClass('active');
			}
		});
	});


	/*smooth scroll*/
	$('a[href*=#]').click(function() {
		if (location.pathname.replace(/^\//,'') == this.pathname.replace(/^\//,'') && location.hostname == this.hostname) {
			var $target = $(this.hash);
			$target = $target.length && $target || $('[name=' + this.hash.slice(1) +']');
			if ($target.length) {
				var targetOffset = $target.offset().top;
				$('html,body').animate({scrollTop: targetOffset}, 1000);
				return false;
			}
		}
	});

});	
//end document ready


function menuResize(){
	$hMenu = $('body').height() - $('#header').height() - $('#footer').height(); 
	$hContent = $('#content').height() + $('#header').height(); 
	if ($('#content').hasClass('equipo') || $('#content').hasClass('equip')) {$hContent = $hContent - 100}; /*resto height foto del professional*/
	if ($hMenu < $hContent) { $hMenu = $hContent};
	$('#menu').css("height", $hMenu+'px');
	//console.log('menu height = ' + $hMenu + 'px');
}


// http://www.alohatechsupport.net/webdesignmaui/maui-web-site-design/easy_jquery_auto_image_rotator.html
function theRotator() {
	//Set the opacity of all images to 0
	$('div#rotator ul li').css({opacity: 0.0});
	
	//Get the first image and display it (gets set to full opacity)
	$('div#rotator ul li:first').css({opacity: 1.0});
		
	//Call the rotator function to run the slideshow, 6000 = change to next image after 6 seconds
	setInterval('rotate()',3250);	
}

function rotate() {	
	//Get the first image
	var current = ($('div#rotator ul li.show')?  $('div#rotator ul li.show') : $('div#rotator ul li:first'));

	//Get next image, when it reaches the end, rotate it back to the first image
	var next = ((current.next().length) ? ((current.next().hasClass('show')) ? $('div#rotator ul li:first') :current.next()) : $('div#rotator ul li:first'));	
	
	//Set the fade in effect for the next image, the show class has higher z-index
	next.css({opacity: 0.0})
	.addClass('show')
	.animate({opacity: 1.0}, 750);

	//Hide the current image
	current.animate({opacity: 0.0}, 1000)
	.removeClass('show');	
};

function validarCita(lang) {
	var human = document.getElementById("human").value;
	var nom = document.getElementById("nom").value;
	var mail = document.getElementById("mail").value;
	var telf = document.getElementById("tel").value;
	var texte = document.getElementById("texte").value;

	if (human != "") { 
		return false;
	}

	if (nom == "") { 
		if (lang=='cat'){
			alert("Ha d'indicar un nom.")
		}else{
			alert("Debe indicar un nombre.")
		}
		return false;
	}

	if (telf == "" && mail == "") { 
		if (lang=='cat'){
			alert("Ha d'indicar un telefon o adreça de correu de contacte.")
		}else{
			alert("Debe indicar un teléfono o dirección de correo de contacto.")
		}
		return false;
	}

	if (mail !="") { 
		if (validaEmail(mail)==false) {
			if (lang=='cat'){
				alert("Adreça de correu incorrecta.")
			}else{
				alert("Dirección de correo incorrecta.")
			}
			return false;
		}
	}
	
	if (texte == "") { 
		if (lang=='cat'){
			alert("Ha d'indicar dia i hora.")
		}else{
			alert("Debe indicar dia y hora.")
		}
		return false;
	}

	return true;
}

function validar(lang) {
	var human = document.getElementById("sHuman").value;
	var nom = document.getElementById("sNom").value;
	var mail = document.getElementById("sMail").value;
	var telf = document.getElementById("sTel").value;
	var texte = document.getElementById("sText").value;

	if (human != "") { 
		return false;
	}

	if (nom == "Nom" || nom == "Nombre" ) { 
		if (lang=='cat'){
			alert("Ha d'indicar un nom.")
		}else{
			alert("Debe indicar un nombre.")
		}
		return false;
	}

	if (telf == "" && mail == "") { 
		if (lang=='cat'){
			alert("Ha d'indicar un telefon o adreça de correu de contacte.")
		}else{
			alert("Debe indicar un teléfono o dirección de correo de contacto.")
		}
		return false;
	}

	if (telf == "Tel&egrave;fon" || telf == "Tel&eacute;fono" || mail == "Correu electr&ograve;nic" || mail == "Correo electr&oacute;nico") { 
		if (lang=='cat'){
			alert("Ha d'indicar un telefon o adreça de correu.")
		}else{
			alert("Debe indicar un teléfono o dirección de correo.")
		}
		return false;
	}

	if (mail =="" || mail != "Correu electronic" || mail != "Correo electrónico") { 
		if (validaEmail(mail)==false) {
			if (lang=='cat'){
				alert("Adreça de correu incorrecta.")
			}else{
				alert("Dirección de correo incorrecta.")
			}
			return false;
		}
	}
	
	if (texte == "" || texte == "Indiqui la seva consulta o dubte" || texte == "Indique su consulta o duda" ) { 
		if (lang=='cat'){
			alert("Ha d'indicar el text.")
		}else{
			alert("Debe indicar el texto.")
		}
		return false;
	}

	return true;
}

function validaEmail(checkStr) {
	 var nI, sS, nN, nP;
	 // Arroba ...
	 sS = "@"; nN = 0; nP = 0;
	 for(nI = 0; nI < checkStr.length; nI++)
	 { if(checkStr.charAt(nI) == sS) 
	  { nN++; nP = nI;
	  }
	 }
	 if(nN == 0) return (false);
	 if(nN > 1)  return (false);
	 var nPArroba = nP;
	 // Punto ...
	 sS = "."; nN = 0; nP = 0;
	 for(nI = 0; nI < checkStr.length; nI++)
	 { if(checkStr.charAt(nI) == sS) 
	  { nN++; nP = nI;
	  }
	 }
	 if(nN == 0) return (false);
	 var nPPunto = nP;
	 // Posicion Arroba/Punto ...
	 if(nPArroba > nPPunto) return (false);
	 //
	 return (true);
}
