var currentPanelWidget = null;


function getPopupVars (/* optional */ popupHeight, popupWidth) {
	if (popupHeight==null) {
		popupHeight=300;
	}
	if (popupWidth==null) {
		popupWidth=600;
	}
	
	var popupVars = {
		width : popupWidth + "px",
		height : popupHeight + "px",
		fixedcenter : false,
		visible : false,
		draggable: true,
		dragOnly:true,
		//  This is unfortunate, but fixes some issues we've encountered
		zIndex:10, 
		constraintoviewport : true,
		modal: true,
		underlay: "shadow",
		iframe: false
	};
	
	return popupVars;
}


function getNewIframePopupDivElement(iframeSrc, panelId, panelTitle) {
	var headerTag = "<div class='hd'>" + panelTitle + "</div> ";
	var bodyTag = "<div class='bd'> "
				+ 	"<iframe src='" + iframeSrc + "' id='" + panelId + "Iframe' class='popupIframe'/>"
				+ "</div> ";
	var footerTag = "<div class='ft'> " + " </div> ";
	//  create element
	var popupDiv = document.createElement("div");
	popupDiv.setAttribute("id", panelId);
			
	popupDiv.innerHTML = headerTag + bodyTag + footerTag;	
	
		
	return popupDiv;
}

/**
 * @private
 */
function createNewIframePopup(iframeSrc, panelId, panelTitle, 
		/* optional */ height, width) {
	var popupDiv = getNewIframePopupDivElement(iframeSrc, panelId, panelTitle);
	var popupVars = getPopupVars (height, width);
	
	//  create panel
	var panelWidget = new YAHOO.widget.Panel(popupDiv,popupVars);
	panelWidget.render(document.getElementsByTagName("body")[0]);
	
	return panelWidget;
}

function showIframePopup(iframeSrc, panelId, panelTitle, 
		/* optional */ height, width) {
	currentPanelWidget = createNewIframePopup(iframeSrc, panelId, panelTitle, height, width);
	
	function onHideFunction() {
		//  I think the following should be in there..  but it causes errors in the yahoo code..
		//  so we'll leave it commented out for the moment.
		//  currentPanelWidget.destroy();
		
		// Insead, we find the dom element ourselves and remove it (which is what .destroy() 
		// claims it would do if it wasn't so busy crashing).
		var containDiv = document.getElementById(currentPanelWidget.id+"_c")
		document.getElementsByTagName("body")[0].removeChild(containDiv);
		
		currentPanelWidget = null;
		
	}
	currentPanelWidget.hideEvent.subscribe(onHideFunction);	
		
	currentPanelWidget.center();
	currentPanelWidget.show();
	return currentPanelWidget;
}

function closeCurrentPanelPopup() {
	currentPanelWidget.hide();
}

function setCurrentPanelPopupHeight(height) {
	height = height + "px";
	if (currentPanelWidget.cfg.getProperty("height")!=height) {
		currentPanelWidget.cfg.setProperty("height", height);
		currentPanelWidget.center();
	}
}
