var rhfPortletPopups = new Array();

/**
 * Opens a contact form in a Liferay popup. The directrequest portlet must be
 * deployed on a page within the same community. The page may be hidden.
 * 
 * @param plid
 *            The layout id of the page where the directrequest portlet is
 *            deployed. This can be found out by calling
 *            PortalUtil.getPlidFromPortletId. If omitted, the portlet must be
 *            deployed on the current page.
 * @param options
 *            The following options are supported:<br />
 *            subject: overwrites the email subject configured through portlet
 *            preferences.<br />
 *            fromemail: overwrites the sender email configured through portlet
 *            preferences.<br />
 *            rcvemail: overwrites the receiver email configured through portlet
 *            preferences.<br />
 *            email: sets an initial value for the email input field.<br />
 *            phone: sets an initial value for the phone input field.<br />
 *            fullname: sets an initial value for the fullname input field.<br />
 *            firstname: sets an initial value for the firstname input field.<br />
 *            enterprise: sets an initial value for the enterprise input field.<br />
 *            street: sets an initial value for the street input field.<br />
 *            zip: sets an initial value for the zip input field.<br />
 *            city: sets an initial value for the city input field.<br />
 */
function showContactForm(portletId, plid, params) {
	var popupOptions = {
		modal : false,
		width : 500,
		position : [ 'center', 100 ],
		title : "Kontakt"
	}
	//title : '\u004b\u006f\u006e\u0074\u0061\u006b\u0074'
	showPortletPopup(portletId, plid, params, popupOptions, '455px');
}

/**
 * Opens a callback form in a Liferay popup. The directrequest portlet must be
 * deployed on a page within the same community. The page may be hidden.
 * 
 * @param plid
 *            The layout id of the page where the directrequest portlet is
 *            deployed. This can be found out by calling
 *            PortalUtil.getPlidFromPortletId. If omitted, the portlet must be
 *            deployed on the current page.
 * @param options
 *            The following options are supported:<br />
 *            subject: overwrites the email subject configured through portlet
 *            preferences.<br />
 *            fromemail: overwrites the sender email configured through portlet
 *            preferences.<br />
 *            rcvemail: overwrites the receiver email configured through portlet
 *            preferences.<br />
 *            callbackname: sets an initial value for the name input field.<br />
 *            callbackphone: sets an initial value for the phone input field.<br />
 */
function showCallbackForm(portletId, plid, params) {
	var popupOptions = {
		modal : false,
		width : 264,
		position : [ 'center', 200 ],
		title : "Callback Service"
	}
	//title : '\u0043\u0061\u006c\u006c\u0062\u0061\u0063\u006b\u0020\u0053\u0065\u0072\u0076\u0069\u0063\u0065'
	showPortletPopup(portletId, plid, params, popupOptions, '210px');
}

function showPortletPopup(portletId, plid, params, popupOptions, iframeHeight) {
	var portletURL = Liferay.PortletURL.createRenderURL();
	portletURL.setPortletId(portletId);
	if (plid != null) {
		portletURL.setPlid(plid);
	}
	portletURL.setWindowState('pop_up');
	params = params || {};
	for ( var key in params) {
		portletURL.setParameter(key, params[key]);
	}
	var iframeId = 'portlet-popup_' + new Date().getTime();
	var protocol = location.protocol;
	var urlString = portletURL.toString();
	urlString = urlString.replace(/http:/, protocol);
	var message = '<iframe id="'
			+ iframeId
			+ '" name="'
			+ iframeId
			+ '" class="portlet-popup" src="'
			+ urlString.toString()
			+ '" scrolling="no" frameborder="0" onload="parent.resizePortletPopup(this);"></iframe>';
	var popup = Liferay.Popup(jQuery.extend( {}, popupOptions, {
		className : 'portlet-popup-div',
		message : message
	}));
	rhfPortletPopups.push( {
		iframeId : iframeId,
		popup : popup
	});
}

function closePortletPopup(iframeId) {
	for (i in rhfPortletPopups) {
		if (rhfPortletPopups[i].iframeId == iframeId) {
			Liferay.Popup.close(rhfPortletPopups[i].popup);
		}
	}
}

function resizePortletPopup(iframe) {
	// make content wrapper shrinkable
	var contentWrapper = iframe.contentDocument
			.getElementById('content-wrapper');
	if (contentWrapper) {
		contentWrapper.style.minHeight = '0';
	}
	iframe.height = iframe.contentDocument.body.scrollHeight;
}
