
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ //
// Developer => Laura Silvani             									   //
// Qwentès Italia S.r.l. 	 												   //
// July 2009 				 												   //
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ //
		
$(function(){
	
	var lang_translation = { 
		it: "italiano", 
		en: "english",
		es: "español",
		fr: "français"
	};

	//$("#joinisr").validationEngine();

	/* ------------------------------------------------ */
	/* MENU DI SERVIZIO                                 */
	/* ------------------------------------------------ */

	$("#main_menu").children("ul.menu_level_0").children("li.cat_29").css("padding-top","30px");
	

	/* ------------------------------------------------ */
	/* REDIRECT LINKS	                                */
	/* ------------------------------------------------ */
	$("a.cat_51").attr("href","http://www.reteurbs.org").click(function(){ this.target = "_blank"; });
	$("#Rapporto").click(function(){ this.target = "_blank"; });	
	$("a[rel='external']").click(function(){ this.target = "_blank"; });

	/* Archivio -> Links to the old site... */
	//$("a.cat_32").attr("href","http://archivio.istitutosvizzero.it/"+ lang_translation[maui_runtime.current_language] +"/roma/archivio/archivio_scienza/2005/programmi.html").click(function(){ this.target = "_blank"; });
	$("a.cat_83").attr("href","http://archivio.istitutosvizzero.it/"+ lang_translation[maui_runtime.current_language] +"/roma/archivio/archivio_cultura/2005/program_01.html").click(function(){ this.target = "_blank"; }); // Archivio Arte
	$("a.cat_57").attr("href","http://archivio.istitutosvizzero.it/"+ lang_translation[maui_runtime.current_language] +"/roma/archivio/archivio_scienza/2005/programmi.html").click(function(){ this.target = "_blank"; }); // Archivio Scienze
	$("a.cat_28").attr("href","http://archivio.istitutosvizzero.it/"+ lang_translation[maui_runtime.current_language] +"/roma/archivio/archivio_scienza/2005/programmi.html").click(function(){ this.target = "_blank"; });

	/* Archivio membri -> Links to the old site... */ 
	$("a.cat_68").attr("href","http://archivio.istitutosvizzero.it/italiano/roma/archivio/archivio_membri/2004_2005/membri2004_05.html").click(function(){ this.target = "_blank"; });	

	/* ------------------------------------------------ */
	/* CONTENT DIMENSIONS                               */
	/* ------------------------------------------------ */

	var HPBoxesNum 		= parseInt($('.content').children('.HPBox').size(),10);
	var CalendarsNum 	= parseInt($('.content').children(".calendar").size(),10);	
	var ActivitiesNum 	= parseInt($('.content').children(".activities").children(".activities_stack").size(),10);

	var HistoryItemsNum = parseInt($('.content').children(".Hitem").size(),10);	
	var ContentWidth,WinHeight,transition,myMonth;

	if(HPBoxesNum !== 0){
		ContentWidth = (200 * HPBoxesNum - 30); // Single box width * n° boxes - paddingLeft first element
		WinHeight = $(window).height();
		$(".container").css("width",ContentWidth);
		$(".content").css("width",ContentWidth);
		$("#main_menu").css("height",WinHeight);
	} else {
		
		//=> Calendar Dimensions
		if(CalendarsNum !== 0){
			ContentWidth = (680 * CalendarsNum);
			WinHeight = $(window).height();
			$(".content").css("width",ContentWidth).addClass("calendars");
			$("#main_menu").css("height",WinHeight);
		}

		//=> Activities Dimensions
		if(ActivitiesNum !== 0){
			ContentWidth = (820 * ActivitiesNum);
			WinHeight = $(window).height();
			$(".content").css("width",ContentWidth);
			$("#main_menu").css("height",WinHeight);
		}

		//=> History
		if(HistoryItemsNum !== 0){
			
			ContentWidth = (314 * (HistoryItemsNum + 0.5));
			WinHeight = $(window).height();
			$(".content").css("width",ContentWidth);
			$("#main_menu").css("height",WinHeight);
			
		}
	}

	// => Chi siamo/La sede/Storia => <p>Body</p> dims
	/* $("div[id^='HDesc-']").each(function() {
		var CurrentID = $(this).attr("id");
		CurrentID = CurrentID.replace("HDesc-","");
		var RightWidth = $("#Img-" + CurrentID).attr("width");
		$(this).css("width",RightWidth);
		$(this).css("margin-top","20px");
		$(this).css("margin-left","5px");
	});	*/

	/* ------------------------------------------------ */
	/* HOMEPAGE                                         */
	/* ------------------------------------------------ */

	$(".HPBox").hover(function() {
		$(this).children("h4").children("a").css("color","#000000");
		$(this).children("div.ABS").children("p").css("color","#000000");
		$(this).children(":not(span)").css("color","#000000");
		$(this).children("img").hoverFlow('mouseenter', { opacity: 1 }, 'normal');
		
	  }, function() {
		$(this).children(":not(span)").css("color","#C6C7C8");
		$(this).children("div.ABS").children("p").css("color","#C6C7C8");
		$(this).children("h4").children("a").css("color","#C6C7C8");
		$(this).children("img").hoverFlow('mouseleave', { opacity: 0 }, 'normal');
	});

	/* ------------------------------------------------ */
	/* ACTIVITIES CALENDAR                              */
	/* ------------------------------------------------ */

	myMonth = new Date();
	myMonth = myMonth.getMonth() + 1;

	if(myMonth == 13) {
		myMonth = 1; // If we're in December...
	}

	$(".activities").children(":not(.cal_" + myMonth + ")").children("div").addClass("activities_item_nonactive");
	$(".activities").children(".cal_" + myMonth).children("div").addClass("activities_item_active");

	// Activities slide
	if(ActivitiesNum !== 0){
		transition = (820 * (myMonth - 1)) - (109 * 2.76);
		$(".content").animate({ marginLeft: "-" + transition + "px"}, 500);
		$("div.months > a#" + myMonth).addClass("currMonth");
	}

	$("a.activities_month").click(function(){
		$(this).siblings().removeClass("currMonth");
		$(this).addClass("currMonth");
		var selMonth = $(this).attr("id");

		$(".activities").children(":not(.cal_" + selMonth + ")").children("div").removeClass("activities_item_active").addClass("activities_item_nonactive");
		$(".activities").children(".cal_" + selMonth).children("div").removeClass("activities_item_nonactive").addClass("activities_item_active");

		//$(".activities").children(":not(.cal_" + selMonth + ")").children("div").switchClass("activities_item_nonactive","activities_item_active",1000);
		//$(".activities").children(".cal_" + selMonth).children("div").switchClass("activities_item_active","activities_item_nonactive",1000);

		if(selMonth == "1"){
			$(".content").animate({ marginLeft: "310px"}, 500);
		} else if(selMonth == "2") {
			transition = (820 * (selMonth - 1)) - 1350;
			$(".content").animate({ marginLeft: transition + "px"}, 500);
			//console.log(261 * (selMonth - 1));
		} else {
			transition = (820 * (selMonth - 1)) - (109 * 2.76);
			$(".content").animate({ marginLeft: "-" + transition + "px"}, 500);
			//console.log(261 * (selMonth - 1));
		}
	});

	

	/* ------------------------------------------------ */
	/* CALENDAR                                         */
	/* ------------------------------------------------ */

	$("a#BackToCalendar").click(function(){
		refer('&m=' + $.cookie("month"));
	});

	function refer(args) {
		var ref = document.referrer;
		var myStart = ref.indexOf("?");
		if ((ref) && (ref != "")) {
		 location.href = ref;
	   }
	}

	var checkmonth = $.cookie("month");
	//alert(checkmonth);
	if(checkmonth == null){
		myMonth = new Date();
		myMonth = myMonth.getMonth() + 1;
	} else {
		myMonth = $.cookie("month");
	}

	$("a.category").click(function(){
		resetmonth = new Date();
		resetmonth = resetmonth.getMonth() + 1;
		$.cookie("month",resetmonth);
	});


	if(myMonth == 13) {
		myMonth = 1; // If we're in December...
	}
	
	if(myMonth == 1){
		transition = 1;
	} else {
		transition = (680 * (myMonth - 1)) - (109 * 2.76);
		transition = "-" + transition
	}
	
	//transition = (680 * (myMonth - 1)) - (109 * 2.76);
	if(myMonth != 1) {
		$(".calendars").animate({ marginLeft: transition + "px"}, 500);
	}
	$("div.months > a#" + myMonth).addClass("currMonth");
	
	// ++++ Science event or Art event ++++ //
	$(".event").each(function() {
		if($(this).hasClass("cat_11")) {
			$(this).children(".event_det").addClass("Aicon");
		} else if($(this).hasClass("cat_12")) {
			$(this).children(".event_det").addClass("Sicon");
		}
	});

	// ++++ Single Event Hover Effect ++++ //
	$(".event").hover(function() {
		$(this).hoverFlow('mouseenter', { opacity: 1 }, 'normal');
	  }, function() {
		$(this).hoverFlow('mouseleave', { opacity: 0.5 }, 'normal');
	});
	
	// ++++ Higlight events when rollover on events categories ++++ //
	/* $("a[id^='event_']").hover(function() {
		var CurrentClass = $(this).attr("id");
		CurrentClass = CurrentClass.replace("event_","");	
		$("div." + CurrentClass).stop().animate({ opacity: 1 }, 'normal');
	  }, function() {
		var CurrentClass = $(this).attr("id");
		CurrentClass = CurrentClass.replace("event_","");	
		$("div." + CurrentClass).stop().animate({ opacity: 0.5 }, 2000);
	}); */
	
	$("a[id^='event_']").click(function(){
		$("div.Highlighted").stop().animate({ opacity: 0.5 }, 'normal');
		var CurrentClass = $(this).attr("id");
		CurrentClass = CurrentClass.replace("event_","");	
		$("div." + CurrentClass).stop().addClass('Highlighted').animate({ opacity: 1 }, 'normal');
	});

	// ++++ Slide calendar when click on month ++++ //
	$("a.event_month").click(function(){
		$("div[id^='EventDetail']").hide();	
		$(this).siblings().removeClass("currMonth");
		$(this).addClass("currMonth");
		var selMonth = $(this).attr("id");
		$.cookie("month",selMonth);
		if(selMonth == "1"){
			$(".content").animate({ marginLeft: "310px"}, 500);
		} else {
			transition = (680 * (selMonth - 1)) - (109 * 2.76);
			$(".content").animate({ marginLeft: "-" + transition + "px"}, 500);
		}
		//alert("dsadsa" + $.cookie("month"));
	});
	
	function showDetail() {
		$("div[id^='EventDetail']").hide();	

		//=> Get the id and clean it...
		var currID = $(this).attr("id");
		currID = currID.replace("DetEvent_","");
		$("#EventDetail_" + currID).removeShadow();
		
		//get the position of the div element
		var pos = $(this).offset();  
		//var width = $(this).width();
		//show the menu directly over the placeholder
		if($(this).hasClass("last")) {
			$("#EventDetail_" + currID).css({ "left": (pos.left - 110) + "px", "top":pos.top + "px" } );
		} else if($(this).hasClass("row2")) { 
			$("#EventDetail_" + currID).css({ "left": (pos.left) + "px", "top":pos.top - 212 + "px" } );
		} else {
			$("#EventDetail_" + currID).css({ "left": (pos.left) + "px", "top":pos.top + "px" } );
		}
	
		if($(this).hasClass("row3") && $(this).hasClass("last")) { 
			$("#EventDetail_" + currID).css({ "left": (pos.left - 110) + "px", "top":pos.top - 212 + "px" } );
		}		

		

		$("#EventDetail_" + currID).show();
		$("#EventDetail_" + currID).dropShadow({
			left    : 5,
			top     : 5,
			blur    : 5,
			opacity : 0.8
		});
	}

	$("div[id^='DetEvent_']").click(showDetail);

	$("a.closeDet").click(function(){
		$(this).parent().parent().css("display","none");
		$(this).parent().parent().removeShadow();
	});

	$("div.ShowDetail").click(function(){});
	
	// => Footer Banners
	$("#FootBanner1").click(function(){ 
		$(".DetBanner1").fadeIn('normal'); 
		$(".DetBanner2").fadeOut('normal');
		$(".DetBanner3").fadeOut('normal');
		$(".Banner1").css("margin-top","0px"); 
	});
	
	$("#Close1").click(function(){ 
		$(".DetBanner1").fadeOut('normal'); 
		$(".Banner1").css("margin-top","130px");	
	});

	$("#FootBanner3").click(function(){ 
		$(".DetBanner1").fadeOut('normal'); 
		$(".DetBanner2").fadeOut('normal');
		$(".DetBanner3").fadeIn('normal'); 
		$(".Banner3").css("margin-top","0px"); 
	});
	
	$("#Close3").click(function(){ 
		$(".DetBanner3").fadeOut('normal'); 
		$(".Banner3").css("margin-top","130px");	
	});
	
	$("#FootBanner2").click(function(){	
		$(".DetBanner1").fadeOut('normal'); 
		$(".DetBanner2").fadeIn('normal');
		$(".DetBanner3").fadeOut('normal');
		$(".Banner2").css("margin-top","0px"); 
	});
	
	$("#Close2").click(function(){ 
		$(".DetBanner2").fadeOut('normal'); 
		$(".Banner2").css("margin-top","130px");
	});
	// END Footer Banner

	$("a.cat_31").click(function(){
		$(this).attr("href","http://www.facebook.com/group.php?gid=95311075324");
		$(this).attr("target","_blank");
		
	});
	
	
});

