function preparePopupLayout() {
	/* prepare grid */
	var modalDialog = new Element('div', {
		'class' : 'generic_dialog',
		'id' : 'modalDialog',
		'styles' : {
			'opacity' : '0',
			'display' : 'block',
			'top' : '125px'
		}
	});
	var dialogPopup = new Element('div', {
		'class' : 'generic_dialog_popup'

	});
	var dialog_table = new Element('table', {
		'class' : 'pop_dialog_table',
		'id' : 'pop_dialog_table',
		'styles' : {
			'width' : '532px'
		}
	});
	var dialogTitle = new Element('h2', {
		'class' : 'dialog_title',
		'id' : 'dialogTitle'
	});
	var dialogContent = new Element('div', {
		'class' : 'dialog_content',
		'id' : 'dialogContent'
	});
	var dialogSummary = new Element('div', {
		'class' : 'dialog_summary',
		'id' : 'dialogSummary'
	});
	var dialogBody = new Element('div', {
		'class' : 'dialog_body',
		'id' : 'dialogBody'
	});

	var tr1 = new Element('tr');
	var tbd = new Element('tbody');
	var td1_1 = new Element('td', {
		'class' : 'pop_topleft'
	});
	var td1_2 = new Element('td', {
		'class' : 'pop_border pop_top'
	});
	var td1_3 = new Element('td', {
		'class' : 'pop_topright'
	});

	var tr2 = new Element('tr');
	var td2_1 = new Element('td', {
		'class' : 'pop_border pop_sideLeft'
	});
	var td2_2 = new Element('td', {
		'class' : 'pop_content',
		'id' : 'pop_content'
	});
	var td2_3 = new Element('td', {
		'class' : 'pop_border pop_sideRight'
	});

	var tr3 = new Element('tr');
	var td3_1 = new Element('td', {
		'class' : 'pop_bottomleft'
	});
	var td3_2 = new Element('td', {
		'class' : 'pop_border pop_bottom'
	});
	var td3_3 = new Element('td', {
		'class' : 'pop_bottomright'
	});

	/* first line */
	tr1.adopt(td1_1);
	tr1.adopt(td1_2);
	tr1.adopt(td1_3);
	tbd.adopt(tr1);

	/* second line */
	tr2.adopt(td2_1);

	// td2_2.adopt(dialogTitle);
	// td2_2.adopt(dialogContent);
	// dialogContent.adopt(dialogSummary);
	// dialogContent.adopt(dialogBody);

	tr2.adopt(td2_2);
	tr2.adopt(td2_3);

	tbd.adopt(tr2);

	/* third line */
	tr3.adopt(td3_1);
	tr3.adopt(td3_2);
	tr3.adopt(td3_3);
	tbd.adopt(tr3);

	dialog_table.adopt(tbd);
	dialogPopup.adopt(dialog_table);
	modalDialog.adopt(dialogPopup);
	$('content').adopt(modalDialog);

	/* hiders FF, Opera and Safari */
	if (document.addEventListener) {
		document.addEvent('keydown', function(e) {
			if (e.key == 'esc') {
				handle_close_popup();
			}
		});

		$(document.body).addEvent(
				'click',
				function(e) {

					if ($('modalDialog').getStyle('opacity') == 1
							&& !e.target.getParent('.generic_dialog')) {
						handle_close_popup();
					}
				});
	} else if (document.attachEvent) {
		/* hiders IE */
		document.attachEvent('onkeydown', function(e) {
			if (e.keyCode == '27') {
				handle_close_popup();
			}
		});
	}
	return true;
}

function handle_open_popup() {
	$$('input, select').each(function(el) {
		el.setAttribute('disabled', 'true');
	});
	$('pop_dialog_table').setStyle('width', '532px');
}
function handle_close_popup() {
	$('modalDialog').fade('out');

	$$('input, select').each(function(el) {
		el.removeAttribute('disabled');
	});
}
function show_popup(url) {
	/*
	 * <link rel="stylesheet" href="/css/popup.css" media= "screen"
	 * type="text/css">
	 */
	/* set text */

	// alle formularfelder der bestehenden Seite deaktivieren
	handle_open_popup();

	var req = get_ajax_obj(url);
	req.send();

}

function get_ajax_obj(ajaxUrl) {

	/* Setup a AJAX Request object */
	var loaderId = 'ajaxLoader';
	var req = new Request.HTML( {
		url : ajaxUrl,
		onRequest : function() {
			if (!$defined($(loaderId))) {
				var loader = new Element('img', {
					'id' : loaderId,
					'alt' : 'loader',
					'src' : '/img/ico/loader.gif'
				});

				loader.inject('content', 'top');

			} else {
				showLoader(true);
			}
		},
		onSuccess : function(html) {
			showLoader(false);
			$('pop_content').set('text', '');
			$('pop_content').adopt(html);

			/* show */
			$('modalDialog').fade('in');
			var pos = $('modalDialog').getStyle('z-index').toInt() + 30;
			var size1 = $('dialogTitle').getSize();
			if ($defined($('dialogSummary'))) {
				var size2 = $('dialogSummary').getSize().y;
				var size3 = $('dialogBody').getSize();
				var sizeTtl = size1.y + size2 + size3.y;
			} else {
				var size2 = $('dialogBody').getSize();
				var sizeTtl = size1.y + size2.y;
			}

			// Ist der Bildschirmbereich kleiner als das PopUp?
		if (window.innerHeight <= (sizeTtl + pos)) {
			$('modalDialog').setStyle('position', 'absolute');
			$('pop_dialog_table').setStyle('width', '710px');

			// wenn bereits gescrollt wurde.
		if (window.pageYOffset > 0) {
			$('modalDialog').setStyle('top', window.pageYOffset);
		} else {
			$('modalDialog').setStyle('top', pos);
			window.scrollTo(0, 126);
		}
	} else {
		$('modalDialog').setStyle('top', '125px');
		$('modalDialog').setStyle('position', 'fixed');
		$('pop_dialog_table').setStyle('width', '532px');
	}
},
onComplete : function() {
	handle_complete();
},
onFailure : function() {
	$('content').set('text', 'The request failed');
}
	});

	return req;
}

function handle_complete() {
	movePopup();
	return true;
};

function openCenteredWindow(url, name, width, height, status, scrollbars,
		moreProperties, openerName) {
	var x, y = 0;
	if (screen) {
		x = (screen.availWidth - width) / 2;
		y = (screen.availHeight - height) / 2;
	}

	if (!status)
		status = '';
	if (!openerName)
		openerName = '';
	var reference = openPositionedWindow(url, name, width, height, x, y,
			status, scrollbars, moreProperties, openerName);

	return reference;
}

// Open a window at a given position on the screen
function openPositionedWindow(url, name, width, height, x, y, status,
		scrollbars, moreProperties, openerName) {

	// ie 4.5 and 5.0 mac - windows are 2 pixels too short; if a statusbar is
	// used, the window will be an additional 15 pixels short
	var agent = navigator.userAgent.toLowerCase();
	if (agent.indexOf("mac") != -1
			&& agent.indexOf("msie") != -1
			&& (agent.indexOf("msie 4") != -1 || agent.indexOf("msie 5.0") != -1)) {
		height += (status) ? 17 : 2;
	}

	// Adjust width if scrollbars are used (pc places scrollbars inside the
	// content area; mac outside)
	width += (scrollbars != '' && scrollbars != null && agent.indexOf("mac") == -1) ? 16
			: 0;

	var properties = 'width=' + width + ',height=' + height + ',screenX=' + x
			+ ',screenY=' + y + ',left=' + x + ',top=' + y
			+ ((status) ? ',status' : '') + ',scrollbars'
			+ ((scrollbars) ? '' : '=no')
			+ ((moreProperties) ? ',' + moreProperties : '');
	var reference = openWindow(url, name, properties, openerName);

	// resize window in ie if we can resize in ns; very messy
	// commented out because openPositionedWindow() doesn't set the resizable
	// attribute
	// left in for reference
	/*
	 * if (resizable && agent.indexOf("msie") != -1) { if (agent.indexOf("mac") !=
	 * -1) { height += (status) ? 15 : 2; if (parseFloat(navigator.appVersion) >
	 * 5) width -= 11; } else { height += (status) ? 49 : 31; width += 13; }
	 * setTimeout('if (reference != null && !reference.closed) ????????
	 * ?A?�?reference.resizeTo(' + width + ',' + height + ');', 150); }
	 */

	return reference;
}

// Core utility function that actually creates the window and gives focus to it
function openWindow(url, name, properties, openerName) {

	if (openerName == "") {
		openerName = window.document.title;
	}
	// ie4.x pc can't give focus to windows containing documents from a
	// different domain
	// in this case, initially load a local interstisial page to allow focussing
	// before loading final url
	/*
	 * 
	 * funktioniert im ie7 nicht. also: deaktiviert und immer standartm�ssig
	 * oeffnen var agent = navigator.userAgent.toLowerCase();
	 * 
	 * if (agent.indexOf("msie") != -1 && parseInt(navigator.appVersion) == 4 &&
	 * agent.indexOf("msie 5") == -1 && agent.indexOf("msie5") == -1 &&
	 * agent.indexOf("win") != -1 && url.indexOf('http://') == 0) { var
	 * winReference = window.open('about:blank', name, properties); if
	 * (winReference){ alert(winReference); setTimeout('if (winReference &&
	 * !winReference.closed) winReference.location.url = "' + url + '"', 300);
	 * }else{ winReference = window.open(url, name, properties); }
	 * 
	 * }else{ // wenn hier ein fehler angezeigt wird, dann liegt es am Aufruf
	 * und nicht direkt am Browser // sondern am Aufurf // Fehler1: Keine
	 * Abst�nde in den Namen (2. Argument) var winReference = window.open(url,
	 * name, properties); }
	 */
	var winReference = window.open(url, name, properties);

	// ie doesn't like giving focus immediately (to new window in 4.5 on mac; to
	// existing ones in 5 on pc)
	// setTimeout('if (winReference && !winReference.closed)
	// winReference.focus()', 200);

	if (openerName) {
		self.name = openerName;
	}
	// wenn winReference nicht zuruekgegeben wird dann funktioniert 'send_link'
	// im Downloadbereich nicht mehr!
	return winReference;
}

// Open a window at a given position on the screen
function openAlignedWindow(url, name, width, height, align, y, status,
		scrollbars, resizable, moreProperties, openerName) {

	// ie 4.5 and 5.0 mac - windows are 2 pixels too short; if a statusbar is
	// used, the window will be an additional 15 pixels short
	var agent = navigator.userAgent.toLowerCase();
	if (agent.indexOf("mac") != -1
			&& agent.indexOf("msie") != -1
			&& (agent.indexOf("msie 4") != -1 || agent.indexOf("msie 5.0") != -1)) {
		height += (status) ? 17 : 2;
	}

	// Adjust width if scrollbars are used (pc places scrollbars inside the
	// content area; mac outside)
	width += (scrollbars != '' && scrollbars != null && agent.indexOf("mac") == -1) ? 16
			: 0;

	if (align == 'left') {
		var x = 0;
	}
	if (align == 'right') {
		var x = window.screen.width - width;
	}

	var properties = 'width=' + width + ',height=' + height + ',screenX=' + x
			+ ',screenY=' + y + ',left=' + x + ',top=' + y
			+ ((status) ? ',status' : '') + ',scrollbars'
			+ ((scrollbars) ? '' : '=no') + ',resizable'
			+ ((resizable) ? '' : '=no')
			+ ((moreProperties) ? ',' + moreProperties : '');

	var reference = openWindow(url, name, properties, openerName);

	return reference;
}

// Check if a window is a popup
function isWindowPopup(win) {
	return ((win.opener) ? true : false);
}

function handleGallery(id) {

	if ($defined($(id))) {
		if ($(id).getProperty('class') == 'imgGalleryActive') {
			return false;
		}
		var arrGalleryImg = $$('#dialogBody img.imgGalleryActive, #dialogBody img.imgGallery');
		var lastImg = arrGalleryImg.getLast();

		arrGalleryImg.reverse();
		arrGalleryImg.each(function(el) {
			if (el.getProperty('class') == 'imgGalleryActive') {
				el.inject(lastImg, 'after');
			} else {
				el.inject(id, 'after');
			}
			el.removeProperty('style');
			el.setProperty('class', 'imgGallery');
		});

		$(id).setProperty('class', 'imgGalleryActive');

		$(id).setStyle('opacity', '0');

		var imgSize = $(id).width;

		var myFx = new Fx.Morph(id, {
			duration : 500,
			transition : Fx.Transitions.Cubic.easeOut
		});
		myFx.start( {
			'width' : [ imgSize - 60, imgSize ],
			'opacity' : [ 0, 1 ]
		});
	}
	return false;
}

function galleryPreview(id) {

	if ($defined($(id)) && $(id).getProperty('class') != 'imgGalleryActive') {
		var imgSize = $(id).width;
		var myFx = new Fx.Morph($(id), {
			duration : 400,
			transition : Fx.Transitions.Cubic.easeOut
		});
		myFx.start( {
			'width' : [ imgSize, 100 ]
		});
	}
	return false;
}

function galleryThumbnail(id) {

	if ($defined($(id)) && $(id).getProperty('class') != 'imgGalleryActive') {
		var imgSize = $(id).width;
		var myFx = new Fx.Morph($(id), {
			duration : 600,
			transition : Fx.Transitions.Cubic.easeOut
		});
		myFx.start( {
			'width' : [ imgSize, 50 ]
		});
	}
	return false;
}

function movePopup(popupID) {

	if (!$defined(popupID) || popupID == "") {
		popupID = "pop_dialog_table";
	}

	$$('#dialogTitle div, #pop_dialog_table td').each(function(move) {
		if (move.id != "pop_content" || (move.id == "dialogTitle") ) {
			// skalierbar machen
			if (move.getProperty('class') == 'pop_bottomright') {
				// wenn drueber gefahren erscheint anderer cursor
				move.addEvent('mousemove', function() {
					move.setStyle('cursor', 'se-resize');
				});
				// wenn draufgeklickt wird, dann verschieben
				move.addEvent('mousedown', function() {
					var scaleObj = $(popupID).makeResizable( {
						limit : {
							x : [ 400, 600 ],
							y : [ 200, 700 ]
						},
						onComplete : function() { scaleObj.detach(); }
					});
				});
			}else{
				// wenn drueber gefahren erscheint anderer cursor
				move.addEvent('mousemove', function() {
					move.setStyle('cursor', 'move');
				});
				// wenn draufgeklickt wird, dann verschieben
				move.addEvent('mousedown', function() {
					var myDrag = new Drag.Move(popupID, {
						onComplete : function() {
							myDrag.detach();
						}
					});
				});
			}
		}
	});
}
