$(document).ready(function () {
	
	Cufon.replace('#menu span, .notebook .titles a, h1, h2, #cursos .title span, #cursos .titulo, .article h2.title, #portada h2.titulo');
	Cufon.replace('#portada .imagen h2', {textShadow: '#333 1px 1px'});
	
	// Imágenes del blog
	$(".ac img").each(function () { 
				if(this.width > 590) {
					this.height = 590 * this.height/this.width;
					this.width = 590;
					$(this).attr('width', 590);
					var html = '<a class="imagenpop" href="' + this.src + '"></a>';
					$(this).wrap(html);
					// $(".imagenpop").colorbox({maxHeight:800});
				} 
			} 
	);
	$(".ac img").load(function () { 
				if(this.width > 590) {
					this.height = 590 * this.height/this.width;
					this.width = 590;
					$(this).attr('width', 590);
					var html = '<a class="imagenpop" href="' + this.src + '"></a>';
					$(this).wrap(html);
					// $(".imagenpop").colorbox({maxHeight:800});
				} 
			} 
	);
	
	 // Formularios laterales
	 $('.latform').each (function(i) {
	 	var id = $(this).attr('id');
	 	if ('feedbackform' != id) {
			 $('.latbot a', this).click(function() {
			 	toggle_latform(id);
			 	/* if ($(this).hasClass('abierto')) {
			 		$(this).removeClass('abierto');
			 		$('#'+id).animate({'left': '-211px'},'fast');	
			 	} else {
			 		$(this).addClass('abierto');
			 		$('#'+id).animate({'left': '0'},'fast');
			 	}
			 	*/
			 	return false;
			 });
		}
	 });
	 
	// Quicklinks y fotos
	$('.ql').each (function(i) {
		var id = $(this).attr('id');
		$(this).hover(
			function() { 
				$('#'+id+' .title').animate({ 'margin-top': '-160px'}, 'fast', 'linear', function() {
					$('#'+id).addClass('abierto');	
					$('#'+id+' .flechabot').css('display','');
				});
			}, 
			function() { 
				$('#'+id+' .flechabot').css('display','none');
				$('#'+id+' .title').animate({ 'margin-top': '-23px'}, 'fast', 'linear', function() {
					$('#'+id).removeClass('abierto');	
				});	
			}
		);
		
		
	});	
	
	$('#cursos .contenedor').each (function(i) {
		var id = $(this).attr('id');
		$(this).hover(
			function() { 
				$('#'+id+' .titulo').animate({ 'margin-top': '-280px'}, 'fast', 'linear', function() {
					$('#'+id).addClass('abierto');	
					$('#'+id+' .flechabot').css('display','');
				});
			}, 
			function() { 
				$('#'+id+' .flechabot').css('display','none');
				$('#'+id+' .titulo').animate({ 'margin-top': '0'}, 'fast', 'linear', function() {
					$('#'+id).removeClass('abierto');	
				}); 	
			}
		);
		
		
	});	 
	
	// Tabs ficha producto
	if ($('.page-product .notebook').get(0))
	{
		$('.notebook .titles li.solapa').each(function(i) {
			var notebook = $(this).parent().parent();
			var id = $(this).attr('id');
			
			$('a', this).click(function() {
				var nid = id.replace ('t-','b-');
				$('.tabbody', notebook).css('display','none');
				$('.titles li', notebook).removeClass('sel');
				$('#'+nid).css('display','');
				$('#'+id).addClass('sel');
				
				if (Cufon)
					Cufon.refresh();
				
				return false;
			});
			
			$('a', this).focus(function() { blur(); }); // quitar marca de selección del enlace
			
		});
	}
	
	// Tira de vídeos
	$('#videos .tira').serialScroll({
		axis:'x',
		items:'li',
		lazy: false,
		prev:'#videos a.prev',
		next:'#videos a.next',
		step: 4
	});
	
	
	$('#fotos #quicklinks').serialScroll({
		axis:'y',
		items:'div.ql',
		lazy: false,
		prev:'#fotos a.prev',
		next:'#fotos a.next',
		step: 12
	});
	
	if ($('#portada .item').get(0))
	{
		$('#portada > div').cycle({
			pause: 1, /* pause on hover */
			timeout: 5000,
			startingSlide: 0,
			before: function(currSlideElement, nextSlideElement, options, forwardFlag) {
				var tmp = $(nextSlideElement).attr('id');
				tmp = tmp.split('-');
				var id = tmp[1];
				$('#topbar .titulo').css('display','none');
				$('#topbar .titulo:eq(' + id + ')').css('display','');
				// console.log('#topbar .titulo:eq(' + id + ')');
			}
		});
		/* $('#portada .item').click(function() {
			$('#portada').trigger( 'stop' );
		});
		
		$('#portada').serialScroll({
			items:'.item',
			duration:1600,
			force:true,
			axis:'x',
			easing:'linear',
			lazy:true,
			interval:5000,
			step: 1,
			onAfter: function( elem ){
						//'this' is the element being scrolled ($pane) not jqueryfied
						var id = $(elem).attr('id');
						if (id) {
							var tmp = id.split('-');
							$('#topbar .titulo').css('display','none');
							$('#topbar .titulo:eq(' + tmp[1] + ')').css('display','');
						}
					}
		}); */
	}
	
	
	$(".imagenpop").colorbox({maxHeight:800});
	$(".youtube").colorbox({iframe:true, width:650, height:550});
	$('#link-contacto, .popwin').colorbox({iframe:true, width:665, height:520,
		onOpen: function(){
			if (abierto_latform('empleoform')) {
				$('#empleoform .latbot a').removeClass('abierto');
				$('#empleoform').animate({'left': '-211px'},'fast');
			}
			$('body').addClass('formcontacto');
		},
		onClosed:  function() {
			$('body').removeClass('formcontacto');
		}
	});
	$(".linkgrupo").colorbox({
		onOpen: function(){
			$('body').addClass('subgrupo');
		},
		onClosed:  function() {
			$('body').removeClass('subgrupo');
		}
	});
	
	
	$('.atooltip').hover(function (e) {
		var tmp = $(this).attr('id');
		tmp = tmp.split('at-');
		var id = tmp[1];
		$('#' + id).css('display',''); 
		$('#' + id).css('margin-top', -1*($('#' + id).height() + 30) + 'px');
		$('#' + id).css('left', e.pageX + 'px');
	});
	
	$('.tooltip').hover (function(e) {}, function (e) {
	
		$(this).css('display','none');
	});
	
	// Corregir css
	$('.tabbody td:odd').css('padding-right','0');
	$('.tabbody table:first').attr('width','100%');
	/* $('.tabbody td').each (function(i) { 
		var id = $('.notebook li.sel').attr('id');
		tmp = id.split('-');
		id = 'b-' + tmp[1];
		if ($(this).css('width') == '25%') {
			// $(this).css('width', '25% !important');
			$(this).attr('width', '25%');
			if (id == 'b-como-funciona'){
				$('p', this).css('width', '215px');
			}
		}
		
	}); */
	
	$('.tabbody tr').each (function(i) {
		
		var tds = $('td', this);
		if (tds.length == 4) {
			$('td', this).removeAttr('width').removeAttr('style');
			
			for (i = 0; i < 4; i++) {
				$(tds[i]).css('width', '240px !important');
				$('p', tds[i]).css('padding-right', '10px');
				
			}
		}
	});
	
	$('#b-profesores td, #b-tutores td').each (function(i) {

		if ($('img', this).get(0)) {
			if ($('br', this).get(0)) {
				$('br', this).remove();
			}
			$(this).css('padding-right', '5px');
		}
	});
	
	$('li.horarios, .tcon p').each(function(i){
		console.log($(this).children());
	  $('br:first',this).remove();
	});
	
	if (ultimasForo) { // Últimas noticias del foro
		$('#foro li').remove();
		var linea = '';
		for (i = 0; i < ultimasForo.length &&  i < 3; i++) {
			linea = '<li><div class="fecha">' + ultimasForo[i][2] + '</div><div class="resumen"><a href="http://foro.animum3d.com/viewtopic.php?f=' + ultimasForo[i][3] + '&t=' + ultimasForo[i][0] + '" target="_blank">' + ultimasForo[i][1] + '</a></div></li>';
			$('#foro ul').append(linea);
		}
	}
	
	
	
	
});


function abierto_latform (id)
{
	return $('#' + id + ' .latbot a').hasClass('abierto');
}

function toggle_latform(id)
{
	if (abierto_latform(id)) {
		$('#' + id + ' .latbot a').removeClass('abierto');
		$('#'+id).animate({'left': '-211px'},'fast');
	} else {
		$('#' + id + ' .latbot a').addClass('abierto');
		$('#'+id).animate({'left': '0'},'fast');
		$('.latform').each (function (i) {
			var lid = $(this).attr('id');
			if (lid != id && abierto_latform(lid)) {
				toggle_latform(lid);
			}
		});	
	}
}
