function equalHeight(group) {
		tallest = 0;
		group.each(function() {
			thisHeight = $(this).height();
			if(thisHeight > tallest) {
				tallest = thisHeight;
			}
		});
		group.height(tallest);
}

var addthis_config = { ui_click: true };

$(document).ready(function() {

    
   	$('#btn-search').click(function(evento) { 
       	evento.preventDefault();
		$("#form-busqueda").submit();
    }); 
	
	$('#form-busqueda-avanzada, #form-registro, #form-modif-perfil, #form-orden, #form-busqueda-avanzada-foro, #form-crear-tema').NiceIt();

	$('a[rel="external"]').each(function (cual) {
		$(this).attr('target', '_blank');
	});
	
	
	$('#btn-login').click(function(evento) { 
       	evento.preventDefault();
		$.blockUI({ 
            message: $('#lb-login'), 
            css: { 
                top:  ($(window).height() - 164) /2 + 'px', 
                left: ($(window).width() - 260) /2 + 'px'
            } 
        });
        $('.blockOverlay, .lb-cerrar-login').attr('title','Click para cerrar').click($.unblockUI);
    });
	
	
	
	$('#btn-recupero').click(function(evento) { 
       	evento.preventDefault();
		$.blockUI({ 
            message: $('#lb-recupero'), 
            css: { 
                top:  ($(window).height() - 458) /2 + 'px', 
                left: ($(window).width() - 458) /2 + 'px'
            } 
        });
        $('.blockOverlay, .lb-cerrar').attr('title','Click para cerrar').click($.unblockUI);
    });
    
	
    $('#btn-enviar-receta').click(function(evento) { 
       	evento.preventDefault();
		$.blockUI({ 
            message: $('#lb-enviar-receta'), 
            css: { 
                top:  ($(window).height() - 458) /2 + 'px', 
                left: ($(window).width() - 458) /2 + 'px'
            } 
        });
 		$("#Nombre_de, #Email_de").removeClass('idleField').addClass('focusField');
        $('.blockOverlay, .lb-cerrar').attr('title','Click para cerrar').click($.unblockUI);
    });
	
	
	$('#btn-imprimir').click(function(evento) { 
       	evento.preventDefault();
		window.open('receta_imprimir.php?receta='+$(this).attr('rel'), 'Imprimir_Receta');
    });
	
	$('#btn-nueva-busqueda').click(function(evento) { 
       	evento.preventDefault();
		window.location = 'busqueda.php';
    });
	
	$('#btn-ver-recetas').click(function(evento) { 
       	evento.preventDefault();
		window.location = 'recetas.php';
    });
	
	$('#btn-mi-recetario').click(function(evento) { 
       	evento.preventDefault();
		window.location = 'perfil.php';
    });
	
	$('#btn-modif-datos').click(function(evento) { 
       	evento.preventDefault();
		window.location = 'perfil_modificar.php';
    });
	
	$('#btn-foro-responder').click(function(evento) { 
       	evento.preventDefault();
		$.scrollTo("#comments", 500);
    });
	
	$('#btn-confirmar').click(function(evento) { 
       	evento.preventDefault();
		$('#obligados input').removeClass('error');
		$('#obligados label').removeClass('error');
		$('#obligados input').each(function (algo) {
			if ($(this).val()=="") {
				avisar($(this).attr('id')+" no puede estar en blanco.");
				$(this).addClass('error');
				$(this).prev().addClass('error');
				exit;
			}
		});
		if (!verificaMail($("#Email").val())) {
			avisar("El Email es invalido");
			$("#Email").addClass('error');
			$("#Email").prev().addClass('error');
			exit;
		}
		if ($("#Clave").val()!=$("#Clave2").val()) {
			avisar("Las claves no coinciden");
			$("#Clave").addClass('error');
			$("#Clave").prev().addClass('error');
			$("#Clave2").addClass('error');
			$("#Clave2").prev().addClass('error');
			exit;
		}
		if ($('#form-registro')[0]) {
			$('#form-registro')[0].submit();
		} else {
			$('#form-modif-perfil')[0].submit();
		}
		function verificaMail(email) {
			var filtro = /^[_a-zA-Z0-9-]+(\.[_a-zA-Z0-9-]+)*@+([_a-zA-Z0-9-]+\.)*[a-zA-Z0-9-]{2,200}\.[a-zA-Z]{2,6}$/;
			return filtro.test(email);
		}
		function avisar(texto) {
			$('#lb-error .lb-fill h4').html(texto);
			$.blockUI({ 
				message: $('#lb-error'), 
				css: { 
					top:  ($(window).height() - 458) /2 + 'px', 
					left: ($(window).width() - 458) /2 + 'px'
				} 
			});
			$('.blockOverlay, .lb-cerrar').attr('title','Click para cerrar').click($.unblockUI);
		}
		
    });
	
	$('#btn-agregar-recetario').click(function(evento) { 
       	evento.preventDefault();
		$.post("ajax_mirecetario.php", { receta: $(this).attr('rel') }, function (data) {
			$('#lb-error .lb-fill h4').html(data);
			$.blockUI({ 
				message: $('#lb-error'), 
				css: { 
					top:  ($(window).height() - 200) /2 + 'px', 
					left: ($(window).width() - 458) /2 + 'px'
				} 
			});
			$('.blockOverlay, .lb-cerrar').attr('title','Click para cerrar').click($.unblockUI);
			setTimeout($.unblockUI, 1000); 
		});
    });
	
	$('#recetario .btn-eliminar').click(function(evento) { 
       	evento.preventDefault();
		$.post("ajax_mirecetario_del.php", { receta: $(this).attr('rel') }, function (data) {
			$('#lb-error .lb-fill h4').html(data);
			$.blockUI({ 
				message: $('#lb-error'), 
				css: { 
					top:  ($(window).height() - 200) /2 + 'px', 
					left: ($(window).width() - 458) /2 + 'px'
				} 
			});
			$('.blockOverlay, .lb-cerrar').attr('title','Click para cerrar').click(function () { $.unblockUI; window.location.reload(); });
		
		});
    });
	
	$('.xs').live('click', function(evento) {
		evento.preventDefault();
		ladireccion = $(this).attr('href');
		ultimo = 0;
		while (ladireccion.indexOf("?", ultimo)!=-1) {
			ultimo = ladireccion.indexOf("?", ultimo)+1;
		}
		ladireccion = '?'+ladireccion.substring(ultimo);
		$.get($(this).attr('rel')+".php"+ladireccion, {}, function (data) { 
			$('#lb-mas-info .lb-fill').html(data); 
			$.blockUI({ 
				message: $('#lb-mas-info'), 
				css: { 
					top:  ($(window).height() - 576) /2 + 'px', 
					left: ($(window).width() - 576) /2 + 'px'
				} //458
			}); 
			$('.blockOverlay, .lb-cerrar').attr('title','Click para cerrar').click($.unblockUI);
			return false;
		});
    });
		
	$("#slider-palabra").easySlider({
		auto: false,
		controlsShow: true,
		continuous: true,
		nextId: "slider-palabranext",
		prevId: "slider-palabraprev"
	});
	
	$("#slider-nutricion").easySlider({
		auto: false,
		controlsShow: true,
		continuous: true,
		nextId: "slider-nutricionnext",
		prevId: "slider-nutricionprev"
	});
	
	$("#slider-cibershop").easySlider({
		auto: false,
		controlsShow: true,
		continuous: true,
		nextId: "slider-cibershopnext",
		prevId: "slider-cibershopprev"
	});
	
	
	// Paginado de Cosas
	$('.paginado a').live('click', function(evento) {
		evento.preventDefault();
		ladireccion = $(this).attr('href');
		ultimo = 0;
		while (ladireccion.indexOf("/", ultimo)!=-1) {
			ultimo = ladireccion.indexOf("/", ultimo)+1;
		}
		ladireccion = ladireccion.substring(ultimo);
		$.get("ajax_pag_"+ladireccion, {}, function (data) { 
			$('#ajax_actualiza').html(data); 
			equalHeight($(".item-recetas"));
		});
    });
	
	$('#content.glosario #rightcolumn a').live('click', function(evento) {
		evento.preventDefault();
		$.get($(this).attr('rel')+".php"+$(this).attr('href'), {}, function (data) { 
			$('#leftcolumn').html(data); 
		});
    });
	
    $('#content.datachef h3 a').click(function() { 
       	$.blockUI({ 
            message: $('#lb-mas-info'), 
            css: { 
                top:  ($(window).height() - 458) /2 + 'px', 
                left: ($(window).width() - 458) /2 + 'px'
            } 
        });
 		
        $('.blockOverlay, .lb-cerrar').attr('title','Click para cerrar').click($.unblockUI);
    });
	
	//Equal Height - Recetas
	equalHeight($("#content.home h3"));
	equalHeight($(".item-recetas"));
	equalHeight($("#content.perfil .equal"));
	//equalHeight($("#content.datachef .equal"));
	equalHeight($("#content.blog .equal"));
	equalHeight($("#content.receta #principal .columna"));
	equalHeight($("#content.receta #otros .columna"));
	
	//Cebra - Sólo IE
	if ($.browser.msie) {
		$("#tabla-info-adicional tr:nth-child(even)").css("background", "#e4e4e4");
 	}
	
	//Glosario jScrollPane
	$('.scroll-pane').jScrollPane({showArrows: true});

	//Glosario - Tooltip
	$('#abecedario a').each(function() {
		$(this).qtip({
			content: '<div id="tt" class="'+$(this).attr('rel')+'"></div>',		
			position: {
				corner: { target: 'topMiddle', tooltip: 'bottomMiddle' }
			},
			style: {
				padding: 2,
				background: '#fff',
				color: 'white',
				textAlign: 'center',
				border: {
					width: 2,
					radius: 0,
					color: '#000'
				},
			tip: 'bottomMiddle',
			name: 'light'
			}
		});
	});
	
	// Glosario, letras que se agrandan
	$("#abecedario a").hover(function () {
		$(this).animate({fontSize: 36}, 100);
	}, function () {
		if (!$(this).hasClass("selected")) {
			$(this).animate({fontSize: 22}, 50);
		}
	});

	 //Función para dejar los campos del login en blanco on focus
    $('input[type="text"],input[type="password"]').addClass("idleField");
	$('input[type="text"],input[type="password"]').focus(function() {
		$(this).removeClass("idleField").addClass("focusField");
        if (this.value == this.defaultValue){
        	this.value = '';
    	}
        if(this.value != this.defaultValue){
	    	this.select();
        }
    });
	
    $('input[type="text"],input[type="password"]').blur(function() {
    	
        if ($.trim(this.value) == ''){
        	this.value = (this.defaultValue ? this.defaultValue : '');
        	$(this).removeClass("focusField").addClass("idleField");
    	}
    });
    
    //Accordion Sabías / Nutrición
    $(".con-accordion h3").live('click', function () {
			$(this).toggleClass("abierto").next(".holder-accordion").slideToggle().toggleClass("oculto");
			$(this).toggleClass("selected");
	});
	
	$("#foro h3").click(function () {
			$(this).toggleClass("abierto").next(".holder-foro").slideToggle().toggleClass("oculto");
			$(this).toggleClass("selected");
	});
	
	$("#ebook h3").click(function () {
			$(this).toggleClass("abierto").next(".holder-ebook").slideToggle("slow").toggleClass("oculto");
			$(this).toggleClass("selected");
	});
	
	// Archivo del Blog
	$("#blog-archivo h5").click(function () {
			$(this).toggleClass("abierto").next(".sub-archivo").slideToggle("slow").toggleClass("oculto");
	});
	$(".sub-archivo a").click(function(event) {
  		event.preventDefault();
  	});
	
	$(".btn-contactar").click(function (event) {
		event.preventDefault();
		location.href = "mailto:"+$(this).attr('rel');
	});
	
	
	$("#btn-crear-tema").click(function (event) {
		event.preventDefault();
		$("#crear-tema").slideToggle("slow");
	});
	
	$("#btn-no-crear").click(function (event) {
		event.preventDefault();
		$("#crear-tema").slideToggle("slow");
	});
	
	$("#btn-crear").click(function (event) {
		event.preventDefault();
		
		$('#obligados input, #obligados textarea').removeClass('error');
		$('#obligados label').removeClass('error');
		$('#obligados input, #obligados textarea').each(function (algo) {
			if ($(this).val()=="") {
				avisar($(this).attr('id')+" no puede estar en blanco.");
				$(this).addClass('error');
				$(this).prev().addClass('error');
				exit;
			}
		});
		
		$.post("ajax_foro.php", { Titulo: $("#Titulo").val(), Comentario: $("#Comentario").val(), Category: $("#Categoria-tema").children().children().children().html() }, function (data) {
			$('#lb-error .lb-fill h4').html(data);
			$.blockUI({ 
				message: $('#lb-error'), 
				css: { 
					top:  ($(window).height() - 200) /2 + 'px', 
					left: ($(window).width() - 458) /2 + 'px'
				} 
			});
			$('.blockOverlay, .lb-cerrar').attr('title','Click para cerrar').click(cerrar);
			setTimeout(cerrar, 1000); 
			$("#crear-tema").slideToggle("slow");
		});
		
		function cerrar(){
			//$.unblockUI
			location.href="foro.php";
		}
		
		function avisar(texto) {
			$('#lb-error .lb-fill h4').html(texto);
			$.blockUI({ 
				message: $('#lb-error'), 
				css: { 
					top:  ($(window).height() - 458) /2 + 'px', 
					left: ($(window).width() - 458) /2 + 'px'
				} 
			});
			$('.blockOverlay, .lb-cerrar').attr('title','Click para cerrar').click($.unblockUI);
		}
		
	});
});


