$(document).ready(function() {
	
	$.include('jquery/jquery.blockUI.js', function() {
		
		// calculate left margin for layer positioning
		var getLayerMargin = function(doWidth) {
			var scrollbarOffset = 20;
						
			var marginLeft = parseInt(($(window).width() - doWidth) / 2);
			
			if ($(document).height() > $(window).height() && ((marginLeft - scrollbarOffset) >= 0)) {
				marginLeft = marginLeft - scrollbarOffset;
			}
			
			return marginLeft;
		}

		
		
			
		$.extend({
			
			resizeLayer: function(contentHeight, doAnimate) {
				
				var minHeight = 100;
				var maxHeight = $(window).height() - 150;
				var setHeight = contentHeight + 50;
				
				// Mindesthoehe
				if (setHeight < minHeight) setHeight = minHeight;
				// Maximalhoehe
				if (setHeight > maxHeight) setHeight = maxHeight;
				
				if (doAnimate == true) {
					$(".blockPage").animate({"height": setHeight}, {duration: "slow"});
				} else {
					$(".blockPage").height(setHeight);
				}
				
				$(".blockPage iframe").height(setHeight - 50);
				
				// update margin left on window resize
				$('.blockPage').css('left', getLayerMargin($('body').attr('data-layerContentWidth')));
			},
			
			loadContent: function(url, forceHeight, forceWidth, handleSameWindow) {
				
				var iframe = $('<iframe frameborder="no" scrolling1="auto" target="_blank"></iframe>');
				
				if (url != undefined) {
					iframe.attr("src", url);
				}
				
				iframe.css("width", "100%").css("border", "1px dotted #C0C0C0")
					.css("position", "absolute").css("left", "-9999px");
				
				var doWidth = forceWidth != null ? forceWidth : 760;
				// save layer width in body tag
				$('body').attr('data-layerContentWidth', doWidth);
				
				$.blockUI({
		            message: iframe,
		            fadeIn: 50,
		            fadeOut: 700,
		            centerY: false,
		            allowBodyStretch: true,
		            css: {
						left: getLayerMargin(doWidth) + 'px', top: '100px', width: doWidth + 'px', height: '20px',
		                border: 'none', padding: '5px 15px 0 15px',
		                backgroundColor: '#EFEFEF',
		                '-webkit-border-radius': '10px',
		                '-moz-border-radius': '10px',
		                color: '#FFF'
		            }
				});
	
				iframe.load(function() {
	
					var iframeContent = iframe.contents();
					var content = iframeContent.find("#col1_content");
					var contentHeight = content.height() + 35;
					
					var contentTitle = content.find("h1:first");
					var topLayer = $("<div></div>").css("cursor", "auto").css("height", "35px");
					
					if (contentTitle.html()) contentTitle.remove();
					iframeContent.find("html").height("0");
					iframeContent.find("body").addClass("layerContent");
					
					/*
					iframeContent.find("body").empty().removeClass("zweispaltig").addClass("einspaltig").css("background-color", "#FFFFFF");
					content.appendTo(iframeContent.find("body"));
					*/
					
					if (handleSameWindow != true) iframeContent.find("a, form").attr("target", "_top");
					$(this).unbind();
					
					$('<a href="#" class="closeButton">[x]</a>').css("float", "right").css("padding", "5px").appendTo(topLayer);
					if (contentTitle.html()) $("<h1>" + contentTitle.html() + "</h1>").css("padding-top", "5px").appendTo(topLayer);
					topLayer.insertBefore(iframe);
					
					iframe.css("position", "static").css("left", "0");
					
					if ($.browser.msie && $.browser.version == 7) iframeContent.find("body").css("min-height", "1%");
					
					var doHeight = (forceHeight != null) ? forceHeight : contentHeight;
					
					if (!($.browser.msie && $.browser.version == 7)) $(window).resize(function() {$.resizeLayer(doHeight, false)});
					$.resizeLayer(doHeight, true);
					
					$('.blockOverlay, .closeButton').click(function() {
						$.unblockUI({onUnblock: function() {$('.blockUI').remove()}}); /* fix for IE */
						iframe = null;
						return false;
					});
					
				});
				
				return iframe;
			
			}
		
		});
		
		// Trigger
		$("a.layerContent").click(function() {
			
			if ($.browser.msie && $.browser.version < 7) {
				$(this).attr("target", "_blank");
				return true;
			}
			
			$.loadContent($(this).attr("href"));
			return false;
		});
	
	});
	
});
