// JavaScript Document
var INITIAL_WIDTH_POPUP;

jQuery(document).ready(
	function()
	{
		INITIAL_WIDTH_POPUP = jQuery("#product-detail-popup").width();
		
		jQuery("#layer_opacity").click(
			function()
			{
				if(jQuery("#product-detail-popup").hasClass("openDetailPopup"))
					closeProductPopup();
			}
		);
		
		jQuery("#product-detail-popup").click(
			function(ev) { ev.stopImmediatePropagation(); }
		);
	}
);

function openProductPopup(productCode)
{
	jQuery("#layer_opacity").show();
	jQuery("#product-detail-popup").css('visibility', 'hidden').css('display', 'block');
	setTopAlertPopupProduct("product-detail-popup");
	jQuery("#product-detail-popup").width(10);
	jQuery("#product-detail-popup").css('margin-left', '-5px');
	jQuery(".product-detail-popup-expand").height(10);
	jQuery("#product-detail-popup").addClass("openDetailPopup");
	jQuery("#product-detail-popup").css('visibility', 'visible');
	expandProductPopup();
}

function expandProductPopup()
{
	var h = jQuery("#product-detail-popup").find(".product-detail-popup-container").height();
	jQuery("#product-detail-popup").animate({marginLeft: -(INITIAL_WIDTH_POPUP * 0.5) + 'px', width: INITIAL_WIDTH_POPUP + 'px'}, 300, "linear", function()
		{
			jQuery(".product-detail-popup-expand").animate({height: h + 'px'}, 300, "linear");
		});
}

function closeProductPopup()
{
	jQuery(".product-detail-popup-expand").animate({height: '10px'}, 300, "linear", function()
		{
			jQuery("#product-detail-popup").animate({marginLeft: '-5px', width: '10px'}, 300, "linear", function()
				{
					jQuery("#product-detail-popup").removeClass("openDetailPopup");
					jQuery("#product-detail-popup").hide();
					jQuery("#layer_opacity").hide();		
				});
		});
}

function getTopAlertProductPopup(id, top)
{
	var heightPanel = jQuery("#"+id).find(".product-detail-popup-container").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 setTopAlertPopupProduct(id)
{
	var top = Math.ceil(jQuery("#"+id).find(".product-detail-popup-container").height() / 2) * -1;
	id = jQuery.trim(id);
	top = getTopAlertProductPopup(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 loadFbComment(page, codeProduct)
{
	jQuery("#product-detail-popup-fb-content-box").html('<div class="attendere" style="height:'+jQuery("#product-detail-popup-fb-content-box").height()+'px"></div>');
	jQuery.ajax({
	    type: "POST",
	    url: "loadFbComment.php",
	    data: "p=" + page + "&code=" + codeProduct,
	    cache: false,
	    async: false,
	    success: function(html)
	    {
		 	jQuery("#product-detail-popup-fb-content-box").html(html);
	 	}
	 });
}
