Image Loader Gallery

I have this piece of code to change images. I was trying to figure out how you could put a timer on the code, so if the image has been displayed for say 5 seconds it diverts to the next image automatically?

Also if it’s not to hard, when it has played to the end to divert to the 1st image.

preloader.swapDepths(999);
arrows.swapDepths(998);
depth_sorter = 2;
picture_odd = 0;
import mx.transitions.Tween;
import mx.transitions.easing.*;

function move_stuff() {
trace(picture_odd);
if(picture_odd == 0){
var tween_handler:Tween = new Tween(picture_0, “_alpha”, Strong.easeOut, 0, 100, 2, true);
tween_handler.onMotionFinished = function(){
//picture_1._alpha = 100;
}
} else {
var tween_handler:Tween = new Tween(picture_1, “_alpha”, Strong.easeOut, 0, 100, 2, true);
}
}

function loadXML(loadedX) {
if (loadedX) {
xmlNode = this.firstChild;
image = [];
total = xmlNode.childNodes.length;
for (i=0; i<total; i++) {
image* = xmlNode.childNodes*.childNodes[0].firstChild.nodeValue;
}
firstImage();
} else {
content = “file not loaded!”;
}
}

xmlData = new XML();
xmlData.ignoreWhite = true;
xmlData.onLoad = loadXML;
xmlData.load(“images.xml”);
p = 0;
startClipX = 0;
this.onEnterFrame = function() {
if(picture_odd ==0){
filesize = picture_0.holder.getBytesTotal();
loadedX = picture_0.holder.getBytesLoaded();
} else{
filesize = picture_1.holder.getBytesTotal();
loadedX = picture_1.holder.getBytesLoaded();
}
if (loadedX != filesize && filesize > 10) {
loading = 1;
if(loadedX > 10){
preloader._visible = true;
} else{
preloader._visible = false;
}

	preloader.preload_bar._xscale = 100*loadedX/filesize;
} else if(loadedX &gt;= filesize && filesize &gt; 10) {
	if(loadedFile == 0){ 
	move_stuff();
	loadedFile = 1;
	}
	preloader._visible = false;
}

};

function load_pic(){
igm = p+1;
loadedFile = 0;
if (picture_odd == 1){
picture_0.holder.loadMovie(image[p], 1);
picture_odd = 0;
picture_0.swapDepths(depth_sorter);
depth_sorter +1;
}else{
picture_1.holder.loadMovie(image[p], 1);
picture_odd = 1;
picture_1.swapDepths(depth_sorter);
depth_sorter +1;
}
}

function nextImage() {
prev_img = 0;
next_img = 1;
if (p<(total-1)) {
p++;
if (loadedX == filesize) {
load_pic();
picture_num();
}
} else if (p==(total-1)) {
p=0;
if (loadedX == filesize) {
load_pic();
picture_num();
}
}
}

function prevImage() {
prev_img = 1;
next_img = 0;
if (p<(total) && p>0) {
p–;
if (loadedX == filesize) {
load_pic();
picture_num();
}
} else if (p==0) {
p=total-1;
if (loadedX == filesize) {
load_pic();
picture_num();
}
}
}

function firstImage() {
startClip = 1;
if (loadedX == filesize) {
load_pic();
picture_num();
}
}

function picture_num() {
current_pos = p+1;
}