var numGalleryImage;
var numGalleryVisibleImage = 5;
var widthGalleryImage;
var maxScrollGallery;

var currentIdEl = 0;
var currentMediaType = 0;
var currentFile = "";
var currentFileIPad = "";

var currentIdElAudio = 0;
var currentFileAudio = "";
var currentTitleAudio = "";

var MIN_WIDTH_LIGTHBOX = 640;
var MIN_HEIGHT_LIGTHBOX = 400;

var numThumbLightboxGallery = 0;
var widthLightboxGallery = 0;
var maxScrollLightboxGallery = 0;
var numVisibleThumb = 0;
var firstLightboxLoad = true;
var mediaClass = "";
var restartSlider = false;
var loadAudio = false;
var loadImage = false;
var firstLoadVideo = true;
var playVideo = false;
var intervalPlayVideo;

$(document).ready(function() 
{
	//prendo il numero nella gallery
	numGalleryImage = jQuery(".gallery_placeholder_block").children(".gallery_placeholder_box").children(".gallery_placeholder_element").length;
	maxScrollGallery = 0;
	//se non c'è la gallery nulla
	if(numGalleryImage == 0) return;
	
	widthGalleryImage = jQuery(".gallery_placeholder_block").children(".gallery_placeholder_box").children(".gallery_placeholder_element:eq(0)").width() + parseInt(jQuery(".gallery_placeholder_block").children(".gallery_placeholder_box").children(".gallery_placeholder_element:eq(0)").css('margin-right'));
	
	//if(numGalleryImage <= 5) jQuery(".gallery_placeholder_next").addClass('disabled');
	
	if(numGalleryImage >= 5)
	{
		maxScrollGallery = (numGalleryImage - numGalleryVisibleImage) * widthGalleryImage;
		jQuery(".gallery_placeholder_box").css('width', (numGalleryImage*widthGalleryImage)+'px');
	}
	
	$(".gallery_placeholder_element a.thumb_gallery, .gallery_placeholder_element p.gallery_audio_text").each(
		function()
		{
			$(this).hover(
				function() 
				{ 
					if(navigator.platform.indexOf("iPad") == -1)
					{
						jQuery(".gallery_placeholder_box a.thumb_gallery").addClass('disabled');
						jQuery("p.gallery_audio_text").addClass('disabled');
						jQuery(this).removeClass('disabled');
					}
				}, 
				function() 
				{  
					if(navigator.platform.indexOf("iPad") == -1)
					{
						jQuery(".gallery_placeholder_box a.thumb_gallery").removeClass('disabled');
						jQuery("p.gallery_audio_text").removeClass('disabled');
					}
				}
			);
		});
		
	$(".gallery_placeholder_element a.multimedia").each(
		function()
		{
			$(this).hover(
				function() 
				{ 
					if(navigator.platform.indexOf("iPad") == -1)
					{
						jQuery(".gallery_placeholder_box a.thumb_gallery").addClass('disabled');
						jQuery("p.gallery_audio_text").addClass('disabled');
						jQuery(this).parent('div').children(".thumb_gallery").removeClass('disabled');
						jQuery(this).parent('div').children(".gallery_audio_text").removeClass('disabled');
					}
				}, 
				function() 
				{  
					if(navigator.platform.indexOf("iPad") == -1)
					{
						jQuery("p.gallery_audio_text").removeClass('disabled');
						jQuery(".gallery_placeholder_box a.thumb_gallery").removeClass('disabled');
					}
				}
			);
		});
		
	$(".gallery_placeholder_next a, .gallery_placeholder_next span").each(
		function()
		{
			$(this).hover(
				function() 
				{ 
					if(navigator.platform.indexOf("iPad") == -1) jQuery('.gallery_seemore').css('color', '#999999');
				}, 
				function() 
				{  
					if(navigator.platform.indexOf("iPad") == -1) jQuery('.gallery_seemore').css('color', '#FFFFFF');
				}
			);
		});
	
	$(".gallery_placeholder_next span").click( function() { loadFirstLightbox(); } );
	$(".header_close span, #layer_opacity").click( function() { closeGalleryLightbox(); } );
	$("#first_frame_video a").click( function() { restartPlayerVideo(); } );
	
	$(".gallery_ligthbox_container_gallery_element .lightbox_thumb_disabled").each(
		function()
		{
			$(this).hover(
				function() 
				{ 
					if(jQuery(this).parent().attr('idel') == currentIdEl) return;
					jQuery(this).hide();
				}, 
				function() { }
			);
		});
	
	$(".gallery_ligthbox_container_gallery_element a.multimedia_lightbox").each(
		function()
		{
			$(this).hover(
				function() 
				{ 
					if(jQuery(this).parent().attr('idel') == currentIdEl) return;
					jQuery(this).parent().children('.lightbox_thumb_disabled').hide();
				}, 
				function() { }
			);
		});
	
	$(".gallery_ligthbox_container_gallery_element a.thumb_lightbox, .gallery_ligthbox_container_gallery_element p.gallery_audio_text, #layer_opacity").each(
		function()
		{
			$(this).hover(
				function() { }, 
				function() 
				{  
					jQuery(".gallery_ligthbox_container_gallery_element").children('.lightbox_thumb_disabled').show();
					jQuery(".gallery_ligthbox_container_gallery_element[idel="+currentIdEl+"]").children('.lightbox_thumb_disabled').hide();
				}
			);
		});
	
	$(".gallery_placeholder_element .thumb_gallery, .lightbox_thumb_disabled, .gallery_ligthbox_container_gallery_element a.thumb_lightbox, .gallery_ligthbox_container_gallery_element p.gallery_audio_text").each(
		function()
		{
			$(this).click(
				function() 
				{ 
					var currentIdElTmp = jQuery(this).parent('div').attr('idel');
					loadLightbox(currentIdElTmp);
				}
			);
		});
		
	$(".multimedia, .multimedia_lightbox").each(
		function()
		{
			$(this).click(
				function() 
				{ 
					var currentIdElTmp = jQuery(this).parent('div').attr('idel');
					loadLightbox(currentIdElTmp);
				}
			);
		});
	
	jQuery(window).bind("resize", resizeWindow);
});

function resizeWindow( e ) 
{
	if(jQuery("#gallery_ligthbox_container").length == 0 || navigator.platform.indexOf("iPad") != -1) return;
	if(jQuery.trim(jQuery("#gallery_ligthbox_container").css('display')) != "none")
	{
		var widthGallery = jQuery("#gallery_ligthbox_container").width();
		var heightGallery = jQuery("#gallery_ligthbox_container").height();
		var heightScreen = jQuery(window).height();
		var widthScreen = jQuery("#maincontainer").width();
		
		var newLeft = Math.ceil((widthScreen - widthGallery) / 2) + parseInt(jQuery("#maincontainer").offset().left);
		var newTop = Math.ceil((heightScreen - heightGallery) * 0.5);
		if(newTop < 0) newTop = 20;
		newTop = newTop + jQuery(window).scrollTop();	
		jQuery("#gallery_ligthbox_container").css('left', newLeft+"px");
		jQuery("#gallery_ligthbox_container").css('top', newTop+"px");
	}
}

function loadFirstLightbox()
{
	var currentIdElTmp = parseInt(jQuery(".gallery_ligthbox_container_gallery_element:eq(0)").attr('idel'));
	loadLightbox(currentIdElTmp);
}

function getFirstLightboxImage()
{
	var index = 0;
	var idEl = 0;
	var found = false;
	while(index < jQuery(".gallery_ligthbox_container_gallery_element").length && !found)
	{
		if(jQuery(".gallery_ligthbox_container_gallery_element:eq("+index+")").children(".multimedia_lightbox").length == 0)
		{
			idEl = jQuery(".gallery_ligthbox_container_gallery_element:eq("+index+")").attr('idel');
			found = true;	
		}
		index = index + 1;	
	}
	return idEl;
}

function getLastLightboxImage()
{
	var index = jQuery(".gallery_ligthbox_container_gallery_element").length - 1; 
	var idEl = 0;
	var found = false;
	while(index >= 0 && !found)
	{
		if(jQuery(".gallery_ligthbox_container_gallery_element:eq("+index+")").children(".multimedia_lightbox").length == 0)
		{
			idEl = jQuery(".gallery_ligthbox_container_gallery_element:eq("+index+")").attr('idel');
			found = true;	
		}
		index = index - 1;	
	}
	return idEl;
}

function getOtherLightboxImage(idIm, isNext)
{
	var index = 0;
	var idEl = -1;
	var found = false;
	if(isNext)
	{
		index = 0;
		while(index < jQuery(".gallery_ligthbox_container_gallery_element").length && !found)
		{
			if(jQuery(".gallery_ligthbox_container_gallery_element:eq("+index+")").children(".multimedia_lightbox").length == 0)
			{
				if(parseInt(jQuery(".gallery_ligthbox_container_gallery_element:eq("+index+")").attr('idel')) > idIm)
				{
					idEl = parseInt(jQuery(".gallery_ligthbox_container_gallery_element:eq("+(index)+")").attr('idel'))
					found = true;
				}
			}
			index = index + 1;	
		}
	}else
	{
		index = jQuery(".gallery_ligthbox_container_gallery_element").length - 1;
		while(index >= 0 && !found)
		{
			if(jQuery(".gallery_ligthbox_container_gallery_element:eq("+index+")").children(".multimedia_lightbox").length == 0)
			{
				if(parseInt(jQuery(".gallery_ligthbox_container_gallery_element:eq("+index+")").attr('idel')) < idIm)
				{
					idEl = parseInt(jQuery(".gallery_ligthbox_container_gallery_element:eq("+(index)+")").attr('idel'))
					found = true;	
				}
			}
			index = index - 1;	
		}
	}
	if(idEl == -1 && isNext) idEl = getFirstLightboxImage();
	if(idEl == -1 && !isNext) idEl = getLastLightboxImage();
	return idEl;
}

function changeLitghboxImageToClick(isNext)
{
	var currentIdElTmp = getOtherLightboxImage(currentIdEl, isNext);
	if(currentIdElTmp == -1) return;
	loadLightbox(currentIdElTmp);
}


function loadLightbox(idEl)
{
	if(parseInt(jQuery(".gallery_ligthbox_container_gallery_element[idel="+idEl+"]").attr('mediatype')) == 2)
	{
		currentIdElAudio = idEl;
		currentFileAudio = jQuery(".gallery_ligthbox_container_gallery_element[idel="+idEl+"]").attr('file');
		currentMediaType = parseInt(jQuery(".gallery_ligthbox_container_gallery_element[idel="+currentIdElAudio+"]").attr('mediatype'));
		currentTitleAudio = jQuery(".gallery_ligthbox_container_gallery_element[idel="+currentIdElAudio+"]").children(".audio").attr('title');
		
		loadAudio = true;
		if(firstLightboxLoad)
		{
			currentIdEl = getFirstLightboxImage();
			currentFile = jQuery(".gallery_ligthbox_container_gallery_element[idel="+currentIdEl+"]").attr('file');
			currentMediaType = parseInt(jQuery(".gallery_ligthbox_container_gallery_element[idel="+currentIdEl+"]").attr('mediatype'));
			
			firstLightboxLoad = false;
			jQuery("#layer_opacity").show();
			jQuery("#gallery_ligthbox_container").css('left', parseInt(jQuery("#maincontainer").offset().left)+"px");
			jQuery("#gallery_ligthbox_container").css('width', '960px');
			jQuery("#gallery_ligthbox_container").css('height', '512px');
			var heightScreen = jQuery(window).height();
			var newTop = Math.ceil((heightScreen - 512) * 0.5);
			if(newTop < 0) newTop = 20;
			newTop = newTop + jQuery(window).scrollTop();
			var newLeft = Math.ceil((jQuery("#maincontainer").width() - 960) * 0.5) + parseInt(jQuery("#maincontainer").offset().left);
			jQuery("#gallery_ligthbox_container").css('top', newTop+'px');
			jQuery("#gallery_ligthbox_container").css('left', newLeft+'px');
			jQuery(".gallery_ligthbox_container_multimedia").css('height', '400px');
			jQuery(".gallery_image_caption").hide();
			resetWidthTitle();
			
			jQuery(".gallery_ligthbox_container_multimedia").children(".loading_multimedia").show();
			jQuery(".gallery_ligthbox_container_multimedia").children("img").css('width', 'auto');
			jQuery(".gallery_ligthbox_container_multimedia").children("img").css('height', 'auto');
			jQuery(".gallery_ligthbox_container_multimedia").children("img").css('margin-left', '0px');
			jQuery(".gallery_ligthbox_container_multimedia").children("img").css('margin-top', '0px');
			
			jQuery(".gallery_lightbox_audio").hide();
			jQuery(".gallery_ligthbox_container_multimedia").children(".gallery_lightbox_video").hide();
			jQuery(".gallery_ligthbox_container_multimedia").children("img").hide();
			jQuery(".gallery_ligthbox_container_multimedia").children(".prev_lightbox_image").hide();
			jQuery(".gallery_ligthbox_container_multimedia").children(".next_lightbox_image").hide();	
			
			jQuery("#gallery_ligthbox_container").css('visibility', 'hidden');
			jQuery("#gallery_ligthbox_container").css('display', 'block');
		
			numThumbLightboxGallery = jQuery(".gallery_ligthbox_container_gallery_scroll").children('.gallery_ligthbox_container_gallery_element').length;
			widthLightboxGallery = jQuery(".gallery_ligthbox_container_gallery_scroll").children('.gallery_ligthbox_container_gallery_element:eq(0)').width();
			
			jQuery(".gallery_ligthbox_container_gallery_scroll").css('width', (numThumbLightboxGallery * widthLightboxGallery)+'px');
			
			jQuery("#gallery_ligthbox_container").css('visibility', 'visible');
			
			jQuery(".gallery_ligthbox_container_gallery").hide();
			jQuery(".freccia_lightbox").hide();
			jQuery(".freccia_lightbox_right").hide();
			jQuery(".gallery_ligthbox_container_gallery_element").children(".lightbox_thumb_disabled").show();
			
			var newLeftSelected = parseInt(jQuery(".gallery_ligthbox_container_gallery_element[idel="+currentIdEl+"]").offset().left) - parseInt(jQuery(".gallery_ligthbox_container_gallery").offset().left);
			jQuery(".lightbox_thumb_selected").css('left', newLeftSelected+"px");
			jQuery(".lightbox_thumb_selected").show();
			loadImage = false;
			jQuery("#gallery_lightbox_zoom").attr('src', ''); 
			jQuery(".gallery_ligthbox_container_gallery_element[idel="+currentIdEl+"]").children(".lightbox_thumb_disabled").hide();
			
			mediaClass = "";	
			if(jQuery(".gallery_ligthbox_container_gallery_element[idel="+currentIdEl+"]").children("a").attr("title") != null
				&& jQuery(".gallery_ligthbox_container_gallery_element[idel="+currentIdEl+"]").children("a").attr("title") != "undefined"
				&& jQuery.trim(jQuery(".gallery_ligthbox_container_gallery_element[idel="+currentIdEl+"]").children("a").attr("title")) != '')	
				jQuery(".gallery_image_caption").children("p").html(jQuery(".gallery_ligthbox_container_gallery_element[idel="+currentIdEl+"]").children("a").attr("title"));
				
			jQuery("#gallery_lightbox_zoom").attr('src', currentFile); 
			if(!loadImage)
			{
				jQuery("#gallery_lightbox_zoom").load(function ()
				{
					loadImage = true;	
					setTimeout('lightboxZoomLoadComplete()', 200);
				});
			}
		}else
		{
			jQuery(".gallery_ligthbox_container_multimedia").children(".gallery_lightbox_audio").html('<div id="gallery_lightbox_audio_box"><a href="http://www.adobe.com/go/getflashplayer"><img src="/asset/img/get_flash_player.gif" alt="Get Adobe Flash player" border="0" /></a></div>');
			//AUDIO
			if(jQuery.trim(jQuery(".gallery_lightbox_audio").css('display')) != "none")
			{
				loadAudioFile();
			}else
			{
				loadAudioFile();
				jQuery(".gallery_lightbox_audio").show();
				var newHeight = jQuery("#gallery_ligthbox_container").height() + jQuery(".gallery_lightbox_audio").height();
				var heightScreen = jQuery(window).height();
				
				var newTop = Math.ceil((heightScreen - newHeight) * 0.5);
				if(newTop < 0) newTop = 20;
				newTop = newTop + jQuery(window).scrollTop();
				
				jQuery("#gallery_ligthbox_container").css('height', newHeight+"px");
				jQuery("#gallery_ligthbox_container").css('top', newTop+"px");
			}
		}
	}else
	{
		currentIdElAudio = 0;
		currentFileAudio = "";
		currentFileIPad = "";
		
		currentIdEl = idEl;
		currentMediaType = parseInt(jQuery(".gallery_ligthbox_container_gallery_element[idel="+currentIdEl+"]").attr('mediatype'));
		currentFile = jQuery(".gallery_ligthbox_container_gallery_element[idel="+currentIdEl+"]").attr('file');
		if(jQuery(".gallery_ligthbox_container_gallery_element[idel="+currentIdEl+"]").attr('fileIPad') != null && jQuery(".gallery_ligthbox_container_gallery_element[idel="+currentIdEl+"]").attr('fileIPad') != "undefined")
			currentFileIPad = jQuery(".gallery_ligthbox_container_gallery_element[idel="+currentIdEl+"]").attr('fileIPad');
		if(firstLightboxLoad)
		{
			jQuery("#layer_opacity").show();
			jQuery("#gallery_ligthbox_container").css('width', '960px');
			jQuery("#gallery_ligthbox_container").css('height', '512px');
			jQuery("#gallery_ligthbox_container").css('z-index', '10000');
			var heightScreen = jQuery(window).height();
			var newTop = Math.ceil((heightScreen - 512) * 0.5);
			if(newTop < 0) newTop = 20;
			newTop = newTop + jQuery(window).scrollTop();
			var newLeft = Math.ceil((jQuery("#maincontainer").width() - 960) * 0.5) + parseInt(jQuery("#maincontainer").offset().left);
			jQuery("#gallery_ligthbox_container").css('top', newTop+'px');
			jQuery("#gallery_ligthbox_container").css('left', newLeft+'px');
			jQuery(".gallery_ligthbox_container_multimedia").css('height', '400px');
			jQuery(".gallery_image_caption").hide();
			resetWidthTitle();
		}
		
		if(navigator.platform.indexOf("iPad") != -1)
		{
			jQuery(".gallery_ligthbox_container_multimedia").children(".gallery_lightbox_video").html('');
		}else
		{
			jQuery(".gallery_ligthbox_container_multimedia").children(".gallery_lightbox_video").html('<div id="gallery_lightbox_video_box"><a href="http://www.adobe.com/go/getflashplayer"><img src="/asset/img/get_flash_player.gif" alt="Get Adobe Flash player" border="0" /></a></div>');
		}
		
		jQuery(".gallery_ligthbox_container_multimedia").children(".loading_multimedia").show();
		jQuery(".gallery_ligthbox_container_multimedia").children("img").css('width', 'auto');
		jQuery(".gallery_ligthbox_container_multimedia").children("img").css('height', 'auto');
		jQuery(".gallery_ligthbox_container_multimedia").children("img").css('margin-left', '0px');
		jQuery(".gallery_ligthbox_container_multimedia").children("img").css('margin-top', '0px');
		
		if(navigator.platform.indexOf("iPad") == -1) jQuery("#first_frame_video").hide();
	
		if(!loadAudio) jQuery(".gallery_lightbox_audio").hide();
		jQuery(".gallery_ligthbox_container_multimedia").children(".gallery_lightbox_video").hide();
		jQuery(".gallery_ligthbox_container_multimedia").children("img").hide();
		jQuery(".gallery_ligthbox_container_multimedia").children(".prev_lightbox_image").hide();
		jQuery(".gallery_ligthbox_container_multimedia").children(".next_lightbox_image").hide();
			
		jQuery("#gallery_ligthbox_container").css('visibility', 'hidden');
		jQuery("#gallery_ligthbox_container").css('display', 'block');
	
		numThumbLightboxGallery = jQuery(".gallery_ligthbox_container_gallery_scroll").children('.gallery_ligthbox_container_gallery_element').length;
		widthLightboxGallery = jQuery(".gallery_ligthbox_container_gallery_scroll").children('.gallery_ligthbox_container_gallery_element:eq(0)').width();
		
		jQuery(".gallery_ligthbox_container_gallery_scroll").css('width', (numThumbLightboxGallery * widthLightboxGallery)+'px');
		
		jQuery("#gallery_ligthbox_container").css('visibility', 'visible');
		
		jQuery(".gallery_ligthbox_container_gallery_element").children(".lightbox_thumb_disabled").show();
		
		var newLeftSelected = parseInt(jQuery(".gallery_ligthbox_container_gallery_element[idel="+currentIdEl+"]").offset().left) - parseInt(jQuery(".gallery_ligthbox_container_gallery").offset().left);
		jQuery(".lightbox_thumb_selected").css('left', newLeftSelected+"px");
		jQuery(".lightbox_thumb_selected").show();
		jQuery("#gallery_lightbox_zoom").attr('src', ''); 
		jQuery(".gallery_ligthbox_container_gallery_element[idel="+currentIdEl+"]").children(".lightbox_thumb_disabled").hide();
		
		if(firstLightboxLoad)
		{
			firstLightboxLoad = false;
			jQuery(".gallery_ligthbox_container_gallery").hide();
			jQuery(".freccia_lightbox").hide();
			jQuery(".freccia_lightbox_right").hide();	
		}
		
		if(currentMediaType == 0)
		{
			firstLoadVideo = true;
			mediaClass = "";
			
			if(jQuery(".gallery_ligthbox_container_gallery_element[idel="+currentIdEl+"]").children("a").attr("title") != null
				&& jQuery(".gallery_ligthbox_container_gallery_element[idel="+currentIdEl+"]").children("a").attr("title") != "undefined"
				&& jQuery.trim(jQuery(".gallery_ligthbox_container_gallery_element[idel="+currentIdEl+"]").children("a").attr("title")) != '')
				jQuery(".gallery_image_caption").children("p").html(jQuery(".gallery_ligthbox_container_gallery_element[idel="+currentIdEl+"]").children("a").attr("title"));
							
			//jQuery("#gallery_lightbox_zoom").attr('src', currentFile); 
			
			
			
			
			//alert("1 " + currentFile);
			if(!loadImage)
			{
				//alert("2" + currentFile);
				jQuery("#gallery_lightbox_zoom").load(function ()
				{
					//alert("3" + currentFile);
					loadImage = true;
					setTimeout('lightboxZoomLoadComplete()', 200);
				});
			}
			
				
			
				jQuery("#gallery_lightbox_zoom").attr('src', currentFile); 
			
		}else if(currentMediaType == 1)
		{
			if(navigator.platform.indexOf("iPad") == -1) jQuery("#first_frame_video").children("a#preview_video").children("img").attr('src', jQuery(".gallery_ligthbox_container_gallery_element[idel="+currentIdEl+"]").attr('filepreview'));
			
			if(jQuery(".gallery_ligthbox_container_gallery_element[idel="+currentIdEl+"]").children("a").attr("title") != null
				&& jQuery(".gallery_ligthbox_container_gallery_element[idel="+currentIdEl+"]").children("a").attr("title") != "undefined"
				&& jQuery.trim(jQuery(".gallery_ligthbox_container_gallery_element[idel="+currentIdEl+"]").children("a").attr("title")) != '')
				jQuery(".gallery_image_caption").children("p").html(jQuery(".gallery_ligthbox_container_gallery_element[idel="+currentIdEl+"]").children("a").attr("title"));
			mediaClass = "gallery_lightbox_video";
			setTimeout('lightboxLoadMedia()', 200);
		}
		
		
	
		
	}
}

function stopMediaElement(type) // 0: audio, 1: video
{
	if(type == 0 && jQuery("#flash_gallery_video_swf").length > 0)
	{
		getFlashMovie("flash_gallery_video_swf").pauseMediaElementToJS();
	}else if(type == 1 && jQuery(".gallery_lightbox_audio").css('display') != "none" && jQuery("#flash_gallery_audio_swf").length > 0)
	{
		getFlashMovie("flash_gallery_audio_swf").pauseMediaElementToJS();
	}
}

function restartPlayerVideo()
{
	
	//jQuery("#first_frame_video").hide();
	//jQuery(".gallery_lightbox_video").css('visibility', 'visible');
	//jQuery(".gallery_lightbox_video").show();
	jQuery("#first_frame_video").css({ zIndex : -1 });
	jQuery(".gallery_lightbox_video").css({ zIndex : 100 });
	
	playVideo = true;
	setTimeout('startIntervalVideo()', 500);
}

function startIntervalVideo()
{
	getFlashMovie("flash_gallery_video_swf").startMediaElementToJS();
}

function changeStatusVideo()
{
	if(playVideo) { playVideo = false; return; }
	jQuery("#first_frame_video").show();
	jQuery(".gallery_lightbox_video").hide();
}

function getFlashMovie(movieName) {
  var isIE = navigator.appName.indexOf("Microsoft") != -1;
  return (isIE) ? window[movieName] : document[movieName];
}

function loadAudioFile()
{
	jQuery(".gallery_lightbox_audio").html('<div id="gallery_lightbox_audio_box"><a href="http://www.adobe.com/go/getflashplayer"><img src="/asset/img/get_flash_player.gif" alt="Get Adobe Flash player" border="0" /></a></div>');
	jQuery(".gallery_lightbox_audio").css('width', jQuery("#gallery_ligthbox_container").width()+"px");
	if(navigator.platform.indexOf("iPad") != -1)
	{
		setTimeout('loadAudioIPad()', 300);
	}else
	{
		if (swfobject.hasFlashPlayerVersion("10.0.0")) 
		{
			var flashvars = {};
			flashvars.autoPlay = "0";
			flashvars.audio = currentFileAudio;
			var params = {};
			params.menu = "false";
			params.quality = "high";
			params.wmode = "transparent";
			params.align = "top";
			params.allowscriptaccess = "sameDomain";
			var attributes = {};
			attributes.id = "flash_gallery_audio_swf";
			attributes.name = "flash_gallery_audio_swf";
			swfobject.embedSWF("/asset/swf/AdactoVideoPlayer.swf", "gallery_lightbox_audio_box", "600", "40", "10.0.0", false, flashvars, params, attributes);
		} else {
			jQuery("#gallery_lightbox_audio_box").children("a").css("visibility", "visible");
		}	
		appendAudioTitle();	
	}
}

function appendAudioTitle()
{
	if(jQuery(".gallery_lightbox_audio").children('span').length == 0)
		jQuery(".gallery_lightbox_audio").append("<span class=\"thin\">"+currentTitleAudio+"</span>");

	var widthAudioBox = jQuery(".gallery_lightbox_audio").width();
	var newLeft = Math.ceil((widthAudioBox - 600) / 2);
	if(newLeft < 0) newLeft = 0;
	newLeft = newLeft + 55;
	jQuery(".gallery_lightbox_audio").children('span').css('left', newLeft+"px");
}

function loadAudioIPad()
{
	jQuery(".gallery_lightbox_audio").html('<audio style="margin-top:15px;" id="H5Audio" src="' + currentFileAudio + '" type="audio/mp3" width=600 height=40 controls="controls" />');
}

function moveGallery(isNext)
{
	var currentLeft = parseInt(jQuery(".gallery_placeholder_box").css('left'));
	if(isNext)
	{
		currentLeft = currentLeft - widthGalleryImage;
		if(Math.abs(currentLeft) >= maxScrollGallery)
		{
			jQuery(".gallery_placeholder_prev").children("a").addClass('disabled');
			jQuery(".gallery_placeholder_prev").children("a").attr('href', 'javascript:void(0)');
			currentLeft = 0;
		}else 
		{
			jQuery(".gallery_placeholder_prev").children("a").removeClass('disabled');
			jQuery(".gallery_placeholder_prev").children("a").attr('href', 'javascript:moveGallery(false)');
		}
	}else
	{
		currentLeft = currentLeft + widthGalleryImage;
		if(currentLeft == 0)
		{
			jQuery(".gallery_placeholder_prev").children("a").addClass('disabled');
			jQuery(".gallery_placeholder_prev").children("a").attr('href', 'javascript:void(0)');
		}
		
		jQuery(".gallery_placeholder_next").children("a").removeClass('disabled');
		jQuery(".gallery_placeholder_next").children("a").attr('href', 'javascript:moveGallery(true)');
	}
	jQuery(".gallery_placeholder_box").animate({left: currentLeft}, 200, "linear");
}

function moveLightboxSlider(isNext)
{
	var newLeftSelected = parseInt(jQuery(".lightbox_thumb_selected").css('left'));
	var currentLeft = parseInt(jQuery(".gallery_ligthbox_container_gallery_scroll").css('left'));
	restartSlider = false;
	if(isNext)
	{
		currentLeft = currentLeft - widthLightboxGallery;
		newLeftSelected = newLeftSelected - widthLightboxGallery;
		if(Math.abs(currentLeft) > maxScrollLightboxGallery)
		{
			jQuery(".freccia_lightbox").addClass('disabled');
			jQuery(".freccia_lightbox").attr('href', 'javascript:void(0)');
			currentLeft = 0;
			restartSlider = true;
		}else 
		{
			jQuery(".freccia_lightbox").removeClass('disabled');
			jQuery(".freccia_lightbox").attr('href', 'javascript:moveLightboxSlider(false)');
		}
	}else
	{
		currentLeft = currentLeft + widthLightboxGallery;
		newLeftSelected = newLeftSelected + widthLightboxGallery;
		if(currentLeft == 0)
		{
			jQuery(".freccia_lightbox").addClass('disabled');
			jQuery(".freccia_lightbox").attr('href', 'javascript:void(0)');
		}
		
		jQuery(".freccia_lightbox_right").removeClass('disabled');
		jQuery(".freccia_lightbox_right").attr('href', 'javascript:moveLightboxSlider(true)');
	}
	if(!restartSlider) jQuery(".lightbox_thumb_selected").animate({left: newLeftSelected}, 200, "linear");
	jQuery(".gallery_ligthbox_container_gallery_scroll").animate({left: currentLeft}, 200, "linear", function() 
		{
			if(restartSlider) 
			{
				newLeftSelected = parseInt(jQuery(".gallery_ligthbox_container_gallery_element[idel="+currentIdEl+"]").offset().left) - parseInt(jQuery(".gallery_ligthbox_container_gallery").offset().left);
				jQuery(".lightbox_thumb_selected").css('left', newLeftSelected+'px');
			}
		});
}

function resetAudioFile()
{
	jQuery(".gallery_lightbox_audio").css('width', jQuery("#gallery_ligthbox_container").width()+"px");	
	appendAudioTitle();
}

function lightboxLoadMedia()
{
	if(currentMediaType != 1) return;
	jQuery(".gallery_ligthbox_container_multimedia").children("."+mediaClass).css('visibility', 'hidden');
	jQuery(".gallery_ligthbox_container_multimedia").children("."+mediaClass).css('display', 'block');
	var widthImage = jQuery(".gallery_ligthbox_container_multimedia").children("."+mediaClass).width();
	var heightImage = jQuery(".gallery_ligthbox_container_multimedia").children("."+mediaClass).height();
	
	if(jQuery(".gallery_ligthbox_container_gallery_element[idel="+currentIdEl+"]").children("a").attr("title") != null
		&& jQuery(".gallery_ligthbox_container_gallery_element[idel="+currentIdEl+"]").children("a").attr("title") != "undefined"
		&& jQuery.trim(jQuery(".gallery_ligthbox_container_gallery_element[idel="+currentIdEl+"]").children("a").attr("title")) != '')
		jQuery(".gallery_image_caption").show();
	
	if(loadAudio && jQuery(".gallery_lightbox_audio").css('display') == "none")
	{
		loadAudioFile();
		jQuery(".gallery_lightbox_audio").show();
	}
	
	var newHeight = jQuery(".gallery_ligthbox_container_header").height() + heightImage;
	if(jQuery(".gallery_ligthbox_container_gallery_element[idel="+currentIdEl+"]").children("a").attr("title") != null
		&& jQuery(".gallery_ligthbox_container_gallery_element[idel="+currentIdEl+"]").children("a").attr("title") != "undefined"
		&& jQuery.trim(jQuery(".gallery_ligthbox_container_gallery_element[idel="+currentIdEl+"]").children("a").attr("title")) != '')
		newHeight = newHeight + jQuery(".gallery_image_caption").height();
		
	if(numGalleryImage == 1)
	{
		jQuery(".gallery_ligthbox_container_gallery").hide();
		jQuery(".freccia_lightbox").hide();
		jQuery(".freccia_lightbox_right").hide();
	}else
	{
		jQuery(".gallery_ligthbox_container_gallery").show();
		jQuery(".freccia_lightbox").show();
		jQuery(".freccia_lightbox_right").show();
		newHeight = newHeight + jQuery(".gallery_ligthbox_container_gallery").height() + jQuery(".freccia_lightbox").height() + parseInt(jQuery(".freccia_lightbox").css('padding-top')) + parseInt(jQuery(".freccia_lightbox").css('margin-top')) + parseInt(jQuery(".freccia_lightbox").css('padding-bottom'));
	}
	
	if(jQuery.trim(jQuery(".gallery_lightbox_audio").css('display')) != "none")
		newHeight = newHeight + jQuery(".gallery_lightbox_audio").height();
		
	var heightScreen = jQuery(window).height();
	var widthScreen = jQuery("#maincontainer").width();
	
	var newLeft = Math.ceil((widthScreen - widthImage) / 2) + parseInt(jQuery("#maincontainer").offset().left);
	var newTop = Math.ceil((heightScreen - newHeight) * 0.5);
	if(newTop < 0) newTop = 20;
	newTop = newTop + jQuery(window).scrollTop();
	
	jQuery(".gallery_ligthbox_container_multimedia").children(".gallery_lightbox_video").html('<div id="gallery_lightbox_video_box"><a href="http://www.adobe.com/go/getflashplayer"><img src="/asset/img/get_flash_player.gif" alt="Get Adobe Flash player" border="0" /></a></div>');
	if(navigator.platform.indexOf("iPad") != -1)
	{
		jQuery(".gallery_ligthbox_container_multimedia").children(".gallery_lightbox_video").html('<video id="H5Video" src="' + currentFileIPad + '" type="video/mp4" width=960 height=540 controls="controls" />');
	}else
	{
		if (swfobject.hasFlashPlayerVersion("10.0.0")) 
		{
			var flashvars = {};
			flashvars.autoPlay = "0";
			flashvars.video = currentFile;
			var params = {};
			params.menu = "false";
			params.quality = "high";
			params.wmode = "transparent";
			params.align = "top";
			params.allowscriptaccess = "sameDomain";
			params.allowfullscreen = "true";
			var attributes = {};
			attributes.id = "flash_gallery_video_swf";
			attributes.name = "flash_gallery_video_swf";
			swfobject.embedSWF("/asset/swf/AdactoVideoPlayer.swf", "gallery_lightbox_video_box", "960", "540", "10.0.0", false, flashvars, params, attributes);
		} else {
			jQuery("#gallery_lightbox_video_box").children("a").css("visibility", "visible");
		}
	}
	
	jQuery(".gallery_ligthbox_container_multimedia").css('height', heightImage+"px");
	jQuery("#gallery_ligthbox_container").animate({height: newHeight+"px", width: widthImage+"px", top: newTop+"px", left:newLeft+"px"}, 250, "linear", function()
	 {
		resetWidthTitle();
		checkLightboxSlider();
		jQuery(".gallery_ligthbox_container_multimedia").children(".loading_multimedia").hide();
		jQuery(".gallery_ligthbox_container_multimedia").children("."+mediaClass).css('visibility', 'visible');
		if(jQuery(".gallery_lightbox_audio").css('display') != "none") resetAudioFile();
	});
}

function lightboxZoomLoadComplete()
{
	
	//alert("1");
	if(currentMediaType != 0) return;
	var widthScreen = jQuery("#maincontainer").width();
	
	jQuery(".gallery_ligthbox_container_multimedia").children("img").css('visibility', 'hidden');
	jQuery(".gallery_ligthbox_container_multimedia").children("img").css('display', 'block');
	
	//alert("2");
	var widthImage = jQuery(".gallery_ligthbox_container_multimedia").children("img").width();
	var heightImage = jQuery(".gallery_ligthbox_container_multimedia").children("img").height();
	if(widthImage > widthScreen)
	{
		heightImage = Math.floor((widthScreen * heightImage) / widthImage);
		widthImage = widthScreen;
		jQuery(".gallery_ligthbox_container_multimedia").children("img").css("width", widthImage+"px");
		jQuery(".gallery_ligthbox_container_multimedia").children("img").css("height", heightImage+"px");
	}
	
	
	//alert("3");
	if(numGalleryImage > 1)
	{
		jQuery(".gallery_ligthbox_container_multimedia").children(".prev_lightbox_image").show();
		jQuery(".gallery_ligthbox_container_multimedia").children(".next_lightbox_image").show();	
	}
	
	
	//alert("4");
	if(jQuery(".gallery_ligthbox_container_gallery_element[idel="+currentIdEl+"]").children("a").attr("title") != null
		&& jQuery(".gallery_ligthbox_container_gallery_element[idel="+currentIdEl+"]").children("a").attr("title") != "undefined"
		&& jQuery.trim(jQuery(".gallery_ligthbox_container_gallery_element[idel="+currentIdEl+"]").children("a").attr("title")) != '')
		jQuery(".gallery_image_caption").show();
	else
		jQuery(".gallery_image_caption").hide();
	
	//alert("5");	
	if(loadAudio && jQuery(".gallery_lightbox_audio").css('display') == "none")
	{
		loadAudioFile();
		jQuery(".gallery_lightbox_audio").show();
	}
	
	var newWidthImage = widthImage;
	var newHeightImage = heightImage;
	
	//alert("6");
	
	if(widthImage < MIN_WIDTH_LIGTHBOX) newWidthImage = MIN_WIDTH_LIGTHBOX;
	if(heightImage < MIN_HEIGHT_LIGTHBOX) newHeightImage = MIN_HEIGHT_LIGTHBOX;
	
	if(numGalleryImage > 1)
	{
		numVisibleThumb = Math.ceil(newWidthImage / widthLightboxGallery);
		newWidthImage = numVisibleThumb * widthLightboxGallery;
	}
	
	//alert("7");
	
	if(widthImage < newWidthImage) 
	{
		jQuery(".gallery_ligthbox_container_multimedia").children("img").css('margin-left', ((newWidthImage-widthImage) / 2)+'px');
		widthImage = newWidthImage;
	}
	
	//alert("8");
	
	if(heightImage < newHeightImage) 
	{
		jQuery(".gallery_ligthbox_container_multimedia").children("img").css('margin-top', ((newHeightImage-heightImage) / 2)+'px');
		heightImage = newHeightImage;
	}
	
	
	
	var newHeight = jQuery(".gallery_ligthbox_container_header").height() + heightImage;
	
	
	
	
	if(jQuery(".gallery_ligthbox_container_gallery_element[idel="+currentIdEl+"]").children("a").attr("title") != null
		&& jQuery(".gallery_ligthbox_container_gallery_element[idel="+currentIdEl+"]").children("a").attr("title") != "undefined"
		&& jQuery.trim(jQuery(".gallery_ligthbox_container_gallery_element[idel="+currentIdEl+"]").children("a").attr("title")) != '')
		newHeight = newHeight + jQuery(".gallery_image_caption").height();
	
	
	
	if(numGalleryImage == 1)
	{
		jQuery(".gallery_ligthbox_container_gallery").hide();
		jQuery(".freccia_lightbox").hide();
		jQuery(".freccia_lightbox_right").hide();
	}else
	{
		jQuery(".gallery_ligthbox_container_gallery").show();
		jQuery(".freccia_lightbox").show();
		jQuery(".freccia_lightbox_right").show();
		
			
		//alert("freccia " + jQuery(".freccia_lightbox").height() );	
		
		//alert($(".prev_lightbox_image").height());
		
		//jQuery(".freccia_lightbox").height() + 
		
		/*
		newHeight = newHeight + jQuery(".gallery_ligthbox_container_gallery").height() + 
			jQuery(".freccia_lightbox").height() +
			parseInt(jQuery(".freccia_lightbox").css('padding-top')) + 
			parseInt(jQuery(".freccia_lightbox").css('margin-top')) + 
			parseInt(jQuery(".freccia_lightbox").css('padding-bottom'));
		*/	
			
	
			
		
		
	}
	
	
	
	if(jQuery.trim(jQuery(".gallery_lightbox_audio").css('display')) != "none")
		newHeight = newHeight + jQuery(".gallery_lightbox_audio").height();
	
	
	var heightScreen = jQuery(window).height();
	
	
	var newLeft = Math.ceil((widthScreen - widthImage) / 2) + parseInt(jQuery("#maincontainer").offset().left);
	var newTop = Math.ceil((heightScreen - newHeight) * 0.5);
	
	
	if(newTop <= 0) newTop = 20;
	newTop = newTop + jQuery(window).scrollTop();
	
	
	
	jQuery(".gallery_ligthbox_container_multimedia").css('height', heightImage+"px");
	
	
	
	
	
	jQuery("#gallery_ligthbox_container").animate({height: newHeight+"px", width: widthImage+"px", top: newTop+"px", left:newLeft+"px"}, 250, "linear", function()
	 {
		
		resetWidthTitle();
		checkLightboxSlider();
		setTimeout('setVisibleLightboxImage()', 300);
		
	});
}

function setVisibleLightboxImage()
{
	jQuery(".gallery_ligthbox_container_multimedia").children(".loading_multimedia").hide();
	jQuery(".gallery_ligthbox_container_multimedia").children("img").css('visibility', 'visible');
	if(jQuery(".gallery_lightbox_audio").css('display') != "none") resetAudioFile();	
}

function checkLightboxSlider()
{
	if(numVisibleThumb >= numThumbLightboxGallery)
	{
		jQuery(".freccia_lightbox").attr('href', 'javascript:void(0)');
		jQuery(".freccia_lightbox_right").attr('href', 'javascript:void(0)');
		
		jQuery(".freccia_lightbox").addClass('disabled');
		jQuery(".freccia_lightbox_right").addClass('disabled');
		
		if(parseInt(jQuery(".gallery_ligthbox_container_gallery_scroll").css('left')) != 0) jQuery(".gallery_ligthbox_container_gallery_scroll").animate({left: "0px"}, 100, "linear");
	}else
	{
		maxScrollLightboxGallery = (numThumbLightboxGallery - numVisibleThumb) * widthLightboxGallery;	
		var leftSlider = parseInt(jQuery(".gallery_ligthbox_container_gallery_scroll").css('left'));
		if(leftSlider == 0)
		{
			jQuery(".freccia_lightbox").attr('href', 'javascript:void(0)');
			jQuery(".freccia_lightbox").addClass('disabled');
		}else if(Math.abs(leftSlider) == maxScrollLightboxGallery)
		{
			jQuery(".freccia_lightbox_right").attr('href', 'javascript:void(0)');
			jQuery(".freccia_lightbox_right").addClass('disabled');
		}
		
		//var leftThumbSel = parseInt(jQuery(".lightbox_thumb_selected").css('left'));
		var leftThumbSel = Math.abs(parseInt(jQuery(".gallery_ligthbox_container_gallery_scroll").css('left'))) + parseInt(jQuery(".gallery_ligthbox_container_gallery_element[idel="+currentIdEl+"]").offset().left) - parseInt(jQuery(".gallery_ligthbox_container_gallery").offset().left);
		if(leftThumbSel > jQuery(".gallery_ligthbox_container_gallery").width())
		{
			jQuery(".gallery_ligthbox_container_gallery_scroll").animate({left: (jQuery(".gallery_ligthbox_container_gallery").width() - leftThumbSel - widthLightboxGallery)+"px"}, 100, "linear", function()
			{
				jQuery(".lightbox_thumb_selected").css('left', (parseInt(jQuery(".gallery_ligthbox_container_gallery_element[idel="+currentIdEl+"]").offset().left) - parseInt(jQuery(".gallery_ligthbox_container_gallery").offset().left))+"px");	
			});
		}else
		{
			//if(parseInt(jQuery(".gallery_ligthbox_container_gallery_scroll").css('left')) != 0) jQuery(".gallery_ligthbox_container_gallery_scroll").animate({left: "0px"}, 100, "linear");
		}
	}
}

function resetWidthTitle()
{
	var fixedWidth = jQuery(".gallery_ligthbox_container_header img").width() + jQuery(".gallery_ligthbox_container_header .header_close").width();
	var marginTitle = parseInt(jQuery(".header_title").css('margin-right')) + parseInt(jQuery(".header_title").css('margin-left'));
	var widthTitle = (jQuery("#gallery_ligthbox_container").width() - fixedWidth - marginTitle);
	var widthHeaderTitle = jQuery(".header_section_title").width() + parseInt(jQuery(".header_section_title").css('margin-right'));
	jQuery(".header_title").css('width', widthTitle+"px");
	jQuery(".header_section_subtitle").css('width', (widthTitle - widthHeaderTitle)+"px");
}

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 closeGalleryLightbox()
{
	firstLightboxLoad = true;
	currentIdElAudio = 0;
	currentFileAudio = "";
	 currentIdEl = 0;
	currentMediaType = -1;
	currentFile = "";
	loadAudio = false;
	jQuery(".gallery_ligthbox_container_gallery_element").children("a").addClass("disabled_lightbox_thumb");
	loadImage = false;
	jQuery("#gallery_lightbox_zoom").attr('src', ''); 
	jQuery("#layer_opacity").hide();
	jQuery("#gallery_ligthbox_container").css('visibility', 'hidden');
	jQuery(".gallery_ligthbox_container_multimedia").children("img").css('width', 'auto');
	jQuery(".gallery_ligthbox_container_multimedia").children("img").css('height', 'auto');
	jQuery(".gallery_lightbox_audio").hide();
	jQuery(".gallery_ligthbox_container_multimedia").children(".gallery_lightbox_video").hide();
	jQuery(".gallery_ligthbox_container_multimedia").children(".prev_lightbox_image").hide();
	jQuery(".gallery_ligthbox_container_multimedia").children(".next_lightbox_image").hide();	
	jQuery("#gallery_ligthbox_container").hide();	
	jQuery("#gallery_ligthbox_container").css('display', 'none');
	if(navigator.platform.indexOf("iPad") != -1)
	{
		jQuery(".gallery_lightbox_audio").html('');
		jQuery(".gallery_ligthbox_container_multimedia").children(".gallery_lightbox_video").html('');
	}else
	{
		jQuery(".gallery_lightbox_audio").html('<div id="gallery_lightbox_audio_box"><a href="http://www.adobe.com/go/getflashplayer"><img src="/asset/img/get_flash_player.gif" alt="Get Adobe Flash player" border="0" /></a></div>');
		jQuery(".gallery_ligthbox_container_multimedia").children(".gallery_lightbox_video").html('<div id="gallery_lightbox_video_box"><a href="http://www.adobe.com/go/getflashplayer"><img src="/asset/img/get_flash_player.gif" alt="Get Adobe Flash player" border="0" /></a></div>');
	}
}
function hideVideoPlayer()
{
	// jQuery("#first_frame_video").show();
	// jQuery(".gallery_lightbox_video").hide();
	jQuery("#first_frame_video").css({ zIndex : 100 });
	jQuery(".gallery_lightbox_video").css({ zIndex : -1 });
}
