// JavaScript Document

// Requires Prototype.js 1.6.0.3

if(!window.repperGallery)
	var repperGallery = new Object();
	
repperGallery.Methods = {
	
	options: {
		thumbContainer: 'thumbLinks',
		largeImgId: 'largeImg',
		maxWidth: '520',
		maxHeight: '368',
		captions: false,
		captionId: ''
	},
	
	_options: new Object,
	
	init: function(options) {
		Object.extend(this._options, this.options);
		this.setOptions(options);
		Event.observe(this.options.thumbContainer, 'click', this.swapImage.bindAsEventListener(this));
	},
	
	setOptions: function(options) {
		Object.extend(this.options, options || {});
	},
	
	swapImage: function(e) {
		var clicked = Event.findElement(e, 'A');
		if(clicked) {
			var href = clicked.href;
			if(href!='#') {
				this._getImageOrient(href);
				$(this.options.largeImgId).src = href;
				if(this.options.captions) {
					this._setCaptions(clicked);	
				}
			}
		}
		Event.stop(e);
	},
	
	imageWidth: '',
	imageHeight: '',
	
	_getImageOrient: function(imgSrc) {
			var img = new Image();			
			Element.observe(img, 'load', function(e) {
				this.imageHeight = img.height;
				this.imageWidth = img.width;				
				if(this.imageHeight>this.imageWidth) {
					this._setHeight();
				} else if (this.imageWidth>this.imageHeight) {
					this._setWidth();
				} else {
					if(this.options.maxWidth > this.options.maxHeight) {
						this._setHeight();
					} else {
						this._setWidth();	
					}
				}
				
			}.bind(this));
			img.src = imgSrc;
	},
	
	_setWidth: function() {
		if(this.imageWidth>this.options.maxWidth) {
				$(this.options.largeImgId).style.width = this.options.maxWidth+'px';
				$(this.options.largeImgId).style.height = '';
			
			var ratio = this.options.maxWidth/this.imageWidth;
			var newHeight = ratio*this.imageHeight;
			if(newHeight>this.options.maxHeight) {
				$(this.options.largeImgId).style.height = this.options.maxHeight+'px';
				$(this.options.largeImgId).style.width = '';	
			} 
		}else {
			$(this.options.largeImgId).style.width = this.imageWidth+'px';
			$(this.options.largeImgId).style.height = '';	
		}
	},
	
	_setHeight: function() {
		if(this.imageHeight>this.options.maxHeight) {
			$(this.options.largeImgId).style.height = this.options.maxHeight+'px';
			$(this.options.largeImgId).style.width = '';
		
			var ratio = this.options.maxHeight/this.imageHeight;
			var newWidth = ratio*this.imageWidth;
			if(newWidth>this.options.maxWidth) {
				$(this.options.largeImgId).style.width = this.options.maxWidth+'px';
				$(this.options.largeImgId).style.height = '';
			}
		} else {
			$(this.options.largeImgId).style.height = this.imageHeight+'px';
			$(this.options.largeImgId).style.width = '';	
		}
	},
	
	_setCaptions: function(clicked) {
		var cap = clicked.title;
		$(this.options.captionId).innerHTML = cap;
	}
	
}

Object.extend(repperGallery, repperGallery.Methods);

//document.observe('dom:loaded', repperGallery.init()); 