jQuery(document).ready(function($){

function newStructure(){
	$('.keyVisualHeader img').remove();
	$('.keyVisualHeader').addClass('animationholder');
	$('#animation1 img, #animation2 img, #animation3 img').clone().appendTo('.animationholder');
	$('.keyVisualHeader').append('<ul id="animationmenu"></ul>');
	$('#animation1 h2 a, #animation2 h2 a, #animation3 h2 a').clone().appendTo('#animationmenu');
	$('#animationmenu a:first, .animationholder img:first').addClass('active');
}

function newSlide(id){
	if(id == $('.animationholder img.active').index()+1) return;
	//$('.animationholder img').clearQueue();
	$('#animationmenu a.active').removeClass('active');
	$('#animationmenu a:nth-child('+id+')').addClass('active');
	$('.animationholder img.active').fadeOut(500, function(){
		$('.animationholder img:nth-child('+id+')').fadeIn(300, function(){
			$(this).addClass('active');
		});
		$(this).removeClass('active');
	});
}

$('#animationmenu a').live('mouseover', function(){
	$(document).stop(true);
	$(document).stopTime();
	var newid = $(this).index()+1;
	newSlide(newid);
});

$('#animationmenu a').live('mouseout', function(){
	$(document).everyTime(5000, function(){
		var newid = $('.animationholder img.active').index()+2;
		if(newid>3) newid = 1;
		newSlide(newid);
	});
});

$(document).everyTime(5000, function(){
	var newid = $('.animationholder img.active').index()+2;
	if(newid>3) newid = 1;
	newSlide(newid);
});

newStructure();

});
