function printDiv(){  
	var a=window.open('','','width=480,height=640,scrollbars');  
	a.document.open("text/html");  
	a.document.write(document.getElementById('hiddenimprint').innerHTML);  
	a.document.close();  
	a.print();  
}  
function dimensioni(){
	if(typeof(window.innerWidth)=='number'){//Non-IE
		larghezza = window.innerWidth;
		altezza = window.innerHeight;
	}else if(document.documentElement&&(document.documentElement.clientWidth||document.documentElement.clientHeight)){//IE 6+ in 'standards compliant mode'
		larghezza = document.documentElement.clientWidth;
		altezza = document.documentElement.clientHeight;
	}else if(document.body&&(document.body.clientWidth||document.body.clientHeight)){//IE 4 compatible
		larghezza = document.body.clientWidth;
		altezza = document.body.clientHeight;
	}
	if(larghezza<1044) {larghezza=1044;}
	if(altezza<650) {altezza=650;}
}
function adjust(time){
	$(".pagina").width(larghezza).height(altezza-20);
	$("#wrapper").width(larghezza*3);
	switch(pospagina){
		case "gallery":
			$("title").html("Gallery | Igor D'India");
			if(altezza<limitegallery) $("#videogallery").height(limitegallery).width(larghezza-30);
			pagaltezza=$("#videogallery").height();
			paglarghezza=$("#videogallery").width();
			pos=$("#videogallery").position();
			pos.left=larghezza;
			$("#credits").animate({height:0},500);
		break;
		case "news":
			$("title").html("News | Igor D'India");
			pagaltezza=$("#news").height();
			paglarghezza=$("#news").width();
			pos=$("#news").position();
			$("#credits").animate({height:45},500);
		break;
		case "about": 
			$("title").html("About | Igor D'India");
			pagaltezza=$("#about").height();
			paglarghezza=$("#about").width();
			pos=$("#about").position();
			$("#credits").animate({height:45},500);
		break;
		case "contact": 
			$("title").html("Contact | Igor D'India");
			pagaltezza=$("#contact").height();
			paglarghezza=$("#contact").width();
			pos=$("#contact").position();
			pos.left=larghezza;
			$("#credits").animate({height:45},500);
		break;
		default:
		case "home": 
			$("title").html("Igor D'India");
			pagaltezza=$("#index").height();
			paglarghezza=$("#index").width();
			pos=$("#index").position();
			$("#credits").animate({height:45},500);
		break;
	}
	$("#wrapper").stop().animate({"top":-pos.top, "left":-pos.left},time);
	//$("#maschera").width(paglarghezza).height(pagaltezza); 
	setTimeout(function(){$("#maschera").animate({"width":paglarghezza, "height":pagaltezza},0)},500);
	/*$("#argh").html('larghezza: '+larghezza+'<br/>altezza: '+altezza+'<br/>wrapperTop: '+-pos.top+'<br/>wrapperLeft: '+-pos.left+'<br/>pagaltezza: '+pagaltezza+'<br/>paglarghezza: '+paglarghezza);*/
}
function slideshow(timer){
	if(timer>0){
		timer-=3;
		$(".timer").width(timer+'px');
		setTimeout(function(){slideshow(timer);},50);	
	}else{
		$(".multi:visible").fadeOut(500).siblings().fadeIn(500);
		timer=640;
		setTimeout(function(){slideshow(timer);},50);
	}
}
function slider_thumbs(){
	if(passo>=0){
		$("#up").removeClass("abled").addClass("disabled").animate({opacity:0.1},500);
		$("#down[class=disabled]").removeClass("disabled").addClass("abled").animate({opacity:1},500);
	} else if(passo<0 && ((passo + $("#containerthumbs").height())>$("#mascherathumbs").height())){
		$("#up[class=disabled], #down[class=disabled]").removeClass("disabled").addClass("abled").animate({opacity:1},500);
	} else if((passo + $("#containerthumbs").height())<=$("#mascherathumbs").height()){
		$("#down[class=abled]").removeClass("abled").addClass("disabled").animate({opacity:0.1},500);
		$("#up[class=disabled]").removeClass("disabled").addClass("abled").animate({opacity:1},500);
	}
}

$(document).ready(function(){
	/*DIMENSIONI DELLA FINESTRA*/
	limitegallery=725;
	dimensioni();
	adjust(0);
	
	/*CONTROLLO SUL RESIZE*/
	$(window).resize(function(){
		dimensioni();
		adjust(0);
	}).trigger("resize");
	
	/*SPOSTAMENTO MASCHERA*/
	$("a[rel=home]").click(function(event){
		pospagina="home";
		adjust(500);
		event.preventDefault();
		$(this).blur();
	});
	$("a[rel=videogallery]").click(function(event){
		pospagina="gallery";
		adjust(500);
		event.preventDefault();
		$(this).blur();
	});
	$("a[rel=news]").click(function(event){
		pospagina="news";
		adjust(500);
		event.preventDefault();
		$(this).blur();
	});
	$("a[rel=about]").click(function(event){
		pospagina="about";
		adjust(500);
		event.preventDefault();
		$(this).blur();
	});
	$("a[rel=contact]").click(function(event){
		pospagina="contact";
		adjust(500);
		event.preventDefault();
		$(this).blur();
	});
	
	/*ALL*/
		/*ROOT*/
		$("#nav").animate({top:-50},0);
		$("#creditsbg").animate({opacity:0},0);
		//$("#alcolicalogo").animate({width:17},0);
		$("#infocredits").animate({opacity:0},0);

		/*HOVER*/
		thover=0;
		t1hover=0;
		apricredits=0;
		$("#nav").mouseenter(function(){		
			$(this).stop().animate({top:0},500);
			$("#linguettamenu").animate({opacity:0},100);
			clearTimeout(thover);
		}).mouseleave(function(){
			thover=setTimeout(function(){
				$("#nav").stop().animate({top:-50},500);
				$("#linguettamenu").animate({opacity:1},600);
			},1000);
		});
		
		$("#alcolicalogo").mouseenter(function(){
			$("#creditsbg").stop().animate({opacity:1},1000);			
			//$("#alcolicalogo").stop().animate({width:113},500);
			$("#infocredits").stop().animate({opacity:1},1000, function(){apricredits=1;});
			clearTimeout(t1hover);
		});
		$("#infocredits").mouseenter(function(){
			if(apricredits){
				$("#creditsbg").stop().animate({opacity:1},1000);			
				//$("#alcolicalogo").stop().animate({width:113},500);
				$("#infocredits").stop().animate({opacity:1},1000);
				clearTimeout(t1hover);
			}
		}).mouseleave(function(){
			t1hover=setTimeout(function(){
				apricredits=0;
				$("#creditsbg").stop().animate({opacity:0},1250);
				$("#infocredits").stop().animate({opacity:0},1000);
				//$("#alcolicalogo").stop().animate({width:17},500);
			},1000);
		});
	
	/*NEWS*/
		/*ROOT*/
		$("div.newsbox").animate({opacity:0},0).css("z-index", "1");
		$("div.newsbox[id="+newsid+"]").animate({opacity:1},0).css("z-index", "10");
		$("div.newsbox:first .prev").fadeOut(0);
		$("div.newsbox:last .next").fadeOut(0);	
		
		/*CLICK*/
		$(".newsnav .next").click(function(event){
			var box=$(this).parent().parent().parent();
			box.animate({opacity:0},500).css("z-index", "1")
			   .next().stop().animate({opacity:1},500).css("z-index", "10");
			event.preventDefault();
		});
		
		$(".newsnav .prev").click(function(event){
			var box=$(this).parent().parent().parent();
			box.animate({opacity:0},500).css("z-index", "1")
			   .prev().stop().animate({opacity:1},500).css("z-index", "10");
			event.preventDefault();
		});
		
		/*SLIDE IMMAGINI*/
		$(".timer").animate({opacity:0.6},0).next(".multi").fadeIn(0);
		slideshow(640);	
	
	/*INDEX*/
		/*ROOT*/
		aprisottomenu=0;
		$("#indexmenu li.sottomenu").animate({height:0},0);
		
		/*HOVER*/	
		$("#indexmenu li.primomenu").hover(function(){
			$(this).stop().animate({width:550},500);
		}, function(){
			if(aprisottomenu==0)$(this).stop().animate({width:87},500);
		});
		$("#indexmenu li.sottomenu").hover(function(){
			$(this).prev("li").stop().animate({width:550},500);
		}, function(){
			aprisottomenu=0;
			setTimeout(function(){$("#indexmenu li.sottomenu").stop().animate({height:0},{duration:500,queue:false});},500);
			setTimeout(function(){$("#imprint").parent().animate({opacity:1},500);},500);
			setTimeout(function(){$("#indexmenu li.sottomenu").prev("li").stop().animate({width:87},500);},1000);
			$("a.under").parent().animate({backgroundPosition:"0px -135px"},0);
		});
		
		$("a.under").hover(function(){
			$(this).parent().stop().animate({backgroundPosition:'(0px 0px)'},{duration:500,queue:false});
		}, function(){
			$(this).parent().stop().animate({backgroundPosition:'(0px -135px)'},{duration:500,queue:false});
		});
		
		/*CLICK*/
		$("#menu").click(function(event){
			aprisottomenu=1;
			$(this).parent().stop().animate({width:550},500);
			$("#imprint").parent().animate({opacity:0},500);
			$("#indexmenu li.sottomenu").animate({height:135},500);
			event.preventDefault();
		});
		
		$("#imprint").click(function(event){
			$(".sfondoimprint").fadeIn(500);
			event.preventDefault();
		});
		
		$("#imgimprint .close").click(function(event){
			$(".sfondoimprint").fadeOut(500);
			event.preventDefault();
		});
		
	/*ABOUT*/
		/*ROOT*/
		visibleHeight=$("#containertext").height();
		if($("#text").height()>visibleHeight){
			totalContent=$("#text").height();
			minDraggerHeight=$("#scroller").height();
			draggerContainerHeight=$("#containerscroller").height();
			adjDraggerHeight=totalContent-((totalContent-visibleHeight)*5); //adjust dragger height analogous to content
			if(adjDraggerHeight>minDraggerHeight){ //minimum dragger height
				$('#scroller').css('height',adjDraggerHeight+'px');
				$('#scroller').css('line-height',adjDraggerHeight+'px');
			} else {
				$('#scroller').css('height',minDraggerHeight+'px');
				$('#scroller').css('line-height',minDraggerHeight+'px');
			}
			if(adjDraggerHeight<draggerContainerHeight){ //maximum dragger height
				$('#scroller').css('height',adjDraggerHeight+'px');
				$('#scroller').css('line-height',adjDraggerHeight+'px');
			} else {
				$('#scroller').css('height',draggerContainerHeight-10+'px');
				$('#scroller').css('line-height',draggerContainerHeight-10+'px');
			}
			animSpeed=400; //animation speed
			easeType='easeOutCirc'; //easing type
			bottomSpace=1.05; //bottom scrolling space
			targY=0;
			draggerHeight=$('#scroller').height();
			$('#scroller').draggable({ 
				axis: 'y', 
				containment: 'parent', 
				drag: function(event, ui) {
					Scroll();
				}, 
				stop: function(event, ui) {
					DraggerOut();
				}
			});
			//scrollbar click
			$('#containerscroller').click(function(e) {
				var mouseCoord=(e.pageY - $(this).offset().top);
				var targetPos=mouseCoord+$("#scroller").height();
				if(targetPos<$("#containerscroller").height()){
					$("#scroller").css("top",mouseCoord);
					Scroll();
				} else {
					$("#scroller").css("top",$("#containerscroller").height()-$("#scroller").height());
					Scroll();
				}
			});
			//mousewheel
			$(function($) {
				$('#containertext').bind('mousewheel', function(event, delta) {
					vel = Math.abs(delta*10);
					$('#scroller').css('top', $('#scroller').position().top-(delta*vel));
					Scroll();
					if($('#scroller').position().top<0){
						$('#scroller').css('top', 0);
						$('#text').stop();
						Scroll();
					}
					if($('#scroller').position().top>$('#containerscroller').height()-$('#scroller').height()){
						$('#scroller').css('top', $('#containerscroller').height()-$('#scroller').height());
						$('#text').stop();
						Scroll();
					}
					return false;
				});
			});
			function Scroll(){
				var scrollAmount=(totalContent-(visibleHeight/bottomSpace))/(draggerContainerHeight-draggerHeight);
				var draggerY=$('#scroller').position().top;
				targY=-draggerY*scrollAmount;
				var thePos=$('#text').position().top-targY;
				$('#text').stop().animate({top: '-='+thePos}, animSpeed, easeType); //with easing
				//$('#containertext .container').css('top',$('#containertext  .container').position().top-thePos+'px'); //no easing
			}
			$("#scroller").mouseup(function(){
				DraggerOut();
			}).mousedown(function(){
				DraggerOver();
			});
			function DraggerOver(){
				$('#scroller').css('background-color', '#ccc');
			}
			function DraggerOut(){
				$('#scroller').css('background-color', '#1a171b');
			}
		} else {
			$('#scroller').css('display','none');
			$('#containerscroller').css('display','none');
		}
		
		$(".big2, .big3, .big4").animate({opacity:0},0);
		
		/*HOVER*/
		$("img.small").hover(function(){
			$(this).stop().animate({opacity:0.25},500);
			var rel=$(this).attr("rel");
			$("."+rel).stop().animate({opacity:1},500).siblings("img, p").stop().animate({opacity:0},500);
		}, function(){
			$(this).stop().animate({opacity:1},500);
		});
	
	/*CONTACT*/
		/*CLICK*/
		$("#contact div.sfondocontact").click(function(){
			$(this).fadeOut(500);
		});
		
	/*GALLERY*/
		/*ROOT*/
		numero_thumbs=0;
		passo=0;
		$("a.minithumb").each(function(){numero_thumbs++;});
		if(numero_thumbs>3){
			slider_thumbs();			
		} else {$("#down, #up").removeClass("abled").addClass("disabled").animate({opacity:0},500);}
		
		$("a.minithumb").hover(function(){
			$(this).children(".overplay").stop().animate({opacity:1},500);
		}, function(){
			$(this).children(".overplay").stop().animate({opacity:0},500);
		});
		
		/*CLICK*/
		$("#videosCnt .close").click(function(){
			$("#videosCnt").fadeOut(1000);
			$(".cartella").fadeOut(1000);
		});
		$("a.minithumb").click(function(event){
			event.preventDefault();
			var thisRel=$(this).attr("href");
			$("#videosCnt").fadeIn(500);
			$(".cartella[ref="+thisRel+"]").fadeIn(1000);
			$(".cartella[ref!="+thisRel+"]").fadeOut(1000);
		});
		$("#down").bind("click", function(event){
			classe=$(this).attr('class');
			if(classe=="abled"){
				passo=$("#containerthumbs").position().top-114;
				if(passo<-114*(numero_thumbs-5)) passo=-114*(numero_thumbs-3);	
				$("#containerthumbs").animate({top:passo},250);
				slider_thumbs();
				event.preventDefault();
				$(this).blur();
			} else {
				event.preventDefault();
				$(this).blur();
			}
		});
		
		$("#up").bind("click", function(event){
			classe=$(this).attr('class');
			if(classe=="abled"){
				passo=$("#containerthumbs").position().top+114;
				if(passo>0) passo=0;	
				$("#containerthumbs").animate({top:passo},250);
				slider_thumbs();
				event.preventDefault();
				$(this).blur();
			} else {
				event.preventDefault();
				$(this).blur();
			}
		});
});
