$(document).ready(function() {
	
	imgWidth = 1600;
	imgHeight = 1200;

	myWidth = $(window).width()
	myHeight = $(window).height()

	strecciaX = myWidth;
	strecciaY = myWidth*imgHeight/imgWidth;

	if (strecciaY<myHeight){
		strecciaY = myHeight;
		strecciaX = myHeight*imgWidth/imgHeight;
		topcss = 0;
		leftcss = (myWidth - strecciaX) /2;	
	} else {
		topcss = (myHeight - strecciaY) /2;
		leftcss = 0;
	}

	leftdraggable = (myWidth-990)/2+20;
	$("#draggable").css({left: leftdraggable});

	bgCSS = {left: leftcss, top: topcss};	
	
	$("#backstretch img").width(strecciaX).height(strecciaY).css(bgCSS);
	$("div#backstretch div#img_contiene_0").fadeIn(500);	
	
	
	$(window).resize(function () {
		myWidth = $(window).width()
		myHeight = $(window).height()
		strecciaX = myWidth;
		strecciaY = myWidth*imgHeight/imgWidth;
		if (strecciaY<myHeight){
			strecciaY = myHeight;
			strecciaX = myHeight*imgWidth/imgHeight;
			topcss = 0;
			leftcss = (myWidth - strecciaX) /2;	
		} else {
			topcss = (myHeight - strecciaY) /2;
			leftcss = 0;
		}
		bgCSS = {left: leftcss, top: topcss};	
		$("#backstretch img").width( strecciaX ).height( strecciaY ).css(bgCSS);
	});

    $("#draggable").draggable({ appendTo: 'div#container' });

	  /* ANIMAZIONE NEWS LEFT */
	  var timer3;
	  $("div.blocco_news a").mouseenter(function() {
		valoreid = $(this).attr("id");										 
		if ($("div#backstretch div#img_"+valoreid+"").length != 0) {

		   if(timer3) {
				clearTimeout(timer3);
				timer3 = null
			}	
			timer3 = setTimeout(function() {   
				$("div#backstretch div:not(#img_"+valoreid+")").fadeOut('fast');
				$("div#backstretch div#img_"+valoreid+"").fadeIn('fast');	
			}, 200);
		} else {
			$("div#backstretch div:not(#img_contiene_0)").fadeOut('fast');
		  	$("div#backstretch div#img_contiene_0").fadeIn(500);
		}
		
		
		
		
	  }).mouseleave(function() {
		if(timer3) {
			clearTimeout(timer3);
			timer3 = null
		}    
		
	  });
	 
	  $("div.pad_blocco_news").mouseleave(function() {
		  $("div#backstretch div:not(#img_contiene_0)").fadeOut('fast');
		  $("div#backstretch div#img_contiene_0").fadeIn(500);	
	  });
	 
	  
	  	var x_news_home = 1;
		var y_news_home = 1;	
		var divtot_news_home = $('div.mini_nav ul li').length;
		var rand_news_home = 1;
		$("div.mini_nav ul li a").click(function() {
			valoreid_news_home = $(this).attr("id");
			valoreid_news_home = valoreid_news_home.replace("linkhome_", "")
			Play_2_news_home(valoreid_news_home);
			return false;
		})
		
		function Play_2_news_home(valoreclick_news_home){
			$("div.mini_nav ul li a").removeClass('attivo')
			y_news_home = parseInt(valoreclick_news_home);
			
			if (x_news_home > divtot_news_home){
				x_news_home = 1;
			}
			valoreleft=-494*(y_news_home-1);
			$("div.sposta_blocco_news").animate({
				 left:valoreleft
			}, 1000,'easeInOutExpo');
			$("div.mini_nav ul li a#linkhome_"+y_news_home).addClass('attivo')
			x_news_home=y_news_home;
		}
	  
	  
}); 
