var animDuration = 500

var menuTmrId=0

$(function(){
	
	
	
	// rotator init
	$("#rotator").rotator({
		delay: 5000,
		duration:1500,
		debug:false
	});
	
	
	
	// menu li highlight
	$(window).resize(moveMenuHighlight);
	
	
	
	// init acms
	acms.init({
		
		googleAnalytics : "UA-15046404-6",
		
		pagePreloaded : true,
		
		loadingDelay : 1000,
		
		loadIntro : function() {
			
			$("div#header").css({top:"-250px"})	
			$("div#content").css({top:"0px",bottom:"0px"})	
			$("div#footer").css({bottom:"-100px"})

			$("div#player").css({bottom:"-10px",visibility:'hidden'})
			
			$("img#leftIcon").css({left:"-70px"})
			$("img#rightIcon").css({right:"-90px"})
			
			acms.utils.preloadImages(
					[
					 	"img/body.jpg",
					 	"img/menuBar.png",
					 	"img/footerBar.png",
					 	"img/rotator/foto1.jpg"
          			 ],acms.showIntro);			
		},
		
		showIntro : function() {
			var slideDuration = 4000
			
			
			
			
			$("div#header").animate({top:"0px"},slideDuration)
			$("div#content").animate({top:"220px",bottom:"35px"},slideDuration)
			$("div#footer").animate({bottom:"0px"},slideDuration,function(){
				
				$("div#player").css({visibility:'visible'}).animate({bottom:"25px"},slideDuration/2)
				
				$("img#leftIcon").animate({left:"20px"},slideDuration/2)
				$("img#rightIcon").animate({right:"20px"},slideDuration/2)
				
				
				$("div#menu").append("<span id='highlight'>&nbsp;</span>")
				
				acms.gotoPage("home");
				
				
			})
		},
		
		openPage : function(){
			
			moveMenuHighlight();
			
			$("div#content > div#panel")
				.stop()
				.animate({
					opacity:1
				},animDuration,function(){
					acms.pageClosed = false;
				})
		},
		
		
		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#content > div#panel").html(html)
			
			jQuery.globalEval(js)
			
			acms.processHtml($("div#content > div#panel"));
			
			// prettyPhoto
			$('div#content > div#panel a[rel^="prettyPhoto"]').prettyPhoto({
				animation_speed: "slow",
				show_title : false,
				deeplinking : false,
				social_tools : false
			})
				
			
		},
		
		closePage : function(page,state,immediate) {
			var closeAnimDuration = animDuration
			if (immediate) 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 moveMenuHighlight() {
	var li
	
	if(acms.currentPage=="mappa")
		li = $("div#menu ul a[rel='address:/contatti']").parent("li")
	else
		li = $("div#menu ul a[rel='address:/"+acms.currentPage+"']").parent("li")
		
	// append it
	if ($("div#menu span#highlight").size()==0) $("div#menu").append("<span id='highlight'>&nbsp;</span>")
		
	if (li.size()>0) {
		var bg = $("div#menu span#highlight")
		
		
		
		bg.stop().animate({
			left : li.position().left - ( 75 - li.width()/2) +22
		},800);	
	}
	
}



