$(document).ready(function() {
	/*$('ul.menu li').each(function(i) {
		var url = $('a', this).attr('href');
		if (url == noticias_url) {
			$(this).hover(function(){ $('#ultimasnoticias').fadeIn('fast'); });
		} else {
			$(this).hover(function(){ $('#ultimasnoticias').fadeOut('fast'); });
		}
	});
	$('#ultimasnoticias').hover(function() { $('ul.menu a:eq(4)').attr('id','active'); }, function(){  $('ul.menu a:eq(4)').attr('id',''); $('#ultimasnoticias').fadeOut('fast'); });
	*/
	if ($('.jcarousel-skin-tango').get(0))
		$('.jcarousel-skin-tango').jcarousel({ visible:4 });
			
	$(window).resize(fondoResize);
	fondoResize();
	fondoFadeInterval = setInterval(fondoFade,5000);
	
	// Parar las fotos cuando se vaya a escribir algo.
	$('input').click(function() { if (fondoFadeInterval) { clearInterval(fondoFadeInterval); fondoFadeInterval = null; } });
	
	if ($('#page-article').get(0))
	{
		var url = new String(window.location);
		if (url.indexOf('#comments') < 0)
		{
			$('#comment-list, #comment-form').css('display', 'none');
			if (!$('.blog-opiniones').get(0)) {
				$('.articulo:first .cuerpo').append ('<p id="veran" style="text-align:right"><a href="#comments" onclick="$(\'#comment-list, #comment-form\').css(\'display\', \'\');$(\'#veran\').remove();">Ver/añadir comentarios</a></p>');
			}
		}
	}
	
});


function imageResize(image, quitarheight) 
{
	var ww = $(window).width();
	var wh = $(window).height();
	var iw = image.width()? image.width():image.attr('width');
	var ih = image.height()? image.height():image.attr('height');
	var ratio = Math.max(ww/iw,wh/ih);
	if (ww > wh) {
		image.css({width:iw*ratio,height:'auto'});
	} else {
		image.css({width:'auto',height:ih*ratio});
	}
};


function fondoResize()
{
	imageResize($('#bg .current'));
}

var current_fondo = 1;
function fondoFade()
{
	if (current_fondo > 1)
		idimgcur = 'bgimage-'+current_fondo;
	else
		idimgcur = 'bgimage';
		
	$('#' + idimgcur).animate({opacity:0}, 1200, null, function() {
		$('#' + idimgcur).css('display', 'none');
	});
	
	current_fondo++;
	if (current_fondo > 11)
		current_fondo = 1;
		
	if (current_fondo > 1)
		idimgnext = 'bgimage-'+current_fondo;
	else
		idimgnext = 'bgimage';	
		
	$('#bg img').removeClass('current');
	imageResize($('#' + idimgnext).addClass('current'));
	$('#' + idimgnext).css('display', '').animate({opacity:1},1200);
	
	
}


