var active=0,
changeReference=function(){
	var reference=references[active],
	img=$(new Image());
	img.load(function(){
		$('#header img').before(this);
		$('#header .caption').before('<div class="caption"><h3>'+reference.name+'</h3><p>'+reference.text+'</p>');
		$('#header img').eq(1).fadeOut(1500);
		$('#header .caption').eq(1).fadeOut(1500);
		$('#header img').eq(0).fadeOut(0).fadeIn(1500);
		$('#header .caption').eq(0).fadeOut(0).fadeIn(1500,function(){
			$('#header img').eq(1).remove();
                        $('#header .caption').eq(1).remove();
		});
	});
	img.attr('src',reference.image);
	active+=1;
	if(active==references.length){
		active=0;
	}
};
$(document).ready(function(){
	$('a.new-window').click(function(){
		window.open($(this).attr('href'));
		return false;
	});
        var scrollTop=false;
	$('#references-links li span,#references-links li a').each(function(i){
		$(this).click(function(){
			$('#references .reference').hide();
			$('#references .reference').eq(i).show();
			$('#references').show(function(){
                            if(!scrollTop){
                                scrollTop=$(this).offset().top;
                            }
                            $('html,body').animate({scrollTop:scrollTop},1000);
                        });
			return false;
		});
	});
	window.setInterval('changeReference();',5000);
});
