$(function() {
                /* position of the <li> that is currently shown */
                var current = -1;
                
                $('#bg1,#bg2,#bg3,#bg4,#bg5').mouseover(function(e){
                    
                    var $this = $(this);
                    /* if we hover the current one, then don't do anything */
                    
                    if($this.parent().index() == current)
                        return;

                    /* item is bg1 or bg2 or bg3, depending where we are hovering */
                    var item =  $this.attr('id'); //e.target.id;

                    /*
                    this is the sub menu overlay. Let's hide the current one
                    if we hover the first <li> or if we come from the last one,
                    then the overlay should move left -> right,
                    otherwise right->left
                     */
                    
                    var newCurrent = $this.parent().index();
                    
                    if(item == 'bg' || ((current > 0 && current < 0) && item != 'bg5'))
                        $('#menu .sub'+parseInt(current+1)).stop().animate({backgroundPosition:"(-195px 0)"},300,function(){
                            // $(this).find('li').hide();
                            $(this).find('li').fadeOut();
                        });
                    else
                        $('#menu .sub'+parseInt(current+1)).stop().animate({backgroundPosition:"(195px 0)"},300,function(){
                            // $(this).find('li').hide();
                            $(this).find('li').fadeOut();
                        });

                   // if ( item == 'bg' || (current > 0  && item != 'bg5')){
                    if (newCurrent > current){
                    	
                    	/* if we hover the first <li> or if we come from the last one, then the images should move left -> right */
                        $('#menu > li').animate({backgroundPosition:"(-980px 0)"},0).removeClass('bg0 bg1 bg2 bg3 bg4 bg5').addClass(item);
                        move(1,item);
                    }
                    else {   
                    	
                        /* if we hover the first <li> or if we come from the last one, then the images should move right -> left */
                        $('#menu > li').animate({backgroundPosition:"(980px 0)"},0).removeClass('bg0 bg1 bg2 bg3 bg4 bg5').addClass(item);                        
                        move(0,item);
                    }
                    
                    /*THE FOLLOWING LOGIC, FROM THE ORIGINAL SCRIPT IS NO LONGER NEEDED*/

                    /*
                    We want that if we go from the first one to the last one (without hovering the middle one),
                    or from the last one to the first one, the middle menu's overlay should also slide, either
                    from left to right or right to left.
                     */

                    if(current == 2 && item == 'bg1'){
                        $('#menu .sub'+parseInt(current)).stop().animate({backgroundPosition:"(-195px 0)"},300);
                    }
                    if(current == 0 && item == 'bg5'){
                        $('#menu .sub'+parseInt(current+2)).stop().animate({backgroundPosition:"(195px 0)"},300);
                    }


					
                    /* change the current element */
                    current = $this.parent().index();
					
                    /* let's make the overlay of the current one appear */
                   
                    $('#menu .sub'+parseInt(current+1)).stop().animate({backgroundPosition:"(0 0)"},300,function(){
                        $(this).find('li').fadeIn();
                    });
                });
                /*
                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:"(-195px 0)"},300);
                        $('#bg3').parent().stop().animate({backgroundPosition:"(-390px 0)"},400);
                        $('#bg4').parent().stop().animate({backgroundPosition:"(-585px 0)"},500);
                        $('#bg5').parent().stop().animate({backgroundPosition:"(-780px 0)"},600,function(){
                            $('#menuWrapper').removeClass('bg0 bg1 bg2 bg3 bg4 bg5').addClass(item);
                        });
                    }
                    else{
                        $('#bg1').parent().stop().animate({backgroundPosition:"(0 0)"},600);
                        $('#bg2').parent().stop().animate({backgroundPosition:"(-195px 0)"},500);
                        $('#bg3').parent().stop().animate({backgroundPosition:"(-390px 0)"},400);
                        $('#bg4').parent().stop().animate({backgroundPosition:"(-585px 0)"},300);
                        $('#bg5').parent().stop().animate({backgroundPosition:"(-780px 0)"},200,function(){
                            $('#menuWrapper').removeClass('bg0 bg1 bg2 bg3 bg4 bg5').addClass(item);
                        });
                    }
                }
            });


