$(document).ready(function(){
	var current = 0;
	var out = 0;
	
	$('#bg0').mouseover(function(e){
		current = $(this).parent().index();
		if(out != current){
				moveInOut(out, current);
		}
		else{
			moveIn(current);
		}
	});
	
	$('#bg1').mouseover(function(e){
		current = $(this).parent().index();
		if(out != current){
			moveInOut(out, current);
		}
		else{
			moveIn(current);
		}
	});
	
	$('#bg2').mouseover(function(e){
		current = $(this).parent().index();
		if(out != current){
			moveInOut(out, current);
		}
		else{
			moveIn(current);
		}
	});
	
	$('#bg3').mouseover(function(e){
		current = $(this).parent().index();
		if(out != current){
			moveInOut(out, current);
		}
		else{
			moveIn(current);
		}
	});
	
	$('#bg4').mouseover(function(e){
		current = $(this).parent().index();
		if(out != current){
			moveInOut(out, current);
		}
		else{
			moveIn(current);
		}
	});
	
	$('#bg5').mouseover(function(e){
		current = $(this).parent().index();
		if(out != current){
			moveInOut(out, current);
		}
		else{
			moveIn(current);
		}
	});
	
	function moveInOut(id, currentid){
		$('#menu .sub' + id).stop().find('li').fadeOut(200, function(){$('#menu .sub' + id).stop().animate({backgroundPosition: "0px 350px"}, 300, function(){moveIn(currentid);})});
	}
	
	function moveIn(currentid){
		$('#menu .sub' + currentid).stop().animate({backgroundPosition: "0px 0px"}, 300, function(){$(this).find('li').fadeIn(); test(currentid);});
		out = currentid;
	}
	
	function test(currentid){
		$('#content, #content-big').mouseover(function(){
			$('#menu .sub' + currentid).stop().find('li').fadeOut(200, function(){$('#menu .sub' + currentid).stop().animate({backgroundPosition: "0px 350px"}, 300)});
		});
	}
});
