var isAlertOpen = false;
var idVideoOpen = -1;
var currentVideo = "";
var DISTANCE_POPUP_INVITA_AMICO_VIDEO = 20;

jQuery(document).ready(function()
{	
	jQuery("#layer_opacity").click( 
		function() 
		{ 
			if(jQuery("#info_societarie").length > 0 && jQuery("#info_societarie").css('display') != "none") closeGenericAlert('info_societarie', true);
			if(jQuery("#genericAlert").length > 0 && jQuery("#genericAlert").css('display') != "none") closeGenericAlert('genericAlert', true);
		});
		
	resizeWindowPopup();
	jQuery(window).bind("resize", resizeWindowPopup);
});

function openGenericAlert(id, titolo, testo, btn)
{
	var btnText = "conferma";
	if(btn != null && btn != "undefined") btnText = btn;
	if(isAlertOpen) return;
	//if(jQuery.browser.msie && jQuery.browser.version == 6) 
	//	jQuery("#layer_opacity").css('height', jQuery(".total_container_IE6").height()+'px');
	if(jQuery.trim(jQuery("#"+id).css('position')) == 'absolute')
		setTopAlert(id);
	jQuery("#testo_alert").html(testo);
	jQuery("#titolo_alert").html(titolo);
	if(jQuery("#"+id).find(".btn_custom_alert").length > 0) jQuery("#"+id).find(".btn_custom_alert").html(btnText);
	jQuery('#layer_opacity').show();
	if(jQuery.trim(jQuery('#'+id).css('visibility')) != 'visible') jQuery('#'+id).css('visibility', 'visible');
	jQuery('#'+id).show();
	isAlertOpen = true;
}

function openAlert(id, openOpacity)
{
	//if(jQuery.browser.msie && jQuery.browser.version == 6) 
	//	jQuery("#layer_opacity").css('height', jQuery(".total_container_IE6").height()+'px');
	var tmpOpenOpacity = true;
	if(openOpacity != null && openOpacity != "undefined") tmpOpenOpacity = openOpacity;
	if(jQuery.trim(jQuery("#"+id).css('position')) == 'absolute') setTopAlert(id);
	if(tmpOpenOpacity) jQuery('#layer_opacity').show();
	if(jQuery.trim(jQuery('#'+id).css('visibility')) != 'visible') jQuery('#'+id).css('visibility', 'visible');
	jQuery('#'+id).show();
}

function closeGenericAlert(id, closeOpacity)
{
	var tmpCloseOpacity = true;
	if(closeOpacity != null && closeOpacity != "undefined") tmpCloseOpacity = closeOpacity;
	isAlertOpen = false;
	if(tmpCloseOpacity) jQuery('#layer_opacity').hide();
	jQuery('#'+id).hide();
}

jQuery(window).scroll(
	function () 
	{
		var animationTime = 100;
		if(jQuery.browser.msie) animationTime = 20;
		var idAlert = getOpenAlertID();
		var heightBrowser = jQuery(window).height();
		var scrollTop = jQuery(window).scrollTop();
		if(idAlert.length > 0)
		{
			var tmpID;
			var heightAlert;
			var topAlertDefault;
			var isVideo = false;
			var topVideo = 0;
			var stopVideo = false;
			for(var i = 0; i < idAlert.length; i++)
			{
				tmpID = idAlert[i];
				if(jQuery.trim(jQuery("#"+tmpID).css('position')) == 'absolute')
				{
					heightAlert = jQuery("#"+tmpID).height();
					if(heightAlert < heightBrowser)
					{
						topAlertDefault = Math.ceil(heightAlert / 2) * -1;
						topAlertDefault = getTopAlert(tmpID, topAlertDefault);
						jQuery("#"+tmpID).animate({marginTop: topAlertDefault + scrollTop}, animationTime, "linear");
					}
				}
			}
		}
	}
);

function resizeWindowPopup()
{
	if(jQuery.browser.msie && jQuery.browser.version == 6) return;
	var idAlert = getAlertID();
	var heightBrowser = jQuery(window).height();
	var scrollTop = jQuery(window).scrollTop();
	if(idAlert.length > 0)
	{
		var tmpID;
		var heightAlert;
		var topAlertDefault;
		var isVideo = false;
		var topVideo = 0;
		var stopVideo = false;
		for(var i = 0; i < idAlert.length; i++)
		{
			tmpID = idAlert[i];
			if(jQuery.trim(jQuery("#"+tmpID).css('visibility')) != 'visible' &&  jQuery.trim(jQuery("#"+tmpID).css('display')) != 'block')
				jQuery("#"+tmpID).css('visibility', 'hidden').css('display', 'block');;

			heightAlert = jQuery("#"+tmpID).height();
			if(heightAlert < heightBrowser)
			{
				jQuery("#"+tmpID).css('margin-top', Math.ceil(-0.5 * heightAlert) + 'px');
				jQuery("#"+tmpID).css('position', 'fixed');
			}else
			{
				jQuery("#"+tmpID).css('margin-top', (Math.ceil(-0.5 * heightAlert) + scrollTop) + 'px');
				jQuery("#"+tmpID).css('position', 'absolute');
			}
			if(jQuery.trim(jQuery("#"+tmpID).css('visibility')) != 'visible' &&  jQuery.trim(jQuery("#"+tmpID).css('display')) != 'block')
				jQuery("#"+tmpID).css('display', 'none').css('visibility', 'visible');
		}
	}
}

function getTopAlert(id, top)
{
	var heightPanel = jQuery("#"+id).height();
	var heightWin = jQuery(window).height();
	if(heightPanel >= heightWin) top = Math.ceil(heightWin * 0.5) * -1;
	else if(Math.floor(Math.abs((heightWin - heightPanel)) * 0.5) < Math.abs(top)) top = Math.floor((heightPanel * 0.5)) * -1;
	return top;
}

function setTopAlert(id)
{
	var top = Math.ceil(jQuery("#"+id).height() / 2) * -1;
	id = jQuery.trim(id);
	top = getTopAlert(id, top);
	var Y = jQuery(window).scrollTop();
	var pannello_y_default = top;
	document.getElementById(id).style.marginTop = pannello_y_default+'px';
	if(Y > 0)
	{
		var newTop = pannello_y_default + Y;
		pannello_y_default = newTop;
		document.getElementById(id).style.marginTop = pannello_y_default+'px';
	}
}

function getOpenAlertID()
{
	var alertID = new Array();
	if(jQuery.trim(jQuery("#info_societarie").css('position')) == 'absolute' && jQuery("#info_societarie").css('display') == 'block' || jQuery("#info_societarie").css('display') == 'inline') alertID.push("info_societarie");
	if(jQuery.trim(jQuery("#genericAlert").css('position')) == 'absolute' && jQuery("#genericAlert").css('display') == 'block' || jQuery("#genericAlert").css('display') == 'inline') alertID.push("genericAlert");
	return alertID;
}

function getAlertID()
{
	var alertID = new Array();
	alertID.push("info_societarie");
	alertID.push("genericAlert");
	return alertID;
}

//ALERT LOADER
function openGenericLoaderAlert(id, titolo, testo, btn)
{
	var btnText = "conferma";
	if(btn != null && btn != "undefined") btnText = btn;
	if(isAlertOpen) return;
	
	if(jQuery.trim(jQuery("#"+id).css('position')) == 'absolute')
		setTopAlert(id);
	
	if(testo != null && testo != "undefined")jQuery("#testo_alert_loader").html(testo);
	if(titolo != null && titolo != "undefined")jQuery("#titolo_alert_loader").html(titolo);
	
	jQuery('#layer_opacity').show();
	if(jQuery.trim(jQuery('#'+id).css('visibility')) != 'visible') jQuery('#'+id).css('visibility', 'visible');
	jQuery('#'+id).show();
	isAlertOpen = true;
}


