$(function() {
	var current = 0;	
	var loaded  = 0;
	for(var i = 1; i <9; ++i)
		$('<img />').load(function(){
			++loaded;
			if(loaded == 8){
				$('#bg1,#bg2,#bg3,#bg4,#bg5,#bg6,#bg7,#bg8').mouseover(function(e){					
					var $this = $(this);
					if($this.parent().index() == current)
						return;
					var item = e.target.id;
					if(item == 'bg1' || current == 2)
						$('#banner .sub'+parseInt(current+1)).stop().animate({backgroundPosition:"(-143px 0)"},300,function(){
							$(this).find('li').hide();
						});
					else
						$('#banner .sub'+parseInt(current+1)).stop().animate({backgroundPosition:"(143px 0)"},300,function(){
							$(this).find('li').hide();
						});

					if(item == 'bg1' || current == 2){
						$('#banner > li').animate({backgroundPosition:"(-800px 0)"},0).removeClass('bg1 bg2 bg3 bg4 bg5 bg6 bg7 bg8').addClass(item);
						move(1,item);
					}
					else{
						$('#banner > li').animate({backgroundPosition:"(800px 0)"},0).removeClass('bg1 bg2 bg3 bg4 bg5 bg6 bg7 bg8').addClass(item);
						move(0,item);
					}
					if(current == 2 && item == 'bg1'){
						$('#banner .sub'+parseInt(current)).stop().animate({backgroundPosition:"(-143px 0)"},300);
					}
					if(current == 0 && item == 'bg5'){
						$('#banner .sub'+parseInt(current+2)).stop().animate({backgroundPosition:"(143px 0)"},300);
					}
					current = $this.parent().index();
					$('#banner .sub'+parseInt(current+1)).stop().animate({backgroundPosition:"(0 0)"},300,function(){
						$(this).find('li').fadeIn();
					});
				});
			}	
		}).attr('src', 'images/'+i+'.jpg');
	function move(dir,item){
		if(dir){
			$('#bg1').parent().stop().animate({backgroundPosition:"(0 0)"},200);
			$('#bg2').parent().stop().animate({backgroundPosition:"(-143px 0)"},300);
			$('#bg3').parent().stop().animate({backgroundPosition:"(-286px 0)"},400);
			$('#bg4').parent().stop().animate({backgroundPosition:"(-429px 0)"},500);
			$('#bg5').parent().stop().animate({backgroundPosition:"(-572px 0)"},600);
			$('#bg6').parent().stop().animate({backgroundPosition:"(-715px 0)"},700);
			$('#bg7').parent().stop().animate({backgroundPosition:"(-858px 0)"},800);
			$('#bg8').parent().stop().animate({backgroundPosition:"(-1001px 0)"},900,function(){
				$('#bannerWrapper').removeClass('bg1 bg2 bg3 bg4 bg5 bg6 bg7 bg8').addClass(item);
			});
		}
		else{
			$('#bg1').parent().stop().animate({backgroundPosition:"(0 0)"},900,function(){
				$('#bannerWrapper').removeClass('bg1 bg2 bg3 bg4 bg5 bg6 bg7 bg8').addClass(item);
			});
			$('#bg2').parent().stop().animate({backgroundPosition:"(-143px 0)"},800);
			$('#bg3').parent().stop().animate({backgroundPosition:"(-286px 0)"},700);
			$('#bg4').parent().stop().animate({backgroundPosition:"(-429px 0)"},600);
			$('#bg5').parent().stop().animate({backgroundPosition:"(-572px 0)"},500);
			$('#bg6').parent().stop().animate({backgroundPosition:"(-715px 0)"},400);
			$('#bg7').parent().stop().animate({backgroundPosition:"(-858px 0)"},300);
			$('#bg8').parent().stop().animate({backgroundPosition:"(-1001px 0)"},200);
		}
	}
});
