$(document).ready(function(){

	$(".aviso_lega").fancybox({
				'width'				: 780,
				'height'			: 480,
				'autoScale'			: false,
				'transitionIn'		: 'none',
				'transitionOut'		: 'none',
				'type'				: 'iframe'
	});

	$("#noticias li a,#productos li a.open").click(function(e){
			e.preventDefault();
			idOculto 	= $(this).attr("rel");
			divOculto	= $("#"+idOculto);
			
			$(".hide").slideUp(1000);
			
			if(divOculto.is(':hidden')){
				divOculto.slideDown(1000);
				$('.first-and-second-carousel').jcarousel();
				destination = $(this).offset().top;
				$("html:not(:animated),body:not(:animated)").animate({ scrollTop: destination-89}, 1100 );
				
			}else{
				divOculto.slideUp(1000);
			}
			
			
	});
	
	$("#catalogo-midle-bg li a").click(function(e){
			
			href					= $(this).attr("href");
			
			primerDivOculto	= $(this).next('ul:first');
			
			if(primerDivOculto.is(':visible')){
				e.preventDefault();
				primerDivOculto.slideUp(500);
				$("ul",primerDivOculto).slideUp(500);
			}else{
				
				if(href=='#'){
					e.preventDefault();
				}
				primerDivOculto.slideDown(500);
				
			}
	});
	
	$("#equipo_humano ul li").hover(function() {
		$("img",this).animate({opacity: "show"},"fast");
	} , function() {
		$("img",this).animate({opacity: "hide"},"fast");
	});
	
prevCalendario = function(){
	$(".prev").click(function(e){
		fragmentoTexto = this.id.split(':');
		
		var mes = parseInt(fragmentoTexto[0]);// mes
		var ano = parseInt(fragmentoTexto[1]);// año
		$.ajax({
			beforeSend : function (){
				$(".calendar").hide();
				$(".calendario").append('<div class="cargando"></div>');
				$(".cargando").css({"display" : "block", "width" : "422px",	"height" : "192px", "float" : "left"});
			},			   
			type: "POST",
			url: "bin/newcalendario.ajax.php",
			data:"anoNuevo="+ano+"&mesNuevo="+mes,
			success: function(msg){
				$(".calendario").html(msg);
			},complete : function (){
				$(".cargando").remove();
				$(".calendar").fadeIn("slow",function(){
					nextCalendario();
	 				prevCalendario();
	 				tooltip();
				});
			}
		});
	});
}

nextCalendario = function(){		
	$(".next").click(function(e){
		fragmentoTexto = this.id.split(':');
		
		var mes = parseInt(fragmentoTexto[0]);// mes
		var ano = parseInt(fragmentoTexto[1]);// año

		
		$.ajax({
			beforeSend : function (){
				$(".calendar").hide();
				$(".calendario").append('<div class="cargando"></div>');
				$(".cargando").css({"display" : "block", "width" : "422px",	"height" : "192px", "float" : "left"});
			},			   
			type: "POST",
			url: "bin/newcalendario.ajax.php",
			data:"anoNuevo="+ano+"&mesNuevo="+mes,
			success: function(msg){
				$(".calendario").html(msg);
			},complete : function (){
				$(".cargando").remove();
				$(".calendar").fadeIn("slow",function(){
					nextCalendario();
	 				prevCalendario();
	 				tooltip();
				});
			}
		});
	});
}
	
	
	change_categoria1 = function(id){
			var capa = $("#capa_categoria1");
			$("#capa_categoria2").children().remove();
			$("#capa_categoria3").children().remove();
			$("#capa_categoria4").children().remove();
			$("form.search_home button").hide("fast");	
			
			capa.append("<div id='loading'>Loading...</div>");
			
			$.ajax({  
				type: "POST",
				url: "ajax.php",
				data: "opc=change_categoria1&id="+id,
				success: function(msg){
					if(msg=="no_succes"){
						$("form.search_home button").show("fast");
						$("#loading").remove();
					}else{
						capa.html(msg);
					}
				}
		  });				
	}
	
	change_categoria2 = function(id){
			var capa = $("#capa_categoria2");
			$("#capa_categoria3").children().remove();
			$("#capa_categoria4").children().remove();
			$("form.search_home button").hide("fast");
			
			capa.append("<div id='loading'>Loading...</div>");
			
			
			$.ajax({		   
				type: "POST",
				url: "ajax.php",
				data: "opc=change_categoria2&id="+id,
				success: function(msg){
					if(msg=="no_succes"){
						$("form.search_home button").show("fast");
						$("#loading").remove();
					}else{
						capa.html(msg);
					}
				}
		  });				
	}
	
	change_categoria3 = function(id){
			var capa = $("#capa_categoria3");
			$("#capa_categoria4").children().remove();
			$("form.search_home button").hide("fast");
			
			capa.append("<div id='loading'>Loading...</div>");
			
			
			$.ajax({		   
				type: "POST",
				url: "ajax.php",
				data: "opc=change_categoria3&id="+id,
				success: function(msg){
					if(msg=="no_succes"){
						$("form.search_home button").show("fast");
						$("#loading").remove();
					}else{
						capa.html(msg);
					}
				}
		  });				
	}
	change_categoria4 = function(id){
			var capa = $("#capa_categoria4");
			$("form.search_home button").show("fast");		
			$("#loading").remove();	
	}
	
	/*$(".mas_info_inicio").click(function(e){
		e.preventDefault();
		$("#noticias_home .hidden").fadeIn(1000);

		$("#content").css({"height":"670px"});
		$("#contacta_home").css({"height":"670px"});
      	
		$(this).fadeOut();
		
	});*/
	//SexyLightbox.initialize({color:'black', dir: 'css/sexyimages'});
	

});
