// abrir y cerrar opciones de búsqueda con jquery
  $(document).ready(function(){
/* javascript activado */
    $("body").addClass("js_active");

    //$("#busqueda_opciones").addClass("oculto");
    //$("#abrir_opciones").text("[+]");

    /*$("#abrir_opciones").click(function () {
      if ($("#busqueda_opciones").is(":hidden")) {
        $("#busqueda_opciones").slideDown("slow");
        $("#abrir_opciones").text("[ - ]");
      } else {
        $("#busqueda_opciones").slideUp("slow");
        $("#abrir_opciones").text("[+]");
      }
    });*/

// enlaces //
	$("#view-more").click(function (){
		$("#view-more").hide();
        $(".links-list").removeClass("list-height");
         return false;
    });

// overlay professional //
	$("#open-overlay-prof").click(function (){
		$("#overlay-prof").fadeIn();
		$("#contenedor").fadeOut();
	});
// overlay professional CLOSE //
	$("#overlay-prof span").click(function (){
		$("#overlay-prof").fadeOut();
		$("#contenedor").fadeIn();
	});
	
// abrir y cerrar opciones de registro / login / RSS
/* activar registro */
    $(" #li_registro").addClass("activo");
/* abrir/cerrar registro */
    $("#activar_registro").click(function (){
        $("#li_login").removeClass("activo");
        $("#li_rss").removeClass("activo");
        $("#li_registro").addClass("activo");
         return false;
    });
/* abrir/cerrar login */
    $("#activar_login").click(function (){
        $("#li_registro").removeClass("activo");
        $("#li_rss").removeClass("activo");
        $("#li_login").addClass("activo");
         return false;
    });
/* abrir/cerrar rss */
    $("#activar_rss").click(function (){
        $("#li_login").removeClass("activo");
        $("#li_registro").removeClass("activo");
        $("#li_rss").addClass("activo");
        return false;
    });
    $(".imprimir_nb").click(function (){
        window.print();
        return false;
    });
});


function abrirOpciones(){
	//$("#abrir_opciones").click(function () {
		if ($("#busqueda_opciones").is(":hidden")){
			$("#busqueda_opciones").slideDown("slow");
			$("#abrir_opciones").text(cerrar_opciones);
		}else{
			$("#busqueda_opciones").slideUp("slow");
			$("#abrir_opciones").text(abrir_opciones);
		}
	//});
}


// execute your scripts when DOM is ready. this is a good habit
$(function(){
	$("div.scrollable").scrollable();
});



/*** 
    Simple jQuery Slideshow Script
    Released by Jon Raasch (jonraasch.com) under FreeBSD license: free to use or modify, not responsible for anything, etc.  Please link out to me if you like it :)
***/

function slideSwitch(){
    var $active = $('#slideshow IMG.active');

    if ( $active.length == 0 ) $active = $('#slideshow IMG:last');

    // use this to pull the images in the order they appear in the markup
    var $next =  $active.next().length ? $active.next()
        : $('#slideshow IMG:first');

    // uncomment the 3 lines below to pull the images in random order

    // var $sibs  = $active.siblings();
    // var rndNum = Math.floor(Math.random() * $sibs.length );
    // var $next  = $( $sibs[ rndNum ] );


    $active.addClass('last-active');

    $next.css({opacity: 0.0})
        .addClass('active')
        .animate({opacity: 1.0}, 1000, function(){
            $active.removeClass('active last-active');
        });
}

$(function() {
    setInterval( "slideSwitch()", 5000 );
});

/*** 
    Simple jQuery TABS Script
    Released by Jon Raasch (jonraasch.com) under FreeBSD license: free to use or modify, not responsible for anything, etc.  Please link out to me if you like it :)
***/

$(function(){
	$("#nav ul").tabs("#panes > div", {});

	//
	//	Slideshow plugin for the Tabs
	//
	$("div.paraslide_tabs").tabs(".paraslide > div", { 
	 	    
			// start from the beginning after the last tab 

			// enable "cross-fading" effect
	        effect: 'fade', 
	        fadeOutSpeed: "slow",
	        rotate: true
  	 

	 
	    // use the slideshow plugin. It accepts its own configuration 
		}).slideshow({autoplay:true, interval: 10000, autopause: true});
	
	//$("div.paraslide_tabs").tabs().play();

});






//
//	Funciones de envio de datos por form
//
function enviaDadesCerca(url){
	//document.getElementById("clicatCerca").value = 1;

	document.forms.cerca.submit();
	//document.forms.gsc-search-box.submit();
}

//Añade el término de búsqueda a un fichero log
function anadirLog(elemento){
	if(elemento == null){
		frase = document.getElementById("searchterm").value;
	}
	else{
		frase = elemento;
	}
	if(frase !=''){
		$.ajax({
			type: "POST",
			url: "frontoffice/secciones/comunes/createlog.php",
			data: "ajax=1&frase="+frase
	 	});
	}
}

function envia_receta(){

	algunBuid = 0;
	nombre = document.getElementById("nombre").value;
	email = document.getElementById("mail").value;
	nombre_amigo = document.getElementById("nombre_amigo").value;
	email_amigo = document.getElementById("email_amigo").value;
	url_plana = document.getElementById("url_plana").value;
	titol_plana = document.getElementById("titol_plana").value;
	var filter=/^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/;
	

	
	if((email == "")||(email == "-1")||(email_amigo == "")||(email_amigo == "-1")||(nombre == "")||(nombre == "-1")||(nombre_amigo == "")||(nombre_amigo == "-1")){
		algunBuid = 1;
		$("#camp_buid").fadeIn("slow");
	}else{
		$("#camp_buid").fadeOut("slow");
	}

	if(!filter.test(email)){
		algunBuid = 1;
		$("#wrong_mail").fadeIn("slow");
	}else{
		$("#wrong_mail").fadeOut("slow");
	}

	if(!filter.test(email_amigo)){
		algunBuid = 1;
		$("#wrong_mail_amigo").fadeIn("slow");
	}else{
		$("#wrong_mail_amigo").fadeOut("slow");
	}

	if(algunBuid == 0){
	//loading('formulari_contact');
		$.ajax({
			type: "POST",
			url: "frontoffice/secciones/recetas/subscripcio.php",
			data: "ajax=1&nombre="+nombre+"&email="+email+"&nombre_amigo="+nombre_amigo+"&email_amigo="+email_amigo+"&url_plana="+url_plana+"&titol_plana="+titol_plana,

			success: function(msg){
				if(msg){
					cerrarModal();
				}else{
					$('#msg_error_mail').fadeIn("slow");
				}
			}
	 	});
	}
}


function envia_registro(){
	$('#error_1').hide();
	$('#error_2').hide();
	$('#error_3').hide();
	$('#error_4').hide();
	$('#error_aceptar').hide();
	
	nombre = document.getElementById("nombre").value;
	email = document.getElementById("email").value;
	pdf_reg = document.getElementById("pdf_reg").value;
	newsletter_reg = document.getElementById("newsletter_reg").value;
	informacion = document.getElementById("informacion").value;
	acepto = document.getElementById("acepto").checked;
	idioma = document.getElementById("idioma").value;
	var filter=/^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/;

	mailok=filter.test(email);

	if(acepto)
	{
		
		if ((mailok) && (nombre != '') && (email != '')){
	
			$.ajax({
				type: "POST",
				url: "frontoffice/secciones/registro/enviar_registro.php",
				data: "nombre="+nombre+"&email="+email+"&pdf_reg="+pdf_reg+"&newsletter_reg="+newsletter_reg+"&informacion="+informacion+"&acepto="+acepto+"&idioma="+idioma,
				success: function(inMsg){
					
					if(inMsg == 1){
						
						$('#info_orig').hide();
						$('#campos_reg').hide();
						
						$('#missatge_Ok').fadeIn("slow");
						$('#mail_conf').fadeIn("slow");
						
					}else{	
						if(inMsg == -2){
							$('#error_4').fadeIn("slow");
						}
						else{
							$('#error_3').fadeIn("slow");
						}
					}
				}
		 	});
		}else{
			if((nombre == '') || (email == '')|| (!acepto)){
				$('#error_1').fadeIn("slow");
			}
			if((email != '')&&(!mailok)){
				$('#error_2').fadeIn("slow");
			}
			
		}
	}
	else
	{
		//Leer y aceptar
		$('#error_aceptar').fadeIn("slow");
	}
}

function envia_comentario(){
	
	$('#error_1').hide();
	$('#error_2').hide();
	$('#error_3').hide();
	nombre = document.getElementById("author").value;
	
	email = document.getElementById("email").value;
	
	web = document.getElementById("web").value;
	
	comentario = document.getElementById("comment").value;
	
	idioma = document.getElementById("idioma").value;
	
	id_item = document.getElementById("id_item").value;

	seccion = document.getElementById("seccion").value;
	url_actual = document.getElementById("url").value;
	

	var filter=/^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/;

	mailok=filter.test(email);

	if ((mailok) && (nombre != '') && (email != '') && (comentario != '')){
		//loading('formulari_contact');
		$.ajax({
			type: "POST",
			url: "frontoffice/secciones/comentarios/guardar_comentario.php",
			data: "ajax=1&author="+nombre+"&email="+email+"&web="+web+"&comment="+comentario+"&idioma="+idioma+"&id_item="+id_item+"&seccion="+seccion+"&url="+url_actual,
			success: function(msg){
				//alert(msg);
				if(msg == "ok"){
					//alert('ENTRA');
					$('#mail_conf').fadeIn("slow");
					//clearCommentsForm();
					clearForm("commentform");
					
				}else{
					$('#error_3').fadeIn("slow");
					//clearCommentsForm();
					clearForm("commentform");
				}
			}
	 	});
	}else{
		if((nombre == '') || (email == '')|| (comentario == '')){
			$('#error_1').fadeIn("slow");
		}
		if((email != '')&&(!mailok)){
			$('#error_2').fadeIn("slow");
		}
		
	}
}

/*function clearCommentsForm() { 
 	document.getElementById("author").value = '';
 	document.getElementById("email").value = '';
 	document.getElementById("web").value = '';
 	document.getElementById("comment").value = '';
}*/

//Borrador de formularios
function clearForm(formIdent) { 
  var form, elements, i, elm; 
  form = document.getElementById ? document.getElementById(formIdent) : document.forms[formIdent]; 

	if (document.getElementsByTagName) {				
		elements = form.getElementsByTagName('input');
		for( i=0, elm; elm=elements.item(i++); ) {
			if (elm.getAttribute('type') == "text") {
				elm.value = '';
			} else if (elm.getAttribute('type') == "checkbox" || elm.getAttribute('type') == "radio") {
				elm.checked = false;
			} 
		}
		
		elements = form.getElementsByTagName('select');
		for( i=0, elm; elm=elements.item(i++); ) {
			elm.options.selectedIndex=0;
		}
		
		elements = form.getElementsByTagName('textarea');		
		for( i=0, elm; elm=elements.item(i++); ) {
			elm.value='';
		}
		
	} else {
		elements = form.elements;
		
		for( i=0, elm; elm=elements[i++]; ) {
			if (elm.type == "text") {
				elm.value ='';
			} else if (elm.type == "checkbox" || elm.type == "radio") {
				elm.checked = false;
			}
		}
	}
}




function seleccionaTodos(){

	for(var i=1; i<7; i++ ){
		document.getElementById('check_'+i).checked =document.getElementById('todos_rss').checked;
	}
	opciones = "all";
	document.getElementById('opciones_rss').value = opciones;
}

function seleccionaCheck(chk){
	opciones="";
	if(!document.getElementById('check_'+chk).checked){
		document.getElementById('todos_rss').checked = false;
	}
	contador=0;
	for(var i=1; i<7; i++ ){
		if(document.getElementById('check_'+i).checked == true){
			opciones += document.getElementById('check_'+i).value +",";
			contador++;
		}
	}
	if(contador==6){
		document.getElementById('todos_rss').checked = true;
	}

	document.getElementById('opciones_rss').value = opciones;

}

function enviaRSS(){
	opciones = document.getElementById('opciones_rss').value;
	url_rss = document.getElementById('url_rss').value;
	document.getElementById('form_rss').href = url_rss + opciones;
}

