DD.ImageGallery = function(node, e) {
	var self = this;
	this.pathReplacementRegex = new RegExp(/\/h\d\d\d?/);
	this.create = function(node, e) {
		DD.event.prevent(e);
		this.gallery = document.createElement('div');
		if (this.gallery.addEventListener) {
			this.gallery.addEventListener('DOMMouseScroll', this.chooseSibling, false); // Mozilla mousewheel
		}
		this.gallery.onmousewheel = this.chooseSibling; // Opera and IE mousewheel
		this.gallery.className = 'DD_ImageGallery';
		DD.modal.create(this.gallery);
		this.modal = DD.modal.get();
		this.max = {width:0, height:0};
		this.thumbs = DD.dom.ancestor(node, {nodeName: 'UL'}).getElementsByTagName('img');
		this.thumbList = document.createElement('div');
		this.gallery.appendChild(this.thumbList);
		this.thumbList.className = 'thumbs';
		this.dummy = document.createElement('img');
		this.caption = document.createElement('div');
		this.caption.className = 'caption';
		this.captionLeftCap = document.createElement('span');
		this.captionLeftCap.innerHTML = "•";
		this.caption.appendChild(this.captionLeftCap);
		this.captionContent = document.createElement('span');
		this.caption.appendChild(this.captionContent);
		this.captionRightCap = document.createElement('span');
		this.captionRightCap.innerHTML = "•";
		this.caption.appendChild(this.captionRightCap);
		this.pictureFrame = document.createElement('div');
		this.pictureFrame.className = 'pictureFrame';
		this.pictureFrame.innerHTML = '<span>.</span>';
		this.bigPicture = document.createElement('img');
		this.bigPicture.className = 'bigPicture';
		this.pictureFrame.appendChild(this.bigPicture);
		var moreSpan = document.createElement('span');
		moreSpan.innerHTML = '.';
		this.pictureFrame.appendChild(moreSpan);
		this.pictureFrame.appendChild(this.caption);
		this.gallery.appendChild(this.pictureFrame);
		DD.event.add(this.dummy, 'load', this.showImage);
		this.cloneAndReduceThumbnail = function(image) {
			var newImg = image.cloneNode(true);
			newImg.src = image.src.replace(this.pathReplacementRegex, '/h75');
			newImg.id = 'THUMBNAIL_' + newImg.src.substring(newImg.src.lastIndexOf('/'));
			newImg.width = newImg.getAttribute('maxwidth') * (75 / newImg.getAttribute('maxheight'));
			newImg.height = 75;
			this.thumbList.appendChild(newImg);
		};
		this.pickTheLargest = function(a, b) {
			return (a > b)? a : b;
		};
		var thumbLen = this.thumbs.length;
		for (var n=0; n<thumbLen; n++) {
			this.max.width = this.pickTheLargest(this.thumbs[n].getAttribute('maxwidth'), this.max.width);
			this.max.height = this.pickTheLargest(this.thumbs[n].getAttribute('maxheight'), this.max.height);
			this.cloneAndReduceThumbnail(this.thumbs[n]);
		}
		this.max.width = parseInt(this.max.width);
		this.max.height = parseInt(this.max.height);
		this.modal.style.width = this.max.width + 12 + 'px';
		this.modal.style.height = this.max.height + 75 + 17+ 'px';
		this.pictureFrame.style.height = this.max.height + 10 + 'px';
		this.pictureFrame.style.lineHeight = this.max.height + 10 + 'px';
		DD.modal.position();
		this.choose(node, e);
	};
	this.choose = function(node, e) {
		node.nodeName == 'A'? node.blur() : null; /* Konqueror didn't like trying to blur an image element */
		DD.event.prevent(e);
		self.selectImage(node);
	};
	this.chooseSibling = function(e) {
		e = e? e : window.event;
		if (DD.modal.get()) {
			DD.event.prevent(e);
		}
		var sibling = (e.keyCode? ((e.keyCode==34 || e.keyCode==39 || e.keyCode==40 || (e.keyCode==9 && !DD.key.depressed[16]))? 'next' : 'previous') : ((!DD.event.mouseWheel(e))? 'next' : 'previous')) + 'Sibling';
		if (e.keyCode==33 || e.keyCode ==34) {
			var start = self.shown;
			for (var i=0; i<5; i++) {
				start = start[sibling] || start;
			}
			self.selectImage(start);
		}
		else if ((self.shown && !self.shown[sibling]) || e.keyCode==35 || e.keyCode ==36) {
			self.selectImage(self.thumbList.childNodes[(sibling=='nextSibling' || e.keyCode==36)? 0 : self.thumbList.childNodes.length-1], 1000, 'quadInOut');
		}
		else if (self.shown) {
			self.selectImage(self.shown[sibling]);
		}
	};
	this.determineThumbnail = function(node) {
		if (node) {
			var src = node.src || node.getElementsByTagName('img')[0].src;
			return DD.dom.id('THUMBNAIL_' + src.substring(src.lastIndexOf('/')));
		}
	};
	this.requestedAlready = {};
	this.leftOffsetKnownAlready = {};
	this.selectImage = function(node, time, ease) {
		if (node) {
			var thumb = this.determineThumbnail(node);
			var bigSrc = thumb.src.replace(this.pathReplacementRegex, '');
			this.dummy.src = bigSrc;
			this.bigPicture.style.visibility = (this.requestedAlready[bigSrc])? 'visible' : 'hidden';
			window.location.hash = this.dummy.src.substring(this.dummy.src.lastIndexOf('/')+1);
			if (this.shown) {
				this.shown.className = '';
			}
			DD.animate.stop(this.captionContent);
			var thisCaption = this.captionContent.innerHTML;
			var newCaption = thumb.title;
			DD.animate.go(this.captionContent, 300, 10, true, function(obj, tween) {
				tween = DD.animate.ease('quadInOut', tween);
				if (tween < .5) {
					obj.innerHTML = thisCaption.substring(0, thisCaption.length-(tween*2*thisCaption.length));
				}
				else {
					obj.innerHTML = newCaption.substring(0, newCaption.length*(tween-.5)*2);
				}
			});
			this.shown = thumb;
			this.shown.className = 'active';
			var nodeLeft = this.leftOffsetKnownAlready[this.dummy.src] || thumb.offsetLeft;
			var startLeft = this.thumbList.offsetLeft;
			var finishLeft = -nodeLeft-(node.offsetWidth/2)+(this.gallery.offsetWidth/2);
			time = time || 400;
			ease = ease || 'quadOut';
			DD.animate.stop(self.thumbList);
			DD.animate.go(self.thumbList, time, 20, true, function(obj, tween) {
				tween = DD.animate.ease(ease, tween);
				self.thumbList.style.left = startLeft-(startLeft-finishLeft)*tween + 'px';
			});
		}
	};
	this.showImage = function() {
		self.bigPicture.style.visibility = 'visible';
		self.bigPicture.src = this.src;
		if (!self.requestedAlready[this.src]) {
			self.requestedAlready[this.src] = true;
			self.leftOffsetKnownAlready[this.src] = this.offsetLeft;
		}
	};
	return this;
};

var gallery = new DD.ImageGallery;
DD.event.delegate('DIV#doc UL.thumbnails LI A', 'click', function(node, e) {
	gallery.create(node, e);
});
DD.event.delegate('DIV.DD_ImageGallery .thumbs IMG', 'click', gallery.choose);

DD.key.action = {
	9: gallery.chooseSibling, // Tab
	27: DD.modal.destroy, // Escape
	33: gallery.chooseSibling, // Page Up
	34: gallery.chooseSibling, // Page Down
	35: gallery.chooseSibling, // End
	36: gallery.chooseSibling, // Home
	37: gallery.chooseSibling, // Left
	38: gallery.chooseSibling, // Up
	39: gallery.chooseSibling, // Right
	40: gallery.chooseSibling // Down
};

DD.event.delegate('#wrap', 'mouseup', function(node, e) {
	DD.modal.destroy();
});
DD.event.add(window, 'scroll', DD.modal.destroy);

DD.event.onReady('content', function() {
	var hash = window.location.hash.substring(1);
	if (hash != '') { /* Stuff was triggering on its own. Silly. */
		var images = DD.dom.id('content').getElementsByTagName('img');
		var imagesLen = images.length;
		for (var i=0; i<imagesLen; i++) {
			if (images[i].src.indexOf(hash) != -1) {
				setTimeout(function() {
					gallery.create(images[i]);
				}, 1);
				return;
			}
		}
	}
});