BT.ns(function() {with(BT) {
	
	DynamicImageGallery = Class.create({
		initialize: function(options) {
			this.setOptions(options);
			
			this.imageCache = {thumb: {}, main: {}};
		},	
		
		load: function(images) {
			this.images = images;
			
			this.thumbContainer = $(this.options.thumbContainer);
			this.thumbContainer.innerHTML = '';
			
			this.imageContainer = $(this.options.imageContainer);
			this.imageContainer.innerHTML = '';
			
			this.batchAdd(images);
		},
		
		batchAdd: function(images) {
			for(nodeId in images) {
				if(typeof(images[nodeId]) == 'object') {
					this.addThumb(nodeId, images[nodeId].thumb);
					this.addMain(nodeId, images[nodeId].main);
				}
			}
		},
		
		addThumb: function(nodeId, thumb) {
			var image, li;
			li = document.createElement('li');
			
			if(typeof(this.imageCache['thumb'][nodeId]) == 'object') {
				// pull from cache
				image = this.imageCache['thumb'][nodeId];

			} else {
				// Load the image for the first time
				image = document.createElement('img');
				image.id = this.options.thumbPrefix + nodeId;
				Element.addClassName(image, this.options.thumbClass);
				image.src = thumb.url;
				image.width = thumb.width;
				image.height = thumb.height;
				
				this.register(image, nodeId);
				
				this.imageCache['thumb'][nodeId] = image;
			}
			li.appendChild(image);
				
			this.thumbContainer.appendChild(li);;
			
			delete(li);
			delete(image);
		},
		
		addMain: function(nodeId, main) {
			var image;
			if(typeof(this.imageCache['main'][nodeId]) == 'object') {
				// pull from cache
				image = this.imageCache['main'][nodeId];
			} else {
				// Load the image for the first time
				image = document.createElement('img');
				image.id = this.options.imagePrefix + nodeId;
				image.src = main.url;
				image.width = main.width;
				image.height = main.height;
				Element.addClassName(image, this.options.imageClass);
				image.style.display = 'none';
				image.style.opacity = 0;
				
				this.imageCache['main'][nodeId] = image
			}
			
			this.imageContainer.appendChild(image);
			delete(image);
		},
		
		register: function(handler, nodeId) {
			Event.observe(handler, 'click', this.showImage.bindAsEventListener(this, nodeId));
		},
		
		showImage: function(e, nodeId) {

			if(nodeId != this.currentIndex) {

				var currentImage = $(this.options.imagePrefix + this.currentIndex);
				
				if(currentImage) {
					Element.setOpacity( currentImage, 1.0 );
					new Effect.Opacity(currentImage, {duration:this.options.transition_time, from:1.0, to:0.0, afterFinish: function() {Element.setStyle(currentImage, {display: 'none'});}.bind(this)});				
				}
				
				var newImage = $(this.options.imagePrefix + nodeId);

				Element.setOpacity( newImage, 0.0 );
				Element.setStyle(newImage, {display: ''});
	
				new Effect.Opacity(newImage, {duration:this.options.transition_time, from:0.0, to:1.0, afterFinish: function() {Element.setStyle(newImage, {display: ''});}.bind(this)});
				
				var currentThumb = $(this.options.thumbPrefix + this.currentIndex);
				if(currentThumb)
					Element.removeClassName(currentThumb, this.options.selectedClass);
					
				var newThumb = $(this.options.thumbPrefix + nodeId);
				if(newThumb)
					Element.addClassName(newThumb, this.options.selectedClass);
				
				this.currentIndex = nodeId;
			}
		},
		
		showNext: function() {

			var currentElm = $(this.options.thumbPrefix+parseInt(this.currentIndex));
			
			if(currentElm) {

				var nextElm = currentElm.up();
					
				if(nextElm)
					nextElm = nextElm.next();
					
				if(nextElm)
					nextElm = nextElm.down();
				
				if(nextElm)
					this.showImage("null", nextElm.id.replace(this.options.thumbPrefix, ''));
			}
			
		},
		
		showPrevious: function() {
			var currentElm = $(this.options.thumbPrefix+parseInt(this.currentIndex));
			
			if(currentElm) {

				var nextElm = currentElm.up();
					
				if(nextElm)
					nextElm = nextElm.previous();
					
				if(nextElm)
					nextElm = nextElm.down();
				
				if(nextElm)
					this.showImage("null", nextElm.id.replace(this.options.thumbPrefix, ''));
			}
		},
		
		setOptions: function (options) {
			this.options = {
	      		thumbContainer: 'gallery_container',
	      		imageContainer: 'image_gallery_container',
	      		thumbClass: 'gallery_thumb',
	      		imageClass: 'gallery_image',
	      		transition_time: .5,
	      		selectedClass: 'thumb_selected',
	      		thumbPrefix: 'thumb_',
	      		imagePrefix: 'image_'
			};
			Object.extend(this.options, options || {});
		}
	});
}});	
	