Infinite menu

Hi all I change this infinite menu from xml designed by Scotty for show big image from menu

but when I click for example foto1 it shows foto2 and when I click the last foto it shows undefined error. Is there anyone can fix this problem?
Many thanks

pictArray = new Array();
tx=new Array()
my_xml = new XML();
my_xml.ignoreWhite = true;my_xml.onLoad = function(success) {
 if (success) {
  for (var i = 0; i<this.firstChild.childNodes.length; i++) {
   pictArray.push(this.firstChild.childNodes*.attributes.image);
   tx.push(this.firstChild.childNodes*.attributes.titole);
  }
 }
 delay = setInterval(makeMenu, 100);
};
my_xml.load("gallery.xml");
var menu = this.createEmptyMovieClip("menu_tot", 99);
menu.setMask(mask);
var sub_menu = menu.createEmptyMovieClip("menu", 100);
var p = 0;
var q = 0;
var loadper = 0;
function makeMenu() {
 clearInterval(delay);
 var item = sub_menu.createEmptyMovieClip("pic"+q, q);
 item.loadMovie(pictArray[p]);
 
 var temp = _parent.createEmptyMovieClip("tmp", 9999+q);
 temp.onEnterFrame = function() {
  var tot = item.getBytesTotal();
  var loa = item.getBytesLoaded();
  var per = Math.round(((loa/tot)*100)/(pictArray.length*2));
  loadBar._xscale = loadper+per;
  if (tot == loa && tot>4) {
   loadper += per;
   if (q>0) {
    item._x = sub_menu["pic"+(q-1)]._x+sub_menu["pic"+(q-1)]._width;
   } else {
    item._x = 0;
   }
   nextItem();
   temp.testo=tx[p];
   
   item.onRelease=function(){
    
    _root.loader.loadMovie(temp.testo);
   }
   delete this.onEnterFrame;
     
  }
  
 };
 
}
var amount = pictArray.length-1;
function nextItem() {
 if (q<((pictArray.length*2)-1)) {
  q++;
  if (p<(pictArray.length-1)) {
   p++;
   makeMenu();
  } else {
   p = 0;
   makeMenu();
  }
 } else {
  activateMenu();
 }
}
var center = Stage.width/2;
/*
var speed = 5;//change this in the value you like
function activateMenu() {
        menu.onEnterFrame = function() {
                this._x += speed;
                if (this._x<-sub_menu._width/2) {
                        this._x = 0;
                }
                if (this._x>0) {
                        this._x = -sub_menu._width/2;
                }
        };
}

*/
var speed = .01;
function activateMenu() {
 menu.onEnterFrame = function() {
  var distance = _root._xmouse-center;
  if (mask.hitTest(_root._xmouse, _root._ymouse)) {
   this._x += (distance*speed);
   if (this._x<-sub_menu._width/2) {
    this._x = 0;
   }
   if (this._x>0) {
    this._x = -sub_menu._width/2;
   }
  }
 };
}