$(document).ready(function() {
	var intervalslideshow;
	var currentslideshow=0;
	var currentslidegallery=1;
	var currentslidegallerythumb=1;
    var fullscreen_open=true;
	var gallery_position=0;
	var gallery_slide_position=0;
	var abilitato=true;
	
	var numero_colonne_tot=0;
	var hash_ok=false;
	
	var size_immagine = Array();
    	
	fullscreen("#bg>img");
	
	$("#fullscreen .close").click(close_zoom)
	$("#fullscreen .next").click(next_zoom)
	$("#fullscreen .prev").click(prev_zoom)
	$("#fullscreen .footer .share").click(click_share)
	$("#share .close-share").click(close_share)
	
	$("#container_video").find("video").fadeIn(1000)
	
	recalculateFills();

    $(window).resize(function() {
		recalculateFills();
    });
    
        if($(".current-menu-item").hasClass("no-related")){
	        $(".no-related").addClass("current")
	        
	        $("#right_search").click(function(){
	            if(numero_search==(parseInt($("#results .result_div:last-child").attr("class").replace('result_div colonna-', ' '))+2)){
		        	
		        }else if(numero_search==(parseInt($("#results .result_div:last-child").attr("class").replace('result_div colonna-', ' '))+1)){
		        	$("#content_interno_store").animate({
		                "margin-left": '-=220'
		            }, 500, function() {
		                numero_search++;
		            })
		            
		            $("#right_search").addClass("right_inactive")
			        $("#right_search").removeClass("right")
		        }else if(numero_search==0){			        
		            $("#content_interno_store").animate({
		                "margin-left": -760
		            }, 500, function() {
		                numero_search++;
		            })
		            
		            $("#left_search").addClass("left")
			        $("#left_search").removeClass("left_inactive")
		        }else if(numero_search==1){
		            $("#content_interno_store").animate({
		                "margin-left": -1080
		            }, 500, function() {
		                numero_search++;
		            })
		        }else{
		            $("#content_interno_store").animate({
		                "margin-left": '-=220'
		            }, 500, function() {
		                numero_search++;
		            })
		        }
		    })
		    
		    $("#left_search").click(function(){
		        if(numero_search==(parseInt($("#results .result_div:last-child").attr("class").replace('result_div colonna-', ' '))+2)){
		        	$("#right_search").addClass("right")
			        $("#right_search").removeClass("right_inactive")
			        
			        $("#content_interno_store").animate({
		                "margin-left": '+=220'
		            }, 500, function() {
		                numero_search--;
		            })
		        }else if(numero_search==0){
		            
		            
		        }else if(numero_search==1){
		            $("#left_search").addClass("left_inactive")
			        $("#left_search").removeClass("left")
			        
		            $("#content_interno_store").animate({
		                "margin-left": -250
		            }, 500, function() {
		                numero_search--;
		            })
		            
		        }else if(numero_search==2){
		            $("#content_interno_store").animate({
		                "margin-left": -760
		            }, 500, function() {
		                numero_search--;
		            })
		        }else{
		            $("#content_interno_store").animate({
		                "margin-left": '+=220'
		            }, 500, function() {
		                numero_search--;
		            })
		        }
		    })
		            
	        var numero_search=0;
	        $("#results").resize(function(e){
			    $("#content_interno_store").animate({
		            "margin-left": -760
		        }, 500, function() {
		            numero_search=1;
		        })
		    
		        $("#arrow_container_search").delay(500).fadeIn(500, function(){
		            
		        })
		    });
		}
    
    if ($.browser.msie) {
        if($.browser.version<8){
            $("#container_video").click(function(){
               $("#container_video").fadeOut(500)
            })
            
            $("#container_video").delay(100).fadeOut(500, function(){
            
            })
        }else{
            $("#container_video").click(function(){
                $("#container_video").fadeOut(500, function(){
                    $("#my_video_1 video").get(0).play();
                })
            })
            
            $("#container_video video").bind("ended", function() {
                $("#container_video").fadeOut(500, function(){
                    $("#my_video_1 video").get(0).play();
                })
            });
        }
    }else{
        $("#container_video").click(function(){
            $("#container_video").fadeOut(500, function(){
                $("#my_video_1 video").get(0).play();
            })
        })
            
        $("#container_video video").bind("ended", function() {
            $("#container_video").fadeOut(500, function(){
                $("#my_video_1 video").get(0).play();
            })
        });
    }
	
	function getUrlVar( param_name ){
        param_name = param_name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
        var regex = new RegExp( "[\\?&]"+param_name+"=([^&#]*)" );
        var rMatch = regex.exec( window.location.href );
        if( rMatch == null ){
            return "";
        } else {
            return rMatch[1];
        }
    }
    
	//$(".no-related").find("a").attr("href", $(".no-related").find("a").attr("href") + "&rand=" + getUrlVar("rand"))
		
	var hash = window.location
	//alert(window.location)
	var href = $('#nav li.dissolve a').each(function(){
							//alert(hash)

		    var href = $(this).attr('href');
		    if(hash==href){
		        $(this).parent().addClass("current")
		        $('#content').fadeOut(0);
			    var toLoad = hash+'.html #content';
			    $('#content').load(toLoad, function(){
			        $.getScript("http://www.vicmatie.it/wp-content/themes/vicmatie/js/video.js", function() {
			            $('#content').fadeIn(500,function(){
					        caricamento_completato()
			                $('#load').fadeOut(500);
			            });
			        
			            $('#content_interno_text').css({
			                "margin-top": -$('#content_interno_text').height()/2,
			                "margin-left": -$('#content_interno_text').width()/2
		                })
			        });
				$('.thumb_gallery').css('opacity',0)
				
			})
		}
	});
		
	if($("#bg img").length>1){
		intervalslideshow = setInterval(slide, 5000)
		
		for(i=1; i<$("#bg img").length+1; i++){
			if(i>1){
				$("#bg img:nth-child("+i+")").fadeOut(0);
			}
		}
	}

	$('#nav li.dissolve a').click(function(e){
	        $('#nav li').removeClass("current")
	        $(this).parent().addClass("current")
	        
	        var toLoad = $(this).attr('href')+' #content';
		    $('#content').fadeOut(500,loadContent);
		    $('#load').remove();
		    $('#wrapper').append('<span id="load">LOADING...</span>');
		    $('#load').fadeIn(500);
		    window.location.hash = $(this).attr('href');
		    function loadContent() {
		    	$('#content').load(toLoad,'',showNewContent)
		    }
		    function showNewContent() {
		        $.getScript("http://www.vicmatie.it/wp-content/themes/vicmatie/js/video.js", function() {
			        $('#content').fadeIn(500,hideLoader);
			        $('.thumb_gallery').css('opacity',0)
			        //$('.autoplay')[0].player.play();
			        
			        $('#content_interno_text').css({
			            "margin-top": -$('#content_interno_text').height()/2,
			            "margin-left": -$('#content_interno_text').width()/2
		            })
			    })
		    }
		    function hideLoader() {
			    caricamento_completato()
			    $('#load').fadeOut(500);
		    }
		    return false;
	});
	
	$(window).resize(function() {
 		fullscreen("#bg>img");
	});
	
	// funzioni //
	function click_share(e){
	    var url_share = $("#gallery .thumb_gallery:nth-child("+currentslidegallery+") img").attr("alt");
	    var img_share = $("#gallery .thumb_gallery:nth-child("+currentslidegallery+") img").attr("src");
	    var title_share = $("#gallery .thumb_gallery:nth-child("+currentslidegallery+") img").attr("title");
	    
	    $("#share").fadeIn(500, function(){
	        $("#share").find("#twitter").attr("href", "http://twitter.com/share?source=tweetbutton&text=I%20like%20this! " + title_share + " " + url_share)
	        $("#share").find("#facebook").attr("href", "http://www.facebook.com/sharer.php?u=" + url_share)
	        $("#share").find("#pinterest").attr("href", "http://pinterest.com/pin/create/button/?url=" + url_share + "&media=" + img_share + "&description=" + title_share)
	    })
	    
	    currentslidegallery
	}
	
	function close_share(e){
	    $("#share").fadeOut(500);
	}
	
	function zoom_image(e){
	    if($(this).hasClass("video")){
	    
	    }else{
	        if($(this).hasClass("adv")){
	            $("#fullscreen .next, #fullscreen .prev").hide();
	        }
	        
	        fullscreen_open=true;
		    var url=$(this).find('img').attr('src').substring(0,$(this).find('img').attr("src").length - 12)+".jpg";
		    var nome_articolo=$(this).find('img').attr('title')
		    
		    $("#fullscreen .footer .nome").html(nome_articolo)
		    $("#fullscreen").fadeIn(500,function(){
		        $("#fullscreen .image").css("opacity", 0)
			    myImge = $("<img />").attr("src", url +"?"+ new Date().getTime())
			
			    $(myImge).load(function() {
				    $("#fullscreen .image").append(myImge)
				    $("#fullscreen .image").animate({
				        "opacity": 1
			        }, 500, function() {
			
		            });
			    });	
		    })
		}
	}
	
	function close_zoom(e){
	    $("#fullscreen .image").animate({
		    "opacity": 0
		}, 500, function() {
		    $("#fullscreen .image").empty()
		    $("#fullscreen").fadeOut(500, function(){
		        $("#fullscreen .footer .nome").empty();
		        $("#fullscreen .next, #fullscreen .prev").show();
		    })
		});
	}
	
	function rollover(){
		$(this).find('.rollover').stop().fadeIn(500)
	}
	
	function rollout(){
		$(this).find('.rollover').stop().fadeOut(500)
	}
	
	function caricamento_completato(){
		currentslidegallery=1;
				
		$('#content .thumb_gallery').each(function(i){
			$(this).delay(150*i).animate({
				"opacity": 1
			})
		})
		
		var larghezza=0;
		for(i=1; i<$("#gallery_slide > #gallery > .thumb_gallery").length+1; i++){
			if(i==1){
				$("#gallery_slide").css("margin-left", -$("#gallery_slide > #gallery > .thumb_gallery:nth-child(" + i + ")").width()/2 -30)
			}
			
			if($("#gallery_slide > #gallery > .thumb_gallery:nth-child(" + i + ")").hasClass("video")){
				larghezza=larghezza+711;
			}else{
				larghezza=larghezza+$("#gallery_slide > #gallery > .thumb_gallery:nth-child(" + i + ")").width();
			}
		}
		
		$(".right").click(scorri_avanti)
		
		//$("#gallery, #gallery_slide").width(larghezza)
		
		$(".thumb_gallery").mouseenter(rollover);
		$(".thumb_gallery").mouseleave(rollout);
		
		$(".thumb_gallery").click(zoom_image)
		$(".choice_multi").unbind()
		$(".choice_multi").click(show_in_thumb)
		
		$('.scrollbar_tot').tinyscrollbar();
	}
	
	function show_in_thumb(){
		if(abilitato==false){
			
		}else{
			gallery_position=$("#gallery").css('left')
			gallery_slide_position=$("#gallery_slide").css('margin-left')
			
			var url_immagine=Array()
			var titolo_immagine=Array()
			var link_immagine=Array()
			
			for(i=1; i<$("#gallery_slide > #gallery > .thumb_gallery").length+1; i++){
				url_immagine[i-1]=$("#gallery_slide > #gallery > .thumb_gallery:nth-child("+i+") img").attr("src");
				titolo_immagine[i-1]=$("#gallery_slide > #gallery > .thumb_gallery:nth-child("+i+") img").attr("title");
				link_immagine[i-1]=$("#gallery_slide > #gallery > .thumb_gallery:nth-child("+i+") img").attr("alt");
				
				size_immagine[i-1]=$("#gallery_slide > #gallery > .thumb_gallery:nth-child("+i+")").width();
			}
			
			currentslidegallerythumb=Math.floor((currentslidegallery-1)/3);
			
			$("#gallery_slide > #gallery").fadeOut(500, function(){
				$("#gallery_slide > #gallery").css("left", 0)
				$("#gallery_slide").css("margin-left", -75-170*(Math.floor((currentslidegallery-1)/3)))
				
				
				var numero_colonne=0;
				numero_colonne_tot=0;
				$("#gallery_slide > #gallery").empty();
				
				for(i=0; i<url_immagine.length; i++){
					if(numero_colonne==3){
						numero_colonne_tot++;
						$("#gallery_slide > #gallery").append("<div class='thumb_gallery_grid' style='margin-top:-402px; margin-left:"+(170*numero_colonne_tot)+"px;' id='current-"+(i+1)+"'><div class='rollover'></div><img src='"+url_immagine[i]+"' alt='"+link_immagine[i]+"' title='"+titolo_immagine[i]+"'/></div>")
						numero_colonne=0;
					}else{
						$("#gallery_slide > #gallery").append("<div class='thumb_gallery_grid' id='current-"+(i+1)+"' style='margin-left:"+(170*numero_colonne_tot)+"px'><div class='rollover'></div><img src='"+url_immagine[i]+"' alt='"+link_immagine[i]+"' title='"+titolo_immagine[i]+"'/></div>")
					}
					numero_colonne++;
				}
				
				$(".left_inactive").addClass("left")
				$(".left_inactive").removeClass("left_inactive")
				
				$(".right_inactive").addClass("right")
				$(".right_inactive").removeClass("right_inactive")
				
				$(".left, .left_inactive, .right, .right_inactive").unbind()
				
				if(Math.floor((currentslidegallery-1)/3)==0){
					$(".left").addClass("left_inactive")
					$(".left_inactive").removeClass("left")
					
					$(".right").click(scorri_avanti_thumb)
				}else if(Math.floor((currentslidegallery-1)/3)>=numero_colonne_tot){
					$(".right").addClass("right_inactive")
					$(".right_inactive").removeClass("right")
					
					$(".left").click(scorri_indietro_thumb)
				}else{
					$(".left").click(scorri_indietro_thumb)
					$(".right").click(scorri_avanti_thumb)
				}
				
				$("#gallery_slide > #gallery").fadeIn(500,function(){
					abilitato=true	
				})
				
				$(".thumb_gallery_grid").mouseenter(rollover);
				$(".thumb_gallery_grid").mouseleave(rollout);
				
				$(".thumb_gallery_grid").click(show_image)
			})
			
			$(".choice_multi").unbind()
					
			$(".choice_multi").addClass("choice_single")
			$(".choice_single").removeClass("choice_multi")
			
			$(".choice_single").click(show_in_slide)
		}
		
		abilitato=false
	}
	
	function show_in_slide(){
		if(abilitato==false){
			
		}else{
			var url_immagine=Array()
			var titolo_immagine=Array()
			var link_immagine=Array()
			
			for(i=1; i<$("#gallery_slide > #gallery > .thumb_gallery_grid").length+1; i++){
				url_immagine[i-1]=$("#gallery_slide > #gallery > .thumb_gallery_grid:nth-child("+i+") img").attr("src");
				titolo_immagine[i-1]=$("#gallery_slide > #gallery > .thumb_gallery_grid:nth-child("+i+") img").attr("title");
				link_immagine[i-1]=$("#gallery_slide > #gallery > .thumb_gallery_grid:nth-child("+i+") img").attr("alt");
			}
			
			$("#gallery_slide > #gallery").fadeOut(500, function(){
				$("#gallery_slide > #gallery").empty();
				
				for(i=0; i<url_immagine.length; i++){
					$("#gallery_slide > #gallery").append("<div class='thumb_gallery'><div class='rollover'></div><img src='"+url_immagine[i]+"' alt='"+link_immagine[i]+"' title='"+titolo_immagine[i]+"'/></div>")
				}
				
				$("#gallery").css("left", gallery_position)
				$("#gallery_slide").css("margin-left", gallery_slide_position)
				$("#gallery_slide > #gallery").fadeIn(500, function(){
					abilitato=true	
				})
				
				$(".thumb_gallery").mouseenter(rollover);
				$(".thumb_gallery").mouseleave(rollout);
				
				$(".left_inactive").addClass("left")
				$(".left_inactive").removeClass("left_inactive")
				
				$(".right_inactive").addClass("right")
				$(".right_inactive").removeClass("right_inactive")
				
				$(".left, .left_inactive, .right, .right_inactive").unbind()
				
				if(currentslidegallery==1){
					$(".left").addClass("left_inactive")
					$(".left_inactive").removeClass("left")
					
					$(".right").click(scorri_avanti)
				}else if(currentslidegallery>=$("#gallery > .thumb_gallery").length){
					$(".right").addClass("right_inactive")
					$(".right_inactive").removeClass("right")
					
					$(".left").click(scorri_indietro)
				}else{
					$(".left_inactive").addClass("left")
					$(".left").removeClass("left_inactive")
					
					$(".right_inactive").addClass("right")
					$(".right").removeClass("right_inactive")
					
					$(".right").click(scorri_avanti)
					$(".left").click(scorri_indietro)
				}
				
				$(".thumb_gallery").click(zoom_image)
			})
			
			$(".choice_single").unbind()
					
			$(".choice_single").addClass("choice_multi")
			$(".choice_multi").removeClass("choice_single")
			
			$(".choice_multi").click(show_in_thumb)
		}
		
		abilitato=false
	}
	
	function show_image(){
		if(abilitato==false){
			
		}else{
			var corrente = $(this).attr("id").replace('current-', '');
			
			var url_immagine=Array()
			var titolo_immagine=Array()
			var link_immagine=Array()
			
			for(i=1; i<$("#gallery_slide > #gallery > .thumb_gallery_grid").length+1; i++){
				url_immagine[i-1]=$("#gallery_slide > #gallery > .thumb_gallery_grid:nth-child("+i+") img").attr("src");
				titolo_immagine[i-1]=$("#gallery_slide > #gallery > .thumb_gallery_grid:nth-child("+i+") img").attr("title");
				link_immagine[i-1]=$("#gallery_slide > #gallery > .thumb_gallery_grid:nth-child("+i+") img").attr("alt");
			}
			
			$("#gallery_slide > #gallery").fadeOut(500, function(){
				$("#gallery_slide > #gallery").empty();
				
				for(i=0; i<url_immagine.length; i++){
					$("#gallery_slide > #gallery").append("<div class='thumb_gallery'><div class='rollover'></div><img src='"+url_immagine[i]+"' alt='"+link_immagine[i]+"' title='"+titolo_immagine[i]+"'/></div>")
				}
				
				gallery_position=0;
				for(i=1; i<corrente; i++){
					gallery_position = gallery_position + size_immagine[i] + 30
				}
				
				$("#gallery").css("left", -gallery_position)
				$("#gallery_slide").css("margin-left", gallery_slide_position)
				$("#gallery_slide > #gallery").fadeIn(500, function(){
					abilitato=true	
				})
				
				$(".thumb_gallery").mouseenter(rollover);
				$(".thumb_gallery").mouseleave(rollout);
							
				currentslidegallery=corrente;
				
				$(".left_inactive").addClass("left")
				$(".left_inactive").removeClass("left_inactive")
				
				$(".right_inactive").addClass("right")
				$(".right_inactive").removeClass("right_inactive")
				
				$(".left, .left_inactive, .right, .right_inactive").unbind()
				
				if(currentslidegallery==1){
					$(".left").addClass("left_inactive")
					$(".left_inactive").removeClass("left")
					
					$(".right").click(scorri_avanti)
					
					
					
					$("#fullscreen .prev").addClass("inactive")
					$("#fullscreen .prev").unbind()
					
					$("#fullscreen .next").removeClass("inactive")
					$("#fullscreen .next").unbind()
					$("#fullscreen .next").click(next_zoom)
				}else if(currentslidegallery>=$("#gallery > .thumb_gallery").length){
					$(".right").addClass("right_inactive")
					$(".right_inactive").removeClass("right")
					
					$(".left").click(scorri_indietro)
					
					$("#fullscreen .prev").removeClass("inactive")
					$("#fullscreen .prev").unbind()
					$("#fullscreen .prev").click(prev_zoom)
					
					$("#fullscreen .next").addClass("inactive")
					$("#fullscreen .next").unbind()
				}else{
					$(".left_inactive").addClass("left")
					$(".left").removeClass("left_inactive")
					
					$(".right_inactive").addClass("right")
					$(".right").removeClass("right_inactive")
					
					$(".right").click(scorri_avanti)
					$(".left").click(scorri_indietro)
					
					$("#fullscreen .prev").removeClass("inactive")
					$("#fullscreen .prev").unbind()
					$("#fullscreen .prev").click(prev_zoom)
					$("#fullscreen .next").removeClass("inactive")
					$("#fullscreen .next").unbind()
					$("#fullscreen .next").click(next_zoom)
				}
				
				$(".thumb_gallery").click(zoom_image)
			})
			
			$(".choice_single").unbind()
					
			$(".choice_single").addClass("choice_multi")
			$(".choice_multi").removeClass("choice_single")
			
			$(".choice_multi").click(show_in_thumb)
		}
		
		abilitato=false
	}
	
	function slide(){
		$("#bg img:nth-child("+currentslideshow+")").fadeOut(500);
		
		if(currentslideshow==$("#bg img").length){
			currentslideshow=0;
		}
	    
		currentslideshow++
		
		$("#bg img:nth-child("+currentslideshow+")").delay(500).fadeIn(500);
	}
	
	function fullscreen(theItem){
		var winWidth=$(window).width();
		var winHeight=$(window).height();
		var imageWidth=1920;
		var imageHeight=1080;
		var picHeight = imageHeight / imageWidth;
		var picWidth = imageWidth / imageHeight;
		
		if ((winHeight / winWidth) < picHeight) {
			$(theItem).css("width",winWidth);
			$(theItem).css("height",picHeight*winWidth);
		}else{
			$(theItem).css("height",winHeight);
			$(theItem).css("width",picWidth*winHeight);
		};
		
		$(theItem).css("margin-left",winWidth/2 - $(theItem).width() / 2);
		$(theItem).css("margin-top",winHeight/2 - $(theItem).height() / 2);
	}
	
	function next_zoom(){
		if(abilitato==false){
			
		}else{
			if(currentslidegallery==$("#gallery > .thumb_gallery").length){
			
			}else{
				$("#fullscreen .next").unbind()
				$("#fullscreen .prev").unbind()
				scorri_avanti()
				
				var url = $("#gallery .thumb_gallery:nth-child("+currentslidegallery+") img").attr("src").substring(0,$("#gallery .thumb_gallery:nth-child("+currentslidegallery+") img").attr("src").length - 12)+".jpg";
				var nome_articolo = $("#gallery .thumb_gallery:nth-child("+currentslidegallery+") img").attr("title");
				
				$("#fullscreen .nome").animate({
				   "opacity": 0
				}, 500, function() {
					$("#fullscreen .nome").html(nome_articolo)
					
					$("#fullscreen .nome").animate({
						"opacity": 1
					}, 500, function() {
						
					})
				})
			
				$("#fullscreen .image").animate({
					"opacity": 0
				}, 500, function() {
					$("#fullscreen .image").empty()
				
					myImge = $("<img />").attr("src", url +"?"+ new Date().getTime())
				
					$(myImge).load(function() {
						$("#fullscreen .image").append(myImge)
						$("#fullscreen .image").animate({
							"opacity": 1
						}, 500, function() {
							if($("#fullscreen .next").hasClass("inactive")){
								
							}else{
								$("#fullscreen .next").click(next_zoom)
							}
							
							if($("#fullscreen .prev").hasClass("inactive")){
								
							}else{
								$("#fullscreen .prev").click(prev_zoom)
							}
						});
					});	
				});
			}
		}
	}
	
	function prev_zoom(){
		if(abilitato==false){
			
		}else{
			if(currentslidegallery==1){
			
			}else{
				$("#fullscreen .next").unbind()
				$("#fullscreen .prev").unbind()
				
				scorri_indietro()
				
				var url = $("#gallery .thumb_gallery:nth-child("+currentslidegallery+") img").attr("src").substring(0,$("#gallery .thumb_gallery:nth-child("+currentslidegallery+") img").attr("src").length - 12)+".jpg";
				var nome_articolo = $("#gallery .thumb_gallery:nth-child("+currentslidegallery+") img").attr("title");
				
				$("#fullscreen .nome").animate({
				   "opacity": 0
				}, 500, function() {
					$("#fullscreen .nome").html(nome_articolo)
					
					$("#fullscreen .nome").animate({
						"opacity": 1
					}, 500, function() {
						
					})
				})
				
				$("#fullscreen .image").animate({
				   "opacity": 0
				}, 500, function() {
					$("#fullscreen .image").empty()
				
					myImge = $("<img />").attr("src", url +"?"+ new Date().getTime())
				
					$(myImge).load(function() {
						$("#fullscreen .image").append(myImge)
						$("#fullscreen .image").animate({
							"opacity": 1
						}, 500, function() {
							if($("#fullscreen .next").hasClass("inactive")){
								
							}else{
								$("#fullscreen .next").click(next_zoom)
							}
							
							if($("#fullscreen .prev").hasClass("inactive")){
								
							}else{
								$("#fullscreen .prev").click(prev_zoom)
							}
						});
					});	
				});
			}
		}
	}
	
	function scorri_avanti(){
		if(abilitato==false){
			
		}else{
			if(currentslidegallery==1){
				$(".left_inactive").addClass("left")
				$(".left_inactive").removeClass("left_inactive")
				
				$(".left").click(scorri_indietro)
				
				$("#fullscreen .prev").removeClass("inactive")
				$("#fullscreen .prev").unbind()
				$("#fullscreen .prev").click(prev_zoom)
			}else if(currentslidegallery==$("#gallery > .thumb_gallery").length-1){
				$(".right").addClass("right_inactive")
				$(".right_inactive").removeClass("right")
				
				$(".right_inactive").unbind();
				
				$("#fullscreen .next").addClass("inactive")
				$("#fullscreen .next").unbind()
			}
			
			currentslidegallery++;
			
			$("#gallery_slide > #gallery").animate({
			"left": parseInt($("#gallery_slide > #gallery").css("left"))-($("#gallery_slide > #gallery > .thumb_gallery:nth-child(" + (currentslidegallery-1) + ")").width()*.5) - ($("#gallery_slide > #gallery > .thumb_gallery:nth-child(" + currentslidegallery + ")").width()*.5) - 30
			}, 500, function() {
				abilitato=true
			});
		}
		
		abilitato=false;
	}
	
	function scorri_indietro(){
		if(abilitato==false){
			
		}else{
			if(currentslidegallery==2){
				$(".left").addClass("left_inactive")
				$(".left_inactive").removeClass("left")
				
				$(".left_inactive").unbind();
				
				$("#fullscreen .prev").addClass("inactive")
				$("#fullscreen .prev").unbind()
			}else if(currentslidegallery==$("#gallery > .thumb_gallery").length){
				$(".right_inactive").addClass("right")
				$(".right").removeClass("right_inactive")
				
				$(".right").click(scorri_avanti)
				
				$("#fullscreen .next").removeClass("inactive")
				$("#fullscreen .next").unbind()
				$("#fullscreen .next").click(next_zoom)
			}
			
			currentslidegallery--;
			$("#gallery_slide > #gallery").animate({
			"left": parseInt($("#gallery_slide > #gallery").css("left"))+($("#gallery_slide > #gallery > .thumb_gallery:nth-child(" + (currentslidegallery+1) + ")").width()*.5) + ($("#gallery_slide > #gallery > .thumb_gallery:nth-child(" + currentslidegallery + ")").width()*.5) + 30
			}, 500, function() {
				abilitato=true
			});
		}
		
		abilitato=false;
	}
	
	function scorri_avanti_thumb(){
		if(currentslidegallerythumb==0){
			$(".left_inactive").addClass("left")
			$(".left").removeClass("left_inactive")
			
			$(".left").click(scorri_indietro_thumb)
		}else if(currentslidegallerythumb==numero_colonne_tot-1){
			$(".right").addClass("right_inactive")
			$(".right_inactive").removeClass("right")
			
			$(".right_inactive").unbind();
		}
					    
	    currentslidegallerythumb++;
		
		$("#gallery_slide > #gallery").animate({
		    "left": "-=170"
		}, 500, function() {
			
		});
	}
	
	function scorri_indietro_thumb(){
	    if(currentslidegallerythumb==1){
			$(".left").addClass("left_inactive")
			$(".left_inactive").removeClass("left")
			
			$(".left_inactive").unbind()
		}else if(currentslidegallerythumb==numero_colonne_tot){
			$(".right_inactive").addClass("right")
			$(".right").removeClass("right_inactive")
			
			$(".right").click(scorri_avanti_thumb)
		}
			
		currentslidegallerythumb--;
		
		$("#gallery_slide > #gallery").animate({
		    "left": "+=170"
		}, 500, function() {
			
		});
	}
	
	function recalculateFills() {
		var browserHeight = Math.round($(window).height());
        var browserWidth = Math.round($(window).width());
		
		var fills = $('.fill');
		
		fills.each(function (){
            var videoHeight = $(this).height();
            var videoWidth = $(this).width();
            var new_size = full_bleed(browserWidth, browserHeight, videoWidth, videoHeight);
            $(this).width(new_size.width).height(new_size.height).css("margin-left", ((browserWidth - new_size.width)/2)).css("margin-top", ((browserHeight - new_size.height)/2));
		});
	 }
	 
	 function full_bleed(boxWidth, boxHeight, imgWidth, imgHeight) {
        var initW = imgWidth;
        var initH = imgHeight;
        var ratio = initH / initW;

        imgWidth = boxWidth;
        imgHeight = boxWidth * ratio;

        if(imgHeight < boxHeight){
            imgHeight = boxHeight;
            imgWidth = imgHeight / ratio;
        }
		
        return {
            width: imgWidth,
            height: imgHeight
        };
    };
});