var cur_rotator = 2;
var timeout;

$(document).ready( function(){
	$('#body.es .subtitle a').each(function(){
		$(this).click(selectRotator);
		
		$(this).hover(function(){
		  if( !$(this).hasClass('bold') ){
  		  $(this).stop(true);
  		  $(this).animate({'backgroundColor': '#b30000', 'color': '#fff'}, 200);
		  }
		  
		  $('#body.es .subtitle span.name').html($(this).attr('title'));
		}, function(){
		  if( $(this).hasClass('bold') ) return;
		  
		  $(this).animate({'backgroundColor': '#fff', 'color': '#b30000'}, 200);
		});
	});
	
	timeout = setTimeout(rotator, parseInt($('.main .banners > div:first').attr('title')));
});

function rotator(){
  var div = $('.main .banners > div:visible');
	if( div.length == 0 ) return;
	
	$('.subtitle a:nth-child('+cur_rotator+')').removeClass('bold').css({'backgroundColor': '#fff', 'color': '#b30000'});
	
	div.hide();
	++cur_rotator;

	if( (cur_rotator-1) > $('.main .banners > div').length ){
	 div = $('.main .banners > div:first');
	 cur_rotator = 2;
	}else
	 div = div.next();

	$('.subtitle a:nth-child('+cur_rotator+')').addClass('bold').css({'backgroundColor': '#b30000', 'color': '#fff'});
	$('#body.es .subtitle span.name').html($('.subtitle a:nth-child('+cur_rotator+')').attr('title'));
	if( div.find("a").length ) $('#bannerBorder').css('cursor', 'pointer');
	else $('#bannerBorder').css('cursor', 'auto');
	
	div.show();
	
	var time = div.attr('title');
  if( !time || !parseInt(time) ) time = 3000;
	
	timeout = setTimeout(rotator, parseInt(time));
}

function selectRotator(no){
  $(this).stop();
  
	var div = $('.main .banners > div:visible');
	$('.subtitle a:nth-child('+cur_rotator+')').removeClass('bold').css({'backgroundColor': '#fff', 'color': '#b30000'});
	
	if( typeof no != "number" )
		no = parseInt($(this).text());
	cur_rotator = ++no;
	clearTimeout(timeout);
	
	div.hide();
	$(this).addClass('bold').css({'backgroundColor': '#b30000', 'color': '#fff'});
	div = $('.main .banners > div:nth-child('+(no-1)+')').show();
		
	var time = div.attr('title');
	if( !time || !parseInt(time) ) time = 3000;

	timeout = setTimeout(rotator, parseInt(time))
	
	return false;
}

