$(function() {
	var current = 0;
	var loaded  = 0;
	for(var i=1; i<8; ++i)
		$('<img />').load(function(){
			++loaded;
			if(loaded == 5){
				$('#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 == 4 || current == 7)
						$('#menu .sub'+parseInt(current+1)).stop().animate({backgroundPosition:"(-115px 0)"},300,function(){
							$(this).find('li').hide();
						});
					else
						$('#menu .sub'+parseInt(current+1)).stop().animate({backgroundPosition:"(115px 0)"},300,function(){
							$(this).find('li').hide();
						});

					if(item == 'bg1' || current == 4 || current == 7){
						$('#menu > li').animate({backgroundPosition:"(-920px 0)"},0).removeClass('bg1 bg2 bg3 bg4 bg5 bg6 bg7 bg8').addClass(item);
						move(1,item);
					}
					else{
						$('#menu > li').animate({backgroundPosition:"(920px 0)"},0).removeClass('bg1 bg2 bg3 bg4 bg5 bg6 bg7 bg8').addClass(item);
						move(0,item);
					}

					if(current == 4 && current == 7 && item == 'bg1'){
						$('#menu .sub'+parseInt(current)).stop().animate({backgroundPosition:"(-115px 0)"},500);
					}
					if(current == 0 && current == 3 && item == 'bg8'){
						$('#menu .sub'+parseInt(current+2)).stop().animate({backgroundPosition:"(115px 0)"},500);
					}
					
					current = $this.parent().index();
					
					$('#menu .sub'+parseInt(current+1)).stop().animate({backgroundPosition:"(0 0)"},500,function(){
						$(this).find('li').fadeIn();
				});
			});
		}	
	}).attr('src', 'img/org/'+i+'.jpg');
	
	/*
	dir:1 - move left->right
	dir:0 - move right->left
	*/
 
	function move(dir,item){
	    if(dir){
	        $('#bg1').parent().stop().animate({backgroundPosition:"(0 0)"},200);
	        $('#bg2').parent().stop().animate({backgroundPosition:"(-115px 0)"},300);
	        $('#bg3').parent().stop().animate({backgroundPosition:"(-230px 0)"},400);
	        $('#bg4').parent().stop().animate({backgroundPosition:"(-345px 0)"},500);
	        $('#bg5').parent().stop().animate({backgroundPosition:"(-460px 0)"},600);
	        $('#bg6').parent().stop().animate({backgroundPosition:"(-575px 0)"},700);
	        $('#bg7').parent().stop().animate({backgroundPosition:"(-690px 0)"},800);
	        $('#bg8').parent().stop().animate({backgroundPosition:"(-805px 0)"},900,function(){
	            $('#menuWrapper').removeClass('bg1 bg2 bg3 bg4 bg5 bg6 bg7 bg8').addClass(item);
	        });
	    }
	    else{
	        $('#bg1').parent().stop().animate({backgroundPosition:"(0 0)"},900,function(){
	            $('#menuWrapper').removeClass('bg1 bg2 bg3 bg4 bg5 bg6 bg7 bg8').addClass(item);
	        });
	        $('#bg2').parent().stop().animate({backgroundPosition:"(-115px 0)"},800);
	        $('#bg3').parent().stop().animate({backgroundPosition:"(-230px 0)"},700);
	        $('#bg4').parent().stop().animate({backgroundPosition:"(-345px 0)"},600);
	        $('#bg5').parent().stop().animate({backgroundPosition:"(-460px 0)"},500);
	        $('#bg6').parent().stop().animate({backgroundPosition:"(-575px 0)"},400);
	        $('#bg7').parent().stop().animate({backgroundPosition:"(-690px 0)"},300);
	        $('#bg8').parent().stop().animate({backgroundPosition:"(-805px 0)"},200);
	    }
	}
});

