var busy = false, pauzed = false;

$(function(){

	setTimeout('nextAndTimeout();', 5000);

	$('a.link').first().show();
        $('a.link').click(function(){
            pauzed = true;
        });

	$('a.next').click(scrollerNext);

	$('a.prev').click(scrollerPrev);

	$('a.playpause').click(pause);

});

function pause(){

	if(pauzed){
		pauzed = false;
		$('a.playpause').removeClass('play');

		
	} else {
		pauzed = true;
		$('a.playpause').addClass('play');
		
	}
	return false;
}

function nextAndTimeout(){
	if(!pauzed){
		scrollerNext();
	}
	setTimeout('nextAndTimeout();', 5000);
}

function scrollerNext( event ){

	if(busy){ return false; }
	busy = true;
	if(!pauzed && event){
		pauzed = true; $('a.playpause').addClass('play');
	}
	var current = $('a.link:visible');
	$('a.link').css('z-index', '1');
	var next = current.next('a.link').first();
	if(!next.length){
		next = $('a.link').first();
	}
	next.css('z-index', 20);
	next.fadeIn(1000,function(){

		current.hide();
		busy = false;
	});
	return false;
}

function scrollerPrev(){

	if(busy){ return false; }
	busy = true;
	if(!pauzed){
		pauzed = true; $('a.playpause').addClass('play');
	}
	var current = $('a.link:visible');
	$('a.link').css('z-index', '1');
	var prev = current.prev('a.link').first();
	if(!prev.length){
		prev = $('a.link').last();
	}
	prev.css('z-index', 20);
	prev.fadeIn(1000,function(){

		current.hide();
		busy = false;
	});
	return false;

}
