// JavaScript Document
var window_width;
var gallery_image_index = null;
var gallery_name = null;
var is_moving = false;
var flag_autoplay = 1;
var timeout_gallery;
var flag_zoom = 0;
$(document).ready(function() {   
	window_width = 	parseInt($(window).width());	
	$(".scroll-pane").jScrollPane({animateTo:true, scrollbarWidth:15});
	$.ifixpng('img/pixel.gif');                             
    $('img[@src$=.png]').ifixpng();
	$('#centre').ifixpng();
	$('#home_video_contenitor').ifixpng();
	$('#home_text').ifixpng();
	$('.text_sep').ifixpng();
	$(".page").ifixpng();
	$(window).bind('resize', function() {
		adjustResize();
	});


	$("#iscriviti_newsletter").click(function() {
		chiamaBlockUI();									  
	});
	
	$("#iscriviti_newsletter").mouseover(function() {
		$(this).css("color","#000");											  
	}).mouseout(function() {
		$(this).css("color","#FFF");											  
	});
	initialize();
	if (jQuery.browser.msie) {
		$("#didattica_img").css("marginRight","2px");
		if (parseInt(jQuery.browser.version) < 7) {
			$(".gallery").ifixpng();
			$(".gallery").css("backgroundColor","#FFF");
			$(".page").css("top","322px");
		}
	} else {
		$("#home_orari").css("textAlign","left");	
		$("#home_infop").css("textAlign","left");	
	}
	$(".scrollto_submenu_info").click(function() {
		var targetElementSelectorString = $(this).attr('rel');
		var scrollto = parseInt($("#"+targetElementSelectorString).offset().top);
		$("#info_scroll")[0].scrollTo(scrollto - parseInt($("#info_scroll").offset().top));
		return false;
	});
	$(".scrollto_submenu_eventi").click(function() {
		var targetElementSelectorString = $(this).attr('rel');
		var scrollto = parseInt($("#"+targetElementSelectorString).offset().top);

		$("#eventi_scroll")[0].scrollTo(scrollto - parseInt($("#eventi_scroll").offset().top));
		return false;
	});
	
	$(".menu a").click(function() {
		if (is_moving) { return false; }
		if ($(this).attr("clicked") == 1) { return false; } 
		/*if ((parseInt($("#"+$(this).attr("rel")).css("left")) < window_width) && (parseInt($("#"+$(this).attr("rel")).css("left")) > 0)) {
				return false;
		}*/
		initialize();
		
		$(".menu a").attr("clicked","0")
		
		$(this).children().unbind('mouseover').unbind('mouseout');
		//$(this).unbind('click');
		$(this).attr("clicked","1");
		$(this).children().attr("src",$(this).children().attr("src").replace("_OFF","_ON"));
				
		$(".lens").css("display","none");
		movecenteredpage();
		/**/
		if ($(this).attr("rel") == "gallery") {		
			//if (parseInt($("#gallery1").css("left")) == ((window_width/2)-420)) { return false; }
			//if (parseInt($("#gallery2").css("left")) == ((window_width/2)-420)) { return false; }
			if (parseInt($("#gallery1").css("left")) == "50%") { return false; }
			if (parseInt($("#gallery2").css("left")) == "50%") { return false; }
			
			flag_autoplay = 1;
			gallery_name = "opere";
			//switch on opere
			$("img").filter(function(index) {
			  return ($(this).attr("rel") == "gallery_opere");
			}).attr("src","img/submenu_opere_ON.gif").unbind('mouseover').unbind('mouseout');
			

			gallery_image_index = 1;
			setTimeout("showgallery();",1200);
		} else {
			if ($(this).attr("rel") == "video") {
				$("#video_video_contenitor").css("display","block");
			}
			if ($(this).attr("rel") == "home") {
				$("#home_video_contenitor").css("display","block");
			}
			if (flag_autoplay) { clearTimeout(timeout_gallery); }
			setTimeout('$("#loghi").animate({top: "620px"},500,"easeOutQuart");',1000);
			/*setTimeout('$("#'+$(this).attr("rel")+'").css("left",((window_width/2)-420)+"px");',600);
			setTimeout('$("#'+$(this).attr("rel")+'").fadeIn(900)',630);*/
			is_moving = true;
			$("#submenu").animate({top: 230+"px"},400,"easeOutQuad",function() { $("#submenu").css("display","none"); });
			//setTimeout('$("#'+$(this).attr("rel")+'").animate({left: ((window_width/2)-420+"px")},1200,"easeOutQuad", resetmoving);',1200);
			setTimeout('$("#'+$(this).attr("rel")+'").animate({left: "50%", marginLeft: "-420px" },1200,"easeOutQuad", resetmoving);',1200);
		}
		
		return false;	  
	});
	
	initialize_click_submenu();
	
	$(".video_item").click(function() {
									
		/*var v_video_vecchio = $("#video_video_contenitor object param[@name='movie']").attr("value").replace("http://www.youtube.com/v/","").replace("&autoplay=1","");
		alert(v_video_vecchio);*/
		$("#video_video_contenitor").html('<object width="461" height="280"><param name="movie" value="http://www.youtube.com/v/'+$(this).attr("rel")+'"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><param name="wmode" value="transparent"></param><embed src="http://www.youtube.com/v/'+$(this).attr("rel")+'&hl=it&fs=1&" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="461" height="280" wmode="transparent"></embed></object>');
		/*var video_nuovo = $("#video_video_contenitor").html();
		$("#video_video_contenitor object param[@name='movie']").attr("value","http://www.youtube.com/v/"+$(this).attr("rel")+"&autoplay=1");
		$("#video_video_contenitor object embed").attr("src","http://www.youtube.com/v/"+$(this).attr("rel")+"&autoplay=1");*/
	});
	
	$(".page").css("left",window_width);
	//$("#home").css("left",((window_width/2)-420)+"px");
	$("#home").css("left","50%");
	$("#home").css("marginLeft","-420px");
});

function resetmoving() {

	if (parseInt($("#video").css("left")) < 0) { $("#video_video_contenitor").css("display","none"); }
	if (parseInt($("#home").css("left")) <= 0) { $("#home_video_contenitor").css("display","none"); }
	is_moving = false;
}

function initialize_click_submenu() {
	$("#submenu a").click(function() {
		if (is_moving) { return false; }
		initialize_click_submenu();
		initialize_submenu();
		$(this).children().unbind('mouseover').unbind('mouseout');
		$(this).unbind('click').click(function() {
			return false;					   
		});
		$(this).children().attr("src",$(this).children().attr("src").replace("_OFF","_ON"));
		if (is_moving) { return false; }
		clearTimeout(timeout_gallery);
		movecenteredpage("gallery");
		gallery_image_index = 1;
		flag_autoplay = 1;
		gallery_name = $(this).attr("rev");
		showgallery();
		
		return false;
	});
}	

function initialize() {
	for (i=0;i<50;i++) {
		if (($(".hover:eq("+i+")").attr("src")) && ($(".hover:eq("+i+")").attr("clicked") != "1")) {
			$(".hover:eq("+i+")").attr("src",$(".hover:eq("+i+")").attr("src").replace("_ON","_OFF"));	
		}
	}
	$(".hover").mouseover(function() {
		$(this).attr("src",$(this).attr("src").replace("_OFF","_ON"));							   
	}).mouseout(function() {
			$(this).attr("src",$(this).attr("src").replace("_ON","_OFF"));		
	});
}
function initialize_submenu() {
	for (i=0;i<50;i++) {
		if ($("#submenu img:eq("+i+")").attr("src")) {
			$("#submenu img:eq("+i+")").attr("src",$("#submenu img:eq("+i+")").attr("src").replace("_ON","_OFF"));	
		}
	}
	$("#submenu img").mouseover(function() {
		$(this).attr("src",$(this).attr("src").replace("_OFF","_ON"));							   
	}).mouseout(function() {
		$(this).attr("src",$(this).attr("src").replace("_ON","_OFF"));							   
	});
}


function showgallery(side) {
	if (is_moving) { return false; }
	is_moving = true;	
	
	if ((parseInt($("#gallery1").css("left")) >= window_width) || (parseInt($("#gallery1").css("left")) <= 0)) {
		//alert("libero: gallery1");
		id = "#gallery1";
	} else {
		//alert("libero: gallery2");	
		id = "#gallery2";
	}
	if (side == 'right') { 
		$(id).css("left","-840px");
	}
	$.get("get.photo.php", { gallery: gallery_name, image:gallery_image_index },
		function(data){ 
			
			$(id).html(data);
			$(".navigation img").ifixpng();
			//$(id).append($("#precedente_successiva").html());
			//$(".navigation").ifixpng();
			/*if (gallery_name == "opere") {
				$(id+" .lens").css("display","block");
			} else {
				$(id+" .lens").css("display","none");
			}
			
			$(".a_len").lightBox({fixedNavigation:false});
			$(".a_len").attr("href",$(id+" .imagegallery").attr("src").replace("opere/","opere/big/"));
			$(".a_len").click(function() {
				flag_autoplay = 0;
				clearTimeout(timeout_gallery);	
			});*/
			if (gallery_name == "opere") {
				$(id+" .imagegallery_a").lightBox({fixedNavigation:false});
				$(id+" .imagegallery_a").attr("href",$(id+" .imagegallery").attr("src").replace("opere/","opere/big/"));
			} else {
				
				$(id+" .imagegallery").attr("alt","").attr("title","").click(function() { return false; }).css("cursor","default");
			}
			$(".navigation .hover").mouseover(function() {
				$(this).attr("src",$(this).attr("src").replace("_OFF","_ON"));							   
			}).mouseout(function() {
				$(this).attr("src",$(this).attr("src").replace("_ON","_OFF"));							   
			});
			$(".gallery_image_successiva").click(function() {										  
				flag_autoplay = 0;
				clearTimeout(timeout_gallery);
				nextimage();
			});
			$(".gallery_image_precedente").click(function() {
				flag_autoplay = 0;
				clearTimeout(timeout_gallery);
				previmage();
			});
			
			
			
			moving = parseInt($(id).css("height")) - 300;
			$("#loghi").animate({top: 625+moving+10+"px"},800,"easeOutQuart");
			//$(id).animate({left:  ((window_width/2)-420+"px")},1200,"linear", resetmoving);
			$(id).animate({left: "50%", marginLeft: "-420px"},1200,"easeInOutCubic", resetmoving);
			setTimeout('$("#submenu").css("display","block").animate({top: 253+"px"},400,"easeOutQuad");',1100);
			
			/*if (gallery_name == "opere") { timeouttempo = 5000; }
			else { timeouttempo = 3000; }
			if (flag_autoplay) timeout_gallery = setTimeout("nextimage();",timeouttempo);*/
	  });
	
	
	//alert("get.photo.php?gallery="+gallery_name+"&image="+gallery_image_index);
}

function nextimage() {
	movecenteredpage('gallery');
	gallery_image_index = gallery_image_index+1;
	showgallery();		
}
function previmage() {
	movecenteredpage('gallery','right');
	gallery_image_index = gallery_image_index-1;
	showgallery('right');		
}

function movecenteredpage(section, side) {
	if (is_moving) { return false; }
		if (section == "gallery") { animation = "easeInOutCubic"; }
		else { animation = "easeInQuad"; }
		if (side == "right") { left = window_width; }
		else { left = "-840px"; }
		//alert(animation);
		//alert($("#gallery1").css("left"));
		$(".page").filter(function(index) {
		  return parseInt($(this).css("left")) <= 0;
		}).css("left",window_width);
		$(".page").filter(function(index) {
			if ((parseInt($(this).css("left")) < window_width) && (parseInt($(this).css("left")) > 0)) {
				return true;	
			}else{
				return false;
			}
		}).css("left",(window_width/2));
		$(".page").filter(function(index) {
			if ((parseInt($(this).css("left")) < window_width) && (parseInt($(this).css("left")) > 0)) {
				return true;	
			}else{
				return false;
			}
		}).animate({left: left, marginLeft:"0px"},1200,animation, resetmoving);
		
		/*var prima = $(".page").filter(function(index) {
			if ((parseInt($(this).css("left")) < window_width) && (parseInt($(this).css("left")) > 0)) {
				return true;	
			}else{
				return false;
			}
		});
		alert("PRIMA: left:"+prima.css("left")+" margin-left:"+prima.css("marginLeft")+"   DOPO: left:"+left+" margin-left:0");*/

}

function adjustResize() {

	var new_width = parseInt($(window).width());	
	$(".page").filter(function(index) {
		return parseInt($(this).css("marginLeft")) == 0;
	}).css("left",new_width);
	
	/*$(".page").filter(function(index) {
		return ((parseInt($(this).css("left")) < window_width-10) && (parseInt($(this).css("left")) > 10));
	}).css("left",((new_width/2)-420)+"px");*/
	window_width = new_width;
}