(function ($) {
	$.fn.hasScrollBar = function () {
		return this.get(0).scrollHeight > this.innerHeight();
	}
})(jQuery);
$(function () {
	$("div.simple_overlay").wrapInner('<div class="overlayContainer"/>').appendTo('body');
	var wrap,
	overlay,
	img,
	maxHeight = 50,
	scroolBarPadd = 0;
	$(".jquery_overlay[rel]").overlay({ top: 'center', mask: { color: '#000000', opacity: 0.7 },
		onBeforeLoad: function () {
			wrap = this.getOverlay().find(".overlayContainer");
			overlay = this.getOverlay();
			if (this.getTrigger().attr("href") != '#' && this.getTrigger().attr("href") != '../../../#') {
				wrap.load(this.getTrigger().attr("href"));
				wrap.css("min-height", 400);
			}
			wrap.data("initialWidth", overlay.width());
			wrap.css("width", (overlay.width() + scroolBarPadd));
			if (($(window).height() - 100) > 50) { maxHeight = ($(window).height() - 100) }
			wrap.css("max-height", maxHeight);
			wrap.css("max-height", ($(window).height() - 100));
			overlay.css("width", "auto");
		},
		onLoad: function () {
			var defaultWidth = wrap.width();
			var windowWidth = $(window).width();
			img = this.getOverlay().find(".overlayContainer img");
			img.data("initialWidth", img.width())
			if (img.width() > defaultWidth) { img.css('width', '100%'); }
			if (windowWidth - 20 < defaultWidth) {
				wrap.css("max-width", windowWidth - 20);
				var left = (((windowWidth - wrap.width()) / 2) - 12);
				overlay.css("left", left);
				if (img.width() > windowWidth - 50) { img.css('width', wrap.width() - scroolBarPadd); }
				else { img.css('width', img.data("initialWidth")) }
			}
			else { wrap.css("max-width", defaultWidth); }
			$(window).resize(function resize() {
				windowWidth = $(window).width();
				if (($(window).height() - 100) > 50) { maxHeight = ($(window).height() - 100) }
				wrap.css("max-height", maxHeight);
				if (windowWidth - 20 < wrap.data("initialWidth") + scroolBarPadd) { wrap.css("max-width", windowWidth - 20) }
				else { wrap.css("max-width", wrap.data("initialWidth") + scroolBarPadd) }
				if (img.width() > windowWidth - 50) { img.css('width', wrap.width() - scroolBarPadd); }
				else { img.css('width', img.data("initialWidth")) }
				var left = (((windowWidth - wrap.width()) / 2) - 12);
				overlay.css("left", left);
			});
			$("#exposeMask").css("position", "fixed")
			this.getOverlay().find(".close").show();
		},
		onBeforeClose: function () {
			this.getOverlay().find(".close").hide()
		},
		onClose: function () {
			overlay.css("width", wrap.data("initialWidth"))
			img.css("width", img.data("initialWidth"))
		}
	});
});
