/*
POPUP
*/

var popupExists = false;

/**
	*Display the popup on the center of the current page
	* @argument title - title to display
	* @argument body - the "body" tag
	* @argument url - url to display
	* @argument width - width of the popup
	* @argument height - height of the popup
	*/
function popupShow(title, body, url, width, height) {
	// Add the HTML to the body
	if(!popupExists) {
		popmask = document.createElement('div');
		popmask.id = 'popup-bkgrnd';
		
		//Mask height
		windowHeight = getWindowHeight();
		pageHeight = document.getElementById('page').offsetHeight;	
		popHeight = windowHeight;
		if(pageHeight > windowHeight)
			popHeight = pageHeight;
		popmask.style.height = popHeight + "px";

		popcont = document.createElement('div');
		popcont.id = 'popup-border';
		popcont.innerHTML = '' +
					'<div id="popup-body">' +
						'<div id="popup-header">' +
							'<div id="popup-header-left">' +
							'<p>' + title + '<img src="/images/popup_close.png" alt="Close" border="0" onclick="popupClose();" /></p>' + 
							'</div>' +
						'</div>' +
						'<div id="popup-content">' +
							'<iframe style="width:100%;height:100%;background-color:transparent;" scrolling="auto" frameborder="0" allowtransparency="true" id="popup-frame" name="popup-frame" width="100%" height="100%"></iframe>' +
						'</div>' +
					'</div>';
					
		body.appendChild(popmask);
		body.appendChild(popcont);
		popupExists = true;
	// Popup already exists, we just have to unhide it
	}else {
		popmask = document.getElementById("popup-bkgrnd");
		popcont = document.getElementById("popup-border");
		poptitle = document.getElementById("popup-header-left");

		popmask.style.display = 'inline';
		popcont.style.display = 'inline';
		poptitle.innerHTML = '<p>' + title + '</p>';
	}
	
	// popup size
	popcont = document.getElementById("popup-border");
	popbody = document.getElementById("popup-content");
	popcont.style.width = width + "px";
	popcont.style.height = height + "px";
	popbody.style.height = (height-25) + "px";
	
	// center popup
	popcont.style.top = (getViewportHeight() / 2) - (height / 2) + "px";
	popcont.style.left = (getViewportWidth() / 2) - (width / 2) + "px";
	
	// Hiding the frame in Safari, Konqueror and Opera breaks the modal
	popframe = document.getElementById("popup-frame");
	/*if(navigator.userAgent.indexOf('Opera') == -1 && navigator.userAgent.indexOf('Safari') == -1 && navigator.userAgent.indexOf("Konqueror") == -1) {
		popframe.style.display = "none";
	}*/

	// If the modal has been closed with reseting "this.URL" but the
	// requested URL is the same as the old one, we have to force Opera to
	// reload the page or it will never fire an onload event. Opera
	// recognizes that the URL hasn't changed and does nothing.
	if (url == null && navigator.userAgent.indexOf('Opera') != -1 && popframe.src != 'about:blank') {
		popframe.location.reload();
	}
	
	// Change frame source
	popframe.src = url;
	popframe.style.display = 'block';
}


/**
	*Close the popup
	*/
function popupClose() {
	popmask = document.getElementById("popup-bkgrnd");
	popcont = document.getElementById("popup-border");

	popmask.style.display = 'none';
	popcont.style.display = 'none';
}


/**
	*Screen height
	*/
function getViewportHeight() {
	if (window.innerHeight!=window.undefined) return window.innerHeight;
	if (document.compatMode=='CSS1Compat') return document.documentElement.clientHeight;
	if (document.body) return document.body.clientHeight;
	return window.undefined;
}


/**
	*Screen width
	*/
function getViewportWidth() {
	if (window.innerWidth!=window.undefined) return window.innerWidth;
	if (document.compatMode=='CSS1Compat') return document.documentElement.clientWidth;
	if (document.body) return document.body.clientWidth;
	return window.undefined;
}
