// Initialize.
function init_modal(srcForModal, progressView) {

	// Detect IE6 (boolean).
	var $IE6 = typeof document.addEventListener !== 'function' && !window.XMLHttpRequest;

	// Do some math.
	function sizeModal() {

		// Modal dimensions.
		var $modal = $('#modal_window');
		var $modal_width = $modal.outerWidth();
		var $modal_height = $modal.outerHeight();
		var $window_height = $(window).height();
//		var $document_height = $(document).height();
		if($('#modal_wrapper').height() < $window_height)
		{
			$('#modal_wrapper').css('height', $window_height + 'px');
		}
//		alert('$("#modal_wrapper").height(): ' + $('#modal_wrapper').height() + 'px');
		
		// exact center position for modal window
		var $modal_margin_top = '-' + Math.floor($modal_height / 2) + 'px';
		var $modal_margin_left = '-' + Math.floor($modal_width / 2) + 'px';
		var $modal_top = Math.floor($window_height / 2) + 'px';
		
		// Set modal.
		$('#modal_window').css('top', $modal_top);
		$('#modal_window').css('margin-top', $modal_margin_top).css('margin-left', $modal_margin_left);
		
		// iframe solution for webkit
		if(progressView === 'iframe')
		{
			$('#upload_frame').attr({width: $modal.width()});
		}
	}

	/* For IE6. */
	function positionModal() {
		// Force modal into place.
		$('#modal_wrapper').css('top', $(document).scrollTop() + 'px');
	}

	// Reveal the modal.
	function showModal() {
		/* For IE6. */
		if ($IE6) {
			positionModal();
		}
		
		$('#modal_wrapper').css({'height': $('body').css('height'), 'width': $('body').css('width')})
		
		// Unveil the wrapper.
		$('#modal_wrapper').show();
		
		// Size it.
		sizeModal();
		
		// Reveal modal window.
		$('#modal_window').css('visibility', 'visible').show();
		
		// iframe solution for webkit
		if(progressView === 'iframe')
		{
			// show iframe
			$('#upload_frame').show();
		}
	}
	
	// Insert modal at end of </body>.
	if(progressView === 'modal' || progressView === 'iframe')
	{
		$('#modal_wrapper').append('<div id="modal_window"><div id="modal_bar"><strong>Modal window</strong><a href="#" id="modal_close">Close</a></div><div id="modal_content"></div></div>');
//		$('#modal_wrapper').append('<div id="modal_window"><div id="modal_bar"><strong>Modal window</strong><a href="#" id="modal_close"><img src="http://app-maschine.hm07.heubach-media.de/hmcms_media/public/images/upload_frame/close.gif" width="28" height="15" alt="close" title="close" /></a></div><div id="modal_content"></div></div>');
	}
	else
	{
		// ???
		return false;
	}
	
	var $modalWindowTitle = '';
	// Determine link target.
	if (srcForModal[0] === '#')
	{
		// Assume, local content from site
//		$('#modal_content').html($(srcForModal).html());
		if(srcForModal === '#loader')
		{
			$('#modal_content').html('<div style="text-align: center;"><h1 style="margin: 0px; padding: 0xp;">&nbsp;</h1><h1 style="margin: 0px; padding: 0xp;">SPEICHERN</h1><h1 style="margin: 0px; padding: 0px;" id="textLoader"></h1><h1 style="margin: 0px; padding: 0xp;">&nbsp;</h1></div>');
			showModal();
		}
		$modalWindowTitle = 'Speichern';
	}
	else if(progressView === 'iframe')
	{
		// iframe solution for webkit
		$('#modal_content').append('<iframe id="upload_frame" name="upload_frame" frameborder="0" border="0" src="" scrolling="no" scrollbar="no" ></iframe>');
		$('#upload_frame').attr('src', srcForModal);
		$modalWindowTitle = 'Upload-Fortschritt';
		showModal();
	}
	else
	{
		// Assume external Ajax content.
		$('#modal_content').load(srcForModal, '', showModal);
		$modalWindowTitle = 'Upload-Fortschritt';
	}

	// Determine modal title.
	$('#modal_bar strong').html($modalWindowTitle);
	
	// Hide modal elements.//#modal_overlay, 
	$('#modal_close').click(function() {
		closeModal(progressView);
	});

	// Listen for browser scroll, if IE6.
	if ($IE6) {
		$(window).scroll(function() {
			if ($('#modal_wrapper').is(':visible')) {
				positionModal();
			}
		});
	}
}

function closeModal(progressView){
	var stopUploading = confirm('Möchten Sie wirklich den Vorgang abbrechen?');
	if(stopUploading === true)
	{
		// Hide the modal.
		$('#modal_wrapper').hide();

		// Hide, because images might load later.
		$('#modal_window').css('visibility', 'hidden');

		// Destroy modal content.
		$('#modal_content').html('');

		// Reset modal title.
		$('#modal_bar strong').html('Modal window');
		
		if(progressView === 'modal')
		{
			// Hide the modal war vorher hier
			
			location.reload();
		}
		else if(progressView === 'iframe')
		{
			// iframe
			$('#progress_container').hide(); 
			$('#progress_container').html(''); 
			window.parent.stop();
			window.parent.location.reload();
		}
		if($.browser.msie || $.browser.webkit)
		{
//			this.focus();
		}
	}
	
	// Nofollow.
	this.blur();
	return false;
}
