var velocidad = 0;
var intervalo = 0;

(function($) {
	$(function() {	
		creaDetalleDisco();
		
		creaGalerias();
	});

	function creaDetalleDisco() {
		$('.detalle_disco').each(function() {
			var ul = $('<ul id="tabs" />');

			// Leemos el alto máximo de las cajicas para fijar el tamaño y que no cambie al pasar de año
			var alto = 0;

			$('h4', this).each(function(i) {
				var section = $(this).closest('section');
				alto = Math.max(alto, section.height());

				var li = $('<li><a href="javascript:void(0)">' + $(this).text() + '</a></li>').appendTo(ul);
				if(i == 0) {
					li.addClass('activo');
				} else {
					section.hide();
				}
				$(this).remove();

				li.click(function() {
					$(this).addClass('activo').siblings().removeClass('activo');

					$(this).closest('ul').nextAll('section').hide();
					section.show();
				});
			});
			$('section', this).css('height', alto);
			$('section:eq(0)', this).before(ul);
		}).find('.pistas a').click(function() {
			$.colorbox({
				href: $(this).attr('href') + '&ajax=1',
				onComplete:function() {
					var letras = $('#letras');
					var texto = $('.texto', letras);
					if(letras.innerHeight() < texto.innerHeight()) {
						texto.addClass('scroll');
						var ul = $('<ul id="scroll"><li id="arriba">-</li><li id="abajo">+</li></ul>');
						letras.prepend(ul);
						
						$('#arriba', ul).hover(function() {
							velocidad = 5;
							intervalo = setInterval(mueveTexto, 30);
						}, function() {
							clearInterval(intervalo);
						});
						
						$('#abajo', ul).hover(function() {
							velocidad = -5;
							intervalo = setInterval(mueveTexto, 30);
						}, function() {
							clearInterval(intervalo);
						});
					}

					$('#cboxClose').hide().addClass('cargado').fadeIn();
				},
				onCleanup:function() {
					$('#cboxClose').removeClass('cargado');
				}
			});
			
			return false;
		});
	}
	
	function mueveTexto() {
		var letras = $('#letras');
		var texto = $('.texto', letras);

		var pos = parseInt(texto.css('marginTop')) + velocidad;
		if(pos > 0) {
			clearInterval(intervalo);
			pos = 0;
		} else if(pos < letras.height() - texto.height()) {
			clearInterval(intervalo);
			pos = letras.height() - texto.height();
		};

		texto.css('marginTop', pos);
	}
	
	function creaGalerias() {
		var contenedor = $('*:has(>a:has(>img))').each(function() {
			var imagenes = $('>a:has(>img)', this);
			if(imagenes.length > 1) {
				$(this).addClass('galeria');
				imagenes.each(function() {
					$(this).css('background-image', 'url(' + $(this).find('img').attr('src') + ')').attr('target', '_blank');
					$(this).find('img').remove();
				});
			}
		});
	}
})(jQuery);
