// JavaScript Document
var indexPhotos = 0;
var numbOfSlideImages;
var intervalPhoto;
$(document).ready(function() {
	$.ifixpng('img/v.gif');                        
    $('img[src$=.png]').ifixpng();
	adjustResize();
	$(window).bind('resize', function() {
		adjustResize();
	});
	
	$(".left_side ul li a").css("display","inline-block");	
	
	$(".left_side ul li a").hover(function() {   
		$(this).css("background","transparent");
		if($(this).parent().children(".js_bg_hover").css("background") == undefined) {
			$(this).parent().prepend("<div class='js_bg_hover'>"+$(this).text()+"</div>");	
			$(this).parent().children(".js_bg_hover").css("opacity","0");
		}
		//$(this).parent().children(".js_bg_hover").css("background","#fff").fadeIn("400");
		$(this).parent().children(".js_bg_hover").stop();
		$(this).parent().children(".js_bg_hover").css("color","#9a9794").animate({ 
        	opacity: 1
      	}, 400);
		$(this).stop();
		$(this).animate({ 
        	opacity: 0
      	}, 400);

	}).mouseout(function() {
		//$(this).parent().children(".js_bg_hover").fadeOut("400");
		$(this).parent().children(".js_bg_hover").animate({ 
        	opacity: 0
      	}, 400);
		$(this).animate({ 
			opacity:1
      	}, 400);
		
	});
	
	
	initPhoto();
});

var isMovingGallery = false;

function initPhoto() {
	
	$(".img_slide ul").each(function(i) {
		var width = 0;
		for(i=0;i<$(this).find("li").length;i++) {
			width += parseInt($(this).find("li:eq("+i+")").css("width"));
		}

		if ($(this).find("li").length > 1)  {
				$(this).parent().prepend("<div class='next' style='position:absolute; margin-left:455px; margin-top:350px; color:#000; cursor:pointer;'><img src='img/Next.png'></div>");
				$(this).parent().prepend("<div class='prev' style='display:none; position:absolute; margin-left:10px; margin-top:350px; color:#000; cursor:pointer;'><img src='img/Back.png'></div>");

		}
		$(this).css("width",width+"px");
	});
	
	$(".img_slide ul li img").each(function(i) {
		if ($(this).attr("ref") != "total") $(this).css("marginTop","132px");
		//var mT = ((parseInt($(this).parent().parent().parent().css("height")) - parseInt($(this).css("height"))) / 2);
		//$(this).css("marginTop",mT + "px");
	});
	
	$(".img_slide .next").click(function() {
		if (isMovingGallery == true) return;
		isMovingGallery = true;
		var imageWidth = parseInt($(this).parent().find("li:eq(0)").css("width"));
		var mL = parseInt($(this).parent().find("ul").css("marginLeft")) - imageWidth;
		$(this).parent().find("ul").animate({ 
        	marginLeft: mL+"px"
      	}, 400, function() { isMovingGallery = false; });
		if (mL < 0) { $(this).parent().find(".prev").css("display","block"); }
		if (-(mL-imageWidth) == parseInt($(this).parent().find("ul").css("width"))) {
			$(this).parent().find(".next").css("display","none");							
		}
		//if (mL+imageWidth >= parseInt($(this).parent().find("ul").css("width"))) { $(this).parent().find(".prev").css("display","block"); }
	});
	$(".img_slide .prev").click(function() {
		if (isMovingGallery == true) return;
		isMovingGallery = true;
		var mL = parseInt($(this).parent().find("ul").css("marginLeft")) + parseInt($(this).parent().find("li:eq(0)").css("width"));
		$(this).parent().find("ul").animate({ 
        	marginLeft: mL+"px"
      	}, 400, function() { isMovingGallery = false; });
		if (mL >= 0) { $(this).parent().find(".prev").css("display","none"); }
		$(this).parent().find(".next").css("display","block");
	});
	
	/*numbOfSlides = $(".home .img_slide").length;
	for(var j=0;j<numbOfSlides;j++) {
		numbOfSlideImages = $(".home .img_slide ul:eq("+j+") li").length;	
		$(".home .img_slide ul:eq("+j+") li:eq(0)").css("opacity","1");
		for(var i = 1; i<numbOfSlideImages; i++) {
			$(".home .img_slide ul:eq("+j+") li:eq("+i+")").css("opacity","0");
		}
	}
	indexPhotos = 1;
	if (intervalPhoto) clearInterval(intervalPhoto);
	intervalPhoto = setInterval(changePhoto,4000);*/
}

function adjustResize() {
	var marginleft = 253;
	var rientranza_sfondo = 0;
	if ($(window).width() < 1326) {
		var summargin = $(window).width() - 820;
		marginleft = Math.round((summargin / 2),0);
		if (marginleft < 0) { marginleft = 0; }
		rientranza_sfondo = 253 - marginleft;
	}
	$(".total_contenitor").css("marginLeft",marginleft+"px");
	$("body").css("backgroundPosition","-"+rientranza_sfondo+"px 0");
	
}

function closeVideo() {
	$("#videoContenitor").remove();	
	$(".total_contenitor").css("display","block");
	
	//$("#home_video_contenitor").html('<object id="video" width="480" height="385"><param name="movie" value="swf/betullavideo_mini.swf"></param><param name="scale" value="noscale"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><embed src="swf/betullavideo_mini.swf" scale="noscale" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="480" height="385"></embed></object>');
}
