As3 Xml Gallery

Hi, I need help, i certainly spent too long on this one, now smoke is coming out of my ears and my poor restricted brain needs a break.

I’m trying -hard- to do a xml as3 gallery. I’m stuck on very basic things, my questions right now are how can I remove the movieclip “gallery” when adding a new one (for a new category, works but i can’t remove the previous gallery)?
How can I access the info of my thumbnail ,created in navig(), on.CLICK ? throws error 1009, null referene object… Please help!

here is the code:

package {

	import flash.events.*;
	import flash.display.MovieClip;
	import flash.display.*;
	import flash.text.*;
	import flash.net.URLRequest;
	import fl.containers.UILoader;
	import flash.net.URLLoader;
	import flash.xml.XMLDocument;
	import flash.xml.XMLNode;
	import flash.xml.XMLNodeType;
	import flash.xml.*;
	import caurina.transitions.*;
	import Thumbnail;


	public class main extends MovieClip {


		public var colour:String;
		public var colour2:String;
		public var fondMenu:MovieClip;
		public var gallery:MovieClip;
		public var image:MovieClip;
		public var thumb:Bitmap;
		public var titre:TextField;
		public var Thumbnail:Object;
		public var BigThumb:UILoader;
		public var menu:TextField;
		public var tf:TextFormat;
		public var tf2:TextFormat;
		public var xmlLoader:URLLoader;
		public var xmlTitle:XMLList;
		public var xmlList:XMLList;
		public var xml:XML;
		public var i:Number;
		public var a:Number;
		public var b:Number;
		public var c:int;
		public var d:int;
		public var j:int;
		public var numero:int;
		public var thumbLoader:Loader;
		public var url:String;
		public var loader:Loader;
		public var arrayThumb:Array;
		public var arrayUrl:Array;
		public var arrayName:Array;
		public var arrayUrlBig:Array;
		public var arrayDesc:Array;
		public var arrayLnk:Array;




		public function populate(e:Event) {
			xml = new XML(e.target.data);
			xmlTitle = xml.menu.title;
			//trace (xmlTitle[1]);
			//trace(xmlTitle.text());
			//trace(xmlTitle.length());


			colour2 = "0xE2CFD9";
			tf2 = new TextFormat();
			tf2.font = "Helvetica Light";
			tf2.color = colour;
			tf2.size = 18;


			for (i=0; i < xmlTitle.length(); i++) {
				//trace(i);
				menu = new TextField();
				fondMenu = new MovieClip();
				menu.selectable = false;
				fondMenu.buttonMode = true;
				menu.x = 25;
				menu.y = 75+ (10*i);
				fondMenu.x = menu.x;
				fondMenu.y = menu.y;
				//fondMenu.width = menu.width;
				//fondMenu.height = 15;

				menu.text = xmlTitle*;
				menu.setTextFormat(tf2);
				//fondMenu.callin =menu.text;
				fondMenu.addEventListener(MouseEvent.CLICK, navig);

				fondMenu.addChild(menu);
				addChild(fondMenu);
			}
		}
		function onCompleteSmall(e:Event) {
			e.target.removeEventListener(Event.COMPLETE, onCompleteSmall);
			trace ("j="+j);
			trace ("a="+a);
			var image = new MovieClip();
			var gallery = new MovieClip();
			if (j>a || j==a){
				trace ("somethiung");
				//while (gallery.image.numChildren >0){
					//gallery.removeChild(image);
				//while (gallery.numChildren >0) {
					//this.removeChild(gallery);
				//gallery.x+=100;
				//.removeChildAt(1);
			//gallery.removeChild(image);}
			
				c=0;
				d=0;
				j=0;
			}
			
			// var thumb:Bitmap= new Bitmap();//trace (e.target);
			// thumb = e.target.content as Bitmap;
			//thumb.smoothing = true;
			//thumb.x = -thumb.width * .5;
			//thumb.y = -thumb.height * .5;
			//trace (thumb);
			if (c>3) {
				c=0;
				d++;
			}
			image.rank = j;
			image.numero = numero;//outputs 0...
			trace (image.url);
			image.x = 235 + e.target.content.width*c;
			image.y = menu.y + e.target.content.height*d;
			image.addChild(e.target.content);
			gallery.addChild(image);
			
			
			//trace (a);
			image.addEventListener(MouseEvent.CLICK, enlarge);
			image.addEventListener(MouseEvent.MOUSE_OUT, onOut);
			image.addEventListener(MouseEvent.MOUSE_OVER, onOver);
			addChild(gallery);
			c++;
			j++;
		}
		function clearThumbs() {
			//if (image.numChildren >0) {
			if (contains(this.image)) {
				//parent.image.removeChild(loader);
			}
		}
		function loadImage(url:String,  onCompleteSmall:Function, a:Number, numero:int) {
			var loader:Loader = new Loader();

			loader.contentLoaderInfo.addEventListener(Event.COMPLETE, onCompleteSmall);

			loader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, ioError, false, 0, true);
			loader.load(new URLRequest(url));


		}
		function ioError(event:IOErrorEvent) {
			trace("the **** is"+ event);
		}
		public function navig(Event:MouseEvent) {
			//clearThumbs();
			var c =0;
			var d =0;
			
			var arrayUrl= new Array();
			var arrayName= new Array();
			var arrayUrlBig= new Array();
			var arrayDesc= new Array();
			var arrayLnk= new Array();

			xmlList = xml.work.piece;
			//trace (xmlList);
			trace(Event.target.text);
			a=0;
			for (i=0; i< xmlList.length(); i++) {


				if (String(xmlList*.@cat) == Event.target.text ) {
					//trace (xmlList*.@cat);
					//trace(a);
					//trace(Event.currentTarget.text);
					var url:String= String(xmlList*.url.text());
					//trace (url);
					var numero:int = a;
					arrayUrl.push(url);
					arrayName.push(xmlList*.name);
					trace(arrayName[a]);
					arrayUrlBig.push(xmlList*.url_big);
					arrayDesc.push(xmlList*.description);
					arrayLnk.push(xmlList*.link);
					loadImage(url, onCompleteSmall, a, numero);
					a++;
				}
			}

		}
		public function enlarge(event:MouseEvent) {
			var big:MovieClip = new MovieClip;
			var square:Sprite = new Sprite();
			square.graphics.beginFill(0xFFFFFF,1);
			square.graphics.drawRect(100, 75,200, 550);
			square.graphics.endFill();
			big.addChild(square);
			trace(event.target.rank);
			trace(arrayUrlBig[1]);
			//bigThumb.source = 
			//bigThumb.load();
			
		}
		public function onOver(event:MouseEvent) {
		}
		public function onOut(event:MouseEvent) {
		}
		public function main() {

			colour = "0xE2CFD9";
			tf = new TextFormat();
			tf.font = "Helvetica Light";
			tf.color = colour;
			tf.size = 28;

			var titre = new TextField();
			titre.x=20;
			titre.y=15;
			titre.width = 120;
			titre.text = "Ventoline";
			titre.selectable = false;
			titre.setTextFormat(tf);
			addChild(titre);

			xmlLoader = new URLLoader();
			xmlLoader.load(new URLRequest("ventoline.xml"));
			xmlLoader.addEventListener(Event.COMPLETE, populate);
			var d=0;
			var c=0;
			var j=0;
			
			
		}
	}
}