           $(function() {
                var current = 0;

				var loaded  = 0;
				for(var i = 1; i <4; ++i)
					$('<img />').load(function(){
						++loaded;
						if(loaded == 3){
							$('#bg1,#bg2,#bg3').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 = e.target.id;

								if(item == 'bg1' || current == 2){
									/* 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:"(-1024px 0)"},0).removeClass('bg1 bg2 bg3').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:"(1024px 0)"},0).removeClass('bg1 bg2 bg3').addClass(item);
									move(0,item);
								}

								/* change the current element */
								current = $this.parent().index();

							});
						}	
					}).attr('src', 'img/'+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:"(-347px 0)"},300);
                        $('#bg3').parent().stop().animate({backgroundPosition:"(-694px 0)"},400,function(){
                            $('#menuWrapper').removeClass('bg1 bg2 bg3').addClass(item);
                        });
                    }
                    else{
                        $('#bg1').parent().stop().animate({backgroundPosition:"(0 0)"},400,function(){
                            $('#menuWrapper').removeClass('bg1 bg2 bg3').addClass(item);
                        });
                        $('#bg2').parent().stop().animate({backgroundPosition:"(-347px 0)"},300);
                        $('#bg3').parent().stop().animate({backgroundPosition:"(-694px 0)"},200);
                    }
                }
            });
