// JavaScript Document

caricaSfondo("1","boxFullscreen",iEnd);
resizeDivImgAbc("imgFull"+iAct,"boxFullscreen");
setTimeout(autoSlide,timer);

$("#iback").hover(function(){ 
	this.src = 'img/back-icoSel.png';
});
$("#iback").mouseout(function(){ 
	this.src = 'img/back-ico.png';
});

$("#inext").hover(function(){ 
	this.src = 'img/next-icoSel.png';
});
$("#inext").mouseout(function(){ 
	this.src = 'img/next-ico.png';
});

function resizeDivImgAbc(img,div){
	/*var window_height = $("#"+div).height();
	var window_width = $("#"+div).width();*/
	var window_height = window.innerHeight;
	var window_width = window.innerWidth;
	var sh = $("."+img).height();
	var sw = $("."+img).width();
	var ratio =sh/sw;
	//alert("ratio="+ratio);
	var oh = window_width*(ratio);
	var ow = window_width;
	
	if ( oh < window_height ){
		//size based on height.
		var ow = window_height/ratio;
		var oh = window_height; 			
	} 
	$("."+img).css("height", oh).css("width", ow);
};

function autoSlide(){
		//setTimeout(caricaSfondoSlade(iNext,"boxFullscreen",iEnd),5000);
		setTimeout(autoSlide,timer);
		setTimeout(boxNewsAutoFade,timer);
}


function caricaSfondo(idImg,iiEnd){
	zAll(idImg,iiEnd);
	$("#imgFull-"+idImg).css("display","block");
	$("#boxFullscreen").css("display","block");
	hideAll(idImg,iiEnd);
	//alert(".imgFull-"+idImg);
	
	//alert("Prova");
	var sorgenteTmp = $("#imgFull-"+idImg).attr("name");
	var sorgente = sorgenteTmp.replace(/---/gi,"/");
	//alert(sorgente);
	$("#imgFull-"+idImg).attr("src", sorgente);
	resizeDivImgAbc("imgFull","boxFullscreen");
};

function caricaSfondoSlade(idImg,div,iiEnd,verso){
	zAll(idImg,iiEnd);
	var window_width = $("#"+div).width();
	if(verso == "back"){
		var anim1 = {right: +window_width};
	}else{
		var anim1 = {right: -window_width};
	}
	
	var anim2 = {right: 0};
	$("#imgFull-"+idImg).animate(anim1, 0 );
	
	var sorgenteTmp = $("#imgFull-"+idImg).attr("name");
	var sorgente = sorgenteTmp.replace(/---/gi,"/");
	//alert(sorgente);
	$("#imgFull-"+idImg).attr("src", sorgente);
	$("#imgFull-"+idImg).css("display","block");
	$("#boxFullscreen").css("display","block");
	resizeDivImgAbc("imgFull","boxFullscreen");
	
	$("#imgFull-"+idImg).animate(anim2, timerSlide );
	
	hideAll(idImg,iiEnd);
	
};

function caricaSfondoFade(idImg,div,iiEnd,verso){
	zAll(idImg,iiEnd);
	var window_width = $("#"+div).width();
	var anim1 = {opacity: 0};
	var anim2 = {opacity: 1};
	$("#imgFull-"+idImg).animate(anim1, 0 );
	
	var sorgenteTmp = $("#imgFull-"+idImg).attr("name");
	var sorgente = sorgenteTmp.replace(/---/gi,"/");
	//alert(sorgente);
	$("#imgFull-"+idImg).attr("src", sorgente);
	$("#imgFull-"+idImg).css("display","block");
	$("#boxFullscreen").css("display","block");
	resizeDivImgAbc("imgFull","boxFullscreen");
	
	$("#imgFull-"+idImg).animate(anim2, timerSlide );
	
	hideAll(idImg,iiEnd);
	
};

function caricaSfondoKenBurns(idImg,div,iiEnd,verso){
	zAllKenBurns(idImg,iiEnd);
	var window_width = $("#"+div).width();
	var window_height = $("#"+div).height();
	var maxHeight = window_height*2;
	var maxWidth = window_width*2;
	var middleHeight = window_height*1.2;
	var middleWidth = window_width*1.2;
	var tolleranzaH = window_height*1.2-window_height;
	var tolleranzaW = window_width*1.2-window_width;
	var num = Math.round(3*Math.random());
	$("#imgFull-"+idImg).css("top","").css("right","").css("left","").css("bottom","");
	switch(num){
		case 1:
			var anim1 = {
				opacity: 0,
				right:-tolleranzaW,
				top:-tolleranzaH,
				height:maxHeight,
				width:maxWidth
			};
			var anim2 = {
				opacity: 1,
				right:0,
				top:0,
				height:middleHeight,
				width:middleWidth
			};
		break;
			
		case 2:
			var anim1 = {
				opacity: 0,
				right:0,
				top:-tolleranzaH,
				height:maxHeight,
				width:maxWidth
			};
			var anim2 = {
				opacity: 1,
				right:0,
				top:0,
				height:middleHeight,
				width:middleWidth
			};
		break;
		
		case 3:
			var anim1 = {
				opacity: 0,
				right:-tolleranzaW,
				top:-tolleranzaH,
				height:maxHeight,
				width:maxWidth
			};
			var anim2 = {
				opacity: 1,
				right:0,
				top:0,
				height:middleHeight,
				width:middleWidth
			};
		break;
		
		default:
			var anim1 = {
				opacity: 0,
				right:0,
				top:0,
				height:maxHeight,
				width:maxWidth
			};
			var anim2 = {
				opacity: 1,
				right:0,
				top:0,height:middleHeight,
				width:middleWidth
			};
		break;
		
		
	}
	
	$("#imgFull-"+idImg).animate(anim1, 0 );
	
	var sorgenteTmp = $("#imgFull-"+idImg).attr("name");
	var sorgente = sorgenteTmp.replace(/---/gi,"/");
	//alert(sorgente);
	$("#imgFull-"+idImg).attr("src", sorgente);
	$("#imgFull-"+idImg).css("display","block");
	$("#boxFullscreen").css("display","block");
	//resizeDivImgAbc("imgFull","boxFullscreen");
	
	$("#imgFull-"+idImg).animate(anim2, timerSlideKB );
	
	//setTimeout(hideAll(idImg,iiEnd),timerSlideKB);;
	
};


function zAllKenBurns(idImg,iiEnd){
	for(var i=1; i<=iiEnd; i++){
		if(i != idImg && i != idImg-1){
			$("#imgFull-"+i).css("z-index","-3");
		}else{
			if(i != idImg){
				$("#imgFull-"+i).css("z-index","-1");
			}else{
				$("#imgFull-"+i).css("z-index","0");
			}
			
		}
		//alert(".imgFull-"+i);
	}
}


function zAll(idImg,iiEnd){
	for(var i=1; i<=iiEnd; i++){
		if(i != idImg){
			$("#imgFull-"+i).css("z-index","-2");
		}else{
			$("#imgFull-"+i).css("z-index","-1");
		}
		//alert(".imgFull-"+i);
	}
}


function hideAll(idImg,iiEnd){
	setTimeout(function(){
		for(var i=1; i<=iiEnd; i++){
			if(i != idImg){
				$("#imgFull-"+i).css("display","none");
			}
			//alert(".imgFull-"+i);
		}
	},timerSlide);
	
}

window.onresize = function() {
	//alert("ciao");
	resizeDivImgAbc("imgFull","boxFullscreen");
};

$("#microns a").click(function(e){
		e.preventDefault();
		checkAuto = 1;
		// Prevent Double-Click
		//ABCINTERACTIVE
	
		document.getElementById("btnnext").name = iNext;
		document.getElementById("btnback").name = iBack;
		if($(this).attr("name") == iNext){
			if(iNext == iEnd){
				iNext = 1;
			}else{
				iNext++;
			}
			
			if(iBack == iEnd){
				iBack = 1;
			}else{
				iBack ++;
			}
		}else{
			if(iNext == 1){
				iNext = iEnd;
			}else{
				iNext--;
			}
			
			if(iBack == 1){
				iBack = iEnd;
			}else{
				iBack --;
			}
		}
		if($(this).attr("id") == "btnback"){
			caricaSfondoSlade($(this).attr("name"),"boxFullscreen",iEnd,"back");
		}else{
			caricaSfondoSlade($(this).attr("name"),"boxFullscreen",iEnd,"next");
		}
		
		boxNews("boxNewsHomeInt"+$(this).attr("name"));
		//END ABCINTERACTIVE
});

function boxNewsAutoSlide(){
	if(checkAuto == 0){
		document.getElementById("btnnext").name = iNext;
		document.getElementById("btnback").name = iBack;
		if(iNext == iEnd){
			iNext = 1;
		}else{
			iNext++;
		}
		
		if(iBack == iEnd){
			iBack = 1;
		}else{
			iBack ++;
		}
		caricaSfondoSlade($("#btnnext").attr("name"),"boxFullscreen",iEnd,"next");		
		boxNews("boxNewsHomeInt"+$("#btnnext").attr("name"));
		//END ABCINTERACTIVE
	}else{
		checkAuto = 0;
	}
}

function boxNewsAutoFade(){
	if(checkAuto == 0){
		document.getElementById("btnnext").name = iNext;
		document.getElementById("btnback").name = iBack;
		if(iNext == iEnd){
			iNext = 1;
		}else{
			iNext++;
		}
		
		if(iBack == iEnd){
			iBack = 1;
		}else{
			iBack ++;
		}
		caricaSfondoFade($("#btnnext").attr("name"),"boxFullscreen",iEnd,"next");		
		boxNews("boxNewsHomeInt"+$("#btnnext").attr("name"));
		//END ABCINTERACTIVE
	}else{
		checkAuto = 0;
	}
}

function boxNewsAutoKenBurns(){
	if(checkAuto == 0){
		document.getElementById("btnnext").name = iNext;
		document.getElementById("btnback").name = iBack;
		if(iNext == iEnd){
			iNext = 1;
		}else{
			iNext++;
		}
		
		if(iBack == iEnd){
			iBack = 1;
		}else{
			iBack ++;
		}
		caricaSfondoKenBurns($("#btnnext").attr("name"),"boxFullscreen",iEnd,"next");		
		boxNews("boxNewsHomeInt"+$("#btnnext").attr("name"));
		//END ABCINTERACTIVE
	}else{
		checkAuto = 0;
	}
}


	function boxNews(idbox){
		$("#boxNewsHomeInt1").css("display","none");
		$("#boxNewsHomeInt2").css("display","none");
		$("#boxNewsHomeInt3").css("display","none");
		$("#boxNewsHomeInt4").css("display","none");
		$("#boxNewsHomeInt5").css("display","none");
		$("#boxNewsHomeInt6").css("display","none");
		$("#"+idbox).css("display","block");
		$("#"+idbox).css("opacity","0");
		var anim1 = {opacity: 1};
		$("#"+idbox).animate(anim1, timerSlide );
	};
	
	
	$("#btnComunicazione").click(function(){ 
	 //boxNewsHomeInt6
		
		$("#homeComunicazioneDW").animate({
			opacity: 'toggle'
		}, 1 );
		$("#homeComunicazioneUP").animate({
			opacity: 'toggle'
		}, 1 );
		if( $("#boxSlideHome1Up").height() == '160'){
			$("#boxSlideHome1Up").animate({ 
				height:0
			}, 500 );	
			$("#boxSlideHome1Dwn").animate({
				opacity: 'toggle',
				height:140
			}, 500 );
			iNext = 3;
			iBack = 1;
			
		
			document.getElementById("btnnext").name = iNext;
			document.getElementById("btnback").name = iBack;
			checkAuto = 1;
			caricaSfondo("2",iEnd);
			boxNews("boxNewsHomeInt2");
		}else{
			$("#boxSlideHome1Up").animate({ 
				height:160
			}, 500 );	
			$("#boxSlideHome1Dwn").animate({ 
				opacity: 'toggle',
				height:0
			}, 500 );
		}			
	});
	
	
	$("#btnWeb").click(function(){ 
			
		$("#homeWebDW").animate({
			opacity: 'toggle'
		}, 1 );
		$("#homeWebUP").animate({
			opacity: 'toggle'
		}, 1 );
		if( $("#boxSlideHome2Up").height() == '160'){
			$("#boxSlideHome2Up").animate({ 
				height:0
			}, 500 );	
			$("#boxSlideHome2Dwn").animate({
				opacity: 'toggle',
				height:140
			}, 500 );
			
			iNext = 4;
			iBack = 2;
			
		
			document.getElementById("btnnext").name = iNext;
			document.getElementById("btnback").name = iBack;
			checkAuto = 1;
			caricaSfondo("3",iEnd);
			boxNews("boxNewsHomeInt3");
		}else{
			$("#boxSlideHome2Up").animate({ 
				height:160
			}, 500 );	
			$("#boxSlideHome2Dwn").animate({
				opacity: 'toggle', 
				height:0
			}, 500 );
		}			
	});
	
	$("#btnSoftware").click(function(){ 
			
		$("#homeSoftwareDW").animate({
			opacity: 'toggle'
		}, 1 );
		$("#homeSoftwareUP").animate({
			opacity: 'toggle'
		}, 1 );
		if( $("#boxSlideHome3Up").height() == '160'){
			$("#boxSlideHome3Up").animate({ 
				height:0
			}, 500 );	
			$("#boxSlideHome3Dwn").animate({
				opacity: 'toggle',
				height:140
			}, 500 );
			iNext = 5;
			iBack = 3;
			
		
			document.getElementById("btnnext").name = iNext;
			document.getElementById("btnback").name = iBack;
			checkAuto = 1;
			caricaSfondo("4",iEnd);
			boxNews("boxNewsHomeInt4");
		}else{
			$("#boxSlideHome3Up").animate({ 
				height:160
			}, 500 );	
			$("#boxSlideHome3Dwn").animate({ 
				opacity: 'toggle',
				height:0
			}, 500 );
		}
	});

