$(document).ready(function(){

	if ($('body').width() < 1035) {
		$('.trama').css({ 'width': '1035px', 'float':'left' });
	}

	$(window).resize(function() {
		if ($(window).width() < 1036) {
			$('.trama').css({ 'width': '1035px', 'float':'left' });
		}
		else{
			$('.trama').css({ 'width': '100%', 'float':'left' });
		}
	})

	var velocidad = 500;

	// Galeria-carousel
	function galeria(){
		$('.list_carousel').fadeIn();
		$('.list_carousel').css('width', $(window).width()-100);

		if($("#galeria").length)
			$("#galeria").carouFredSel({
				auto: false,
				prev : {
			        button  : "#foo2_prev",
			        key     : "left"
			    },
			    next : {
			        button  : "#foo2_next",
			        key     : "right"
			    }
			});

		tb_init('.thickbox');
	}

	// Replica de las funciones de ajax para los botones "ajax2" (submenus), para que no se interfieran
	function cargaAjax(){
		// Carga de contenido
		$('.ajax2').click(function(){

			var enlace = $(this).attr('href');

			// Cargamos y mostramos los datos
			function cargar(){
				$('#content .cuerpo').load(enlace +' #content .cuerpo', function(){
					$('#content .cuerpo').html($(this).children('.cuerpo').html()).animate({ width: 'toggle' }, velocidad);

					cargaAjax();
				});
			}

			// Si ya se ve el contenido, lo ocultamos antes de cargar el nuevo
			$(".cuerpo").animate({ width: 'toggle' }, velocidad);
			setTimeout(cargar, velocidad*2);

			setTimeout(galeria, 1500);

			function popup(){
				$('.popup').qtip({
				   content: { attr: 'alt' },
				   position: { at: 'bottom center' }
				});
			}
			setTimeout(popup, velocidad*4);

			function scroll(){
				$('#content .not .info').jScrollPane();
			}
			setTimeout(scroll, velocidad*3);

			return false;
		});
	}

	// Carga de contenido
	$('.ajax').click(function(){

		var enlace = $(this).attr('href');

		// Ocultamos el index
		$('#content-index').fadeOut(velocidad);

		// Mostramos la zona de contenido si no está visible
		if($('#content').css('display') == 'none')
			$("#content").animate({ width: 'toggle' }, velocidad);

		// Cargamos y mostramos los datos
		function cargar(){
			$('#content .cuerpo').load(enlace +' #content .cuerpo', function(){

				$('#content .cuerpo').html($(this).children('.cuerpo').html()).animate({ width: 'toggle' }, velocidad);

				cargaAjax();

				if($('.contacto-form').length > 0)
					$('.contacto-enviar').click(function(){
						$.ajax({
						       type :  'POST',
						       url  :  enlace,
						       data :  $('#contacto-form').serialize(),
						       dataType : "html",
						       success: function(htmlResponse){
									$('.mensaje-envio').html($(htmlResponse).find('.mensaje-envio').html()).fadeIn().delay(2000).fadeOut();
						       }
						});

						return false;
					});

				// Cambio de pagina
				$('.seccion-paginador a').click(function(){
					var pag = $(this).attr('id').replace('p', '');

					$(".cuerpo").animate({ width: 'toggle' }, velocidad*2); // ocultamos

					$('.seccion-pagina').delay(velocidad).fadeOut();
					$('#seccion-pg'+ pag).delay(velocidad).fadeIn();
					$('.seccion-paginador a').removeAttr('class');
					$(this).attr('class', 'activa');

					$(".cuerpo").animate({ width: 'toggle' }, velocidad*2); // mostramos

					return false;
				});

				setTimeout(galeria, 500);

				$('.adj-not ul').hoverscroll();

				// Botón de descargar para los adjuntos
				if($('.adj-not').length)
					$('.adj-not li').hover(
						function(){
							$(this).children('a').stop(true, true).slideDown();
						},
						function(){
						    $(this).children('a').stop(true, true).slideUp();
						}
					);


			});
		}

		// Si ya se ve el contenido, lo ocultamos antes de cargar el nuevo
		if($(".cuerpo").css('display') == 'block'){
			$(".cuerpo").animate({ width: 'toggle' }, velocidad);
			setTimeout(cargar, velocidad*2);
		}else{
			cargar();
		}

		return false;
	});

	$('.vuelta-home').click(function(){
		$('#content').animate({ width: 'toggle' }, velocidad);
		$('#content-index').fadeIn(velocidad);
		return false;
	});
	// Fin carga de contenido

	$('#content .menu li:last').css('border-right', 0);

	// Cambio de scroll
	 $('.noticias').jScrollPane();
});
