Gallery = Class.create();
Gallery.prototype = {

 	initialize: function(triggers, contentClass, imagesClass, contentWrapper, imageWrapper, imageNavWrapper) {
		this.triggers = triggers;
		this.contentWrapper = contentWrapper;
		this.imageWrapper = imageWrapper;
		this.imageNavWrapper = imageNavWrapper;
		if (this.triggers && this.contentWrapper && this.imageWrapper && this.imageNavWrapper) {
			for (var i=0; i<this.triggers.length; i++) {
				if (!this.triggerClass) {
					if (this.triggers[i].className.match(/(.*)\s/)) {
						this.triggerClass = this.triggers[i].className.match(/(.*)\s/)[1];
					} else {
						this.triggerClass = this.triggers[i].className;
					}
				}
				
				if (this.triggers[i].hasClassName('active') && !this.lastIndex){
					this.lastIndex = i; 
				}
				this.triggers[i] = this.triggers[i].up();
				this.triggers[i].content = this.triggers[i].down('.'+contentClass);
				this.triggers[i].images = this.triggers[i].down('.'+imagesClass);
				this.triggers[i].index = i;
				this.setEvent(this.triggers[i], i);
			}

			if (this.lastIndex) this.showContent(null, this.lastIndex);
		}

 	},
 	
 	setEvent: function(item, i) {
 		Event.observe(item, 'click', this.showContent.bindAsEventListener(this, i))
 	},
 	
 	showContent: function(evt, i) {
		this.triggers[this.lastIndex].down('.'+this.triggerClass).removeClassName('active');

 		this.triggers[i].down('.'+this.triggerClass).addClassName('active');
 		this.contentWrapper.innerHTML = this.triggers[i].content.innerHTML;
 		this.imageWrapper.innerHTML = '';
 		this.image = Builder.node('img', {src:'', border:'0'});
 		this.imageWrapper.appendChild(this.image);

 		this.imageNavWrapper.innerHTML = this.triggers[i].images.innerHTML;
 		var images = this.imageNavWrapper.getElementsBySelector('a');
 		
 		if (images.length>1) {
			for (var j=0; j<images.length; j++) {
				var image = images[j];
				Event.observe(image, 'click', this.showImage.bindAsEventListener(this, i, image))
			}
		} else {
	 		this.imageNavWrapper.innerHTML = '';
		}
 		this.showImage(null, 0, images[0]);

 		this.lastIndex = i;
 	},
 	
 	showImage: function(evt, i, image) {
 		if (evt) Event.stop(evt);
 		this.image.src = image.href;

		var wrapper = this.contentWrapper.up('.popup');
		wrapper.style.zIndex = parseInt(wrapper.getStyle('zIndex'))+1;
 		
 	}

};
