LightboxOptions = Object.extend({
	overlayOpacity : 0.8,
	animate : true,
	resizeSpeed : 400,
	borderSize : 10,
	loop : false,
	allowSave : false,
	slideshowAutoplay : false,
	slideshowInterval : 3000,
	slideshowAutoclose : true,
	labelImage : "Image",
	labelOf : "of"
}, window.LightboxOptions || {});
var Lightbox = Class.create();
Lightbox.prototype = {
	imageArray : [],
	activeImage : undefined,
	initialize : function() {
		var g, c, f, e, a, d;
		this.updateImageList();
		this.keyboardAction = this.keyboardAction.bindAsEventListener(this);
		if (LightboxOptions.animate) {
			this.overlayDuration = LightboxOptions.resizeSpeed / 1000;
			this.resizeDuration = LightboxOptions.resizeSpeed / 1000
		} else {
			this.overlayDuration = 0;
			this.resizeDuration = 0
		}
		var h = (LightboxOptions.animate ? 250 : 1) + "px";
		var j = $$("body")[0];
		j.appendChild(Builder.node("div", {
			id : "overlay"
		}));
		j.appendChild(Builder.node("div", {
			id : "lbLightbox"
		}, [ Builder.node("div", {
			id : "lbOuterImageContainer"
		}, Builder.node("div", {
			id : "lbImageContainer"
		}, [ Builder.node("img", {
			id : "lbLightboxImage"
		}), Builder.node("div", {
			id : "lbHoverNav"
		}, [ Builder.node("a", {
			id : "prevLink",
			href : "#"
		}), Builder.node("a", {
			id : "nextLink",
			href : "#"
		}) ]), Builder.node("div", {
			id : "lbLoading"
		}, Builder.node("a", {
			id : "loadingLink",
			href : "#"
		})) ])), Builder.node("div", {
			id : "lbImageDataContainer"
		}, Builder.node("div", {
			id : "lbImageData"
		}, [ Builder.node("div", {
			id : "lbImageDetails"
		}, [ Builder.node("span", {
			id : "lbCaption"
		}), Builder.node("span", {
			id : "lbNumberDisplay"
		}), Builder.node("span", {
			id : "lbPresent"
		}) ]), Builder.node("div", {
			id : "lbBottomNav"
		}, [ Builder.node("a", {
			id : "closeLink",
			href : "#"
		}), Builder.node("a", {
			id : "playLink",
			href : "#"
		}), Builder.node("a", {
			id : "pauseLink",
			href : "#"
		}), Builder.node("a", {
			id : "saveLink",
			href : "#",
			target : "_blank"
		}) ]) ])) ]));
		Event.observe(Element.hide('overlay'), "click", (function() {
			this.end()
		}).bind(this));
		Event.observe(Element.hide('lbLightbox'),"click", (function(k) {
			if (k.element().id == "lbLightbox") {
				this.end()
			}			
		}).bind(this));
		$("lbOuterImageContainer").style.width = h;
		$("lbOuterImageContainer").style.height = h;	
		Event.observe('prevLink',"click", (function(k) {
			k.stop();
			this.previous()	
		}).bindAsEventListener(this));	
		Event.observe('nextLink',"click", (function(k) {
			k.stop();
			this.next()	
		}).bindAsEventListener(this));			
		Event.observe('loadingLink',"click", (function(k) {
			k.stop();
			this.next()	
		}).bind(this));	
		Event.observe('playLink',"click", (function(k) {
			k.stop();
			this.play()
		}).bindAsEventListener(this));
		Event.observe('pauseLink',"click", (function(k) {
			k.stop();
			this.pause()
		}).bindAsEventListener(this));		
		Event.observe('closeLink',"click", (function(k) {
			k.stop();
			this.end()
		}).bind(this));		
		var b = this;
		(function() {
			var k = "overlay lbLightbox lbOuterImageContainer lbImageContainer lbLightboxImage lbHoverNav prevLink nextLink lbLoading loadingLink lbImageDataContainer lbImageData lbImageDetails lbCaption lbNumberDisplay lbPresent lbBottomNav playLink pauseLink saveLink closeLink";
			$w(k).each(function(l) {
				b[l] = $(l)
			})
		}).defer()
	},
	updateImageList : function() {
		this.updateImageList = Prototype.emptyFunction;
		document.observe("click", (function(a) {
			var b = a.findElement("a[rel^=lightbox]")
					|| a.findElement("area[rel^=lightbox]");
			if (b) {
				b.rel.match(/present.+/) ? this.presentationMode = true
						: this.presentationMode = false;
				b.rel.match(/slideshow.+/) ? this.slideshowMode = true
						: this.slideshowMode = false;
				this.slideshowActive = LightboxOptions.slideshowAutoplay
						&& this.slideshowMode;
				a.stop();
				this.start(b)
			}
		}).bind(this))
	},
	start : function(f) {
		$$("select", "object", "embed").each(function(g) {
			g.style.visibility = "hidden"
		});
		var c = this.getPageSize();
		$("overlay").setStyle({
			width : c[0] + "px",
			height : c[1] + "px"
		});
		new Effect.Appear(this.overlay, {
			duration : this.overlayDuration,
			from : 0,
			to : LightboxOptions.overlayOpacity
		});
		this.imageArray = [];
		var b = 0;
		if ((f.rel == "lightbox")) {
			this.imageArray.push([ f.href, f.title ])
		} else {
			this.imageArray = $$(f.tagName + '[href][rel="' + f.rel + '"]')
					.collect(function(g) {
						return [ g.href, g.title ]
					}).uniq();
			while (this.imageArray[b][0] != f.href) {
				b++
			}
		}
		var a = document.viewport.getScrollOffsets();
		var e = a[1] + (document.viewport.getHeight() / 10);
		var d = a[0];
		this.lbLightbox.setStyle({
			top : e + "px",
			left : d + "px"
		}).show();
		this.changeImage(b)
	},
	previous : function() {
		this.changeImage(this.prevImage)
	},
	next : function() {
		this.changeImage(this.nextImage)
	},
	changeImage : function(b) {
		this.activeImage = b;
		LightboxOptions.loop = LightboxOptions.loop
				&& (this.imageArray.length > 1);
		this.prevImage = (this.activeImage || (LightboxOptions.loop ? this.imageArray.length
				: 0)) - 1;
		this.nextImage = ((this.activeImage + 1) % this.imageArray.length)
				|| (LightboxOptions.loop ? 0 : -1);
		if (LightboxOptions.animate) {
			this.lbLoading.show()
		}
		this.lbLightboxImage.hide();
		this.lbHoverNav.hide();
		this.prevLink.hide();
		this.nextLink.hide();
		this.lbImageDataContainer.setStyle({
			opacity : 0.0001
		});
		this.lbImageDataContainer.hide();
		this.lbNumberDisplay.hide();
		var a = new Image();
		a.onload = (function() {
			this.lbLightboxImage.src = this.imageArray[this.activeImage][0];
			this.lbLightboxImage.width = a.width;
			this.lbLightboxImage.height = a.height;
			this.resizeImageContainer(a.width, a.height)
		}).bind(this);
		a.src = this.imageArray[this.activeImage][0];
		if (LightboxOptions.allowSave) {
			this.saveLink.setAttribute("href",
					this.imageArray[this.activeImage][0]);
			this.saveLink.setStyle({
				display : ""
			})
		} else {
			this.saveLink.setStyle({
				display : "none"
			})
		}
	},
	resizeImageContainer : function(e, f) {
		var h = this.lbOuterImageContainer.getWidth();
		var c = this.lbOuterImageContainer.getHeight();
		var g = (e + LightboxOptions.borderSize * 2);
		var k = (f + LightboxOptions.borderSize * 2);
		var l = (g / h) * 100;
		var b = (k / c) * 100;
		var j = h - g;
		var a = c - k;
		if (a != 0) {
			new Effect.Scale(this.lbOuterImageContainer, b, {
				scaleX : false,
				duration : this.resizeDuration,
				queue : "front"
			})
		}
		if (j != 0) {
			new Effect.Scale(this.lbOuterImageContainer, l, {
				scaleY : false,
				duration : this.resizeDuration,
				delay : this.resizeDuration
			})
		}
		var d = 0;
		if ((a == 0) && (j == 0)) {
			d = 100;
			if (Prototype.Browser.IE) {
				d = 250
			}
		}
		(function() {
			this.prevLink.setStyle({
				height : f + "px"
			});
			this.nextLink.setStyle({
				height : f + "px"
			});
			this.lbImageDataContainer.setStyle({
				width : g + "px"
			});
			this.showImage()
		}).bind(this).delay(d / 1000)
	},
	showImage : function() {
		this.lbLoading.hide();
		new Effect.Appear(this.lbLightboxImage, {
			duration : this.resizeDuration,
			queue : "end",
			afterFinish : (function() {
				this.updateDetails()
			}).bind(this)
		});
		this.preloadNeighborImages()
	},
	toInt : function(a) {
		return parseInt(a.replace(/\D/g, ""))
	},
	updateDetails : function() {
		this.lbNumberDisplay.update("");
		this.lbCaption.update("");
		if (this.imageArray[this.activeImage][1] != "") {
			this.lbCaption.update(this.imageArray[this.activeImage][1]).show()
		}
		if (this.imageArray.length > 1) {
			this.lbNumberDisplay.update(
					LightboxOptions.labelImage + " " + (this.activeImage + 1)
							+ " " + LightboxOptions.labelOf + "  "
							+ this.imageArray.length).show()
		}
		if (this.presentationMode) {
			this.lbNumberDisplay.setStyle({
				display : "none"
			});
			this.lbPresent.update("").setStyle({
				display : ""
			});
			for (i = 0; i < this.imageArray.length; i++) {
				var b = this.activeImage == i ? "act" : "no";
				var a = new Element("a", {
					id : "pmi" + (i + 1),
					"class" : b,
					href : "#"
				});
				i < 9 ? a.update("0" + (i + 1)) : a.update(i + 1);
				a.observe("click", (function(e) {
					e.stop();
					var d = Event.findElement(e, "a");
					var c = this.toInt(d.id) - 1;
					this.pause();
					this.changeImage(c)
				}).bindAsEventListener(this));
				this.lbPresent.insert({
					bottom : a
				})
			}
		} else {
			this.lbNumberDisplay.setStyle({
				display : ""
			});
			this.lbPresent.setStyle({
				display : "none"
			})
		}
		this.playLink.setStyle({
			display : "none"
		});
		this.pauseLink.setStyle({
			display : "none"
		});
		if (this.slideshowMode) {
			clearTimeout(this.slideshowTimer);
			if (this.slideshowActive) {
				this.pauseLink.setStyle({
					display : ""
				});
				if (this.activeImage != (this.imageArray.length - 1)) {
					this.slideshowTimer = setTimeout(this.next.bind(this),
							LightboxOptions.slideshowInterval)
				} else {
					if (LightboxOptions.slideshowAutoclose) {
						this.slideshowTimer = setTimeout(this.end.bind(this),
								LightboxOptions.slideshowInterval)
					} else {
						if (LightboxOptions.loop) {
							this.slideshowTimer = setTimeout(this.changeImage
									.bind(this, 0),
									LightboxOptions.slideshowInterval)
						} else {
							this.pause()
						}
					}
				}
			} else {
				this.playLink.setStyle({
					display : ""
				})
			}
		}
		new Effect.Parallel([ new Effect.SlideDown(this.lbImageDataContainer, {
			sync : true,
			duration : this.resizeDuration,
			from : 0,
			to : 1
		}), new Effect.Appear(this.lbImageDataContainer, {
			sync : true,
			duration : this.resizeDuration
		}) ], {
			duration : this.resizeDuration,
			afterFinish : (function() {
				var c = this.getPageSize();
				this.overlay.setStyle({
					width : c[0] + "px",
					height : c[1] + "px"
				});
				this.updateNav()
			}).bind(this)
		})
	},
	updateNav : function() {
		this.lbHoverNav.show();
		if (this.prevImage >= 0) {
			this.prevLink.show()
		}
		if (this.nextImage >= 0) {
			this.nextLink.show()
		}
		this.enableKeyboardNav()
	},
	enableKeyboardNav : function() {
		document.observe("keydown", this.keyboardAction)
	},
	disableKeyboardNav : function() {
		document.stopObserving("keydown", this.keyboardAction)
	},
	keyboardAction : function(d) {
		var a = d.keyCode;
		var b;
		if (d.DOM_VK_ESCAPE) {
			b = d.DOM_VK_ESCAPE
		} else {
			b = 27
		}
		var c = String.fromCharCode(a).toLowerCase();
		if (c.match(/x|o|c/) || (a == b)) {
			this.end()
		} else {
			if ((c == "s") || (a == 83)) {
				this.togglePlayPause()
			} else {
				if ((c == "p") || (a == 37)) {
					if (this.activeImage != 0) {
						this.disableKeyboardNav();
						this.changeImage(this.activeImage - 1)
					}
				} else {
					if ((c == "n") || (a == 39)) {
						if (this.activeImage != (this.imageArray.length - 1)) {
							this.disableKeyboardNav();
							this.changeImage(this.activeImage + 1)
						}
					}
				}
			}
		}
	},
	preloadNeighborImages : function() {
		var a, b;
		if (this.imageArray.length > this.activeImage + 1) {
			a = new Image();
			a.src = this.imageArray[this.activeImage + 1][0]
		}
		if (this.activeImage > 0) {
			b = new Image();
			b.src = this.imageArray[this.activeImage - 1][0]
		}
	},
	togglePlayPause : function() {
		if (this.slideshowActive) {
			this.pause()
		} else {
			this.play()
		}
	},
	play : function() {
		this.slideshowActive = true;
		if (this.activeImage != (this.imageArray.length - 1)) {
			this.changeImage(this.activeImage + 1)
		} else {
			if (LightboxOptions.slideshowAutoclose && !LightboxOptions.loop) {
				this.slideshowTimer = setTimeout(this.end.bind(this), 0)
			} else {
				if (LightboxOptions.loop) {
					this.changeImage(0)
				} else {
				}
			}
		}
	},
	pause : function() {
		this.slideshowActive = false;
		clearTimeout(this.slideshowTimer);
		this.playLink.setStyle({
			display : ""
		});
		this.pauseLink.setStyle({
			display : "none"
		})
	},
	end : function() {
		this.pause();
		this.disableKeyboardNav();
		this.lbLightbox.hide();
		new Effect.Fade(this.overlay, {
			duration : this.overlayDuration
		});
		$$("select", "object", "embed").each(function(a) {
			a.style.visibility = "visible"
		})
	},
	getPageSize : function() {
		var c, a;
		if (window.innerHeight && window.scrollMaxY) {
			c = window.innerWidth + window.scrollMaxX;
			a = window.innerHeight + window.scrollMaxY
		} else {
			if (document.body.scrollHeight > document.body.offsetHeight) {
				c = document.body.scrollWidth;
				a = document.body.scrollHeight
			} else {
				c = document.body.offsetWidth;
				a = document.body.offsetHeight
			}
		}
		var b, d;
		if (self.innerHeight) {
			if (document.documentElement.clientWidth) {
				b = document.documentElement.clientWidth
			} else {
				b = self.innerWidth
			}
			d = self.innerHeight
		} else {
			if (document.documentElement
					&& document.documentElement.clientHeight) {
				b = document.documentElement.clientWidth;
				d = document.documentElement.clientHeight
			} else {
				if (document.body) {
					b = document.body.clientWidth;
					d = document.body.clientHeight
				}
			}
		}
		if (a < d) {
			pageHeight = d
		} else {
			pageHeight = a
		}
		if (c < b) {
			pageWidth = c
		} else {
			pageWidth = b
		}
		return [ pageWidth, pageHeight ]
	}
};
document.observe("dom:loaded", function() {
	new Lightbox()
});

