$(function() {	
	$('.site-content').each(function() {
		var siteContent = $(this);
		var width = 500;

		siteContent.find('img, table').each(function() {
			var content = $(this);
			content.width('auto');
			var contentWidth = content.width(); 
			if (contentWidth > width) {
				var title = null;
				if (content.is('table')) {
					contentWidth += 200;
					title = content.find('caption').text();
				} else {
					title = content.attr('title') ? content.attr('title') : content.attr('alt');
				}
				var contentHeight = content.height();
				
				var newContent = content.clone();
				var wrapper = content.after('<div class="content-scale-down"></div>').next();
				wrapper.append(newContent);
				var magnifier = wrapper.append('<div class="magnifier"></div>').children(':last');
				
 				var scalePercent = Math.floor((width / contentWidth) * 100);
				
 				newContent.effect('scale', {
					percent: scalePercent,
					duration: 0
				}).css({
					opacity: 0.5
				});
 				wrapper.width(newContent.width());
				
				content.css('margin', '0 30px').hide();
				
				wrapper.hover(function () {
					$(this).addClass('content-scale-down-hover');
				}, function () {
					$(this).removeClass('content-scale-down-hover');
				});
				
				magnifier.click(function() {
					content.dialog({
						title: title,
						width: contentWidth + 60,
						height: contentHeight,
						modal: true
					});	
				});
			}
		});
	});
});