// init vars
var toggleSpeed = 250;
var bottomMenuHeight = 150;
var minimumContentHeight = 45;
var homeContentHeight = 320;
var diaryContentHeight = 328;
var baseurl = $("meta[name='BEdita.base']").attr('content');


$(document).ready(function()
{
	/* **************************************************************
	 * Menus
	 */	
	
	// center menus
	var menu_width = 30;
	$('#bottom_menu .menu_column').each(function(index) {
		menu_width += $(this).width();
	});
	$('#bottom_menu').css ("width", menu_width + "px");
	menu_width = 0;
	$('#top_menu .menu_column').each(function(index) {
		menu_width += $(this).width() + 2;
		$(this).css ("width", $(this).width() + "px");
	});
	$('#top_menu').css ("width", menu_width + "px");

	// set bottom menu columns height
	$("#bottom_menu .menu_column").each(function() {
		// altezza ul
		var thisColumn = $(this).children("ul");
		var columnHeight = thisColumn.innerHeight();
		
		// sommo all'altezza dell'ul il margine superiore del primo li e il margine inferiore del secondo li
		columnHeight += parseInt(thisColumn.find("li:first").css("margin-top")) + parseInt(thisColumn.find("li:last").css("margin-bottom"));
		
		// sommo l'altezza dei fratelli (tipo h2)
		thisColumn.siblings().each(function() {
			columnHeight += $(this).innerHeight();
		}); 
		
		$(this).css({
			"height": columnHeight
		});
	});


	// top menu columns
	$("#top_menu .menu_column").hoverIntent({
		sensitivity: 8,
		interval: 40,
		over: function() {
			$(this).children("ul").slideDown();
		},
		timeout: 0,
		out: function () {
			$(this).children("ul").slideUp();
		}
	});

	// bottom menu columns
	$("#bottom_menu .menu_column").hoverIntent({
		sensitivity: 8,
		interval: 40,
		over: function() {
			toggleBottomMenu ($(this).children("ul"));
		},
		timeout: 0,
		out: function() {
			toggleBottomMenu ($(this).children("ul"));
		}
	}).css("cursor", "pointer");


	/* **************************************************************
	 * Video
	 */	
	
	// load video
	$(".multimedia_container a").live("click", function(event) {
		event.preventDefault();
		var url = baseurl + "pages/showMedia/" + $(this).attr("href");
		
		$('html,body').animate({scrollTop: Math.round($("#content").offset().top - $("#top_container").height())}, toggleSpeed);
		
		var columnWidth = 0;
		$(".column").each(function() {
			columnWidth += parseInt($(this).outerWidth());
		});
		var postData = {"mediaWidth": columnWidth - 240};
		
		var	loaderClass = "loader";
		if ($(".column").length < 3) {
			loaderClass = "loader2cols";
		}

		$(".media_container").empty().slideDown(toggleSpeed).addClass(loaderClass).load(url, postData, function() {
			
			var mediaHeight;

			mediaHeight = $(this).children(".media_embed").height();
			
			$(this).removeClass(loaderClass).animate({
				"height": mediaHeight
			}, toggleSpeed, function() {});

			$(".media_description").html($(".media_description_tmp").html());
			
			var cssShort = {
				"marginTop": - mediaHeight,
				"height": $(".column").height() + mediaHeight
			};

			// faccio salire la colonna di destra e impongo altezza = media + quella delle colonne iniziali
			$(".column:last").animate(cssShort, toggleSpeed, function() {});
		});
	});
	
	// close media
	$(".closeMedia").live("click", function() {
		$(".media_container").slideUp(toggleSpeed);
		$(".media_description").empty();
		$(".column:last").css({
			"margin-top":0,
			"height": $(".column:first").height()
		});
	});
	


	/* **************************************************************
	 * Form
	 */	
	
	// toggle subscribe form
	$(".toggle_dialogbox").click(function() {
		var idBox = $(this).attr("rel");
		$("#" + idBox).toggle("fast");		
//		if ($("#" + idBox + ":visible").length > 0) {
//			$("#" + idBox).find("input:first").focus();
//		}
	});
	
	// close subscribe form
	$(".close_dialogbox").click(function() {
		$(this).parent("div:first").hide("fast");
	});


	/*
	// menu links ajax
	$('#accordion .ui-accordion-content a').live("click", function() {
		// ajax call
		hideContent();
		$("#content").children().css("visibility", "hidden");
		// force remove width from center_content, used for diary section
		$("#center_content").css("width", "");

		url = $(this).attr("href");
		$("#content").addClass("loader").load(url, function() {
			$(this).removeClass("loader");
			showContent();
		});
	});
	*/



	// link home page
	$("#publication_description")
	.css("cursor", "pointer")
	.click(function() {	location.href = baseurl; });
	
	$("#top_logo")
	.css("cursor", "pointer")
	.click(function() {	location.href = baseurl; });

});




/* **************************************************************
 * Functions
 */	
	

// bottom menu
function toggleBottomMenu (thisColumn) {
	
	var targetBottom;
	
	if (!thisColumn.hasClass("menu_column_open")) {
		thisColumn.addClass("menu_column_open");
		targetBottom = thisColumn.parent().innerHeight() - 25; //thisColumn.parent().innerHeight();
		
		// animate this
		thisColumn.parent().animate({
			bottom: targetBottom
		}, toggleSpeed, function (){
			
		});
		
	} else {
		thisColumn.removeClass("menu_column_open")
		targetBottom = 0;
		
		// animate this
		thisColumn.parent().animate({
			bottom: targetBottom
		}, toggleSpeed);
		
	}

	// reset other columns
	thisColumn.parent().siblings(".menu_column").animate({
		bottom: 0
	}, toggleSpeed);
}

jQuery.fn.toggleText = function(a, b) {
	return this.each(function() {
		jQuery(this).text(jQuery(this).text() == a ? b : a);
	});
};




/* **************************************************************
 * Ajax errors
 */	
$(document).ajaxError( function(event, jqXHR, ajaxSettings, thrownError) {
	$("#content").empty().html('<div class="column pad_column_w4 short">'+jqXHR.responseText+'</div>');
	showContent();
});

// analytics logging of ajax calls
(function ($) {

  // Log all jQuery AJAX requests to Google Analytics
  $(document).ajaxSend(function(event, xhr, settings){ 
    if (typeof _gaq !== "undefined" && _gaq !== null) {
      _gaq.push(['_trackPageview', settings.url]);
    }
  });

})(jQuery);
