var alignDelay;
var centerDelay;

function flickrAlign(){
	clearTimeout(alignDelay);
	$("tr.align-bottom").each(function(){
		var maxHeight = 0;
		$(this).find("img").each(function(){
			if($(this).height() == "0") {
				// dom's not really ready
				alignDelay = window.setTimeout(flickrAlign, 1000);
				return;
			} else if($(this).height() > maxHeight){
				maxHeight = $(this).height();
			}
		});
		$(this).find("img").each(function(){
			if($(this).height() == "0") {
				// dom's not really ready
				alignDelay = window.setTimeout(flickrAlign, 1000);
				return;
			} else if($(this).height() < maxHeight){
				var offset = parseInt(maxHeight - $(this).height()) + "px";
				$(this).parent("a").css("margin-top", offset);
			}
		});
	});
}

function flickrCenter() {
	clearTimeout(centerDelay);
	$("img.centerize").each(function() {
		if($(this).height() == "0") {
			// dom's not really ready
			centerDelay = window.setTimeout(flickrCenter, 1000);
			return;
		} else {
			var parentWidth = $(this).parent().width();
			var parentHeight = $(this).parent().height();
			if(parentWidth < $(this).width()) {
				var widthOffset = 0 - (($(this).width() - parentWidth) / 2);
				$(this).css("margin-left", widthOffset + "px");
			}
			if(parentHeight < $(this).width()) {
				var heightOffset = 0 - (($(this).height() - parentHeight) / 2);
				$(this).css("margin-top", heightOffset + "px");
			}
		}
	});
}

function externalLink(){
	$("a[rel='external']").click(function(){
		window.open(this.href);
		return false;
	});
}

$(document).ready(function() {
	externalLink()
	flickrAlign();
	flickrCenter();
});

