

// Darstellung im Frame verhindern

// 11.06.2010 tmarkus wieder deaktiviert da die Einbindung ins MC per iframe passiert und da auch bleiben soll
/*
if (top != self) {
	var s = String(document.location.pathname);
	if (s.length<5 || s.substr(0,5) != '/r/i/') {
		top.location = self.location;
	}1
}
*/

function findDialogMasterWindow() {
	var w = window;
	
	try {
		while ( w && !w.dialogMasterWindow ) {
			if (w.parent && w.parent != w) {
				w = w.parent;
			} else {
				w = null;
			}
		}
	} catch (e) {
		w = null;
	}
	if (!w) {
		w = window;
		w.dialogMasterWindow = true;
	}
	return w;
}

(function(jQuery){
	if (jQuery.ui && jQuery.ui.dialog) {
		if(!jQuery.ui.dialog.defaults) {
			jQuery.ui.dialog.defaults = {};
		}
		jQuery.ui.dialog.defaults.bgiframe = true;
	}
	findDialogMasterWindow();
})(jQuery);

var URL_PATTERN = /^((\w+?:\/\/([\w-]+\.)*\w+(:\d+)?)(\/[\w-\.]+)*)\/[\w-\.]*(\?.*)?(#.*)?$/;

/**
 * opens a modal dialog with an iframe
 *
 * http://jqueryui.com/demos/dialog
 * @param url url to open in iframe
 * @param title dialog title
 * @param params map with any option from http://jqueryui.com/demos/dialog/#options
 * 		special parameters:
 * 		- width : defaults to window width with offset
 * 		- height : defaults to window height with offset
 * @return the dialog element
 */
function openModalDialog(url, title, params) {
	if (!url || url=='') {
		alert('missing url in openModalDialog');
		return;
	}

	// make url absolute
	url = String(url);
	if (URL_PATTERN.exec(url) == null) {
		// not absolute url
		var e = URL_PATTERN.exec(document.location.href);
		if (e == null)
			e = URL_PATTERN.exec(window.location.href);
		if (e == null || e[1] == null || e[2] == null) {
			alert("Der gewünschte Dialog ("+url+") konnte nicht geöffnet werden. Bitte kontaktieren Sie die Administration und geben Sie die URL an.");
			url = null;			
		} else {
			if (url.charAt(0) == '/') {
				url = e[2] + url;
			} else {
				url = e[1] + '/' + url;
			}
		}		
	}
	
	if (url != null) {
		// check root window
		var w = findDialogMasterWindow();
			
		if (!w.jQuery) {
			alert('jQuery is missing in root window!');
			return;
		}
		
		if (!w.jQuery.ui.dialog) {
			alert('jQuery dialog extension is missing in root window!');
			return;
		}
		
		if ((typeof params) != 'object')
			params = {};
		
		var closefunction = params.close;
		if (title)
			params.title = title;
		if ((typeof params.resizable) == 'undefined') 
			params.resizable = false;
		params.modal = true;
		params.close = function(event,ui) {
			if (event && event.view) {
				var dialog = event.view.jQuery(event.target);
				closeModalDialog(dialog);
			}
			if ((typeof closefunction) == 'function') {
				closefunction(d);
			}
		};
		
		// wg browsercache
		//url += '?'+Math.random();
		
		var wnd = jQuery(window);
		var wndoffset = Math.min(wnd.width()*.2 , wnd.height()*.2);
		if (!params.width)
			params.width = wnd.width() - wndoffset;
		if (!params.height)
			params.height = wnd.height() - wndoffset;
		
		var d = w.jQuery('<div><iframe allowtransparency="true" width="100%" frameborder="0" height="99%" src="'
		+url+
		'" /></div>');
		
		d.dialog(params);
			
		return d;
	} else {
		return null;
	}
}

/**
 * close a modal dialog
 * 
 * @param dialog the active dialog element (result from #openModalDialog)
 */
function closeModalDialog(dialog) {
	if (dialog) {
		dialog.dialog('destroy');
		dialog.remove();
	}
}

/**
 * get current dialog
 * @return the dialog element (same from #openModalDialog)
 */
function getCurrentModalDialog() {
	var master = findDialogMasterWindow();
	var iframes = jQuery('iframe',master.document);
	for(var i=0, j=iframes.size(); i<j; i++) {
		var iframe = iframes.get(i);
		if (iframe.contentWindow == window)
			return master.jQuery(iframe).parent();
	}
}

/**
 * close the current dialog
 */
function closeCurrentDialog() {
	var d = getCurrentModalDialog();
	if (d) {
		d.dialog('close');
		closeModalDialog(d);
	}
}

