if (Object.isUndefined(Prototype) || parseFloat(Prototype.Version) < 1.6) throw('gallery.js requires prototype.js 1.6+');

var Gallery = Class.create({
	
	initialize: function(id, images) {
		this._id						= id;
		this._current_image	= null;
		this._dom						= {};
		this._dom.gallery 	= $(id);
		this._images				= images;
		
		this.create();
		this.changeImage(1);
	},
	
	changeImage: function(direction) {
		if (this._images.length == 0) return;
		
		this._dom.image_container.hide();
		this._dom.image.hide();
		this._dom.controls.hide();
		this._dom.caption_div.hide();
		
		this._current_image	= (this._current_image == null) ? 0 : this._current_image + direction;
		// this should not be necessary as prev/next links shouldn't be shown at beginning/end of gallery
		if (this._current_image < 0) {
			this._current_image = this._images.length - 1;
		} else if (this._current_image >= this._images.length) {
			this._current_image = 0;
		}
		
		var _this_ = this;
		image_preloader = new Image();
		image_preloader.onload = function() {
			_this_._dom.image.setAttribute('src', _this_._images[_this_._current_image].src);
			_this_._dom.image.setAttribute('alt', _this_._images[_this_._current_image].caption);
			_this_._dom.image.setAttribute('title', _this_._images[_this_._current_image].caption);
			_this_._dom.caption_p.update(_this_._images[_this_._current_image].caption);
			
			_this_.showImage();
			image_preloader.onload = function() {}; // for IE weirdness
		}
		
		image_preloader.src = this._images[this._current_image].src;

	},
	
	create: function() {
		var _this_ = this;
		
		var	image_container = document.createElement('div');
		image_container.setAttribute('id', this._id + '_image_container');
		image_container.style.display = 'none';
		this._dom.gallery.insert(image_container);
		this._dom.image_container = $(image_container.id);
		
		// viewable image
		var	image = document.createElement('img');
		image.setAttribute('id', this._id + '_image');
		image.style.display = 'none';
		this._dom.image_container.insert(image);
		this._dom.image = $(image.id);
		
		// controls container
		var	controls = document.createElement('div');
		controls.setAttribute('id', this._id + '_controls');
		controls.style.display = 'none';
		this._dom.gallery.insert(controls);
		this._dom.controls = $(controls.id);
		
		// prev link
		var	a_prev = document.createElement('a');
		a_prev.setAttribute('id', this._id + '_prev');
		a_prev.setAttribute('href', '#');
		this._dom.controls.insert(a_prev);
		this._dom.a_prev = $(a_prev.id);
		this._dom.a_prev.update('Previous ');
		this._dom.a_prev.onclick = function() {
			_this_.changeImage(-1);
			return false;
		};
		
		
		// image count
		var count = document.createElement('span');
		count.setAttribute('id', this._id + '_count');
		this._dom.controls.insert(count);
		this._dom.count = $(count.id);
		
		// next link
		var a_next = document.createElement('a');
		a_next.setAttribute('id', this._id + '_next');
		a_next.setAttribute('href', '#');
		this._dom.controls.insert(a_next);
		this._dom.a_next = $(a_next.id);
		this._dom.a_next.update('Next');
		this._dom.a_next.onclick = function() {
			_this_.changeImage(1);
			return false;
		};
		
		// caption
		var caption_div = document.createElement('div');
		caption_div.setAttribute('id', this._id + '_info');
		this._dom.gallery.insert(caption_div);
		this._dom.caption_div = $(caption_div.id);
		
		var caption_h = document.createElement('h3');
		caption_h.setAttribute('id', this._id + '_info_h');
		this._dom.caption_div.insert(caption_h);
		this._dom.caption_h = $(caption_h.id);
		this._dom.caption_h.update('This Image:');
		
		var caption_p = document.createElement('p');
		caption_p.setAttribute('id', this._id + '_caption');
		this._dom.caption_div.insert(caption_p);
		this._dom.caption_p = $(caption_p.id);
	},
	
	showImage: function() {
		var count 	= this._current_image + 1;
		var count_s	= count + ' of ' + this._images.length;
		
		if (count > 1) { 
			this._dom.a_prev.show();
			count_s = ' | ' + count_s;
		} else { 
			this._dom.a_prev.hide();
		}
		
		if (count < this._images.length) { 
			this._dom.a_next.show();
			count_s += ' | ';
		} else { 
			this._dom.a_next.hide();
		}
		
		this._dom.count.update(count_s);
		this._dom.controls.show();
		this._dom.image.show();
		this._dom.image_container.show();
		this._dom.caption_div.show();
	}
	
});