// JavaScript Document
var currentGalleryGroup = "";
var indexGalleryGroupImage = 0;

var currentVideo = "";
var currentImage = "";

var firstLightboxLoad = true;

var MIN_WIDTH_LIGTHBOX = 640;
var MIN_HEIGHT_LIGTHBOX = 400;

var VIDEO_HEIGHT = 415;
var VIDEO_WIDTH = 738;

var INITIAL_WIDTH_LIGHTBOX = 50;
var INITIAL_HEIGHT_LIGHTBOX = 50;

var mouseOutGallery = true;

jQuery(document).ready(function()
{
	jQuery("#gallery-lightbox-cnt").css('visibility', 'hidden').css('display', 'block');
	jQuery("#gallery_lightbox_zoom").load(function ()
	{
		setTimeout('lightboxZoomLoadComplete()', 200);
	});
	jQuery("#gallery-lightbox-cnt").css('display', 'none').css('visibility', 'visible');
	
	jQuery("#layer_opacity").click(
		function()
		{
			if(jQuery.trim(jQuery("#gallery-lightbox-cnt").css("display")) != 'none')
				galleryClose();
		}
	);
	
	jQuery("#gallery-lightbox-cnt").click(
		function(ev) { ev.stopImmediatePropagation(); }
	);
	
	jQuery("#gallery-lightbox-cnt").hover(
		function()
		{
			mouseOutGallery = false;
			if(jQuery.trim(currentVideo) == '') jQuery(".gallery-lightbox-cnt-multimedia-condividi-video").fadeIn(200);
			//jQuery(".gallery-lightbox-cnt-multimedia-condividi-video").animate({bottom: '0px'}, 200, "linear");
			jQuery(".prev_lightbox_image").fadeIn(200);
			jQuery(".next_lightbox_image").fadeIn(200);
		},
		function()
		{
			mouseOutGallery = true;
			if(jQuery.trim(currentVideo) == '') jQuery(".gallery-lightbox-cnt-multimedia-condividi-video").fadeOut(100);
			//jQuery(".gallery-lightbox-cnt-multimedia-condividi-video").animate({bottom: -(jQuery(".gallery-lightbox-cnt-multimedia-condividi-video").height() + 10) + 'px'}, 200, "linear");
			jQuery(".prev_lightbox_image").fadeOut(100);
			jQuery(".next_lightbox_image").fadeOut(100);
		}
	);

	
	jQuery(window).bind("resize", resizeWindow);
});

function resizeWindow( e ) 
{
	if(jQuery("#gallery-lightbox-cnt").length == 0 || navigator.platform.indexOf("iPad") != -1 || navigator.platform.indexOf("iPhone") != -1) return;
	if(jQuery.trim(jQuery("#gallery-lightbox-cnt").css('display')) != "none")
	{
		var widthGallery = jQuery("#gallery-lightbox-cnt").width();
		var heightGallery = jQuery("#gallery-lightbox-cnt").height();
		var heightScreen = jQuery(window).height();
		var widthScreen = jQuery("#main").width();
		
		var newLeft = Math.ceil((widthScreen - widthGallery) * 0.5) + ((jQuery(window).width() - widthScreen) * 0.5);
		var newTop = Math.ceil((heightScreen - heightGallery) * 0.5);
		if(newTop < 0) newTop = 20;
		newTop = newTop + jQuery(window).scrollTop();	
		jQuery("#gallery-lightbox-cnt").css('left', newLeft+"px");
		jQuery("#gallery-lightbox-cnt").css('top', newTop+"px");
	}
}

function initialLoadGallery(group, index)
{
	firstLightboxLoad = true;
	currentGalleryGroup = group;
	indexGalleryGroupImage = parseInt(index, 10);
	loadGallery();
}

function loadGallery()
{
	currentIdContent = parseInt(jQuery(".box[gallerygroup="+currentGalleryGroup+"]:eq("+indexGalleryGroupImage+")").attr("idContent"), 10);
	currentVideo = jQuery(".box[gallerygroup="+currentGalleryGroup+"]:eq("+indexGalleryGroupImage+")").attr("galleryvideosource");
	currentImage = jQuery(".box[gallerygroup="+currentGalleryGroup+"]:eq("+indexGalleryGroupImage+")").attr("gallerysource");
	
	if(firstLightboxLoad)
	{
		jQuery("#layer_opacity").show();
		jQuery("#gallery-lightbox-cnt").css('width', INITIAL_WIDTH_LIGHTBOX + 'px');
		jQuery("#gallery-lightbox-cnt").css('height', INITIAL_HEIGHT_LIGHTBOX + 'px');
		jQuery("#gallery-lightbox-cnt").css('z-index', '10000');
		
		var heightScreen = jQuery(window).height();
		var newTop = Math.ceil((heightScreen - INITIAL_HEIGHT_LIGHTBOX) * 0.5);
		if(newTop < 0) newTop = 20;
		newTop = newTop + jQuery(window).scrollTop();
		
		var newLeft = Math.ceil((jQuery("#main").width() - INITIAL_WIDTH_LIGHTBOX) * 0.5) + parseInt(jQuery("#main").offset().left);
		jQuery("#gallery-lightbox-cnt").css('top', newTop+'px');
		jQuery("#gallery-lightbox-cnt").css('left', newLeft+'px');
		jQuery(".gallery-lightbox-cnt-video").css('height', '419px');
		
		firstLightboxLoad = false;
	}
	
	jQuery(".gallery-lightbox-cnt-video").hide();
	jQuery(".gallery-lightbox-cnt-multimedia").children(".loading-multimedia").show();
	jQuery(".gallery-lightbox-cnt-multimedia").children("img#gallery_lightbox_zoom").css('width', 'auto');
	jQuery(".gallery-lightbox-cnt-multimedia").children("img#gallery_lightbox_zoom").css('height', 'auto');
	jQuery(".gallery-lightbox-cnt-multimedia").children("img#gallery_lightbox_zoom").css('margin-left', '0px');
	jQuery(".gallery-lightbox-cnt-multimedia").children("img#gallery_lightbox_zoom").css('margin-top', '0px');

	jQuery(".gallery-lightbox-cnt-multimedia").find(".gallery_lightbox_video").hide();
	jQuery(".gallery-lightbox-cnt-multimedia").find("img#gallery_lightbox_zoom").hide();
	jQuery(".gallery-lightbox-cnt-multimedia").find(".prev_lightbox_image").hide();
	jQuery(".gallery-lightbox-cnt-multimedia").find(".next_lightbox_image").hide();
		
	jQuery("#gallery-lightbox-cnt").css('visibility', 'visible');
	jQuery("#gallery-lightbox-cnt").css('display', 'block');
	
	if(jQuery.trim(currentVideo) == '')
	{
		jQuery("#gallery_lightbox_zoom").attr('src', "img/repository/gallery/" + currentIdContent + "/big/" + currentImage); 
	}else
	{
		//VIDEO
		jQuery(".gallery-lightbox-cnt-video").html('<div id="gallery-lightbox-cnt-video-box"><a href="http://www.adobe.com/go/getflashplayer" id="videoplayerflash"><img src="img/get_flash_player.gif" alt="Get Adobe Flash player" border="0" /></a></div>');
		if(navigator.platform.indexOf("iPad") != -1 || navigator.platform.indexOf("iPhone") != -1)
		{
			jQuery(".gallery-lightbox-cnt-multimedia").children(".gallery-lightbox-cnt-video").html('<video id="H5Video" src="repository/gallery/' + currentIdContent + '/video/' +currentVideo + '" type="video/mp4" width=' + VIDEO_WIDTH +' height=' + VIDEO_HEIGHT + ' controls="controls" />');
		}else
		{
			if (swfobject.hasFlashPlayerVersion("10.0.0")) 
			{
				var flashvars = {};
				flashvars.autoPlay = "0";
				flashvars.video = "repository/gallery/" + currentIdContent + "/video/" +currentVideo;
				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("img/AdactoVideoPlayer.swf", "gallery-lightbox-cnt-video-box", VIDEO_WIDTH, VIDEO_HEIGHT, "10.0.0", false, flashvars, params, attributes);
			} else {
				jQuery("#gallery-lightbox-cnt-video-box").children("a").css("visibility", "visible");
			}
		}
		
		var newTopArrow = (VIDEO_HEIGHT - jQuery(".prev_lightbox_image").height()) * 0.5;
		jQuery(".prev_lightbox_image").css('top', newTopArrow + 'px');
		jQuery(".next_lightbox_image").css('top', newTopArrow + 'px');
		
		var widthScreen = jQuery("#main").width();
		var heightScreen = jQuery(window).height();
		
		var newLeft = Math.ceil((widthScreen - VIDEO_WIDTH) / 2) + parseInt(jQuery("#main").offset().left);
		var newTop = Math.ceil((heightScreen - VIDEO_HEIGHT) * 0.5);
		if(newTop <= 0) newTop = 20;
		newTop = newTop + jQuery(window).scrollTop();
		
		jQuery(".gallery-lightbox-cnt-multimedia-condividi-video").hide();
		jQuery(".gallery-lightbox-cnt-multimedia").css('height', VIDEO_HEIGHT+"px");
		jQuery("#gallery-lightbox-cnt").animate({height: VIDEO_HEIGHT+"px", width: VIDEO_WIDTH+"px", top: newTop+"px", left:newLeft+"px"}, 250, "linear", function()
		 {
			setTimeout('setVisibleLightboxVideo()', 300);	
		});
	}
}

function lightboxZoomLoadComplete()
{
	var widthScreen = jQuery("#main").width();
	
	jQuery(".gallery-lightbox-cnt-multimedia").children("img#gallery_lightbox_zoom").css('visibility', 'hidden');
	jQuery(".gallery-lightbox-cnt-multimedia").children("img#gallery_lightbox_zoom").css('display', 'block');
	
	jQuery(".gallery-lightbox-cnt-multimedia").find("img#gallery_lightbox_zoom").css('margin-left', '2px');
	jQuery(".gallery-lightbox-cnt-multimedia").find("img#gallery_lightbox_zoom").css('margin-top', '2px');
	
	var widthImage = jQuery(".gallery-lightbox-cnt-multimedia").find("img#gallery_lightbox_zoom").width();
	var heightImage = jQuery(".gallery-lightbox-cnt-multimedia").find("img#gallery_lightbox_zoom").height();

	if(widthImage > widthScreen)
	{
		heightImage = Math.floor((widthScreen * heightImage) / widthImage);
		widthImage = widthScreen;
		jQuery(".gallery-lightbox-cnt-multimedia").find("img#gallery_lightbox_zoom").css("width", widthImage+"px");
		jQuery(".gallery-lightbox-cnt-multimedia").find("img#gallery_lightbox_zoom").css("height", heightImage+"px");
	}

	var newWidthImage = widthImage;
	var newHeightImage = heightImage;
	
	jQuery(".gallery-lightbox-cnt-multimedia").find("img#gallery_lightbox_zoom").css("width", widthImage+"px");
	jQuery(".gallery-lightbox-cnt-multimedia").find("img#gallery_lightbox_zoom").css("height", heightImage+"px");
		
	jQuery(".gallery-lightbox-cnt-multimedia-condividi-video").css("width", widthImage + "px");	
		
	var newHeight = heightImage + parseInt(jQuery("#gallery-lightbox-cnt").css('padding-top'), 10) + parseInt(jQuery("#gallery-lightbox-cnt").css('padding-bottom'), 10);
	widthImage = widthImage + parseInt(jQuery("#gallery-lightbox-cnt").css('padding-left'), 10) + parseInt(jQuery("#gallery-lightbox-cnt").css('padding-right'), 10);	
	var heightScreen = jQuery(window).height();
	
	var newLeft = Math.ceil((widthScreen - widthImage) / 2) + parseInt(jQuery("#main").offset().left);
	var newTop = Math.ceil((heightScreen - newHeight) * 0.5);
	if(newTop <= 0) newTop = 20;
	newTop = newTop + jQuery(window).scrollTop();
	
	var newTopArrow = (heightImage - jQuery(".prev_lightbox_image").height()) * 0.5;
	jQuery(".prev_lightbox_image").css('top', newTopArrow + 'px');
	jQuery(".next_lightbox_image").css('top', newTopArrow + 'px');
		
	jQuery(".gallery-lightbox-cnt-multimedia").css('height', heightImage+"px");
	jQuery("#gallery-lightbox-cnt").animate({height: newHeight+"px", width: widthImage+"px", top: newTop+"px", left:newLeft+"px"}, 250, "linear", function()
	 {
		setTimeout('setVisibleLightboxImage()', 300);	
	});
}

function setVisibleLightboxImage()
{
	if(!mouseOutGallery)
	{
		jQuery(".prev_lightbox_image").fadeIn(200);
		jQuery(".next_lightbox_image").fadeIn(200);	
	}
	
	jQuery(".gallery-lightbox-cnt-multimedia").find(".loading-multimedia").hide();
	jQuery(".gallery-lightbox-cnt-multimedia").find("img#gallery_lightbox_zoom").css('visibility', 'visible');
}

function setVisibleLightboxVideo()
{
	jQuery(".gallery-lightbox-cnt-video").show();
	jQuery(".gallery-lightbox-cnt-multimedia").find(".loading-multimedia").hide();
	jQuery(".gallery-lightbox-cnt-multimedia").find("img#gallery_lightbox_zoom").css('visibility', 'visible');
}

function changeLitghboxImageToClick(isNext)
{
	if(isNext)
	{
		indexGalleryGroupImage = indexGalleryGroupImage + 1;
		if(indexGalleryGroupImage > (jQuery(".box[gallerygroup="+currentGalleryGroup+"]").length - 1))
			indexGalleryGroupImage = 0;
	}else
	{
		indexGalleryGroupImage = indexGalleryGroupImage - 1;
		if(indexGalleryGroupImage < 0) 
			indexGalleryGroupImage = (jQuery(".box[gallerygroup="+currentGalleryGroup+"]").length - 1);
	}
	loadGallery();
}

function galleryClose()
{
	indexGalleryGroupImage = 0;
	currentGalleryGroup = "";
	if(jQuery.trim(currentVideo) == '') jQuery(".gallery-lightbox-cnt-multimedia-condividi-video").hide();
	jQuery(".prev_lightbox_image").hide();
	jQuery(".next_lightbox_image").hide();
	jQuery("#gallery_lightbox_zoom").attr('src', '');
	jQuery(".gallery-lightbox-cnt-video").html('<div id="gallery-lightbox-cnt-video-box"><a href="http://www.adobe.com/go/getflashplayer" id="videoplayerflash"><img src="img/get_flash_player.gif" alt="Get Adobe Flash player" border="0" /></a></div>');
	jQuery("#layer_opacity").hide();	
	jQuery("#gallery-lightbox-cnt").hide();
}
