﻿var baseDivName = "#divMiddle";

function showCoverDiv(type, source, width, height) 
{
	
    if ($(baseDivName).length === 0) {
        alert(baseDivName + " nicht gefunden! Overlay kann nicht angezeigt werden!");
        return;
    }
	
	attachCoverDiv();

	resizeCoverDiv();
	var globalDiv = $('body');

	window.onresize = function()
	{
	    resizeCoverDiv();
		resizeContent( content, width, height);
	}
	
	var content;

	if( type.toLowerCase() == 'frame')
	{
		content =  createIFrame(width, height, source);
		globalDiv.append($(content));
	}
	else if( type.toLowerCase() == 'image')
	{
		content = createImage( source, width, height);
		globalDiv.append(content);
		return;
    }
    else if (type.toLowerCase() == 'div') 
	{
	    content = createFromDiv(source, width, height);
	    globalDiv.append(content);
	    return;
	}
	else if( type.toLowerCase() == 'googlemap')
	{
		content = createGoogleMap( source, width, height);
		globalDiv.append(content);
		initializeGMaps('Popup42GoogleMap');
		return;
	}
	else if( type.toLowerCase() == 'html')
	{
//		content = createHtml( width, height, source);
//		globalDiv.append(content);

//		$.ajax(
//			{
//				url: source,
//				type: 'POST',
//				dataType: 'html',
//				cache: false,
//				success: function(data, textStatus) { alert(data); },
//				error: function(XMLHttpRequest, textStatus, errorThrown) { alert('ERROR'); }
//			}
//		);

//		new Ajax.Updater('Popup42HtmlDiv', source, 
//			{ 
//				evalScripts: true,
//				evalJS: true,
//				onComplete: function(e)
//				{
//					d$("Popup42ContentDiv").setStyle(
//					{
//						display: 'block'
//					});
//	
//					$("Popup42HtmlDiv").setStyle(
//					{
//						display: 'block'
//					});
//	
//					resizeContent( $("Popup42ContentDiv"), width, height);
//				},
//				onFailure: function(e)
//				{
//					$("Popup42ContentDiv").setStyle(
//					{
//						display: 'block'
//					});
//					
//					resizeContent( $("Popup42ContentDiv"), width, height);
//				}
//			});
		return;
	}

	resizeContent(content, width, height);

	return true;
}

function attachCoverDiv()
{
	var newCoverDiv = $('<div>');
	newCoverDiv.attr('id', 'Popup42CoverDiv');
	
	newCoverDiv.css(
	{
		'left': '0px',
		'top': '0px',
		'z-index': '999',
		'background-color': '#000000',
		'filter': 'alpha(opacity=70)',
		'cursor': 'pointer',
		'opacity': '0.7',
		'-moz-opacity': '0.7',
		'position': 'absolute'
	});

	newCoverDiv.click(function() { removeCoverDiv(); });
	$('body').append( newCoverDiv);
}

function removeCoverDiv()
{
	window.onresize = null;

	if( $('#Popup42GoogleMap').length > 0)
	{
		GUnload();
	}

	$('#Popup42ContentDiv').remove();
	$('#Popup42CoverDiv').remove();
}

function createIFrame(width, height, source)
{
	var containerDiv = createContainerDiv();
	
	containerDiv.css(
	{
		'display': 'none',
		'position': 'absolute',
		'z-index': '1000',
		'cursor': 'pointer',
		'left': '-10000px',
		'top': '-10000px',
		'width': width,
		'height': height,
		'float': 'left'
	});
	
	containerDiv.attr('scrolling', 'auto');

	var iframeDiv = $('<iframe>');
	iframeDiv.attr('id', 'Popup42IFrameDiv');
	iframeDiv.attr('frameborder', '0px');
	iframeDiv.attr('marginwidth', '5px');
	iframeDiv.attr('marginheight', '5px');
	iframeDiv.attr('scrolling', 'auto');
	iframeDiv.attr('src', source);
	
	iframeDiv.css(
	{
		'border-style': 'none',
		'width': '100%',
		'height': height + 'px',
		'z-index': '1000',
		'left': '0px',
		'top': '0px',
		'background-color': '#ffffff'
	});

	containerDiv.append(iframeDiv);
	containerDiv.append(createCloserDiv());

	return containerDiv;
}

function createHtml( width, height, source)
{
	var containerDiv = createContainerDiv();
	
	containerDiv.css(
	{
		'display': 'none',
		'position': 'absolute',
		'z-index': '1000',
		'left': '-10000px',
		'top': '-10000px',
		'width': width,
		'height': height,
		'float': 'left'
	});
	
	containerDiv.attr('scrolling', 'auto');

	var contentDiv = $('<div>');
	contentDiv.attr('id', 'Popup42HtmlDiv');
	
	contentDiv.css(
	{
		'border-style': 'none',
		'width': '100%',
		'height': height + 'px',
		'position': 'relative',
		'z-index': '1000',
		'padding': '0px',
		'left': '0px',
		'top': '0px',
		'overflow': 'auto',
		'background-color': '#ffffff'
	});

	containerDiv.append(contentDiv);
	containerDiv.append(createCloserDiv());

	return containerDiv;
}

function createGoogleMap( source, width, height)
{
	var containerDiv = createContainerDiv();
	
	containerDiv.css(
	{
		'display': 'none',
		'position': 'absolute',
		'z-index': '1000',
		'cursor': 'pointer',
		'left': '-10000px',
		'top': '-10000px',
		'width': width,
		'height': height,
		'float': 'left'
	});

	containerDiv.attr('scrolling', 'auto');
	
	var contentDiv = $('<div>');
	contentDiv.attr('id', 'Popup42GoogleMap');
	
	contentDiv.css(
	{
		'display': 'block',
		'border-style': 'none',
		'width': '100%',
		'height': height + 'px',
		'position': 'relative',
		'z-index': '1000',
		'padding': '0px',
		'left': '0px',
		'top': '0px',
		'overflow': 'auto',
		'background-color': '#ffffff'
	});

	containerDiv.append(contentDiv);
	containerDiv.append(createCloserDiv());

	
	resizeContent( containerDiv, width, height);

	return containerDiv;
}

function createImage( source, width, height)
{
	var containerDiv = createContainerDiv();
	
	containerDiv.css(
	{
		'display': 'none',
		'position': 'absolute',
		'z-index': '1000',
		'cursor': 'pointer',
		'left': '-10000px',
		'top': '-10000px',
		'width': width,
		'height': height,
		'float': 'left'
	});
	
	containerDiv.attr('scrolling', 'auto');
	containerDiv.click(function() { removeCoverDiv(); });
	
	var image = $('<img>');
	image.attr('id', 'Popup42ImageDiv');
	
	image.css(
	{
		'z-index': '1000',
		'border-style': 'none'
	});
	
	containerDiv.append(image);

	image.load(function() { imageHasLoaded(image, width, height); });
	image.attr('src', source);

	containerDiv.append(createCloserDiv());

	return containerDiv;
}

function createFromDiv(source, width, height) 
{
	var containerDiv = createContainerDiv();

    containerDiv.css(
	{
	    'display': 'none',
	    'position': 'absolute',
	    'z-index': '1000',
	    'cursor': 'pointer',
	    'left': '-10000px',
	    'top': '-10000px',
	    'width': width,
	    'height': height,
	    'float': 'left'
	});

	containerDiv.attr('scrolling', 'auto');

	var contentDiv = $('<div>');
	contentDiv.attr('id', 'Popup42DivDiv');

	contentDiv.css(
	{
	    'display': 'block',
	    'border-style': 'none',
	    'width': '100%',
	    'height': height + 'px',
	    'position': 'relative',
	    'z-index': '1000',
	    'padding': '0px',
	    'left': '0px',
	    'top': '0px',
	    'overflow': 'auto',
	    'background-color': '#ffffff'
	});

	var sourceDiv = $('#'+source);

	contentDiv.html(sourceDiv.html());

	containerDiv.append(contentDiv);
	containerDiv.append(createCloserDiv());

	resizeContent(containerDiv, width, height);

    return containerDiv;
}

function imageHasLoaded( image, width, height)
{
	resizeContent( image.parent(), width, height);
}

function createContainerDiv()
{
	var containerDiv = $('<div>');
	containerDiv.attr('id', 'Popup42ContentDiv');
	return containerDiv;
}

function createCloserDiv()
{
	var closerDiv = $('<div>');
	
	closerDiv.attr('id', 'Popup42CloserDiv');
	closerDiv.click(function() { removeCoverDiv(); });
	closerDiv.css(
	{
		'z-index': '1000',
		'background-color': '#ffffff',
		'width': '100%',
		'margin-top': '2px',
		'padding-top': '4px',
		'padding-bottom': '4px',
		'cursor': 'pointer',
		'text-align': 'center',
		'text-transform': 'uppercase',
		'font-size': '14px'
	});
	
	var closeP = $('<p>');
	closeP.css({'font-size': '14px', 'margin' : '0px', 'padding': '0px', 'height': '18px'});
	
	var text = $("<span>");
	text.css({'vertical-align': 'middle'});
	text.text('Schliessen');
	
	closerDiv.append(text);
	
	var imgSpan = $('<span>');
	imgSpan.css({'vertical-align': 'middle', 'font-size': '22px'});
	
	var closeImage = $('<img>');
	closeImage.attr('src', '/Style%20Library/Custom/Images/LightboxCloseButton.gif');
	
	closeP.append(text);
	imgSpan.append(closeImage);
	closeP.append(imgSpan);
	
	closerDiv.append(closeP);
	
	return closerDiv;
}

function resizeContent( $content, originalWidth, originalHeight)
{
	if( $content == null || $content.length === 0)
	{
		return;
	}

	$content.show();

	var width = $content.width();
	var height = $content.height();

	if( originalWidth != null && originalWidth > 0)
	{
		width = originalWidth;
	}

	if( width > 0)
	{
		$content.width( width);

		if (width > $(window).width())
		{
			$content.width( $(window).width());
		}
	}

	if( originalHeight != null && originalHeight > 0)
	{
		height = originalHeight;
	}

	if( height > 0)
	{
		$content.height( height);

		if (height > $(window).height())
		{
			$content.height( $(window).height());
		}
	}

	var left = (($(window).width() / 2) - (width / 2) + $(window).scrollLeft());
	$content.css('left', (left > 0 ? left : 0) + 'px');

	var top = (($(window).height() / 2) - (height / 2) + $(window).scrollTop());
	$content.css('top', (top > 0 ? top : 0) + 'px');
}

function resizeCoverDiv()
{
	var $cover = $("#Popup42CoverDiv");

	if( $cover.length === 0)
	{
		return;
	}

	var position = $(baseDivName).position();

	var mainHeight = position.top + $(baseDivName).height();

	if ($(window).height() > mainHeight)
	{
		mainHeight = $(window).height();
	}

	var mainWidth = position.left + $(baseDivName).width();

	if ($(window).width() > mainWidth)
	{
		mainWidth = $(window).width();
	}

	$cover.width(mainWidth + 'px').height(mainHeight + 'px');
}


