var cur_rotator = 1;

$(document).ready( function(){
	$('.rotator_number a').each(function(){
		$(this).click(selectRotator);
	});
});

function start(){
	var div = $('.rotator_hide div:first');
	if( div.length == 0 ) return;
	
	$('.rotator_number a:nth-child('+cur_rotator+')').removeClass('bold');
	$('.rotator').hide();
	
	var time = div.attr('title');
	if( !time || !parseInt(time) ) time = 3000;
	
	cur_rotator = 2;
	$('.rotator_number a:nth-child('+cur_rotator+')').addClass('bold');
	div.show();
	
	timeout = setTimeout(showNext, parseInt(time));
}

function showNext(){
	$('.rotator_number a:nth-child('+cur_rotator+')').removeClass('bold');
	
	var div = $('.rotator_hide div:visible');
	div.hide();
	
	div = div.next();
	if( div.length == 0 ){
		div = $('.rotator_hide div:first');
		cur_rotator = 2;
	}else
		++cur_rotator;
	
	$('.rotator_number a:nth-child('+cur_rotator+')').addClass('bold');
	div.show();
	var time = div.attr('title');
	if( !time || !parseInt(time) ) time = 3000;

	timeout = setTimeout(showNext, parseInt(time))
}

function selectRotator(no){
	var div = $('.rotator_hide div:visible');	
	if( cur_rotator == 1 ) div = $('.rotator');
	
	$('.rotator_number a:nth-child('+cur_rotator+')').removeClass('bold');
	
	if( typeof no != "number" )
		no = parseInt($(this).text());
	cur_rotator = no;
	clearTimeout(timeout);
	
	div.hide();
	$(this).addClass('bold');
	if( no == 1 )
		div = $('.rotator').show();
	else
		div = $('.rotator_hide div:nth-child('+(no-1)+')').show();
		
	var time = div.attr('title');
	if( !time || !parseInt(time) ) time = 3000;

	if( no == 1 )
		timeout = setTimeout(start, parseInt(time));
	else
		timeout = setTimeout(showNext, parseInt(time))
	
	return false;
}
