InitShadowBox = {};
InitShadowBox.redirectUrlOnClose;
InitShadowBox.DEFAULT_IFRAME_WIDTH = 800;
InitShadowBox.DEFAULT_IFRAME_HEIGHT = 600;

Shadowbox.init({
	adapter: "base",
	modal:true,
	enableKeys:false,
    players: ["iframe", "html","img","swf"],
    onClose : onCloseEvent,
    overlayColor : "#000000",
    overlayOpacity : "0.8"
});


function parseToInt(param){
	var res = param.indexOf('px') > 0 ? param.substring(0, param.indexOf('px')) : param;
	return parseInt(res);
}

function openShadow(player, title, content, width, height, redirectUrlOnClose) {
   	
	var iWidth = InitShadowBox.DEFAULT_IFRAME_WIDTH;
	var iHeight = InitShadowBox.DEFAULT_IFRAME_HEIGHT;
	
	// Le trasformo in stringa   
	width = width+'';
	height = height+'';
	
	if (!width.blank()){		
		iWidth = parseToInt(width) + 80;
	}
	
	if (!height.blank()){
		iHeight = parseToInt(height) + 40;
	}
	
	var text = content.indexOf('?') > 0 ? '&' : '?';
	content += text + 'width='+iWidth +'&height='+iHeight+'&shadowbox=true';
	
	Shadowbox.open({
	    player:     player,
	    title:      title,
	    content:    content,
	    width: 		width,
	    height:     height	    
	});
	
	InitShadowBox.redirectUrlOnClose = redirectUrlOnClose;
};

function ShadowBoxClose() {	
	
	Shadowbox.close();	
};


function onCloseEvent() {
	
	ShadowBoxClose();
	
	if (InitShadowBox.redirectUrlOnClose) {
		window.location.href = InitShadowBox.redirectUrlOnClose;
	}	
}

/**
 * Apre un pop up shadowbox modale visualizzando un messaggio
 * 
 * @param title
 * @param messageKey - a portion of a message key starting with 'common.popup.' 
 * @param w
 * @param h
 * @param redirectUrlOnClose
 */
function showPopUp(title, messageKey, w, h, redirectUrlOnClose) {				
	openShadow('iframe', title, '/fe-web/definition/popupWarning.jsp?messageKey=' + messageKey, w, h, redirectUrlOnClose);	
}

/**
 * Apre un pop up shadowbox modale visualizzando un messaggio
 * 
 * @param title
 * @param messageKey - a portion of a message key starting with 'common.popup.' 
 * @param w
 * @param h
 * @param redirectUrlOnClose
 * @param arg0 first argument for the message identified by messageKey
 * @param arg1 second argument for the message identified by messageKey
 */
function showPopUp(title, messageKey, w, h, redirectUrlOnClose, arg0, arg1) {				
	openShadow('iframe', title, '/fe-web/definition/popupWarning.jsp?messageKey=' + messageKey + '&arg0=' + arg0 + '&arg1=' + arg1, w, h, redirectUrlOnClose);	
}

function showWarningPopUp(title, messageKey, w, h, redirectUrlOnClose,custom) {				
	openShadow('iframe', title, '/fe-web/definition/popupWarning.jsp?custom='+custom+'&messageKey=' + messageKey, w, h, redirectUrlOnClose);	
}


/**
 * Apre un pop up shadowbox modale avente come contenuto una jsp specificata nel parametro pageUrl
 * 
 * @param title
 * @param pageUrl
 * @param w
 * @param h
 * @param redirectUrlOnClose
 */
function showPopUpWithUrl(title, pageUrl, w, h, redirectUrlOnClose) {		
	openShadow('iframe', title, pageUrl, w, h, redirectUrlOnClose);	
}
