/** Modifica le ancore di un documento (tags <a>) che hanno una delle seguenti classi CSS:
 *    'targetBlank'   apre l'URL dell'ancora in una nuova finestra del browser
 *    'openPopup'     apre l'URL dell'ancora in popup
 *    'openPopup<.*>' apre l'URL dell'ancora utilizzando le opzioni indicate.
 *                    Es: 'openPopup128-640-480'
 *
 * NB: Funzione da chiamare sull'onload del documento!
 */
function popupAccessibili(newWindowMessage) {
	newWindowMessage = newWindowMessage || ' (collegamento in nuova finestra)';
	$('a').each(function() {
		if ($(this).attr('class').indexOf('openPopup') != -1) {
			$(this).attr('title', $(this).attr('title') + newWindowMessage);
			// Funzione di popup custom
			var popupType   = 128+32; // Centrato + scroll
			var popupWidth  = 400;
			var popupHeight = 300;
			var popupOption = extractFrom($(this).attr('class'), 'openPopup', ' ');
			if (popupOption) {
				var m = popupOption.split(/-/);
				popupType = m[0] ? m[0] : popupType;
				popupWidth = m[1] ? m[1] : popupWidth;
				popupHeight = m[2] ? m[2] : popupHeight;
			}
			$(this).click(function() {
				popup($(this).attr('href'), popupType, popupWidth, popupHeight);
				return false;
			});
		} else if ($(this).attr('class').indexOf('targetBlank') != -1) {
			$(this).attr('title', $(this).attr('title') + newWindowMessage);
			$(this).attr('target', '_blank');
		}
	});
}

function extractFrom(str, beginStr, endStr)
{
	var start = str.indexOf(beginStr)
	var end = str.indexOf(endStr, start)
	return str.substring(start + beginStr.length, (end != -1 ? end : str.length))
}

function alreadyOpen(nome)
{
    var Match = document.cookie.match( /popup=([^;]+)/ )
    if (Match) {
        var popupNames = Match[1].split(",")
        for (var i=0; i<popupNames.length; i++) {
            if (popupNames[i] == nome) return true
        }
        document.cookie = "popup=" + Match[1] + "," + nome + "; PATH=/"
    } else {
        document.cookie = "popup=" + nome + "; PATH=/"
    }
    return false
}


function openPopup(url, windowParam, windowName)
{
	var now = new Date();
	windowName = (windowName ? windowName : 't'+now.getTime()+'r'+Math.floor(Math.random()*1000)) 
	var popUpWindow = window.open(url, windowName, windowParam)
	if (popUpWindow && popUpWindow.focus) {
		popUpWindow.focus()
	}
}


function popup(url, kind, width, height, windowName, leftPos, topPos)
{
	var toolbarFlag         = (kind % 2 ? 1 : 0);  kind = parseInt(kind / 2, 10);   //    1
	var locationFlag        = (kind % 2 ? 1 : 0);  kind = parseInt(kind / 2, 10);   //    2
	var directoriesFlag     = (kind % 2 ? 1 : 0);  kind = parseInt(kind / 2, 10);   //    4
	var statusFlag          = (kind % 2 ? 1 : 0);  kind = parseInt(kind / 2, 10);   //    8
	var menubarFlag         = (kind % 2 ? 1 : 0);  kind = parseInt(kind / 2, 10);   //   16
	var scrollbarsFlag      = (kind % 2 ? 1 : 0);  kind = parseInt(kind / 2, 10);   //   32
	var resizableFlag       = (kind % 2 ? 1 : 0);  kind = parseInt(kind / 2, 10);   //   64
	var centerFlag          = (kind % 2 ? 1 : 0);  kind = parseInt(kind / 2, 10);   //  128
	var openOnlyOneTimeFlag = (kind % 2 ? 1 : 0);  kind = parseInt(kind / 2, 10);   //  256
	var fullScreen          = (kind % 2 ? 1 : 0);  kind = parseInt(kind / 2, 10);   //  512
	var channelMode         = (kind % 2 ? 1 : 0);  kind = parseInt(kind / 2, 10);   // 1024

	if (centerFlag) {
		leftPos = window.screen.width/2 - width /2 - 10
		topPos = window.screen.height/2 - height/2 - 50
	}
	var param = 'toolbar='+toolbarFlag
			  + ',location='+locationFlag
			  + ',directories='+directoriesFlag
			  + ',status='+statusFlag
			  + ',menubar='+menubarFlag
			  + ',scrollbars='+scrollbarsFlag
			  + ',resizable='+resizableFlag
			  + (fullScreen                     ? ',fullscreen=1'  : '')
			  + (channelMode                    ? ',channelmode=1' : '')
			  + (!fullScreen && typeof(width)   != 'undefined' ? ',width=' +width  : '')
			  + (!fullScreen && typeof(height)  != 'undefined' ? ',height='+height : '')
			  + (!fullScreen && typeof(leftPos) != 'undefined' ? ",left=" + leftPos + ",screenX=" + leftPos : '')
			  + (!fullScreen && typeof(topPos)  != 'undefined' ? ",top="  + topPos  + ",screenY=" + topPos  : '')
    if (!openOnlyOneTimeFlag || !alreadyOpen(windowName)) {
		openPopup(url, param, windowName)
    }
}
