if (typeof(LightBox) == 'undefined') {
	var LightBox = new Class({
		Implements : [Options, Events],
		options : {
			source : '/images/icons/picture.png',
			overlayColour : 'black',
			overlayOpacity : 0.6,
			padding : 12
		},
		initialize : function(source, options){
			this.setOptions(options);
			this.source = source;
			this.isIE6 = Browser.Engine.trident && parseFloat(/MSIE ([0-9\.]+)/.exec(navigator.appVersion)[1]) < 7;
		},
		setFormSize : function(width, height, options) {
			new Fx.Morph(this.form, {
				onComplete : function() {
					if (options.onComplete) options.onComplete();
				}
			}).start({
				width : [this.form.getStyle('width'), width],
				height : [this.form.getStyle('height'), height],
				marginLeft : [this.form.getStyle('marginLeft'), -width / 2],
				marginTop : [this.form.getStyle('marginTop'), -height / 2]
			});
		},
		show : function() {
			// Start by dimming the page.
			this.overlay = new Element('div', {
				styles : {
					position : this.isIE6 ? 'absolute' : 'fixed',
					left : 0, top : 0,
					width : '100%', height : '100%',
					backgroundColor : this.options.overlayColour,
					opacity : 0,
					zIndex : 10000 + 0,
					padding : 0,
					margin : 0
				}
			});
			this.overlay.inject(document.body, 'top');
			// Add the form.
			this.form = new Element('div', {
				styles : {
					position : this.isIE6 ? 'absolute' : 'fixed',
					left : '50%', top : '50%',
					marginLeft : '-50px', marginTop : '-50px',
					width : '100px', height : '100px',
					background : 'white url(/images/throbbers/medium.gif) no-repeat scroll center center',
					opacity : 0,
					zIndex : 10000 + 1
				}
			});
			this.form.inject(this.overlay, 'after');
			
			// IE 6 fixes.
			if (this.isIE6) {
				this.overlay.style.height = document.body.clientHeight + 'px';
				this.form.setStyle('top', Math.floor(document.documentElement.scrollTop + (document.documentElement.clientHeight / 2)) + 'px');
			}
			
			// Fade in the overlay and form.
			this.overlay.fade(this.options.overlayOpacity);
			this.form.fade(1.0);
			// Start loading the asset.
			var lightBox = this;
			this.content = new Asset.image(this.source, {
				onload : function() {
					var contentWidth = lightBox.content.width;
					var contentHeight = lightBox.content.height;
					var maxSize = lightBox.overlay.getCoordinates();
					if (contentHeight > (maxSize.height - lightBox.options.padding * 4)) {
						contentHeight = maxSize.height - lightBox.options.padding * 4;
						contentWidth = contentHeight * (lightBox.content.width / lightBox.content.height);
					}
					if (contentWidth > (maxSize.width - lightBox.options.padding * 4)) {
						contentWidth = maxSize.width - lightBox.options.padding * 4;
						contentHeight = contentWidth * (lightBox.content.height / lightBox.content.width);
					}
					lightBox.setFormSize(
						contentWidth + lightBox.options.padding * 2,
						contentHeight + lightBox.options.padding * 2, {
							onComplete : function() {
								new Element('img', {
									src : lightBox.content.src,
									styles : {
										opacity : 0,
										margin : lightBox.options.padding + 'px',
										width : contentWidth + 'px',
										height : contentHeight + 'px'
									},
									events : {
										click : function() { lightBox.hide(); }
									}
								}).inject(lightBox.form).fade('in');
								var closeButton = new Element('div', {
									text : '\u00D7',
									styles : {
										width : '40px',
										height : '40px',
										position : 'absolute',
										right : 0, bottom : 0,
										textAlign : 'center',
										backgroundColor : 'white',
										fontSize : '32px',
										fontWeight : 'bold',
										opacity : 0,
										cursor : 'pointer'
									},
									events : {
										click : function() { lightBox.hide(); }
									}
								}).inject(lightBox.form);
								lightBox.form.addEvent('mouseenter', function() { closeButton.fade('in'); });
								lightBox.form.addEvent('mouseleave', function() { closeButton.fade('out'); });
							}
						}
					);
				}
			});
		},
		hide : function() {
			var lightBox = this;
			if (this.overlay) {
				new Fx.Tween(this.overlay, {
					property : 'opacity',
					onComplete : function() {
						try {
							lightBox.overlay.dispose();
							lightBox.overlay = null;
						} catch (ex) { }
					}
				}).start(0);
			}
			if (this.form) {
				new Fx.Tween(this.form, {
					property : 'opacity',
					onComplete : function() {
						try {
							lightBox.form.dispose();
							lightBox.form = null;
						} catch (ex) { }
					}
				}).start(0);
			}
		}
	});
	window.addEvent('domready', function() {
		$$('a.lightbox').each(function(lightBoxLink) {
			lightBoxLink.addEvent('click', function(e) {
				e.stop();
				var lightBox = new LightBox(lightBoxLink.href);
				lightBox.show();
			});
		});
	});
}
