var animDuration = 500

var menuTmrId=0
var menuMouseOver = false

var musicPaused = false;

$(function(){

	// menu hover
	$("div#menu").hover(function(){
		if ($("div#menu").data("hover_tmr"))
			clearTimeout($("div#menu").data("hover_tmr"))
		showMenu()
	},function(){
		if (acms.currentPage=="home") return;
		if ($("div#menu").data("hover_tmr"))
			clearTimeout($("div#menu").data("hover_tmr"))
		
		$("div#menu").data("hover_tmr",setTimeout(hideMenu,250))
	})
	
	$("div#menu li img").hover(function(){
		$(this).animate({
			left : "-20px"
		},animDuration)
	},function(){
		$(this).animate({
			left : "0px"
		},animDuration/2)
	})
	
	
	// jspane
	$('div#panel').jScrollPane({
		showArrows :true,
		verticalDragMinHeight : "20",
		verticalDragMaxHeight : "20"
	});
	$(window).resize(function(){
		if ($('div#panel').data("jsp")) $('div#panel').data("jsp").reinitialise()
	})
	
	
	
	// init acms
	acms.init({
		
		googleAnalytics : "UA-15046404-8",
		
		pagePreloaded : true,
		
		loadingDelay : 1000,
		
		loadIntro : function() {
			
			
			var introDiv = $("<div/>")
			introDiv.attr("id","introDiv")
			var imgDiv = $("<div/>")
			imgDiv.append("<img src='img/AdrianoGherardini.png' alt='' />")
			imgDiv.append("<br/>")
			imgDiv.append("<img src='img/baffo.png' alt='' />")
			imgDiv.find("img").fadeTo(0,0)
			introDiv.append(imgDiv)
			$("div#container").append(introDiv)
			
			acms.utils.preloadImages(
					[
					 	"img/AdrianoGherardini.png",
					 	"img/baffo.png",
          			 ],acms.showIntro);	
			
			
		},
		
		showIntro : function() {
			
			$("div#introDiv img:eq(0)").animate({
				opacity:1
			},animDuration*2,function(){
				
				$("div#introDiv img:eq(1)").animate({
					opacity:1
				},animDuration*2,function(){
					
					
					setTimeout(function(){
						
						$("div#introDiv").animate({
							opacity:0
						},function(){
							$(this).remove()
							acms.gotoPage("home");
						})
						
					},animDuration*4)
					
					
				})
				
			})
			
			
			
			
			
		},
		
		openPage : function(page,state){
						
			acms.utils.preloadImages([
			      "img/titoli/"+page+".png",
			      "img/adriano/"+page+".jpg",
			],function(){
				
			
				
				if ($("img#adriano").height()>100) $("img#adriano").fadeTo(animDuration,1); // use always images width height >100
				$("div#title").fadeTo(animDuration,1)
				setTimeout(function(){
					$("div#content > div#panel")
								.stop()
								.animate({
									opacity:1
								},animDuration,function(){
									acms.pageClosed = false;
									if (page=="home") {showMenu()}
								})
						
				},animDuration)
				
				
			})
			
		},
		
		
		injectPage : function(xml) {
			var title = $("response > title",xml).text()
			var css   = $("response > css",xml).text()
			var html  = $("response > html",xml).text()
			var js    = $("response > js",xml).text()
				 
			document.title = title
			
			//$("html > head > style").text(css) not working in IE8
			//$("html > head > style").html(css) not working in IE8
			$("html > head > style#pageStyle").remove()
			$("html > head").append("<style id='pageStyle'>"+css+"</style>")
			
			$("div#title").css("background","transparent url(img/titoli/"+acms.currentPage+".png) no-repeat center center")
			$("img#adriano").attr("src","img/adriano/"+acms.currentPage+".jpg")
			
			$("div#content > div#panel .jspPane").html(html)
			
			jQuery.globalEval(js)
			
			acms.processHtml($("div#content > div#panel .jspPane"));
			
			if ($('div#panel').data("jsp")) {
				var jspapi = $('div#panel').data("jsp")
				jspapi.reinitialise()
				jspapi.scrollTo(0,0)
			}
						
		},
		
		closePage : function(page,state,immediate) {
			var closeAnimDuration = animDuration
			if (immediate) closeAnimDuration = 0
			
			hideMenu()
			
			$(".aSlideshow-container").aSlideshow("destroy");
			$(".aTip-hook").aTip("destroy");
			
			if (page=="musica") {
				$("div#bg").fadeOut(closeAnimDuration,function(){
					$(this).remove()
				})
				
				
			}
			
			
			
			if (page=="musica" || page=="video") {
				if (musicPaused) {
					soundManager.stopAll();
					if (acms.utils.cookie.get("bgmusic")!="no") {
						playing = true
						playNext();
					}
					musicPaused=false;
				}
				$("div#player").fadeIn(animDuration)
			}
			
			
			
			$("div#title").fadeTo(closeAnimDuration,0)
			$("img#adriano").fadeTo(closeAnimDuration,0)
			
			$("div#content > div#panel")
				.stop()
				.animate({
					opacity:0
				},closeAnimDuration,function(){				
					acms.pageClosed = true;
				})
		},
		
		
		showLoading : function() {
			$("div#loading").stop().fadeIn(500)
		},
		
		
		hideLoading : function() {
			$("div#loading").stop().fadeOut(500)
		}
		
		
		
		
		
		
	})

	
	
})


function hideMenu() {
	$("div#menu li").fadeOut(animDuration,function(){
		$("div#menu").stop().animate({
			width:"140px",
			height:"120px"
		},animDuration)
	})
}

function showMenu() {
	$("div#menu").stop().animate({
		height:"385px",
		width:"405px"
	},animDuration,function(){
		$("div#menu li").fadeIn(animDuration)
	})
	
}



