/**  
 *  Popup handler, this 
 *
 */

var _lightbox_;
var win="";
var timed=false;
function checkStatoPopup(){
	window.setTimeout('checkStatoPopup()', 1000);
	if(timed && win!="" && win.closed){
		win="";
		try{
			window.detachEvent(checkStatoPopup);
		} catch(e){
			window.removeEventListener('focus', checkStatoPopup, false)
		}
	}
	timed = true;
}


/**  
 * La funzione windowOpen consente l'apertura di una finestra popup su cui redirigire una action faces. E' testata su explorer e firefox.
 * Per utilizzarla basta creare un commandLink in pagina nascosto come il seguente:
 *
 * <h:commandLink  style="display:none;" id="linkNascosto"  target="Popup" value="aaaaaaaa" action="#{mioBean.miaAction}"/>			
 *
 * Dove target č il nome assegnato alla popup ed action č la action di destinazione quindi anche expression language
 * Un button visibile in pagina permetterā di aprire la popup con il seguente codice:
 *
 * <h:commandButton value="LINK POPUP" onclick="windowOpen('myForm3:linkNascosto','Popup',500,500,false);return false;"/>
 *
 *
 */
 
function windowOpen(hiddenLinkId, popupName, width, height, scrollbar) {
	var popupProps = "width=" + width + ",height=" + height;
	if (scrollbar != "undefined" && scrollbar) {
		popupProps = popupProps + ",scrollbars=yes";
	}
	id=(new Date()).getMilliseconds();
	win = window.open('',popupName,popupProps);
	try{
		window.attachEvent('onfocus',checkStatoPopup);
		document.getElementById(hiddenLinkId).click();
	} catch (e){
		window.addEventListener('focus', checkStatoPopup, false)
	    document.getElementById(hiddenLinkId).onclick();
	}
}

var PopupOpener = Class.create();
PopupOpener.prototype = {
	initialize: function(href, props) {
		this.href = href;
		if(props) {
			this.width = props.width;
			this.height = props.height;
			this.closeParent = props.closeParent;
			this.layerOnParent = props.layerOnParent;
		}
	},
	
	openWindow: function() {
		var popup;
		if(this.width == null || this.height == null) {
			popup = window.open(this.href, '_blank', 'scrollbars=yes, status=yes, menubar=no, location=no, resizable=no, left=0, top=0, fullscreen=yes');
			if(parseInt(navigator.appVersion) >= 4){popup.focus();}
		} else {
			var winl = (screen.width-this.width)/2;
			var wint = (screen.height-this.height)/2;
			popup = window.open(this.href, '_blank', 'left=' + winl + ', top=' + wint + ', width=' + this.width + ' height=' + this.height + '');
			if(parseInt(navigator.appVersion) >= 4){popup.focus();}
		}
		
		
		if (navigator.appName != "Microsoft Internet Explorer") 
			document.body.innerHTML = "" 
		
		if(this.closeParent != null && this.closeParent == true) {
			if (!popup.opener) 
				popup.opener = self;
			
			daddy = window.self;
			daddy.opener = window.self;
			daddy.close();
		} else if(this.layerOnParent) {
			_lightbox_ = new Lightbox.base(document.getElementsByTagName('body')[0]);
			//popup.onclose = function() {opener._lightbox_.;}
		}
		this.popup = popup;
		return popup;
	}
}

function openPopUp(address) {
	po = new PopupOpener(address, {width:screen.width-4, height:screen.height-60, closeParent: true});
	po.openWindow();
}
