window.addEvent('domready', function() {
	$$('.gallery td').each( function(td, i) {
		var link = td.getElement('a');
		var img = td.getElement('img');
		if (link) {
			link.addEvent('click', function(e) {
				var event = new Event(e).stop();

				if(this.hasClass('pdf')) {
					var i = window.open(link.href, 'pdf');
				}
				else if(this.hasClass('nohires')) {
					// do nothing
				}
				else if(this.hasClass('linkto')) {
					// do nothing
					var i = window.open(link.href, 'linkto');
				}
				else {
					var newhtml = '<img src="' + link.href + '" alt="" />';
					showHTMLPopup(newhtml);
				}
			});
		}
	});
});

function showHTMLPopup(html) {
	var popupcontainer = $('popupcontainer');
	if(!popupcontainer) {
		popupcontainer = new Element('div').inject(document.body, 'bottom');
		popupcontainer.id = 'popupcontainer';
	}

	popupcontainer.setStyles({
		position: 'fixed',
		top: '0',
		left: '0',
		right: '0',
		bottom: '0',
		background: 'black',
		opacity: '0',
		zIndex: '100000'
	});

	if(navigator.userAgent.indexOf('MSIE 6') > -1) {
		popupcontainer.setStyles({
			position: 'absolute',
			width: document.getWidth(),
			height: document.getHeight(),
			top: $(document.body).getScroll().y
		});
	}

	popupcontainer.html = html;
	popupcontainer.html += '<a class="close" href="javascript:closeHTMLPopup()"><span>X</span></a>';
	popupcontainer.innerHTML = "";
	var fx = new Fx.Morph(popupcontainer, {
		duration: 500,
		wait: false,
		onComplete: function() {
			var popupcontent = $('popupcontent');
			if(!popupcontent) {
				popupcontent = new Element('div').inject(document.body, 'bottom');
				popupcontent.id = 'popupcontent';
			}
			popupcontent.setStyles({
				border: '2px solid gray',
				background: 'white',
				position: 'fixed',
				top: '50%',
				left: '50%',
				width: 300,
				marginLeft: -150,
				height: 300,
				marginTop: -150,
				padding: 20,
				zIndex: popupcontainer.getStyle('zIndex')+1,
				opacity: 0,
				visibility: 'hidden',
				overflow: 'hidden',
				textAlign: 'center'
			});

			popupcontent.innerHTML = popupcontainer.html;
			var img = popupcontent.getElement('img');
			if(img) {
				img.setStyles({
					opacity: 0
				});
			}
			var close = popupcontent.getElement('.close');
			if(close) {
				close.addEvent('mouseover', function() {
					this.setStyle('opacity', 0.5);
				});
				close.addEvent('mouseout', function() {
					this.setStyle('opacity', 1);
				});
			}
			popupcontent.origHeight = popupcontent.getHeight(); //save for later
			if(popupcontent.getHeight() > document.body.offsetHeight) {
				popupcontent.setStyles({
					overflow: 'auto',
					height: (document.body.offsetHeight - popupcontent.getStyle('padding').toInt() - 40)
				});
			}
			popupcontent.setStyles({
				visibility: 'visible'
			});
			var aphex = new Fx.Morph(popupcontent, {
				duration: 500,
				wait: false,
				onComplete: function() {
					popupcontainer.addEvent('click', function() {
						closeHTMLPopup();
					});
					var img = popupcontent.getElement('img');
					if(img) {
						var popup_content_height = img.offsetHeight;
						if (img.offsetHeight > (document.body.offsetHeight - (2 * popupcontent.getStyle('padding').toInt()) - 20)) {popup_content_height = document.body.offsetHeight - (2 * popupcontent.getStyle('padding').toInt()) - 20;}

						if(navigator.userAgent.indexOf('MSIE 6') > -1) {
							var confx = new Fx.Morph(popupcontent, {
								duration: 500,
								wait: false
							}).start({
								width: img.offsetWidth + popupcontent.getStyle('padding').toInt(),
								height: popup_content_height,
								top: (document.body.offsetHeight/2) - (popup_content_height/2),
								marginTop: 0,
								marginLeft: 0 - Math.round((img.offsetWidth+popupcontent.getStyle('padding').toInt())/2)
							});

						} else {
							var confx = new Fx.Morph(popupcontent, {
								duration: 500,
								wait: false
							}).start({
								width: img.offsetWidth + popupcontent.getStyle('padding').toInt(),
								height: popup_content_height,
								marginTop: 0 - Math.round(popup_content_height/2) -20, 
								marginLeft: 0 - Math.round((img.offsetWidth+popupcontent.getStyle('padding').toInt())/2)
							});

						}
						if (img.offsetHeight > popup_content_height) {popupcontent.style.overflow = 'auto';}
						img.setStyles({
							position: 'absolute',
//							top: '50%',
							left: '50%',
//							marginTop: 0-Math.round(img.offsetHeight/2),
							marginLeft: 0-Math.round(img.offsetWidth/2)
						});
						var imgfx = new Fx.Morph(img, {
							duration: 500,
							wait: false
						}).start({
							opacity: 1
						});
					}
				}
			}).start({
				opacity: 1
			});

			if(navigator.userAgent.indexOf('MSIE 6') > -1) {
				popupcontent.setStyles({
					position: 'absolute',
					marginTop: 0,
					top: $(document.body).getScroll().y + (document.body.offsetHeight / 2)  - (popupcontent.getHeight() / 2)
				});
			}
		}
	}).start({
		opacity: '0.4'
	});
}

function closeHTMLPopup() {
	[$('popupcontainer'), $('popupcontent')].each(function(el, i) {
		if(el) {
			el.removeEvents('click');
			var fx = new Fx.Morph(el, {
				duration: 500,
				wait: false,
				onComplete: function() {
					if($('popupcontent'))
						$('popupcontent').destroy();
				}
			}).start({
				opacity: 0
			});
		}
	});
}

function resizeHTMLPopup() {
	var popupcontent = $('popupcontent');
	var popupcontainer = $('popupcontainer');
	if(popupcontent) {
		if(popupcontent.getHeight() > document.body.offsetHeight) {
			popupcontent.setStyles({
				overflow: 'auto',
				height: (document.body.offsetHeight - popupcontent.getStyle('padding').toInt() - 40)
			});
		}
		else {
			popupcontent.setStyles({
				height: popupcontent.origHeight - 40
			});
		}
		popupcontent.setStyles({
			marginTop: 0-Math.round(popupcontent.getHeight()/2)
		});

		if(navigator.userAgent.indexOf('MSIE 6') > -1) {
			popupcontainer.setStyles({
				top: $(document.body).getScroll().y,
				width: document.body.getWidth(),
				height: document.body.getHeight()
			});
			popupcontent.setStyles({
				top: $(document.body).getScroll().y + (document.body.offsetHeight / 2)  - (popupcontent.getHeight() / 2),
				marginTop: 0
			});
		}
	}
}
