$(function() {
    var current = 0;

var loaded  = 0;
for(var i = 1; i <4; ++i)
   $('<img />').load(function(){
      ++loaded;
      if(loaded == 3){
         $('#bg1,#bg2,#bg3,#bg4,#bg5').mouseover(function(e){
            
            var $this = $(this);
            if($this.parent().index() == current)
               return;

            var item = e.target.id;

            if(item == 'bg1' || current == 2)
               $('#menu .sub'+parseInt(current+1)).stop().animate({backgroundPosition:"(-198px 0)"},300,function(){
                  $(this).find('li').hide();
               });
            else
               $('#menu .sub'+parseInt(current+1)).stop().animate({backgroundPosition:"(198px 0)"},300,function(){
                  $(this).find('li').hide();
               });

            if(item == 'bg1' || current == 2){
               $('#menu > li').animate({backgroundPosition:"(-800px 0)"},0).removeClass('bg1 bg2 bg3 bg4 bg5').addClass(item);
               move(1,item);
            }
            else{
               $('#menu > li').animate({backgroundPosition:"(800px 0)"},0).removeClass('bg1 bg2 bg3 bg4 bg5').addClass(item);
               move(0,item);
            }

            if(current == 2 && item == 'bg1'){
               $('#menu .sub'+parseInt(current)).stop().animate({backgroundPosition:"(-198px 0)"},300);
            }
            if(current == 0 && item == 'bg5'){
               $('#menu .sub'+parseInt(current+2)).stop().animate({backgroundPosition:"(198px 0)"},300);
            }

            
            current = $this.parent().index();
            
            
            $('#menu .sub'+parseInt(current+1)).stop().animate({backgroundPosition:"(0 0)"},300,function(){
               $(this).find('li').fadeIn();
            });
         });
      }  
   }).attr('src', '/org.auctores.fitnesspark.hp2/org/auctores/fitnesspark/hp2/view/jsp/images/'+i+'.jpg'); /** MUSS ANGEPASST WERDEN **/
         
    function move(dir,item){
        if(dir){
            $('#bg1').parent().stop().animate({backgroundPosition:"(0 0)"},400);
            $('#bg2').parent().stop().animate({backgroundPosition:"(-198px 0)"},300);
            $('#bg3').parent().stop().animate({backgroundPosition:"(-396px 0)"},200);
        	$('#bg4').parent().stop().animate({backgroundPosition:"(-594px 0)"},400);
    		$('#bg5').parent().stop().animate({backgroundPosition:"(-792px 0)"},300,function(){
                $('#menuWrapper').removeClass('bg1 bg2 bg3 bg4 b5').addClass(item);
            });
        }
        else{
            $('#bg1').parent().stop().animate({backgroundPosition:"(0 0)"},400,function(){
                $('#menuWrapper').removeClass('bg1 bg2 bg3').addClass(item);
            });
            $('#bg2').parent().stop().animate({backgroundPosition:"(-198px 0)"},300);
            $('#bg3').parent().stop().animate({backgroundPosition:"(-396px 0)"},200);
            $('#bg4').parent().stop().animate({backgroundPosition:"(-594px 0)"},400);
            $('#bg5').parent().stop().animate({backgroundPosition:"(-792px 0)"},200);
        }
    }
});
