Two (Grid Gallery) Problems

I have created this grid gallery however I am having to problems which I cannot for the life of me work out what i’m doing wrong am I just being dumb

  1. I have tried to restrict the count of thumbs so I can use a next and previous base on xml length however it just keeps going through them regardless and also previous.

  2. When selecting next for the next set of thumbs its not unloading the first is just adding more children to the container I have tried container.contains(thumbs) thumbs being a type of loader.

here is the code



var container:MovieClip = new MovieClip();
var thumbContainer:MovieClip = new MovieClip();
var large_image:MovieClip = new MovieClip();
var thumb_image:MovieClip = new MovieClip();
var _close:Close = new Close();

var columns:Number = 9;
var xCounter:Number = 0;
var yCounter:Number = 0;

var page:Number = 0;
var thumbsPerPage:Number = 18;

var xmlLoader:URLLoader;
var imageList:XMLList;
var totalThumbs:Number;

var loader:Loader;

var thumbURL:String;
var thumbLocation:String = "http://local.bennraistrick.co.uk/local/images/creative/graphic/thumb/";
var thumbLoader:Loader;

var imageLocation:String = "http://local.bennraistrick.co.uk/local/images/creative/graphic/image/";

var lastPage = page;

xmlLoader = new URLLoader();
xmlLoader.load(new URLRequest("http://local.bennraistrick.co.uk/local/net/xml/creative_gallery.xml"));
xmlLoader.addEventListener(Event.COMPLETE, loadXML);
    
addChild(container);
addChild(large_image);

next_btn.buttonMode = true;
next_btn.addEventListener(MouseEvent.CLICK, nextImages);
next_btn.addEventListener(MouseEvent.MOUSE_OVER, over);
next_btn.addEventListener(MouseEvent.MOUSE_OUT, out);
previous_btn.buttonMode = true;
previous_btn.addEventListener(MouseEvent.CLICK, prevImages);
previous_btn.addEventListener(MouseEvent.MOUSE_OVER, over);
previous_btn.addEventListener(MouseEvent.MOUSE_OUT, out);

function nextImages(e:MouseEvent)
{
    xCounter = 0;
    yCounter = 0;
    page = Math.min(page + 1, Math.floor((totalThumbs - 1) / thumbsPerPage));
    createThumbs();
}
function prevImages(e:MouseEvent)
{
    xCounter = 0;
    yCounter = 0;
    page = Math.max(page - 1, 0);
    createThumbs();
}
function over(e:MouseEvent)
{
    e.target.gotoAndStop(2);
}
function out(e:MouseEvent)
{
    e.target.gotoAndStop(1);
}


function loadXML(e:Event)
{
    // temp variable
    var xml:XML = new XML(e.target.data);
    
    imageList = xml.element;
    totalThumbs = imageList.length();
    
    createThumbs();
}

function createThumbs()
{
    lastPage = page;
    
    for(var i:uint = page * thumbsPerPage; i < Math.min((page + 1) * thumbsPerPage, totalThumbs); i++)
    {
        thumbURL = imageList*.thumb_image;
        
        thumbLoader = new Loader();
        
        thumbLoader.load(new URLRequest(thumbLocation + thumbURL));
        thumbLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, thumbLoaded);
        thumbLoader.name = String(i);
    }
}

function thumbLoaded(e:Event):void
{
    var thumbs:Loader = Loader(e.target.loader);
    
    while(container.numChildren > 14) {
     container.removeChildAt(0);
    }  
    
    container.addChild(thumbs);
    thumbs.alpha = 0.4;
    thumbs.addEventListener(MouseEvent.CLICK, loadImages);
    thumbs.addEventListener(MouseEvent.MOUSE_OVER, thumbOver);
    thumbs.addEventListener(MouseEvent.MOUSE_OUT, thumbOut);
    container.buttonMode = true;
    
    // Create A Grid Of Thumbs
    thumbs.x = (thumbs.width +11) * xCounter;
    thumbs.y = (thumbs.height +11) * yCounter;
            
    if (xCounter+1 < columns)
    {
        xCounter++;
    } 
    else 
    {
        xCounter = 0;
        yCounter++;
    }
    container.y = 0;
    next_btn.y = container.height + 5;
    previous_btn.y = container.height + 5;
}
function thumbOver(e:MouseEvent):void
{
    TweenLite.to(e.target, 1, {alpha:1});
}
function thumbOut(e:MouseEvent):void
{
    TweenLite.to(e.target, 1, {alpha:0.4});
}



function loadImages(e:MouseEvent):void
{
    var imageURL = imageList[e.target.name].main_image;
    
    loader = new Loader();
    loader.load(new URLRequest(imageLocation + imageURL));
    loader.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS, showProgress);
    loader.contentLoaderInfo.addEventListener(Event.INIT, addImage);
    
    loader.removeEventListener(MouseEvent.CLICK, addImage);
}

function showProgress(e:Event)
{
    preloadtxt.text = "";
    preloadtxt.text = "loaded:"+Math.round((e.currentTarget.bytesLoaded/e.currentTarget.bytesTotal)*100) + "%";
}

function addImage(e:Event):void
{
    var loader:Loader = Loader(e.target.loader);
    preloadtxt.text = "";
    
    while(large_image.numChildren)
    {
        large_image.removeChildAt(0);
    }
    large_image.addChild(loader);
    
    large_image.x = stage.stageWidth / 2;
    large_image.y = (container.y + container.height) + next_btn.height + 18;
    
    loader.x = - large_image.width / 2;
    
    loader.alpha = 0;
    TweenLite.to(loader, 2, {alpha:1});

    large_image.buttonMode = true;
    loader.addEventListener(MouseEvent.CLICK, removeImage);
    
}

function removeImage(e:MouseEvent):void
{
    var loader:Loader = Loader (e.currentTarget);
    
    loader.unload();
    
    if(large_image.contains(loader))
    {
        large_image.removeChild(loader);
    }
}

any help would be great